Leebber

Lyu Sen's Personal Blog Website

0%

好的,这是为您生成的财经新闻报告。

第一部分:全球市场综合报告(昨日与今日)

全球重大事件

法国政坛陷入危机,总理塞巴斯蒂安·勒科尔尼在上任不足一个月后意外辞职,总统马克龙要求其与各政党进行最后谈判以打破僵局。此事件引发欧洲市场动荡,法国CAC40指数大跌,法德国债利差扩大至九个月新高。在日本,高市早苗赢得执政党党魁选举,市场普遍预期其将推行扩张性财政和持续宽松的货币政策,导致日元大幅贬值。地缘政治方面,以色列与哈马斯在埃及的新一轮停火谈判仍在继续,哈马斯提出永久结束占领并需国际保障等要求。俄乌冲突持续,双方均宣称对对方无人机进行了大规模拦截,乌克兰多地能源基础设施遭袭。

美国重大事件

美国联邦政府停摆进入第七天,参议院周一未能通过两党各自提出的拨款法案,导致结束停摆尚无明确途径。此次停摆已造成关键经济数据(如非农就业报告)发布推迟,并开始影响空中交通管制,导致多地机场航班延误。白宫警告称,政府停摆每周对美国经济造成的损失约为150亿美元。美国总统特朗普将停摆归咎于民主党,并威胁若谈判无进展将开始大规模裁员。

经济重大事件

世界贸易组织(WTO)大幅下调2026年全球商品贸易增长预测至0.5%,理由是美国关税的全面影响将在明年显现。德国8月工厂订单意外连续第四个月下降,主要受汽车行业及海外需求疲软拖累。美国8月消费信贷增长远不及预期,仅增加3.63亿美元,显示消费者借贷步伐放缓。

短期股票市场

全球股市因政治不确定性承压,尤其欧洲市场受法国政局影响显著下跌。然而,美股市场在人工智能(AI)领域的重磅消息提振下表现分化。AMD与OpenAI达成价值数十亿美元的芯片供应协议,强力提振了整个芯片板块,AMD股价盘前大涨。

货币市场

美元指数因避险需求而走强,突破98.5关口。欧元因法国政治危机而承压,跌至两周低位。日元兑美元汇率暴跌,自2024年2月以来首次突破152关口,主要原因是市场预期日本新任领导层将采取进一步的财政刺激和货币宽松政策。

重金属

受美国政府停摆、全球政治风险及美联储降息预期等多重因素推动,避险情绪急剧升温。现货黄金价格飙升,盘中一度突破3997美元/盎司,创下历史新高,距离4000美元大关仅一步之遥。现货白银、铂金和钯金也同步大幅上涨。

大宗商品

国际油价在OPEC+决定温和增产后企稳,但对全球供应过剩和需求放缓的担忧限制了其上行空间。布伦特原油在65美元附近徘徊,WTI原油在61美元附近。俄罗斯一座重要炼油厂因无人机袭击导致部分停产,为油价提供短期支撑。欧洲天然气价格因乌克兰基础设施遇袭而上涨。


重要消息源详述

  • 美联储理事米兰(MİLAN):他表示,货币政策需要放松,且行动要赶在中性利率下调之前。他认为中性利率可能为0.5%,而人工智能(AI)的发展可能会推动中性利率上调。他同时指出,目前的预测显示关税不会大幅推高通胀,并非推动通胀的重要因素。
  • 美联储官员卡什卡利(Kashkari):他警告称,数据显示出一些“滞胀信号”。他认为,小幅降息可能不会导致抵押贷款利率下降,但如果美联
    储大幅降息,预计经济将出现一轮高通胀。
  • 美联储官员博斯蒂克(Bostic):他指出,当前的经济动荡正促使企业与家庭采取更为谨慎的立场。
  • 世界贸易组织(WTO):总干事表示,全球贸易体系仍显韧性,但关税影响或将波及2026年。WTO将2025年货物贸易增长预测上调至2.4%,但将2026年预测从先前的大幅增长大幅下调至仅0.5%,并指出人工智能相关商品是上半年贸易增长的核心动力。
  • 桥水基金创始人瑞·达利欧(Ray Dalio):他表示,现在的情况就像20世纪70年代,投资者应该比平常持有更多的黄金。他认为黄金是“非常优秀的投资组合分散化工具”,最优投资组合中约15%应配置于黄金,其创纪录的涨势与1970年代在高通胀和经济不稳定背景下的飙升类似。
  • 美国总统特朗普:他表示,将与加拿大总理讨论关税问题,并称将公平对待加拿大。关于加沙谈判,他透露“我们的团队现在就在那里”,并认为中东有可能实现和平。

第二部分:今日美股市场详细动态

  • 人工智能与芯片行业

    • **AMD (AMD.O)**:摩根士丹利将其目标价从168美元大幅上调至246美元。杰富瑞(Jefferies)更是将评级从“持有”上调至“买入”,目标价从170美元上调至300美元。公司确认与OpenAI达成协议,将为其提供多代AI芯片支持,预计将带来数百亿美元的新收入。
    • **IBM (IBM.N)**:股价上涨3.5%,创下历史新高。公司宣布与Anthropic达成企业级AI软件合作协议。
    • **NVIDIA (NVDA.O)**:被高盛投资组合经理列为首选人工智能概念股之一。同时,消息称英伟达将作为股权投资者,参与马斯克旗下xAI公司高达200亿美元的新一轮融资。
    • **xAI (私有)**:马斯克的人工智能初创公司即将完成一笔与英伟达芯片相关的200亿美元融资,远超最初计划。
    • 高盛交易员观点:高盛宏观交易员Bobby Molavi表示,投资者不应与当前的人工智能投资热潮逆向而行,强调“对抗资本支出浪潮很难生存”。
    • JPMorgan报告:与AI相关的债券规模已扩大到1.2万亿美元,超越银行业,成为美国投资级债券市场中最大的板块。
  • 科技与汽车巨头

    • **Dell (DELL.N)**:股价盘前上涨超4%。公司上调了长期财务框架,预计到2030财年,销售额将以每年7%至9%的速度增长,主要得益于对人工智能产品的强劲需求。
    • **Tesla (TSLA.O)**:股价在盘中跌幅一度扩大至4%。公司正式发布了价格更低的Model Y标准后轮驱动版,售价为39,990美元,续航里程为321英里。同时,Model 3标准版售价为36,990美元。
    • **Ford (F.N)**:股价收盘下跌6.1%,创下自2月份以来的最大单日跌幅。
    • **General Motors (GM.N) & Stellantis (STLA.N)**:据文件显示,美国能源部正考虑撤销给予两家公司近11亿美元用于汽车工厂改造的拨款。消息传出后,通用汽车股价下跌2.2%。
  • 市场分析与评级

    • 花旗集团:策略师警告,美国科技股在年内创纪录上涨后,面临获利了结的风险加剧,其中纳斯达克100指数尤为突出。
    • **Applovin (APP.O)**:在周一因监管调查消息暴跌超14%后,周二反弹收高7.64%。有分析师认为市场对此前消息反应过度。
    • **洲际交易所 (ICE.N)**:盘前上涨超3%,据悉公司计划向Polymarket投资20亿美元。
  • 其他重要公司动态

    • **马士基 (AMKBY)**:宣布将暂时上调部分红海航线的附加费。
    • **味好美公司 (McCormick, MKC)**:股价下跌2.9%。尽管三季度盈利超预期,但公司因大宗商品成本和关税压力下调了全年每股收益指引。

好的,这是根据您提供的信息生成的财经新闻报告。

第一部分:全球市场动态摘要 (昨日与今日)

全球重大事件

  • 法国政局剧变:总理勒科尔尼上任不足一月即辞职,总统马克龙组建的新内阁未能稳定局势。此举引发市场剧烈震荡,法国CAC40指数大跌,银行股重挫,法德10年期国债收益率利差扩至九个月新高,欧元承压。已辞职的总理勒科尔尼受命与各政党展开“最后谈判”。
  • 日本政坛更迭:高市早苗赢得自民党总裁选举,将成为日本首位女首相。市场预期她将推行扩张性财政和持续宽松的货币政策,导致日经指数飙升至历史新高,而日元兑美元汇率则暴跌,突破150关键心理关口。
  • 中东局势:哈马斯与以色列代表团在埃及就美国总统特朗普提出的停火“20点计划”展开间接谈判。特朗普称谈判进展顺利,并敦促以色列总理内塔尼亚胡接受协议。哈马斯代表团称谈判取得积极进展,但以方持续轰炸构成挑战。

美国重大事件

  • 政府停摆持续:美国联邦政府“停摆”进入第六天,参议院否决了民主党与共和党各自提出的拨款法案,结束僵局尚无明确途径。停摆已导致经济数据发布暂停,并引发对大规模裁员的担忧。
  • 特朗普多线施压:总统特朗普将政府停摆归咎于民主党,并称共和党在削减开支方面“正在获胜”。他同时指示房利美和房地美推动建筑商释放住房供应,并就加沙协议、对乌军援等外交事务发表看法。
  • 司法与行政冲突:联邦法官阻止特朗普政府向俄勒冈州波特兰派遣国民警卫队,但美国国防部确认已调派加州国民警卫队人员抵达该州,引发宪政争议。

经济与市场趋势

  • 短期股票市场:全球股市分化。受政治危机影响,欧洲股市普遍下跌,法国股市领跌。日本股市因新首相的宽松政策预期而暴涨,日经指数创历史新高。美股方面,科技股受重大利好消息提振表现强劲。
  • 货币市场:美元因避险需求和美联储降息预期放缓而走强。日元因日本政治变动和宽松预期而暴跌。欧元则因法国政局动荡而跌至数周低点。
  • 贵金属:避险情绪急剧升温,贵金属价格全面飙升。现货黄金价格突破3900美元,一度逼近4000美元大关,创下历史新高。现货白银触及14年高点,铂金价格也攀升至近13年峰值。
  • 大宗商品:OPEC+同意自11月起每日小幅增产13.7万桶,增产幅度远低于市场担忧,为油价提供了支撑,布伦特原油和WTI原油价格均录得上涨。

重要官员讲话详述

  • **美联储官员施密德 (堪萨斯城联储主席)**:

    • 政策立场:认为当前货币政策仅具有“轻微的限制性”,政策校准“恰到好好处”。
    • 通胀观点:当前通胀率“过高”,物价上涨范围愈发广泛的情况令人“担忧”。
    • 政策警告:警告称激进地刺激需求可能会提升物价大幅上涨的风险。在平衡各项目标时,美联储务必维护其在通胀问题上的“公信力”。
    • 数据依赖:由于政府停摆,将密切监测替代性的劳动力市场和价格数据。
  • 欧洲央行行长拉加德

    • 通胀判断:明确表示“通缩进程已结束”,且通胀的双向风险区间已经收窄。
    • 经济展望:认为欧元区在面临不确定性时表现良好,预计明年经济增长的不利因素将逐渐消退。
    • 政策路径:重申欧洲央行的利率决策将取决于数据,不会盯住汇率,但会关注其对物价的影响。
  • 美国总统特朗普

    • 政府停摆:声称已就停摆问题取得进展,并表示“很乐意与民主党人合作,但他们必须首先让我们的政府重新开门”。若参议院投票失败,可能会出现裁员。
    • 加沙局势:称以色列与哈马斯达成协议的可能性“非常大”,预计很快会达成协议。
    • 对乌军援:表示已就向乌克兰运送“战斧”巡航导弹一事做出决定,但需确认乌克兰将如何使用。

第二部分:今日短期股票市场新闻详述 (仅限今日)

  • AMD (AMD.O) 与OpenAI合作细节公布,股价飙升:AMD与OpenAI于10月6日宣布达成6吉瓦(GW)算力协议,为OpenAI提供多代GPU支持。AMD首席财务官胡锦于今日表示,与OpenAI的合作预计将为公司带来数百亿美元的收入。根据协议,AMD已向OpenAI发行最高1.6亿股AMD普通股认股权证。受此消息提振,AMD股价昨日收盘时上涨**24%**,创下2024年3月以来的最高水平。

  • 英伟达 (NVDA.O) 目标价获上调:因英伟达与OpenAI的合作,高盛今日将其目标价上调至210美元。坎托证券(Cantor)表示,此次合作进一步巩固了英伟达在人工智能领域的统治地位。

  • 特斯拉 (TSLA) 股价上涨,将推廉价新车型:此前预告10月7日发布新品的特斯拉,昨日尾盘上涨**5.4%**。市场消息今日显示,特斯拉计划于周二(今日)推出Model Y的廉价车型。

  • AppLovin (APP) 股价暴跌:因美国证券交易委员会(SEC)对其数据收集行为展开调查,AppLovin股价昨日下跌**18%**,创下4月4日以来的最大跌幅。

  • Lucid Group (LCID) 公布交付数据:电动汽车制造商Lucid Group今日宣布,第三季度交付了4,078辆汽车。

  • 埃克森美孚 (XOM) 发布业绩预警:埃克森美孚今日表示,相较于第二季度,行业利润率变化将使其第三季度能源产品收益减少3亿至7亿美元。同时,由于液体产品价格变动,上游业务业绩预计变动幅度在**-1亿至-3亿美元之间。天然气价格预计将对第三季度业绩造成-2亿至+2亿美元**的影响。

  • Trilogy Metals (TMQ) 股价盘后飙升:美国总统特朗普昨日签署行政命令,修建通往阿拉斯加州安布勒矿区的通道公路。今日消息确认,美国政府将收购Trilogy Metals公司10%**的股份,并有权购买额外7.5%的股权。受此影响,该公司股价在美国盘后交易中飙升逾100%**。

  • 其他美股动态

    • 威瑞森电信 (VZ)**:因任命新CEO,昨日尾盘下跌4.9%**。
    • 精神航空 (SAVE)**:在破产重组过程中,退还了87架**客机。
    • 斯特兰蒂斯 (STLA)**:计划投资百亿美元重注美国核心市场,昨日尾盘上涨0.9%**。
    • 微软 (MSFT)**:在澄清退出Xbox传闻后股价回升,涨幅达2.55%**。
    • Meta Platforms (META)**:公民金融(Citizens)重申对其900美元的目标价,看好其AI广告工具前景,股价上涨0.3%**。
  • 分析师观点

    • 华尔街传奇投资者Paul Tudor Jones:认为美股正处于牛市末期爆发前夕,短期或迎来强劲升浪,计划年底前持有黄金、加密货币及纳斯达克科技股。
    • 高盛(GS.N)**:将其2026年12月黄金价格预测从4300美元上调至4900美元**。
    • 德意志银行:将阿里巴巴(BABA)港股目标价从172.00港元上调至220.00港元

