Skip to content

Latest commit

 

History

History
643 lines (452 loc) · 21.3 KB

20231029_01.md

File metadata and controls

643 lines (452 loc) · 21.3 KB

macOS 通过“oversea region ECS和ssh隧道转发代理请求” OR “openVPN” 提升github等访问体验 - chrome Proxy SwitchyOmega , cli ... 可用

作者

digoal

日期

2023-10-29

标签

PostgreSQL , PolarDB , macOS , 软路由 , 代理 , 网关 , Proxy SwitchyOmega , 浏览器代理 , openVPN , cli , curl , http , https , socks


背景

很多好的开源项目都在github上, 例如PolarDB开源数据库:

https://github.com/digoal/blog/blob/master/README.md

https://apsaradb.github.io/PolarDB-for-PostgreSQL/zh/

https://github.com/polardb

但是github的访问经常不稳定, 对于渴望学习的同学来说非常痛苦, 为了提升学习体验, 终极解决办法是弄个ECS来转发http请求提升访问体验.

本文仅供热爱学习的爱好者参考, 请勿用于其他用途.

  • PS: 生我们养我们的是我们脚下的土地和我们的国家, 我们是命运共同体, 很多信息有立场才有判断的标准, 更重要的应该是我们的时间应该更多的花在能让这个命运共同体往更好的方向发展的事情上, 而不是花在看热闹、谈资、心理内耗等上面. 干就对了, 用战绩去证明你, 而不是打嘴炮. 放在全球化合作供需连的模式中, 你在全球化中的位置也是由你的比较优势决定, 所以努力吧少年.

如果你不想使用本文提到的方法, 也可以研究一下这一篇的方法:

准备

1、macOS
2、chrome 浏览器
3、在aliyun选购1台新加坡(或其他oversea region)的ecs(1c, 0.5G 的突发型即可), 选择使用debian 11.7系统, 开启公网(选择固定带宽模式, 1Mib足够使用, 可以看youtube 1080p视频不卡.), 20GiB云盘, 关闭其他任何可能产生费用的功能. 最终价格约1700元(5年).

  • 记下公网IP, root密码.

进入ecs控制台, 配置:

  • 安全组配置: 禁止icmp 和 3389端口, 只保留22端口入连接.

为了进一步提高安全性, 可以关闭root的ssh远程登录, 还可以配置sshd_config把ssh监听端口号改成其他大端口号, 同时在aliyun控制台安全组配置中开放此端口. (此处略)

配置ecs

1、添加普通用户 digoal

# 连接到ecs  
ssh root@公网IP  
  
# 添加用户  
useradd digoal  
  
# 创建新用户home目录  
mkdir /home/digoal  
  
# 修改home目录owner  
chown digoal:digoal /home/digoal  
  
# 设置新用户密码, 建议具有一定复杂度. 当然你也可以使用key登陆, 这里忽略.   
password digoal  

路由跟踪, 检查网络跳数, 质量:

# 本地执行
traceroute -v ECS公网IP

2、配置内核参数.

可以参考如下:

https://www.cnblogs.com/jmbt/p/16653630.html

https://developer.aliyun.com/article/700149

# 连接到ecs  
ssh root@公网IP  
  
# 修改配置文件  
vi /etc/sysctl.d/99-sysctl.conf   
  
# add by digoal  
net.ipv4.ip_forward = 1  
net.ipv4.tcp_syncookies = 1  
fs.file-max = 65535  
net.ipv4.tcp_fin_timeout = 15  
net.ipv4.tcp_tw_reuse = 1  
net.ipv4.tcp_syn_retries = 1  
net.ipv4.tcp_synack_retries = 1  
net.ipv4.tcp_max_orphans = 3276800  
net.core.rmem_max = 16777216  
net.core.rmem_default = 8388608  
net.core.wmem_max = 16777216  
net.core.wmem_default = 8388608  
net.ipv4.tcp_wmem = 4096 131072 1048576  
net.ipv4.tcp_rmem = 4096 131072 1048576  
net.ipv4.tcp_window_scaling = 1  
net.ipv4.tcp_sack = 1  
net.core.netdev_max_backlog = 262144   
net.core.somaxconn = 262144   
net.ipv4.tcp_max_syn_backlog = 262144   
net.ipv4.tcp_timestamps = 0   
net.ipv4.ip_local_port_range = 2048 65000  
net.ipv4.icmp_echo_ignore_broadcasts = 1  
net.ipv4.icmp_ignore_bogus_error_responses = 1  
kernel.pid_max = 65536  
net.ipv4.tcp_keepalive_time=15
net.ipv4.tcp_keepalive_intvl=5
net.ipv4.tcp_keepalive_probes=3
  
# 使参数生效  
sysctl -p  

3、配置sshd

# 连接到ecs  
ssh root@公网IP  
  
# 修改配置文件  
vi /etc/ssh/sshd_config  
GatewayPorts yes  
# 禁止root远程登录ssh, 使用普通用户登陆ssh后su - root, 更安全
PermitRootLogin no
  
# 使配置生效  
systemctl restart sshd  

4、关闭aliyundun (可选, 因为我这个ecs配置太低, 担心aliyundun影响性能, 而且我的使用场景没有安全诉求, 所以关掉aliyundun.)

# 连接到ecs  
ssh root@公网IP  
  
# 关闭aliyundun  
systemctl disable aliyun.service  
systemctl stop aliyun.service  
systemctl disable aegis  
systemctl stop aegis  
  
# 重启ecs  
reboot  

cli代理 及 google chrome代理配置

假设通过本地1111端口进行代理转发.

1、macOS, 使用ssh创建转发链路

ssh -CqtfnN -D 127.0.0.1:1111 -p 22 digoal@ecs公网IP   
  
# 或 设置cipher. 加密算法越弱, 对性能的损耗越小.    
  
ssh -CqtfnN -c chacha20-poly1305@openssh.com -o "MACs umac-64@openssh.com" -D 127.0.0.1:1111 -p 22 digoal@ecs公网IP  
ssh -CqtfnN -c aes128-ctr -o "MACs umac-64@openssh.com" -D 127.0.0.1:1111 -p 22 digoal@ecs公网IP  
  
# 使用ssh命令查询当前系统支持哪些cipher? 
# ssh -Q cipher
3des-cbc
aes128-cbc
aes192-cbc
aes256-cbc
rijndael-cbc@lysator.liu.se
aes128-ctr
aes192-ctr
aes256-ctr
aes128-gcm@openssh.com
aes256-gcm@openssh.com
chacha20-poly1305@openssh.com
  
# 通过ChaCha20-Poly1305提升SCP传输速率: 
# https://blog.csdn.net/nanhai_happy/article/details/126655065

macOS, 你也可以把以上过程写成脚本, 方便调用.

# 编辑脚本  
vi ~/proxy.sh   
  
  
#!/bin/bash  
    
user="digoal"  
host="ecs公网IP"  
port="22"  
pwd="digoal用户密码"  
  
/usr/bin/expect <<-EOF
spawn ssh -CqtfnN -c aes128-ctr -o "MACs umac-64@openssh.com" -o TCPKeepAlive=yes -o ServerAliveInterval=10 -o ServerAliveCountMax=300000 -D 127.0.0.1:1111 $user@$host -p $port
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$pwd\r" }
}
interact
expect eof
EOF
  
# 设置shell中使用代理的环境变量  
# 设置所有请求代理都走 127.0.0.1:1111    
export all_proxy=socks5://127.0.0.1:1111  
  
# 如果要取消all_proxy代理设置, 执行如下unset即可  
# unset all_proxy    
  
date  
# proxy.sh 脚本内容 完    
  
  
  
# 设置脚本权限  
chmod 500 ~/proxy.sh   

macOS, 创建ecs的ssh登陆脚本, 方便调用以登陆ecs.

# 编辑脚本  
vi ~/ecs.sh  
  
  
#!/usr/bin/expect    
    
set user "root"    
set host "ecs公网IP"    
set port "22"    
set pwd "root用户密码"  
    
spawn ssh -C -o TCPKeepAlive=yes -o ServerAliveInterval=10 -o ServerAliveCountMax=300000 $user@$host -p $port
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$pwd\r" }
}
interact
  
  
  
# 设置脚本权限  
chmod 500 ~/ecs.sh    

2、macOS, 创建ssh代理转发链路. 这个链路要保持时刻存在, 用于转发代理请求到oversea region ECS. 如果进程不存在了, 再次调用proxy.sh即可建立连接.

# 在shell中执行脚本:  
~/proxy.sh   
  
  
# 查看是否连上ecs ssh?      
ps -efw|grep ssh  
  
# 看到有一条这样的结果, 表示已连接ssh  
501 96986     1   0 12:16下午 ??         0:07.84  ssh -CqtfnN -c aes128-ctr -o MACs umac-64@openssh.com -D 127.0.0.1:1111 digoal@ecs公网IP -p 22  

3、macOS, 在cli命令行测试socks5是否可正常代理? 以及测试转发速度.

https://zhuanlan.zhihu.com/p/269878724

https://www.cnblogs.com/LBSD/p/15675131.html

macOS, 在命令行测试socks5是否可正常代理?

digoaldeMacBook-Pro:Downloads digoal$ export all_proxy=socks5://127.0.0.1:1111    
  
# 如果返回的是ecs公网IP, 表示代理正常.    
digoaldeMacBook-Pro:Downloads digoal$ curl cip.cc    
  
  
IP	: ecs公网IP  
地址	: 中国  中国  
  
数据二	: 中国 | 阿里云  
  
数据三	: 中国 | 阿里巴巴  
  
URL	: http://www.cip.cc/ecs公网IP  

macOS, 开启cli代理的情况下, 进行速度测试: 我的Ecs带宽是1Mb, 1Mb/8=128kBytes/s.

export all_proxy=socks5://127.0.0.1:1111    
  
cd ~/Downloads  
  
# curl --limit-rate 128k -Z --connect-timeout 120 -m 36000 --retry 12000 --retry-delay 5 --retry-max-time 1200 -O -L https://github.com/duckdb/duckdb/releases/download/v0.9.1/duckdb_cli-osx-universal.zip   
  

curl -Z --connect-timeout 120 -m 36000 --retry 12000 --retry-delay 5 --retry-max-time 1200 -O -L https://github.com/duckdb/duckdb/releases/download/v0.9.1/duckdb_cli-osx-universal.zip   

  
  
DL% UL%  Dled  Uled  Xfers  Live Total     Current  Left    Speed    
100 --  14.3M     0     1     0   0:01:40  0:01:52 --:--:--  146k    

speed达标.

关闭cli的代理方法如下:

# 关闭cli的代理  
unset all_proxy  
  
# 再次使用curl测试, 你会发现返回的IP是你当前地区的IP:    
curl cip.cc  
  
  
digoaldeMacBook-Pro:Downloads digoal$ unset all_proxy  
digoaldeMacBook-Pro:Downloads digoal$ curl cip.cc  
  
# 返回如下:   
IP	: 183.159.23.85  
地址	: 中国  浙江  杭州  
运营商	: 电信  
  
数据二	: 浙江省杭州市 | 电信  
  
数据三	: 中国浙江省杭州市 | 电信  
  
URL	: http://www.cip.cc/183.159.23.85  

4、速度问题分析

有时候不稳定, 可能是bgp网络不稳定导致的. 可以尝试kill ssh后重新执行proxy.sh, 如:

ps -efw|grep ssh  
  501 96986     1   0 12:16下午 ??         0:07.84  ssh -CqtfnN -c aes128-ctr -o MACs umac-64@openssh.com -D 127.0.0.1:1111 digoal@ecs公网IP -p 22  
  
  
# 杀掉之前的ssh隧道
killall -c ssh 
  
~/proxy.sh  

ciper 复杂度对速度也有一定的影响, 不过本文的瓶颈主要在1Mb的带宽, 这个带宽下加密算法复杂度对速度影响很小. 参考:

5、配置chrome, 让chrome可以用代理来访问github.

5.1、下载方法1. 用safari 浏览器下载 SwitchyOmega_Chromium.crx 插件:

5.2、下载方法2. 如果你使用的是chrome, 可以在下载页面选中链接, 在另存为里面改一下保存的文件后缀为zip. 否则下载的crx文件会被自动删除.

macOS shell, 解压SwitchyOmega_Chromium.crx:

cd ~/Downloads  
mkdir chrome_proxy
mv SwitchyOmega_Chromium.crx chrome_proxy/SwitchyOmega_Chromium.zip
cd ~/Downloads/chrome_proxy
unzip SwitchyOmega_Chromium.zip  

配置chrome: URL栏右侧菜单栏 - 扩展程序 - 管理扩展程序 - 开启 开发者模式开关 - 加载已解压的扩展程序 - 选中~/Downloads目录 - 确认(忽略错误)

配置chrome: URL栏右侧插件图标 - Proxy SwitchyOmega右侧钉子图标 (固定在导航栏方便配置)

配置chrome Proxy SwitchyOmega插件: URL栏右侧插件图标 - Proxy SwitchyOmega - 选项

5.1、设置 情景模式: proxy

(默认) SOCKS5 127.0.0.1 1111  

5.2、设置 情景模式: auto switch

# 添加要走代理的网站, 不在配置里的URL都不走需要走代理  
*.github.com  使用 proxy  
  
  
默认情景模式: 直接连接  

打开url, 配置使用auto switch模式:

  • chrome URL栏右侧插件图标 - Proxy SwitchyOmega - 选中auto switch

自助添加需要代理的URL:

  • 打开网站时, SwitchyOmega"小圆点"图标里面会显示无法加载的网站(也可能是正在加载中), 点击小圆点, (勾选需要走代理的网址)点击"添加条件", 然后进入SwitchyOmega配置模式 - 点击"应用选项"会自动添加规则.

导出PAC规则:

  • chrome URL栏右侧插件图标 - Proxy SwitchyOmega - 选项 - 情景模式 auto switch - 导出PAC
# 规则内容可以使用文本查看, 可能会得到类似如下结果:       
  
cat ~/Downloads/OmegaProfile_auto_switch.pac   
  

var FindProxyForURL = function(init, profiles) {
    return function(url, host) {
        "use strict";
        var result = init, scheme = url.substr(0, url.indexOf(":"));
        do {
            result = profiles[result];
            if (typeof result === "function") result = result(url, host, scheme);
        } while (typeof result !== "string" || result.charCodeAt(0) === 43);
        return result;
    };
}("+auto switch", {
    "+auto switch": function(url, host, scheme) {
        "use strict";
        if (/(?:^|\.)google\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)translate\.goog$/.test(host)) return "+proxy";
        if (/(?:^|\.)translate\.googleapis\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)gstatic\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)proxifier\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)youtubekids\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)avatars\.githubusercontent\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)raw\.githubusercontent\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)apsaradb\.github\.io$/.test(host)) return "+proxy";
        if (/(?:^|\.)duckduckgo\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)github\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)githubassets\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)camo\.githubusercontent\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)twitter\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)ads-twitter\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)twimg\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)wisdomfish\.org$/.test(host)) return "+proxy";
        if (/(?:^|\.)wikipedia\.org$/.test(host)) return "+proxy";
        if (/(?:^|\.)wikimedia\.org$/.test(host)) return "+proxy";
        if (/(?:^|\.)ytimg\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)googlevideo\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)youtube\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)google\.com\.sg$/.test(host)) return "+proxy";
        if (/(?:^|\.)ggpht\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)jnn-pa\.googleapis\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)googlesyndication\.com$/.test(host)) return "+proxy";
        if (/(?:^|\.)doubleclick\.net$/.test(host)) return "+proxy";
        if (/(?:^|\.)blogspot\.com$/.test(host)) return "+proxy";
        return "DIRECT";
    },
    "+proxy": function(url, host, scheme) {
        "use strict";
        if (/^127\.0\.0\.1$/.test(host) || /^::1$/.test(host) || /^localhost$/.test(host)) return "DIRECT";
        return "SOCKS5 127.0.0.1:1111; SOCKS 127.0.0.1:1111";
    }
});

git-clone, git-pull, git-push 使用ssh隧道代理

参考:

1、macOS git-clone 使用ssh代理:

方法1: 配置全局参数

# 配置  
git config --global http.proxy 'socks5://127.0.0.1:1111'  
git config --global https.proxy 'socks5://127.0.0.1:1111'  
  
# 查看配置  
cat ~/.gitconfig  
  
[https]  
	proxy = socks5://127.0.0.1:1111  
[http]  
	proxy = socks5://127.0.0.1:1111  
  
# git clone将使用ssh代理  
git clone --depth 1 https://github.com/digoal/blog blog-test  

方法2: 单次clone使用ssh proxy:

git clone -c http.proxy=socks5://127.0.0.1:1111 https://github.com/digoal/blog blog-test  

2、macOS git-clone , git-push , git-pull 使用github repo的esh地址:

参考:

首先确保你的macOS和github 官网个人setting中都已经配对了key:

配置macOS本地.ssh/config:

vi ~/.ssh/config  
  
  
Host github.com  
  User git  
  Port 22  
  Hostname github.com  
  # 注意修改路径为你的ssh key路径  
  IdentityFile "/Users/digoal/.ssh/id_rsa"  
  TCPKeepAlive yes  
  # 如果不想使用ssh代理, 注释ProxyCommand这行即可. 更多信息查看 man nc    
  ProxyCommand nc -v -x 127.0.0.1:1111 %h %p  
  
Host ssh.github.com  
  User git  
  Port 443  
  Hostname ssh.github.com  
  # 注意修改路径为你的ssh key路径  
  IdentityFile "/Users/digoal/.ssh/id_rsa"  
  TCPKeepAlive yes  
  # 如果不想使用ssh代理, 注释ProxyCommand这行即可. 更多信息查看 man nc    
  ProxyCommand nc -v -x 127.0.0.1:1111 %h %p  

找一个项目, 使用ssh地址进行clone:

code下拉框中拷贝SSH clone地址如下:

  • git@github.com:duckdb/duckdb.git

克隆:

git clone --depth 1 git@github.com:duckdb/duckdb.git duckdb-test  
  
  
Cloning into 'duckdb-test'...  
Connection to github.com port 22 [tcp/ssh] succeeded!  # 这行信息实际上就是nc的-v开关打印的详细信息, 表示已经使用了ssh代理
Enter passphrase for key '/Users/digoal/.ssh/id_rsa':   
...  

注释~/.ssh/config ProxyCommand 表示不使用代理后, 克隆信息变化如下:

vi ~/.ssh/config  
  
# 不想使用ssh代理, 注释ProxyCommand这行即可. 更多信息查看 man nc    
# ProxyCommand nc -v -x 127.0.0.1:1111 %h %p  

# 测试如下:   
git clone --depth 1 git@github.com:duckdb/duckdb.git duckdb-test  


# nc 打印的信息没了, 表示没有使用ssh代理:  
Cloning into 'duckdb-test'...  
Enter passphrase for key '/Users/digoal/.ssh/id_rsa':   
...  

chrome 谷歌翻译的使用

chrome谷歌翻译插件无法生效, 可能是插件不支持使用ssh代理的原因. 有网页翻译需求的同学, 可以使用翻译功能的web版, 将需要翻译的URL输入即可:

如果你还想研究一下如何实现chrome插件的谷歌翻译如何使用ssh代理, 可以参考:

补充: 翻译丝滑的解决了, 使用socks代理.

搭建 openVPN

如果你想更加通用一点, 也可以把这台ecs配置成OpenVPN server, 客户端通过VPN拨号的形式来配置适当的路由.

补充, 不需要搭建vpn, 配置网络代理也可以很丝滑, 如果家里有破解过带http server、ssh客户端功能的路由器会更丝滑, 参考: 《iphone/macOS PC 使用nginx配置proxy.pac共享socks5 代理加速github网络访问》

参考:

ecs, openVPN Server:

ssh root@ecs公网IP  
OR ssh 普通用户@ecs_IP  ; su - root ;
  
apt update   
apt install openvpn  

完整配置参考:

《macOS 通过 “debian 11 配置的 openVPN Server” 提升github等访问体验》

CA Server:

为了提高安全性, 使用另一台server来颁发证书, 例如使用我的学习镜像, 也是base on debian 11.7.

macOS, 启动docker容器

# 拉取镜像, 第一次拉取一次即可. 或者需要的时候执行, 将更新到最新镜像版本.    
docker pull registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts    
  
# 启动容器    
docker run -d -it -P --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name pg --shm-size=1g --entrypoint /bin/bash registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts_arm64  
  
# 进入容器    
docker exec -ti pg bash  

如何在容器中颁发证书, 待补充

macOS openVPN client:

待补充

iOS openVPN client:

待补充

路由器 VPN client:

待补充

digoal's wechat