Cloudflare warp proxy

简介

  • Cloudflare 作为全球最大的网络服务提供商,全球大约有30%的网络服务由它提供。
  • WARP是Cloudflare提供的免费的,没有带宽上限或限制的VPN服务,可以让你在任何地方访问互联网,而不会受到地域限制。
    • WARP软件是基于wireguard魔改的。
    • WARP有安卓和Windows的客户端,但是使用人数过多,体验并不好
    • Linux下通过WARP代理能实现20MB/s的下载速度。

WARP on Linux

安装配置

  • 缘由:WARP下的PT做种快得多。(不是,是因为网络硬盘,所以下载多少要占用多少上传)
  • 参考教程
  • 脚本和所需文件在Z:\shaojiemike\Documents\文献\计算机网络目录下。这里先使用fjw的脚本。
  1. 通过注册脚本register.py,获得私钥和分配的ip
  2. 配置wg。其中Endpoint端口可从官方文档中找到,默认的2408很可能被封。WARP with firewall · Cloudflare Zero Trust docs
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [Interface]
    PrivateKey = xxxx #register.py的私钥
    Address = xxx/32,xxx/128 #register.py的ipv4和ipv6
    Table = off # off禁止wg修改路由表

    [Peer]
    PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=
    AllowedIPs = 0.0.0.0/0,::/0
    Endpoint = [2606:4700:d0::a29f:c001]:500 #2408, 1701, 500, 4500
    PersistentKeepalive = 25
  3. warp使用的不是标准的wg协议,root下运行,需要通过一个nft脚本main.sh修改包的3个字节。
    1. 安装nft apt-get install nftables
    2. 需要/etc/default/warp-helper文件填写对应的
    3. ROUTING_ID对应register.py的ROUTING_ID。注意三个数之间没空格
    4. UPSTREAM对应wg-conf里Endpoint。比如:
    5.   ROUTING_ID=11,45,14
        UPSTREAM=[2606:4700:d0::a29f:c001]:500
        
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      2. 最后开启路由表,Root权限运行` ip route add default dev warp proto static scope link table default`

      ## WARP on OpenWRT

      * 目的:为了防止大量流量通过WARP,导致被官方封禁,所以只在OpenWRT上配置WARP分流github的流量。
      * 实现思路:
      * 运行python脚本,通过github的API获得所有的github域名ip,
      * 使用iptables的warp_out表,将目的地址为github域名ip路由到WARP的虚拟网卡上。

      ### WARP Wireguard Establishment

      ```bash
      python register.py #自动生成warp-op.conf,warp.conf和warp-helper
      mv warp-helper /etc/default
      # cat main.sh
      # cat warp-op.conf
      vim /etc/config/network #填写warp-op.conf内容,默认只转发172.16.0.0/24来测试连接
      ifup warp #启动warp, 代替wg-quick up warp.conf
      bash main.sh #启动防火墙实现报文头关键三字节修改
      nft list ruleset #查看防火墙,是否配置成功
      wg #查看warp状态,测试是否连接成果

这时还没创建warp_out路由表,所以还不能通过WARP出数据。

1
2
3
4
5
6
7
8
#/etc/config/network
config interface 'warp'
option proto 'wireguard'
option private_key 'wKpvFCOk4sf8d/RD001TF7sNQ39bWnllpqaFf8QnHG4='
option listen_port '51825'
list addresses '172.16.0.2/32'
list addresses '2606:4700:110:8466:d4ea:ffb8:10da:470f/128'
#option disabled '1'

然后WebUI点击apply 或者命令行运行ifconfig warp down && ifup

Network planning and design

添加了WARP的网络出口后,路由器不在只是通过WAN出数据。防火墙需要更新:

  • 原路返回规则。
    • 针对有公网ip的接口,需要原路返回。
      • 配置来自wan和WARP的信报,使用wan和WARP的路由表,优先级3
      • 来自wan的比如来自外部的ssh,为了防止失联。
      • 来自WARP的比如wget --bind-address=WARP_ip来模拟
    • 内网地址没有必要配置,因为通过内网地址访问host,则dst必然也是内网地址。因此会匹配main中的内网地址规则。
  • wan和WARP的路由表内各自走wan和WARP的网卡
  • 为了使得原本wg正常运行,10: from all lookup main suppress_prefixlength 1
    • 假如warp_out是defualt规则,该项也是为了防止失联。
  • 创建warp_out的空路由表1000: from all lookup warp_out,优先级1000
1
2
3
4
5
6
7
8
root@tsjOp:~/warp# ip rule
0: from all lookup local
3: from 114.214.233.141/22 iif eth1 lookup wan
3: from 172.16.0.2 iif warp lookup warp
10: from all lookup main suppress_prefixlength 1
1000: from all lookup warp_out
32766: from all lookup main
32767: from all lookup default

填充warp_out路由表

1
2
3
cd ip_route
mv ../github_ipv4.txt .
python fill_ip_table.py --table warp_out --iface warp --p2p -f github_ipv4.txt

对所有github域名的ip执行类似ip ro add 192.30.252.0/22 dev warp proto static table warp_out操作。

测试

1
2
3
mtr www.github.com
ssh -vT [email protected]
git clone https://github.com/llvm/llvm-project

添加到启动项

修改/etc/rc.local

1
2
3
4
5
6
7
8
9
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.


sleep 30 && cd /root/warp/ip_route && python fill_ip_table.py --table warp_out --iface warp --p2p -f github_ipv4.txt

/root/warp/main.sh #重新添加防火墙

exit 0

WARP on Windows

基于1.1.1.1 的安装windows版本直接白嫖

需要进一步的研究学习

暂无

遇到的问题

暂无

开题缘由、总结、反思、吐槽~~

参考文献

https://gist.github.com/iBug/3107fd4d5af6a4ea7bcea4a8090dcc7e

glados

Clash on LAN/linux/Dockers

导言

在国内的linux服务器上,往往需要clash来代理访问github等外网资源。

有几种解决方案:

  1. 透明代理
  2. ssh 转发实现代理,类似ssh -fNgR 7333:127.0.0.1:7890 [email protected]
  3. Clash 的 Allow LAN功能
  4. Clash in Docker
  5. Clash in Linux

Clash的模式

  • 系统代理模式:只代理127.0.0.1:7890上的数据
  • TUN代理模式:虚拟网卡,并接管所有的网络层的数据
    • 无法封装网络层数据包,无法代理ping, fake-ip还会返回假ip
    • TUN与TAP是操作系统内核中的虚拟网络设备:
      • TAP等同于一个以太网设备,它操作第二层数据包如以太网数据帧。
      • TUN模拟了网络层设备,操作第三层数据包比如IP数据包。
  • 舍弃的redir-host模式由于必须返回一个真实ip,因此必需发起dns请求,存在dns泄露
  • 默认的fake-ip会对域名的DNS请求返回fake-ip,从而避免DNS泄露。然后根据域名分流将信包发送到对应的上游代理机器,把域名DNS解析工作留给上游机器。
    • fake-ip模式,将fake-ip-filter设置为+.*便等价于redir-host模式

Clash的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# RESTful web API listening address
external-controller: 127.0.0.1:9090


# DNS server settings
# This section is optional. When not present, the DNS server will be disabled.
dns:
enable: false
listen: 0.0.0.0:53
ipv6: false # when the false, response to AAAA questions will be empty

# These nameservers are used to resolve the DNS nameserver hostnames below.
# 默认只支持ip
default-nameserver:
- 8.8.8.8

# 对于下面的域名,fake-ip模式会返回真实ip
fake-ip-filter:
- '*.lan'
- localhost.ptlogin2.qq.com

# 支持 UDP, TCP, DoT, DoH. 和指定端口
# 所有DNS请求都会不经过代理被转发到这些服务器,Clash会选择一个最快的返回结果
nameserver:
- https://223.5.5.5/dns-query # 阿里云
- https://doh.pub/dns-query #腾讯云
- tls://dns.rubyfish.cn:853 # DNS over TLS
- https://1.1.1.1/dns-query # DNS over HTTPS
- dhcp://en0 # dns from dhcp

# 对于所有DNS请求,fallback和nameserver内的服务器都会同时查找
# 如果DNS结果为非国内IP(GEOIP country is not `CN`),会使用fallback内的服务器的结果
# 因为nameserver内为国内服务器,对国外域名可能有DNS污染。fallback内是国外服务器,能防止国外域名被DNS污染
fallback:
- https://162.159.36.1/dns-query
- https://dns.google/dns-query
- tls://8.8.8.8:853

# DNS污染攻击的对策
fallback-filter:
geoip: false # If geoip is true, when geoip matches geoip-code, clash will use nameserver results. Otherwise, Clash will only use fallback results.
# geoip-code: CN
ipcidr: # IPs in these subnets will be considered polluted, when nameserver results match these ip, clash will use fallback results.
- 0.0.0.0/8
- 10.0.0.0/8
- 100.64.0.0/10
- 127.0.0.0/8
- 169.254.0.0/16
- 172.16.0.0/12
- 192.0.0.0/24
- 192.0.2.0/24
- 192.88.99.0/24
- 192.168.0.0/16
- 198.18.0.0/15
- 198.51.100.0/24
- 203.0.113.0/24
- 224.0.0.0/4
- 240.0.0.0/4
- 255.255.255.255/32
domain: #Domains in these list will be considered polluted, when lookup these domains, clash will use fallback results.
- +.google.com
- +.facebook.com
- +.youtube.com
- +.githubusercontent.com

Clash use Allow LAN

假如服务器和笔记本在LAN下,笔记本的clash软件只需要打开LAN就可以给服务器代理了,是最简单方便的方式。

Clash in Docker

由于UGREEN NAS一直开机,作为代理节点很适合。

首先注意修改代理机场的文件config.yaml0.0.0.0:9090,以便haishanh/yacd使用。

1
2
3
4
5
6
mixed-port: 7890
allow-lan: true
bind-address: '*'
mode: rule
log-level: info
external-controller: '0.0.0.0:9090'
  • 使用dreamacro/clash^1
  • UI使用haishanh/yacd

Clash in Linux

下载

下载可执行文件

1
2
3
4
wget https://github.com/Dreamacro/clash/releases/download/v1.11.8/clash-linux-amd64-v1.11.8.gz
scp
gunzip clash-linux-amd64-v1.11.8.gz
chmod u+x clash-linux-amd64-v1.11.8
  • Clash 运行时需要 Country.mmdb 文件,Country.mmdb 文件利用 GeoIP2 服务能识别互联网用户的地点位置,以供规则分流时使用。
  • 当第一次启动 Clash 时(使用 ./clash 命令) 会自动下载(会下载至 /home/XXX/.config/clash 文件夹下)。自动下载可能会因网络原因较慢,可以访问该链接手动下载。

配置

根据订阅链接配置文件

1
2
cd ~/.config/clash
curl -o config.yaml 'longURL'

验证

成功结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# shaojiemike @ node6 in ~/Download [10:22:54] C:130       
$set_proxy

# shaojiemike @ node6 in ~/Download [10:22:57]
$ curl -v www.google.com

# shaojiemike @ node6 in ~/Download [10:21:46]
$ ./clash-linux-amd64-v1.11.8
INFO[0000] Start initial compatible provider 🍃 Proxies
INFO[0000] Start initial compatible provider ☁️ Others
INFO[0000] Start initial compatible provider 🍂 Domestic
INFO[0000] Start initial compatible provider ⭐️ Auto
INFO[0000] Mixed(http+socks) proxy listening at: [::]:7890
INFO[0000] RESTful API listening at: [::]:9090
INFO[0000] DNS server listening at: [::]:5323
INFO[0070] [TCP] 127.0.0.1:52664 --> www.google.com:80 match DomainKeyword(google) using 🍃 Proxies[专线 日本 03]

图形化界面管理

http://clash.razord.top/#/proxies

输入

1
2
3
Host: node6.swangeese.fun
Port: 9090
Secret: 配置文件配置的 secret

查看config.yaml,发现是空

1
2
3
4
5
6
mixed-port: 7890                                      
allow-lan: true
mode: rule
log-level: info
external-controller: '0.0.0.0:9090'
secret: ''

常见问题

address already in use

1
2
3
4
5
6
7
8
9
# shaojiemike @ node6 in ~/Download [10:14:25]
$ ./clash-linux-amd64-v1.11.8
INFO[0000] Start initial compatible provider ⭐️ Auto
INFO[0000] Start initial compatible provider 🍃 Proxies
INFO[0000] Start initial compatible provider ☁️ Others
INFO[0000] Start initial compatible provider 🍂 Domestic
INFO[0000] RESTful API listening at: [::]:9090
INFO[0000] Mixed(http+socks) proxy listening at: [::]:7890
ERRO[0000] Start DNS server error: listen udp 0.0.0.0:5353: bind: address already in use

修改配置文件里的端口即可

参考文献

[^2]: Docker 安装 Clash 并对外提供代理服务

https://blog.iswiftai.com/posts/clash-linux/

https://einverne.github.io/post/2021/03/linux-use-clash.html

SSHForward

导言

服务器没网,姜师兄说可以ssh转发网络请求到本地windows

四类ssh转发

SSH 端口转发自然需要 SSH 连接,而SSH 连接是有方向的,从 SSH Client 到 SSH Server 。

而我们所要访问的应用也是有方向的,应用连接的方向也是从应用的 Client 端连接到应用的 Server 端。比如需要我们要访问Internet上的Web站点时,Http应用的方向就是从我们自己这台主机(Client)到远处的Web Server。

本地转发Local Forward

如果SSH连接和应用的连接这两个连接的方向一致,那我们就说它是本地转发。

1
2
3
ssh -L [bind_address:]port:host:hostport <SSH hostname>
ssh -L 3333:127.0.0.1:2333 -vN -f -l shaojiemike 222.195.72.218
debug1: Local connections to LOCALHOST:3333 forwarded to remote address(222.195.72.218) 127.0.0.1:2333


本地转发在本地这台机器上监听一个端口,然后所有访问这个端口的数据都会通过ssh 隧道传输到远端的对应端口上。命令中的 host 和 <SSH hostname> 可以是不同的主机。

远程转发Remote Forward

如果SSH连接和应用的连接这两个连接的方向不同,那我们就说它是远程转发。

1
ssh -R [bind_address:]port:host:hostport <SSH hostname>

远程转发与本地转发正好相反,打开ssh隧道以后,在远端服务器监听一个端口,所有访问远端服务器指定端口都会通过隧道传输到本地的对应端口上,下面是例子。

动态转发

  • TODO

X转发

  • TODO

实用参数

1
2
3
4
-C:压缩数据传输。
-f :后台认证用户/密码,通常和-N连用,不用登录到远程主机。
-N :不执行脚本或命令,通常与-f连用。
-g :在-L/-R/-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。
1
2
3
4
5
6
-f      Requests ssh to go to background just before command execution.  This is useful if ssh is going to ask for passwords or
passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 programs at a remote
site is with something like ssh -f host xterm.

If the ExitOnForwardFailure configuration option is set to "yes", then a client started with -f will wait for all remote port
forwards to be successfully established before placing itself in the background.

常见例子

将发往本机的80端口访问转发到174.139.9.66的8080端口

1
ssh -C -f -N -g -L 80:174.139.9.66:8080 [email protected]

将发往174.139.9.66的8080访问转发到本机的80端口

1
ssh -C -f -N -g -R 80:174.139.9.66:8080 [email protected]

使用远程管理服务器上的MySQL

1
ssh -C -f -N -g -L 80:174.139.9.66:8080 [email protected]

一次同时映射多个端口

1
ssh -L 8888:www.host.com:80 -L 110:mail.host.com:110 -L    25:mail.host.com:25 user@host -N  

反向隧道技术

  1. 先在公司机器(LAN_ip)上执行
    1
    ssh -NfR 2222:localhost:22 home_ip
    -R : 建立反向连接 将 home_ip port转发
  2. 然后到home_ip上面
    ssh localhost -p 2222

SOCKS代理

1
ssh -NfD 8888 user@remote_ip

现在在浏览器socks 5 proxy设置为localhost:8888,所有之前无法访问的网站现在都可以访问了。

假设本地主机A提供了HTTP服务,主机B无网络

1
ssh -fNgR 80:localhost:80 root@host-B

通过访问 http://host-B 来访问主机A上的HTTP服务了。

如果没有主机B的root账号,则只能远程转发到1024以后的端口号

1
ssh -fNgR 8080:localhost:80 lige@host-B

通过访问http://host-B:8080 来访问主机A上的HTTP服务

假设本地主机A无网络,主机B提供了HTTP服务

但是由于怕防火墙屏蔽,而不想直接访问

1
ssh -fNgL 80:localhost:80 root@host-B

ssh_config设置技巧

客户端

.ssh/config修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Host *
ControlPersist yes
ControlMaster auto
ControlPath /tmp/sshcontrol-%C
ControlPersist 1d
# 以上四条配合使用,实现多条ssh连接共享,而且保持1天内ssh存在。再次执行ssh命令几乎秒连
TCPKeepAlive=yes
# 发送空TCP包来保持连接,但是可能被防火墙过滤
ServerAliveInterval 30
# 表示每隔多少秒(30秒),从客户端向服务器发送一次心跳(alive检测)
# 心跳具体格式: debug1: client_input_global_request: rtype [email protected] want_reply 1
ServerAliveCountMax 240
# 表示服务端多少次(240次)心跳无响应后, 客户端才会认为与服务器到SSH链接已经断开,然后断开连接。

Port 443

Host *
ForwardAgent yes
# 可以讓本地的 SSH Key 在遠端 Server 上進行轉送,也就是经过跳板机Server1,使用本地key访问Server2,不用把key传到Server1上导致泄露
# 虽然Server1不会获得key,但是可以使用key。所以该选项不宜用于Host *,应该只添加您信任的服务器以及打算用于代理转发的服务器。
# 注意跳板机需要设置允许代理转发, /etc/ssh/sshd_config 将AllowAgentForwarding的值设置为yes, 并重启服务
AddKeysToAgent yes
ForwardX11 yes
ForwardX11Trusted yes
Compression yes
# 压缩,加快数据传输速度

服务器端

更改ssh服务器的配置文件/etc/ssh/sshd_config

1
2
3
ClientAliveInterval 60
# 默认是0,不发送
ClientAliveCountMax 3

原理同上
重启ssh服务以使配置生效

1
systemctl restart sshd

服务器端如何将端口绑定到外部地址上

我们可以把这个映射的端口绑定在0.0.0.0的接口上,方法是加上参数-b 0.0.0.0。

同时修改SSH服务器端 /etc/sshd_configGatewayPorts noGatewayPorts yes来打开它。

自动重连/保持长时间连接

1
2
Host *
ServerAliveInterval 60

检查隧道状态

  1. netstat
  2. ps
  3. autossh

实践

服务器网站端口转发到本地

1
ssh -L 6006:127.0.0.1:6006 -N -f -l acsacom snode6.swangeese.fun

给服务器提供网络代理

本地网络代理到服务器

1
2
ssh -fNgR 7333:127.0.0.1:7890 [email protected]
ssh -fNgR 7333:127.0.0.1:80 [email protected]
  • 7333数字不要太小,以免冲突。
  • 7890是本地clash端口,80也可以。
title
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#YJH proxy
export proxy_addr=localhost
export proxy_http_port=7890
export proxy_socks_port=7890
function set_proxy() {
export http_proxy=http://$proxy_addr:$proxy_http_port #如果使用git 不行,这两个http和https改成socks5就行
export https_proxy=http://$proxy_addr:$proxy_http_port
git config --global https.proxy http://$proxy_addr:$proxy_http_port
git config --global https.proxy https://$proxy_addr:$proxy_http_port
export all_proxy=socks5://$proxy_addr:$proxy_socks_port
export no_proxy=127.0.0.1,.huawei.com,localhost,local,.local
}
function unset_proxy() {
git config --global --unset http.proxy
git config --global --unset https.proxy
unset http_proxy
unset https_proxy
unset all_proxy
}
function test_proxy() {
curl -v -x http://$proxy_addr:$proxy_http_port https://www.google.com | egrep 'HTTP/(2|1.1) 200'
# socks5h://$proxy_addr:$proxy_socks_port
}
# set_proxy # 如果要登陆时默认启用代理则取消注释这句

服务器git下载设置代理端口

1
2
3
4
5
6
export http_proxy=http://127.0.0.1:7333
# wget 正常使用
export all_proxy=socks5://127.0.0.1:7333
# git 正常使用
unset http_proxy
unset all_proxy

特定软件也需要设置代理

1
2
3
4
5
6
7
git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

git config --global --unset http.proxy
git config --global --unset https.proxy

mac上转发失败

首先看看Windows上的输出


Mac的错误

在完成windows所有输出后,

  1. 首先调用了shell
  2. 错误
1
2
3
debug1: Remote: Forwarding listen address "localhost" overridden by server GatewayPorts
debug1: remote forward failure for: listen 7890, connect 127.0.0.1:7890
Warning: remote port forwarding failed for listen port 7890

猜测应该是已经端口占用了

1
netstat -nat |grep -i '7233'

解决办法一:换端口

首先,可以选择换端口,换成 7233

1
sudo lsof -i TCP:7233
  1. 进程验证
  2. wget验证
  3. curl验证(原本会走WLT)
1
redirecting to http://wlt.ustc.edu.cn


解决办法二:Kill掉相关进程

1
2
3
4
sudo lsof -i TCP:7890
sudo kill -9 process_id_1 process_id_2 process_id_3
sudo ps -ef | grep 'nc -X' | grep -v grep | awk '{print $2}' | sudo xargs -r kill -9
sudo lsof -i TCP:7233 |grep shaojiemike| awk '{print $2}'|sudo xargs -r kill -9

  • FD: File Descriptor number of
  • 不敢kill师兄的,然后发现并没有用,寄。kill掉师兄的之后就行了,嘻嘻~~ 师兄不要怪我

参考文献

http://blog.sina.com.cn/s/blog_704836f40100lwxh.html

https://blog.csdn.net/xyyangkun/article/details/7025854?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link