好的,这是根据您提供的信息生成的财经新闻报告。

第一部分:全球市场综述 (昨日与今日)

全球重大事件
以色列与哈马斯代表团已抵达埃及,准备就美国总统特朗普提出的停火“20点计划”进行间接谈判。市场对此持乐观态度,推动以色列股市创下历史新高。然而,以色列总理内塔尼亚胡强调,在所有人质获释前,不会推进计划的任何条款。哈马斯方面则否认了已同意解除武装的报道。在俄乌冲突方面,俄罗斯对乌克兰发动新一轮大规模空袭,造成人员伤亡。

重要人物观点:

  • 俄罗斯总统普京警告称,如果美国向乌克兰提供可深入打击俄罗斯本土的“战斧”导弹,将导致俄美关系破裂。

美国重大事件
美国政府停摆危机持续,市场担忧加剧。白宫与国会民主党人的谈判仍陷僵局,特朗普对达成积极结果表示有信心,但其顾问已发出警告。

重要人物观点:

  • 美国总统特朗普表示,若与民主党的停摆谈判没有进展,将开始裁员。他已授权向伊利诺伊州派遣国民警卫队,并呼吁房利美和房地美敦促大型建筑商释放住房供应,以重建“美国梦”。其顾问团队为缓解选民对经济的焦虑,提出了新的宣传口号:“等到明年就会好。”

经济重大事件
日本执政党自民党选举高市早苗为新任总裁,她大概率将成为日本新首相。市场普遍认为其支持宽松的财政和货币政策,导致日本央行年内加息预期大幅降温。OPEC+在仅持续9分钟的会议上决定,11月将小幅增产13.7万桶/日,增产幅度低于部分市场预期。

短期市场趋势

  • 股票市场:受新首相人选影响,日本股市出现历史性暴涨,日经225指数首次突破48000点,单日涨幅一度超过4.8%。
  • 货币市场:日元汇率暴跌,美元兑日元(USD/JPY)自2022年以来首次突破150关口,市场正在消化日本将维持超宽松货币政策的预期。
  • 重金属:受美国政府停摆引发的避险需求和美联储降息预期推动,现货黄金价格飙升,历史上首次突破3900美元/盎司,并一度触及3945美元的历史新高。
  • 大宗商品:因OPEC+增产幅度温和,缓解了市场对供应过剩的担忧,原油价格企稳回升。

第二部分:今日短期股票市场新闻

  • **波音 (BA.N)**:据知情人士透露,波音公司正指导供应商,计划最早于今年10月将737系列飞机的月产量提高至42架。公司还计划在明年4月再次提速,并力争在2026年底将月产量提升至约53架。

  • 美国汽车制造商:共和党参议员透露,特朗普正考虑为在美国生产的汽车提供重大关税减免,此举可能使福特 (Ford)、特斯拉 (TSLA)、通用汽车 (GM) 以及在美国本土化率高的丰田 (Toyota) 和本田 (Honda) 等公司受益。

  • **苹果 (AAPL)**:报道称,因Apple Intelligence发布不力及Siri重大升级推迟,苹果公司正在为其人工智能(AI)部门寻找新的负责人,以接替现任主管John Giannandrea。

  • **萤火虫航空航天公司 (Firefly Aerospace)**:该公司宣布将以约8.55亿美元的价格收购国家安全科技公司SciTec。该交易由3亿美元现金和5.55亿美元的Firefly公司股票构成。

  • **法拉第未来 (FF)**:公司表示,其FF SuperOne车型的美国量产总装方案已确定,即将启动相关零部件供应,全力冲刺年底首车下线。

  • 日本银行股:因市场预期日本央行将推迟加息,导致短期国债收益率下跌,日本银行股普遍下挫。理索纳控股 (Resona Holdings) 股价下跌4.0%,三菱日联金融集团 (MUFG) 股价下跌2.3%。

分析师评级与目标价调整

  • ASML:摩根大通将其目标价从822欧元上调至1000欧元。
  • **宁德时代 (CATL, 03750.HK)**:摩根大通将其H股评级从“增持”下调至“中性”,但将目标价上调13%至600港元,理由是基石投资者禁售期即将届满或带来抛压。
  • **中芯国际 (SMIC) 与华虹半导体 (Hua Hong Semiconductor)**:高盛因看好中国AI生态带来的机遇,将两家公司的港股目标价均上调至117港元。
  • **招金矿业 (Zhaojin Mining, 01818.HK)**:花旗银行因上调金价预测,将其目标价从21.6港元大幅上调至37.5港元,维持“买入”评级。
  • **中国金茂 (China Jinmao, 00817.HK)**:摩根士丹利将其目标价从1.2港元上调至1.32港元,评级为“与大市同步”。
  • **潼关黄金 (Tongguan Gold, 00340.HK)**:中金公司首次覆盖,给予“跑赢行业”评级,目标价3.52港元。
  • **兴业科技 (Xingye Technology, 002674.HK)**:天风证券发布研报,维持“增持”评级。

QCon 2022 Note

