Skip to content

Instantly share code, notes, and snippets.

@qiwihui
Last active October 26, 2017 02:29
Show Gist options
  • Save qiwihui/c3c337419077fe08eefc175823330694 to your computer and use it in GitHub Desktop.
Save qiwihui/c3c337419077fe08eefc175823330694 to your computer and use it in GitHub Desktop.
在Mac上使用 hans+shadowsocks-libev 翻墙

简介

最近因为电信白名单,国外的服务器只能ping通,高端口基本无法使用,一种解决办法是将翻墙服务启动在80,443等可访问端口,另一种就是本文方法,将数据包封装在ping包中。

使用 hans 这个项目结合 shadowsocks-libev 翻墙。

安装

服务器端

编译安装hans

wget -c https://github.com/friedrich/hans/archive/v1.0.tar.gz
tar zxvf v1.0.tar.gz
cd hans-1.0/
make

编译完成后会产生hans程序,按以下命令以root启动,程序会进入后端模式,如果要先在前端检测,可以加上 -f 参数。

./hans -s 10.1.2.0 -p password

使用 netstat -rn 可以看到多了一个 tun0 设备

# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
(省略其他的)
10.1.2.0        0.0.0.0         255.255.255.0   U         0 0          0 tun0

客户端

在 Mac 上,先安装 tuntap 内核扩展来允许创建虚拟网卡,可以直接使用 Homebrew-cask 安装,安装过程中需要按照指示给程序权限。

# brew cask install tuntap

下载 Mac 版本程序并解压:here

运行程序:

sudo ./hans -c <server-ip> -p password -d tun0

其中 server-ip 是你翻墙服务器的 IP,-d 指定Mac上新启设备的名称。Mac上停止 hans 程序请使用 kill -9

如果启动正常,这时在Mac上也同样可以观察到tun0设备:

# ifconfig

(省略其他)
tun0: flags=8851<UP,POINTOPOINT,RUNNING,SIMPLEX,MULTICAST> mtu 1467
        inet 10.1.2.100 --> 10.1.2.1 netmask 0xffffffff 
        open (pid 74236)

理论上是无法ping通服务器了,

# ping <server-ip>
PING <server-ip>: 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
^C
--- <server-ip> ping statistics ---
5 packets transmitted, 0 packets received, 100.0% packet loss

此时,就建立了一条从本地到服务器的icmp tunnel了。

现在只需要将 ss-local 的本地配置中的 server 参数改为 tun0 的 gateway 地址(10.1.2.1)即可,其他不需要修改。

查看

在服务器网卡上抓包可以不断看到 ICMP 的 `echo request` 和 `echo reply` 包,在 tun0 上可以看到实际的数据包。

# tcpdump -ni ens3

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens3, link-type EN10MB (Ethernet), capture size 262144 bytes
02:16:29.146644 IP <local-ip> > <server-ip>: ICMP echo request, id 38174, seq 7330, length 93
02:16:29.146647 IP <local-ip> > <server-ip>: ICMP echo request, id 38174, seq 7330, length 13
02:16:29.146652 IP <local-ip> > <server-ip>: ICMP echo request, id 38174, seq 7330, length 13
02:16:29.146684 IP <local-ip> > <server-ip>: ICMP echo request, id 38174, seq 7330, length 93
02:16:29.146704 IP <server-ip> > <local-ip>: ICMP echo reply, id 38174, seq 7330, length 257
02:16:29.146858 IP <server-ip> > <local-ip>: ICMP echo reply, id 38174, seq 7330, length 833
02:16:29.146942 IP <server-ip> > <local-ip>: ICMP echo reply, id 38174, seq 7330, length 257

(略去一堆)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment