#import "@tola/source:0.0.0": tola-meta #import "@tola-modules/nasapunk:0.0.0": callout #tola-meta(( title: "Pass the Wall", date: "2025-03-30", summary: "汇总 SSH 端口转发、Xray 与 FRP 容器代理、Docker daemon 代理、OpenWrt 旁路由设置及常用开发工具国内镜像配置。", tags: ("Dev",), weight: 10, )) #quote(block: true)[ \"Across the Great Wall we can reach every corner in the world.\"#html.sup(class: "fnref", id: "fnref-1", link("#fn-1")[1]) ] = Remote Port Forwarding #quote(block: true)[ 让云服务器方便快捷地使用本地代理 ] + 建立远程端口转发 ```shell ssh -R 1082:127.0.0.1:1082 s2-ljy -p 22 ``` #block[ #set enum(numbering: "1.", start: 2) + 配置云服务器的代理环境变量 ] ```shell export HTTP_PROXY="http://127.0.0.1:1082" export HTTPS_PROXY="http://127.0.0.1:1082" ``` #callout(kind: "warning", title: "CAUTION")[ 这是临时设置, 仅对当前终端生效, 如果想永久设置, 请编辑 `~/.bashrc` ] == Local vs Remote Port Forwarding === Local Port Forwarding ```shell ssh -L [本地端口]:[目标主机]:[目标端口] [SSH服务器] ``` 作用: - 在本地机器上监听一个端口,所有发往该端口的流量都会通过 SSH 隧道转发到目标主机:目标端口 - 适用于访问远程内网服务(如数据库、Web 服务) Example: ```shell ssh -L 8080:localhost:80 user@remote-server ``` 本地访问 `localhost:8080` → 实际访问的是 `remote-server` 上的 `localhost:80` === Remote Port Forwarding ```shell ssh -R [远程端口]:[目标主机]:[目标端口] [SSH服务器] ``` 作用: - 在远程 SSH 服务器上监听一个端口,所有发往该端口的流量都会通过 SSH 隧道转发到目标主机:目标端口(通常是你的本地机器) - 适用于让外部访问你的本地服务(如本地开发环境暴露给公网) Example: ```shell ssh -R 1082:127.0.0.1:1082 s2-ljy -p 22 ``` s2-ljy 这台服务器上访问 `localhost:1082` -\> 实际访问的是本机的 `localhost:1082` #callout(kind: "info", title: "SUMMARY")[ | 特性 | Local Port Forwarding (`-L`) | Remote Port Forwarding (`-R`) | |-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-|-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-|-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-| | #strong[监听端口的机器] | 你的本地电脑 | 远程 SSH 服务器 | | #strong[数据流向] | 本地 → 远程 | 远程 → 本地 | | #strong[典型用途] | 访问远程内网服务 | 暴露本地服务给远程 | | #strong[示例] | `ssh -L 8080:localhost:80 user@server` | `ssh -R 9000:localhost:3000 user@server` | ] = Xray Container 如果想在服务器的 docker container 里使用代理,如果只使用远程端口转发非常不方便,我们可以开启一个 Xray 容器来使用代理。 ```shell mkdir xray touch xray/config.json ``` 然后把下面的配置写入 `xray/config.json` 里: ```json { "log": { "loglevel": "info" }, "inbounds": [ { "listen": "0.0.0.0", "port": 10809, "protocol": "http", "settings": {} } ], "outbounds": [ { "tag": "proxy", "protocol": "vless", "settings": { "vnext": [ { // let GPT generate it for you } ] }, "streamSettings": { "network": "tcp", "security": "reality", "realitySettings": { "publicKey": "2O1Q7AJJC5sTUGsnxn_xE4APz-SNeFUEIHfAO00463g", "serverName": "yahoo.com", "shortId": "c35f90d2", "fingerprint": "chrome" } } }, { "tag": "direct", "protocol": "freedom" }, { "tag": "block", "protocol": "blackhole" } ], "dns": { "servers": ["223.5.5.5", "119.29.29.29", "1.1.1.1", "8.8.8.8"] }, "routing": { "domainStrategy": "IPIfNonMatch", "rules": [ { "type": "field", "ip": ["geoip:private"], "outboundTag": "direct" }, { "type": "field", "domain": ["geosite:cn"], "outboundTag": "direct" }, { "type": "field", "ip": ["geoip:cn"], "outboundTag": "direct" }, { "type": "field", "domain": ["geosite:category-ads-all"], "outboundTag": "block" }, { "type": "field", "network": "tcp,udp", "outboundTag": "proxy" } ] } } ``` 然后运行 Xray 容器: ```shell docker network create proxy docker run -d -p 10809:10809 --name xray --network proxy --restart=always -v ./xray:/etc/xray teddysun/xray ``` #callout(kind: "note", title: "NOTE")[ 注意这里的 `-p 10809:10809`,如果你要修改的话,需要把 `config.json` 里的 `inbounds` 的 `port` 一起修改。 ] 然后你就能愉快的使用代理了,在服务器里设置环境变量: ```shell export http_proxy="http://localhost:10809" export https_proxy="http://localhost:10809" export HTTP_PROXY="http://localhost:10809" export HTTPS_PROXY="http://localhost:10809" export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com,$(hostname)" export NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com,$(hostname)" ``` 如果需要让其他容器使用这个代理,可以把其他容器加入到 `proxy` 这个网络里: ```shell docker network connect proxy [other-container-name] ``` 在容器里设置代理环境变量: ```shell export http_proxy="http://xray:10809" export https_proxy="http://xray:10809" export HTTP_PROXY="http://xray:10809" export HTTPS_PROXY="http://xray:10809" export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com,$(hostname)" export NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com,$(hostname)" ``` #callout(kind: "info", title: "SUMMARY")[ 不需要透明代理的话,这样设置是最简单的 如果需要透明代理(比如 TUN 模式),可以直接在宿主机上使用 Xray 的 TUN 功能 ] = FRP FRP (Fast Reverse Proxy) 是一个高性能的反向代理应用,主要用于内网穿透。它允许你将内网服务暴露到公网上,方便远程访问。 具有公网 IP 的服务器上运行 FRP 的服务端 (frps),而在内网机器上运行 FRP 的客户端 (frpc)。 == frps 配置 ```toml bindPort = 7000 auth.method = "token" auth.token = "your_secure_token" allowPorts = [ { start = 6000, end = 6299 } ] ``` 然后通过以下命令启动 frps: ```shell docker run -d \ --name frps \ --restart unless-stopped \ -p 7000:7000 \ -p 6000-6299:6000-6299 \ -v $(pwd)/frps.toml:/etc/frp/frps.toml \ snowdreamtech/frps ``` == frpc 配置 ```toml serverAddr = "your_frps_server_ip" serverPort = 7000 auth.method = "token" auth.token = "your_secure_token" [[proxies]] name = "ssh" type = "tcp" localPort = 22 remotePort = 6000 ``` 然后通过以下命令启动 frpc: ```shell docker run -d \ --name frpc \ --restart unless-stopped \ --network host \ -v $(pwd)/frpc.toml:/etc/frp/frpc.toml \ snowdreamtech/frpc ``` 查看一下日志,确保正常启动了 ```shell docker logs frpc start frpc service for config file [/etc/frp/frpc.toml] try to connect to server... login to server success proxy [ssh] start success ``` 然后就可以通过下面的命令来访问: ```shell ssh -p 6000 kkkzoz@[your_frps_server_ip] ``` = Docker == Docker save/load ```shell docker save snowdreamtech/frpc:latest > frpc-image.tar docker load < frpc-image.tar ``` == Docker http-proxy #quote(block: true)[ 假设已经在本地 10809 端口开启了代理 ] 注意这么设置的是 Docker daemon 的代理,只会用于: - docker pull / push - docker build(RUN apt / curl / wget) - daemon 自身的 HTTP 请求 #strong[容器本身的 Proxy 不会受影响] ```shell sudo mkdir -p /etc/systemd/system/docker.service.d sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf ``` 写入以下内容 ```conf [Service] Environment="HTTP_PROXY=http://localhost:10809" Environment="HTTPS_PROXY=http://localhost:10809" Environment="NO_PROXY=localhost,127.0.0.1" ``` 然后执行 ```shell sudo systemctl daemon-reexec sudo systemctl restart docker ``` = Network Proxy 工位上的主机网络带宽被限制了,在白天只有 1MB/s 但是所机房的服务器网络带宽没有被限(通常有 20MB/s),而且工位的主机访问所机房服务器的速度也很快 所以我们就可以通过流量转发的方式,把工位主机的网络流量转发到所机房服务器上,达到网络加速的效果 在服务器上: ```shell docker run -d \ --name gost-server \ --restart=always \ --net=host \ ginuerzh/gost \ -L admin:123456@:1080 ``` 在工位主机上: ```shell export http_proxy="http://admin:123456@124.16.138.62:1080" export https_proxy="http://admin:123456@124.16.138.62:1080" export HTTP_PROXY="http://admin:123456@124.16.138.62:1080" export HTTPS_PROXY="http://admin:123456@124.16.138.62:1080" ``` 如果有图形界面,可以在系统设置 -\> 网络 中设置代理,这样浏览器之类的也会走这个代理 = Software Router 记录一下折腾软路由的过程,我主要是用于旁路由 #quote(block: true)[ 很早之前买了一个电犀牛的 R66s,大三大四还有研一在用,回所后吃灰了一段时间 ] 第一步是刷机,可以从#link("https://github.com/haiibo/OpenWrt")[这里]获取镜像 刷到内存卡上,然后用网线连接路由器的 LAN 和你的电脑 默认管理后台是 #link("http://192.168.1.1") 打开后主要设置网络接口,把软路由 LAN 口的属性固定下来: ```config IP: 一个固定的 IP 网关:实际路由器的 IP DNS:实际路由器的 IP ``` 最重要的一点,记得关闭这个口的 DHCP 设置好后,在 PassWall 或者类似的插件中设置一下,然后把软路由的 LAN 口连接到路由器的 LAN 口就行了 对于要上网的设备来说,还是正常连接路由器,然后手动设置 IP ```config IP: 一个固定的 IP 网关:软路由的 IP DNS:软路由的 IP ``` = v2rayA ```shell docker run -d \ --restart=always \ --privileged \ --network=host \ --name v2raya \ -e V2RAYA_LOG_FILE=/tmp/v2raya.log \ -e V2RAYA_V2RAY_BIN=/usr/local/bin/xray \ -e V2RAYA_NFTABLES_SUPPORT=off \ -e IPTABLES_MODE=legacy \ -v /lib/modules:/lib/modules:ro \ -v /etc/resolv.conf:/etc/resolv.conf \ -v /etc/v2raya:/etc/v2raya \ mzz2017/v2raya ``` = Configuring Mirrors for Development Tools == pip - 临时使用: ```shell pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple [some-package] ``` - 设为默认: 升级 pip 到最新的版本后进行配置: ```shell python -m pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple --upgrade pip pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple ``` == pyenv ```shell export PYTHON_BUILD_MIRROR_URL="https://registry.npmmirror.com/-/binary/python" export PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM=1 # Then pyenv install 3.12 ``` == poetry 通过以下命令为单个项目设置首选镜像: ```shell poetry source add --priority=primary mirrors https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ ``` == rust 安装 Rust: ```shell export RUSTUP_DIST_SERVER="https://rsproxy.cn" export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup" curl --proto '=https' --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh ``` 设置 crates.io 镜像: 修改 \~/.cargo/config ```toml [source.crates-io] replace-with = 'rsproxy-sparse' [source.rsproxy] registry = "https://rsproxy.cn/crates.io-index" [source.rsproxy-sparse] registry = "sparse+https://rsproxy.cn/index/" [registries.rsproxy] index = "https://rsproxy.cn/crates.io-index" [net] git-fetch-with-cli = true ``` == npm ```shell # 查询当前使用的镜像源 npm get registry # 设置为淘宝镜像源 npm config set registry https://registry.npmmirror.com/ # 验证 npm get registry ``` == yarn ```shell # 查询当前使用的镜像源 yarn config get registry # 设置为淘宝镜像源 yarn config set registry https://registry.npmmirror.com/ ``` == pnpm ```shell # 查询当前使用的镜像源 pnpm get registry # 设置为淘宝镜像源 pnpm config set registry https://registry.npmmirror.com/ ``` == golang ```shell go env -w GOPROXY=https://goproxy.cn,direct ``` == git #quote(block: true)[ 不如直接开代理 ] ```shell git clone https://gitclone.com/github.com/gogs/gogs.git ``` 更多配置方式请参阅#link("https://gitclone.com/docs/feature/gitclone_web")[官网] == maven `vim ~/.m2/settings.xml` ```xml aliyunmaven * 阿里云公共仓库 https://maven.aliyun.com/repository/public ``` #html.hr() #html.section(class: "footnotes", html.ol({ html.li(id: "fn-1")[ #link("https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E7%AC%AC%E4%B8%80%E5%B0%81%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6/15554560")[China first email] #link("#fnref-1")[↩] ] }))