For me, I split all the talks I listened this time in QCon, into 3 categories:

  • Tech Architecture Optimization
    • Tidy First
    • Scaling Bottleneck
  • System Design Concept
    • Microservices Patterns
    • Orchestration vs Choreography
  • System Design Implementation
    • Adopting Continuous Deployment
    • Distributed In-Process Cache
    • Leveraging Determinism
    • Service Mesh
    • DynamoDB in Amazon
    • CosmosDB in Azure
    • Event Processing Service in Doordash
    • Blob Storage Service in Dropbox

The categories are ordered from abstraction to concretization. I know most of the engineers like me are more interested in the low level of the implementations. And yes, some talks here(like the Determinism one is really really good), however, this time the first two categories’ talk gave me the biggest influence. I think listening to these talks help me to understand the career path better. Sometimes people have to view things out of the box.

I really appreciate this time my company, Meta, sponsors me for this event.

TL;DR

I spent 3 days at the conference. The first day is more about academic work form professors and PhD students. The other two days are more about best practice in the industry world. The topics among AI, Distributed System, Advanced Tooling, Product Introduction and Personal Experiences.

I find conferences really help people to think out of the box. And I hope I joined Meta earlier and understand our own implementations so that I could have a deeper understanding of the problem and better compare solutions.

Research Topics:

AI content generation:

Generally, it gets more and more popular. However, the idea is already there. The topic is focused on the gaming content generation. Talk about the best practice of that. Talks about setup rules and how can make it generate more “comfortable” contexts for better game experience

Breakbeats analysis

A topic talks about AI works in breakbeats(pieces background music used in songs). How the company could use that to better understand songs evolution and help them to get song’s patent fees

Textual analyze on code

A talk about how people better understand what code from language-side of view is. And how this kind of thinking could provide people with a new way to understand our code pattern and code practice. Eg:

  • Trace is one pattern for people to understand the story, it is also a way we used to find bugs.

  • When we do bottom-up implementation, we always start from the central piece in the structure. This is reasonable from the language side

Product Introduction

Google’s universal IPC language: FIDL in fuchsia

A system very like thrift, but it is used in a deeper system, for IPC. Also doing the binding, so it will support different languages. And easy to extend to other languages.

Workflow:

A new way for making a durable system

  • A practice that brings “transaction” in DB into service codes, easier for people to write concurrent code. It will be easier to do error handling.

  • The idea is to make actions more atomic. And for the actions that could change the states, make them idempotent

Filibuster:

An End to End test tool, more focus on testing error cases in end-to-end test cases, to help understand big system’s errors

Talk about in a big system, how end to end testing is needed especially for error cases. Those tests need to cover multiple failing situations.

RLBox:

A way to run native code in sandbox to avoid native code’s memory issues

  • Talk about how native code is unsafe, and how many safe languages are built on them

  • Try to run all the native code in the sandbox to make it safer.

  • It is already used in firefox

  • A way of coding, so it will be very easy and clear to add to exist system

Akita software:

A product/practice to add API listeners for developers to understand old systems.

eBPF mode to add listeners to the APIs in the old system. And their product provides a solution to better understand the system based on those listeners.

Shopify solution for flash sale:

solution in both LB layer and service layer to ensure capacity, starvation and fairness problem

Very good topic to talk about flash sale, talked very detailed about how they choose from different solutions.

Runtime Checker:

Java best practice to enhance system in observation, watchdogs and in prod regression tests

  • Very good talk about why we need runtime checker in system, and why sometimes even current wide used system could have bugs

  • Talk about fix them in three topics

  • In observation, the idea is that using some static code analysis tools, to find those errors handled locally but not reported to any response.

  • In watchdog, talks about better catch errors, so the errors could report those error into some monitoring system

  • In tests, define some testing rules to describe the system, and when running the test to verify whether those rules are still valid. (like behaviors testing along the workflows)

WNFS:

a guide on creating a local-based encrypted file system

  • Talks about how to create a good file system, including files and directory. And how to keep the history and how to merge versions

  • Also, talks on how to encrypt those files

Coding Best Practice:

Polymorphism:

different language, different solutions for better polymorphism: Overloading, Override, Interface, Parametric, Structure, Union type, ADT, Multiple dispatching…

Python Performance: scalene

要不要准备DP

我的感受是收益率很低。
近期我参加了9家公司的面试,有7家完成VO,剩下两家止步店面,加起来有31道算法面试,其中只有google vo中1题是dp,而且是较简单的dp。
面试dp其实是很好的机会,因为dp题的代码都很好写,基本上状态转移的核心部分代码不会超过10行。所以会有更多时间去解释dp。然后结合画图,也属于较容易把思维说清楚的情况。建议大家把基础的题目的思路多说几遍,然后根据视频里面学到的多画画图,面试会更容易得到认可。
大家自己选择要不要准备dp

我花在DP上的

我看的资料,首先是课堂教学型

  • 首先我看的*NOIP的资料,这位老师讲的很好 b站 动规的部分大概从9.62到9.100多
  • 然后是MIT open course,youtube

    然后是非教学型

  • 这个人的youtube channel大家应该在很多地方都能找到推荐,其中也有dp部分 youtube
  • C++的专门题解集 github
  • 背包九讲,我觉得大家一定要去看看这个,实在是讲得好,很好的背包dp入门教材
  • 最后就是 oi 竞赛的部分我没有过多深入,不过简单了解一下还是有帮助的

    剩下就是做题

  • algo export上做了112题,其中dp 13题。 11%
  • leetcode上做了210题,其中dp 65题。 30%
  • 总计24%的题目都是dp,并且后期做的leetcode,更偏重dp。

总结

时间上基本上1个月到1个半月花在dp上面。基本做到了常见题型能够解题,简单的变形dp能够比较快转化为基础题型。


NOIP 大家对NOIP可能比较陌生,我稍微介绍一下,NOIP是初中生计算机竞赛,在国内已经办了超过15年了,dp是每年的考题。经过多年的研究,老师们已经可以把基本的dp讲解到初中生都能理解的程度。而且难度并不简单,大家可以自己感受一下。然后也请大家想一个问题,如果大厂对员工的面试中体现的业务能力的要求也就是初中生的学术要求,那么还有什么是你在初中毕业后这么多年读书获得的能够让你超越初中生而被公司录取?


我对dp的一些理解

dp是难的递推。不严谨的通俗来说递推能够继续下去,体现了重叠子问题,而递推能有一个合理的路径,体现了最优子结构。

推的路径

最简单的推的路径就是线性的,其中可以是一个状态推一个状态,也可以是几个状态推一个未来状态。这样的题一般可以表述为从“左推到右”或者从“开始推到结束”。
有时候就算数据变成了图形,其实仍然适用。从“左上推到右下”,仍然可以认为是线性的推广。
树形的推,更多是从小集合推到大集合,有一种合并的感觉,体现在树上就是从叶子往根推。


这里讲的推和树形dp,图形dp等不一定一致。这里是推的方向,也就是状态转移的方向,并不一定等同于数据结构


状态不是一个量,是一组量

我们常用数组表示状态的集合,然后一般数组里面存一个数字,我们就感觉状态其实是一个量,其实不然,状态可以扩展为好几个量。当然你可以最后做成多维数组来写。比如:我们有a,b,c 3个数据来表示一个状态
dp[i].a = 1, dp[i].b = true, dp[i] = "hello"也可以写成一个二维的多类型数组dp[i] = [1, true, "hello"]甚至写成三个数组dpa[i] = 1, dpb[i] = true, dpc[i] = "hello" 这个和根据数据结构来想到dp需要二维,在思维上是不一样的
这类题中的经典就是股票买卖

压缩

上面讲了扩展,那么思维上再过度一下就是压缩了。压缩,用通俗的话讲就是不需要了。思维上就是,如果我需要前三个值算第四个值,那么算到第四个的时候第一个值就不用了。所以就可以通过减少存的状态而节省空间了。
更进一步,上面提到的一个状态里面的某些量可能只需要用一次,那我就直接存就好,不需要用数组存了,那么就变成状态之外的量的感觉。这是通过减少存状态的量减少空间。比较经典的题就是最长不下降子序列,其中维护的数组其实是随着状态变而一直在变的,但是没有必要每一次存一个数组(估计也没有会这么干)。

优化

prefix优化 与 单调队列优化

dp中所有的优化里面,我只能理解这一种优化。这种情况多发生在多重循环下,在目前这个循环内需要再做循环来算一些值,一般是最值。而我们可以发现(先是直觉,然后是数学计算)这些最值的计算可能会用到一些相同的量(prefix或是维护一个单调队列或者单调栈),所以可以将内循环的逻辑简化,一般是O(m*m)-> O(m)计算prefix或是其他 + O(m)计算剩下内容。

Design Chinese health code system 设计健康码

I found a very interesting post here: https://www.v2ex.com/t/834691
Talking about design a chinese health code

Feature

  1. Large amount user could search for their health code status
  2. Change user’s health code to unhealthy code based on their time and location if they contact with other unhealthy code

Requirement

  1. User’s health code change could happen with certain delay. ( If I contact with a unhealthy code people, I might get update to unhealthy code 1 day after )
  2. Assume user need to show their health code to get into some place, which means that user’s phone is always opening and providing their data to us. ( There will not be if people go to one place and we cannot get the data )

Design

The whole system could be separate into 2(3) parts:

  1. Part that support user check their own code. Heavy read.
  2. Part that collect user’s time and location data. Heavy write.
  3. When time goes by, one certain range of data collected in part-2 will be complete. And according to requirement-2, we can assume all the data in that time range is in the database and no more to write in. That time we could get the data out and put into an offline batch process, where we will find all the unhealthy code’s movement and get all contacted health code user. We could let automatic process or human administrate to determine the final code change.

Summary

The design helps to separate the read, write, and online, offline, so each part could optimize itself.



我发现一个很有趣的帖子:https://www.v2ex.com/t/834691
提到设计健康码的问题。

功能:

  1. 海量用户查询自己的码
  2. 根据用户的时空查询是否接触黄码/红码用户而标红或者标黄用户

    需求:

  3. 更新查询过程可以存在一定延迟
  4. 假设用户进入某些地方必须扫码,也就是说用户的时空间数据是比较实时的(不会有长时间停机不传数据的情况)

设计思路:

整个过程分为2(3)个大的部件:

  1. 查询自己码。该部件为读远大于写
  2. 收集用户时空信息。该部件为写远大于读
  3. 当时间过去之后,从2中收集到的某一时间段的写已经全部完成,根据需求2可以认为该时间段所有信息已经录入,此时对这个时间段数据来说没有了写入,所以可以将数据取出来进入离线过程,收集红黄码用户位置,比对,从而获得所有交叉用户名单,由系统自动过程或者管理员决定是否写入1中的数据库

补充:

主要是将读写过程分离,将数据处理离线化,每个部分可以各自优化而不影响。

Coding Lover or Coding Worker or Both?

Recently, my new project is pushed to production. You might think that I would be exciting about it. Because it is the second project that I designed & developed & tested, and it finally pass the review from senior engineers and goes to the production. But I am very exhausted these days about this project. I have to admit I was 100% exiting about it at the beginning, but at the end now, I was not excited at all. So what happened?
At beginning, when I get the project, I would say it is a very good project for me. The requirement is mostly focused on system designing, because the old version is already solve the core algorithm, but people are not appreciated for the stability. What I need to enhance is the system. It needs a more stable and scalable structure, so it will fits more about the production situation. So I was very exiting about this “message queue - worker” pattern implement. I draw the design diagram, I worked on the API document, I create the schema of mongo database collections. And because I learnt a lot from the bugs from the first project I worked. So I think I covered all the corner case and it should be perfect.
So after the development finished, I start to push it to production. I was behaved like the project owner, I talked with customer success team to understand their needs and requirements and of course the timeline. Then I talked with cloud ops about the deployment requirements. After that I believed the project is good to go. So I push it to the channel to go production.
But the things turned ugly at this point. As I described before, this is not a brand new deployment, but a upgrade, though it is a overall upgrade. But apparently our senior engineers are not agree with that. After a week of silence, My new design is heavily challenged. At first I feel it’s OK, because people’s concern is all reasonable. For example, the structure of the message queue. I will say my message queue is well designed, because I carefully review the code from a open source message queue implementation, and I used message queue from my first project, so I was well prepared to explain my design. But their concern is from the other pointer of the view, the reason they wish I change the structure is because they want to apply the auto scaling policy they are using now to fit for my workers. I never thought of that. This indeed is a very good suggestion, so I changed the code and message queue to fit for the standard message queue structure so they could apply the auto scaling policy to it. ( For a little more explain about this, the other service will monitoring the message queue, and if the queue is slow, they will add machine as queue consumer, so a standard queue will help to determine whether the queue is slow or not )
So I took 4 suggestions at this point and go back to enhancement my code. and after two weeks, I push the code again. After that I waited for a week and people agree to proceed to deployment again. And they required a small change at friday afternoon, so I said that I will changed it next Monday. And I did the change next Monday, but cloud team disappear again, I waited two weeks, and they came back requiring stress test and very strict code review.
So this project is delayed and delayed, I tried my best to fit for their suggestions but the bar is higher and higher, and I need to wait more on their deployment plan. I was so exhausted on the waiting. And from this point, I was not excited and try to be like the project owner, I went back as a developer, I just tell everyone what I did and waits for requirements from the others.

What I real feels, is that from that point, I changed from a code lover to a code worker. Yes, before I LOVE to code, so I did the job, and I was really happen about the results. But now, I was more and more standardized. I have to say that I learned a lot from the standards and they are very useful. But I lost my love of this project. I appreciate this change made me a better software engineer but I don’t appreciate this project to made me a code-worker. I hope I could get experience and won’t lose my love of coding.

我的疫苗第一针

资格

话说我也是个年轻小伙,本以为肯定是最后一批疫苗,结果3月就可以打上疫苗,真的是幸运。
稍微解释一下,我住在北卡州。北卡州在两周前爆出即将开始内部group 4的疫苗分配。

group 4 具体支持的有这些

对高血压就是其中之一,而我偏偏有高血压:

高血压的问题之后可以再聊,但是阴差阳错使我有了这一次疫苗的资格

话说啥也离不开抢这一件事,当时我记得是说周三开放group4的疫苗预约。我周二晚上就开始了看了,当时医院的网站就500了。看来美国人下手也挺快的。
基本上我看的就是三个网站,分别是cvs,walgreen和自己的医院。其实还有本地的一家超市也提供,叫harris teeters。只是自己最后没有去找。
cvs预约一直是没有位置的,所以我最后就放弃了
walgreen我其实刷到了一个预约,但那个是第二针的预约。本想直接按照第二针的预约去打第一针,反正也一样
最后大概在10点我刷到了几个医院的位置,最后刷到了这个时间上很早的3月的预约。估计是有人放弃了吧。不过距离挺远的,我要开1个小时到打针的地方。还计较什么呢?反正就是尝鲜先打了,周末开车兜兜风了

打针

周末我就出发了,开了一会到了。到那里是在医院的停车场内,工作人员给了我一份资料,核对了我的信息,然后问了我过敏的事情,然后告诉我不用担心我的对猫过敏。之后我开车进入这样一个绿色的停车棚:

对了里面是禁止拍摄的,我摇下车窗脱下外套,挨了一针,当时没啥感觉,然后排队在停车场等了15分钟离开。

之后和副作用

回家我就开始打state of decay游戏,一款病毒丧尸游戏。看着里面的丧尸和注射解药,不知道为什么觉得真的非常的应景。持续打了大概3个小时游戏,当天我强迫自己睡一个午觉。脱衣服的时候惊觉自己的手臂抬起来已经很痛。之后晚上开始看4小时的超长无聊电影。
第二天正常在家工作,干到下午5点,手上活干完了,想着稍微休息一下准备看一下明天的安排,突然发现自己眼皮非常酸,就开始躺床上休息。此时大概是疫苗打后30小时,然后开始发冷,我记得当时我看室外温度是21摄氏,室内我一直开空调是76华氏度也就是25摄氏。但是我必须要盖我冬天盖的毛毯,不然就会觉得冷。睡了一会到7点,丧失皮肤的冷热感觉,开始口干,但是手臂酸痛开始缓解。然后我就开始了喝水上厕所的循环,我的饮水量大概是一般时候的一倍。期间我量过自己口腔体温,36.6并没有发烧,但是窝在被窝里开始发汗,我换了薄被子仍然发汗,总体感觉就像感冒。
然后就是今天,起床时候已经没有发冷和疲惫状态,手臂酸痛更轻了,洗了一个澡后正常上班。到目前没有什么异样。

Routing with ubuntu server

Goal & Requirement

  1. I bought a NAS, I am going to use it to do backups, in the meantime, enhance the whole networks.
  2. I have an VPN server, I hope I could use that to access home service from outside, instead of using DDNS or static ip.
  3. I wish to open some service from my home networks, eg: vs code server, jupyter notebook server, so that I only need chrome for mobile devices. And for some small projects like this blog project, I could put the code in the server and use one machine to maintain the blog.

Design

Old Design and Problem

The old design is showed above,
Solution is to used a server to run ubuntu as a middle router, and it needs to provide:

  1. connection with the outside internet
  2. connection with the VPN server
  3. provide dhcp assignment for inner-network

Speed become the biggest problem of this. I found that OpenVPN has a speed limit here, normally 100Mbps is the limits. That is unacceptable. And the VPN server is not physically near my home, there already lag, but I thought I could do compromise about the lag. But not the speed, never compromise the speed.

New Design and solution

The new design is showed above:
The solution is to separate the whole inner-network into three parts:

  1. modem will assign/lease the ips of 192.168.1.xxx, this will be used for most the device to connect to the internet, it is a full speed network.
  2. raspberry pi will assign/lease the ips of 192.168.2.xxx, which will be used to auto connect to VPN, it is a limited speed network.
  3. vpn server will assign/lease ips of 10.8.0.xxx. Inner service will be open to this network.

So, if want to get full speed, choose modem network, if want to get vpn, choose raspberry network. Raspberry pi will forwarding ports to VPN networks, so when connection from VPN, they could access inner-network service. And even though we expose this service, they are still inside the VPN, so it is very safe.

ubuntu useful tools

As we mentioned before, the core of this design is in the raspberry pi. There are many manipulate on the ubuntu server in raspberry. I read a lot to learn this. So I have this part as a reference for next steps, here we will talk about some tools for future use.

crontab

cronis a very common ubuntu command for timer behavior. It provide a trigger, so at a certain time, it could trigger a action. We introduce it here because it has a config option @reboot, which could be used to add some command at the time of the server boot up, this is much easy to create a ubuntu service to do the same thing.

install:

1
apt-get install cron

run this

1
crontab -e

add this line in the file:

1
@reboot Your-Command

screen

This is a very common ubuntu tool for running a long-live service, it will help you to keep a service alive, even when you close the terminal.
Here are some common command:

  1. Check all exist screens
    1
    screen -ls
  2. Add a detached screen and run a command
    1
    screen -dmS sessionName command
  3. Connect back to detached screen
    1
    screen -x sessionName
    Three command will be enough for now.

openvpn

Install openvpn server in your machine is this a very good blog from digital ocean, to set up a openvpn server.

openvpn client:
This command will help you to start the client, still you will need to provide configs and password before execution.

1
sudo openvpn --config xx.ovpn --askpass pass.file

ubuntu netplan && network

If you used ubuntu desktop and change to ubuntu server, you will find this netplan is introduced after 16.04(?) and replace of the network. Still you could change back to edit /etc/networks/interfaces with some extra settings, but we will talk about netplan here.
netplan provides some configs in different files.

1
2
00-installer-config.yaml
50-cloud-init.yaml

And you can use one command to create combined rules for test run

1
sudo netplan --debug generate

At the end you could apply the rules

1
sudo netplan apply

And each file is a yaml file,there is a example for a static ip connection setting:

1
2
3
4
5
6
7
8
9
10
network:
ethernets:
eth0:
addresses:
- 192.168.1.2/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
version: 2

iptables

iptables is used to setup ip transfer and port forwarding.
Let’s see some example of the command for iptables.

check

When you need to check the nat status

1
sudo iptables -t nat -vnL

POSTROUTING && PREROUTING

When you need to set routings:

1
2
sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
sudo iptables -t nat -A PREROUTING -p tcp --dport 21 -j DNAT --to-destination 192.168.1.2:21

The first one means that, when all the connection in the post-routing status, set them to handled by network interface tun0. So that all the network will pass to tun0 to handle.
The second one means in the phrase of pre-routing, forwarding all the request for port 21 to port 21 in 192.168.1.21.

And if you want to delete the rule above when you make some mistakes, simply change A to D here

1
2
sudo iptables -t nat -D POSTROUTING -o tun0 -j MASQUERADE
sudo iptables -t nat -D PREROUTING -p tcp --dport 21 -j DNAT --to-destination 192.168.1.200:21

Implementation

I followed this blog to start, and did some adjustments on my own:
Base

  1. Change ip
  2. Set up dhcp server
  3. Config ip transfer
  4. Setting NAT routing

Most of the steps are the works for me in 1-3 steps, and I will need to do some more the step 4.

How I setup my NAT routing rules

Let’s talk about network interfaces first

I have three network interfaces, they are represent to three networks, two of them set in netplan, and tun0 is created automatically when vpn connection set up:

  • eth0 is used for 192.168.1.xxx
  • tun0 is the vpn, used for 10.8.0.xxx
  • eth1 is used for 192.168.2.xxx

And my post-routing is very easy, like this:

1
2
3
4
5
Chain POSTROUTING (policy ACCEPT 14213 packets, 1060K bytes)
pkts bytes target prot opt in out source destination
39 2702 MASQUERADE all -- * eth0 0.0.0.0/0 192.168.1.0/24
2 96 MASQUERADE all -- * eth1 0.0.0.0/0 192.168.2.0/24
4101 2089K MASQUERADE all -- * tun0 192.168.2.0/24 0.0.0.0/0

Some explains:

  • For all connection target 192.168.1.xxx they will be change to eth0 to handle
  • For all connection target 192.168.2.xxx they will be change to eth1 to handle
  • The rest will go to tun0
    First two will used in port forwarding next, the last one is used for inner network connection

Then you could use command like this sudo iptables -t nat -D PREROUTING -p tcp --dport 21 -j DNAT --to-destination 192.168.1.2:21 to do port forwarding. Here I forwarding connection to port 21 to 192.168.1.2

How to save all routing settings persistently

First of all, beside tun0, all the rest settings could saved as the blog talked before using iptables-persistent. Base the only problem is, when updating the rules, I have to reinstall iptables-persistent and saved again, otherwise it wont saved, I dont know whether this is a bug.
However, this cannot work for rules related to tun0, because tun0 only exist when the VPN connection is established. So if you saved the operation above, when you do the restarting, it will auto change will is tun0 to eth0.

1
2
3
4
5
Chain POSTROUTING (policy ACCEPT 14213 packets, 1060K bytes)
pkts bytes target prot opt in out source destination
39 2702 MASQUERADE all -- * eth0 0.0.0.0/0 192.168.1.0/24
2 96 MASQUERADE all -- * eth1 0.0.0.0/0 192.168.2.0/24
4101 2089K MASQUERADE all -- * eth0 192.168.2.0/24 0.0.0.0/0

So what we will do is to used auto start to start VPN client first, then add the NAT routing rule. The way to do it is to use crontab -e to add a command to do execution.

1
@reboot bash /home/xxx/start-client.sh

And the file /home/xxx/start-client.sh is like:

1
2
3
screen -d -m -S openvpn sudo openvpn --config /home/xxx/xxx.ovpn --askpass /home/xxx/openvpnpass.txt
sleep 10
sudo iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o tun0 -j MASQUERADE

Some tips

When you try to setup network, you might lose the connection to the server if you make mistakes. Before this I didnt bought the hdmi cable for raspberry pi, so I have to reset it several times to get connection back. So I highly recommend you put a plan B here when network connection broken, use a hdmi to change the mistake back.
Also, when you do some changes for the whole networks, you might still have a wifi connection from the top level. So if connection is error, you still have a connection to the internet from wifi, you can still do some search on the problem. And it will be easy to set it back when you finished.


ubuntu server实现路由

目标和需求

  1. 买了一个nas,想用来做备份,加强一下家庭网络
  2. 有一个自己的vpn服务器,想用这个代替公网IP/DDNS,来做家庭网络的户外接入
  3. 想在内网开放一些服务,比如vs code的服务器端,或者jupyter notebook这类的服务器端,这样移动设备只需要chrome,而且处理blog的代码也就可以放在一处了。

设计

旧设计与问题

旧设计如图,
思路是用一台跑ubuntu server的服务器作为中间的路由器。它能够:

  1. 连接外网
  2. 连接VPN服务器
  3. 提供内网的ip分配

实现下来发现最大问题是网速。我使用的OpenVPN的网速是有上限的,100Mbps是一般情况下的极限。这个网速是完全不够的,况且本身vpn服务器的地理位置也不是我目前家里的位置,延迟本就很高,本来还想勉强接受一下延迟,但是这个网速肯定不行。

新设计与解决问题

新设计如图:
思路是将整体分为三个部分:

  1. 光猫分配192.168.1.xxx网段,作为大多数设备的主要上网网段,这个网段是全速的。
  2. 树莓派分配的192.168.2.xxx网段,作为自动连接vpn的网段,这个网段是vpn的。
  3. vpn服务器分配的10.8.0.xxx网段,作为外部连接连回时候使用的网段,这个网段主要是要开放内网服务的。

这样解决的是,如果内网要全速就走光猫网段,如果要vpn就走树莓派网段,换个无线连接就可以了。树莓派上面转发内网的服务给vpn网段,这样在外面连上vpn就可以用内网的服务了,这些服务虽说是暴露,但其实是面向vpn内网的,也比较安全。此外树莓派我是买一个usb网络转换器的。

ubuntu server有关的工具

如图所示,我们的核心就是被色彩包围的树莓派的操作,这部分涉及很多ubuntu server的尝试。我也是看了很多资料,边看边学。这部分先整理一下,主要围绕很多必要的工具的介绍和使用方法。

crontab

cron是一种常用的ubuntu定时执行命令的工具。它提供一种触发,在某个时间点,能够执行一个动作。这里提到它是因为它有一个@reboot的选项,可以用来插入一段命令,做ubuntu的自启动。这个的配置比写ubunt的服务要方便的多。

安装:

1
apt-get install cron

运行这个

1
crontab -e

然后在文档里面添加:

1
@reboot Your-Command

screen

ubuntu里面非常常用的跑常驻程序的工具,它让你不会因为终端的断开就结束运行着的程序。
这里介绍几个命令:

  1. 检查已有的screen
    1
    screen -ls
  2. 添加一个screen并运行命令,且离开
    1
    screen -dmS sessionName command
  3. 连接screen
    1
    screen -x sessionName
    有了这里的几个命令就可以很快上手screen

openvpn

首先是服务器部分,digital ocean有一个很好的openvpn server搭建的教程。我就不在多言。

openvpn客户端:
一条命令可以让客户端跑起来。当然你要把配置和密码存好

1
sudo openvpn --config xx.ovpn --askpass pass.file

ubuntu netplan && network

如果你用过ubuntu桌面版,再使用ubuntu服务器版,就会发现,可能是16.04以后新引入了netplan的写法代替了network的default写法。当然可以取消掉netplan的配置仍然编辑/etc/networks/interfaces,不过这里说一下我理解的netplan的配置。
netplan提供了不同等级的配置文件:

1
2
00-installer-config.yaml
50-cloud-init.yaml

之后使用命令来生成一个综合了以上规则的plan

1
sudo netplan --debug generate

最后在用apply来实现

1
sudo netplan apply

具体写法是yaml语法,这里给一个网络接口与静态ip设置的简单例子:

1
2
3
4
5
6
7
8
9
10
network:
ethernets:
eth0:
addresses:
- 192.168.1.2/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
version: 2

iptables

iptables是用来做ip变换的设置工具。
iptables的一般规则使用方法如下,但也不是所有的选项在一条命令内都存在:
iptables [-t 表名] <-A|I|D|R> 链名 [规则编号] [-i|o 网卡名称] [-p 协议类型] [-s 源ip|源子网] [--sport 源端口号] [-d 目的IP|目标子网] [--dport 目标端口号] [-j 动作]

其中动作的选择有:

1
2
3
4
5
6
7
ACCEPT          接收数据包
DROP             丢弃数据包
REDIRECT      将数据包重新转向到本机或另一台主机的某一个端口,通常功能实现透明代理或对外开放内网的某些服务
SNAT             源地址转换
DNAT             目的地址转换
MASQUERADE       IP伪装
LOG               日志功能

以及操作类型的解释

1
2
3
4
-A 增加
-I 插入
-D 删除
-R 替换

我们来看一些具体的例子

check

当你需要查看nat的情况时

1
sudo iptables -t nat -vnL

POSTROUTING && PREROUTING

当你需要设置routing操作的时候

1
2
sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
sudo iptables -t nat -A PREROUTING -p tcp --dport 21 -j DNAT --to-destination 192.168.1.2:21

第一条的意思是将所有的信息在postrouting过程中,将出口设置为tun0网卡来处理。这样所有的网络都会通过tun0网卡来发送。
第二条的意思是在prerouting阶段,对该服务器的21端口的请求转发到192.168.1.2的21端口上

如果你要删去上述两条:

1
2
sudo iptables -t nat -D POSTROUTING -o tun0 -j MASQUERADE
sudo iptables -t nat -D PREROUTING -p tcp --dport 21 -j DNAT --to-destination 192.168.1.200:21

具体实现过程

我的实现,是这位大神实现的一个扩展:
Base

  1. 修改ip
  2. 配置dhcp
  3. 配置ip转发
  4. 设置NAT routing

其中前三项基本没有什么改变,所有的额外操作都在第四条的时候。

如何设置我的NAT routing规则

首先是网卡

我有三张网卡,对应着三个不同的子网,这些配置在netplan之中设置好了(tun0是vpn client自动生成的):

  • 网卡1是eth0,对应 192.168.1.xxx
  • vpn是tun0,对应 10.8.0.xxx
  • 网卡2是eth1,对应 192.168.2.xxx

然后我的post routing很简单

1
2
3
4
5
Chain POSTROUTING (policy ACCEPT 14213 packets, 1060K bytes)
pkts bytes target prot opt in out source destination
39 2702 MASQUERADE all -- * eth0 0.0.0.0/0 192.168.1.0/24
2 96 MASQUERADE all -- * eth1 0.0.0.0/0 192.168.2.0/24
4101 2089K MASQUERADE all -- * tun0 192.168.2.0/24 0.0.0.0/0

解释一下,

  • 所有目的地是 192.168.1.xxx 这类的请求走eth0,
  • 所有目的地是 192.168.2.xxx 的请求走eth1,
  • 剩下的从内网发出来的信息走tun0。
    前两条为之后实现内网端口转发作准备,后一条是实现内网都是vpn连接

然后就是用类似这样的请求 sudo iptables -t nat -D PREROUTING -p tcp --dport 21 -j DNAT --to-destination 192.168.1.2:21把你要转发的端口转发出去

如何永久保存routing信息

首先跟tun0无关的所有信息都是可以利用Base这里的方法进行保存,即iptables-persistent来保存,不过我这里有一个问题,每次更新的时候我需要重装iptables-persistent,再保存,不然就会消失。我不请求是不是bug。
不过跟tun0有关的信息就不能这样了,因为tun0只有在vpn启动的时候才会出现,所以如果你保存了上述的操作,重启后,会看到默认把tun0的地方改成eth0

1
2
3
4
5
Chain POSTROUTING (policy ACCEPT 14213 packets, 1060K bytes)
pkts bytes target prot opt in out source destination
39 2702 MASQUERADE all -- * eth0 0.0.0.0/0 192.168.1.0/24
2 96 MASQUERADE all -- * eth1 0.0.0.0/0 192.168.2.0/24
4101 2089K MASQUERADE all -- * eth0 192.168.2.0/24 0.0.0.0/0

所以我们要利用自启动来启动vpn,再增加这条命令。具体就是利用crontab添加一条执行,执行内容是openvpn,并且写入nat crontab -e 添加

1
@reboot bash /home/xxx/start-client.sh

具体执行脚本/home/xxx/start-client.sh:

1
2
3
screen -d -m -S openvpn sudo openvpn --config /home/xxx/xxx.ovpn --askpass /home/xxx/openvpnpass.txt
sleep 10
sudo iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o tun0 -j MASQUERADE

额外的坑

调试网络很麻烦的点就是网络配置错了,就连不上了。我当时没有买视频线,所以重置了几次树莓派。比较建议一定要用视频线直接连接,你在尝试的时候指不定就掉了。
此外,在做全局调整的时候也要用wifi先保证一个点可以接入互联网,这样也方便查阅资料,不然总机网络错了全部都连不上了。