$> git clone https://github.com/ambrop72/badvpn.git
$> # 因为只需要vpn的,因此加上这些参数(其实我也先把全部功能加上看下有些什么东西,但是我编译不过去!),详细文档可以参看github的文档 $> cmake .. -DBUILD_NOTHING_BY_DEFAULT=1 -DBUILD_TUN2SOCKS=1 -DBUILD_UDPGW=1$> make && make install
1# 2. badvpn_tun2socks 使用
1
2
3
4
5
6
7
8
9
10
11
# 创建tun接口$> ip tuntap add dev tun0 mode tun
# 为其分配ip $> ip addr add 10.0.0.1/24 dev tun0
# 启动接口 $> ip link set tun0 up
# 启动 badvpn_tun2socks socks 转化为 vpn$> badvpn-tun2socks --tundev tun0 --netif-ipaddr 10.1.0.2 --netif-netmask 255.255.255.0 --socks-server-addr "127.0.0.1:1080"# 下面操作就和vpn拨号成功后一样了 # 添加默认路由 serverA 主机同网段的其他主机 $> ip route add 10.172.11.13 via 10.1.0.2
成功运行后就可以直接通过路由连接socks通往段的其他主机。
3. 完成过程
3.1. 通过ssh动态端口转发,在远程主机上生成socks通道端口
1
$> ssh -4 -N -C -D 1080 <user>@<remote_ip>
3.2. 创建tun接口并添加ip(注意ip子网不要冲突)
1
2
3
$> ip tuntap add dev tun0 mode tun
$> ip addr add 10.0.0.1/24 dev tun0
$> ip link set tun0 up
$> vim /usr/local/bin/badvpn-control # 注意执行权限 #!/bin/bash################################################# # author 0x5c0f # date 2019-08-13 # email mail@0x5c0f.cc # web blog.0x5c0f.cc # version 1.0.0# last update 2019-08-13# descript Use : ./badvpn-control -h################################################# PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# SOCKS server IP SOCKS_SERVER="${SOCKS_HOST}"# SOCKS portSOCKS_PORT="${SOCKS_PORT}"# tun dev TUN_NETWORK_DEV="tun0"# tun ip prefix TUN_NETWORK_PREFIX="10.1.0"# route ipTUN_ROUTE_IP=($(evalecho${SOCKS_ROUTE}))badvpn_start(){ ip tuntap add dev "${TUN_NETWORK_DEV}" mode tun
ip addr add "${TUN_NETWORK_PREFIX}.1/24" dev "${TUN_NETWORK_DEV}" ip link set"${TUN_NETWORK_DEV}" up
# add route for _ip in ${TUN_ROUTE_IP[@]};do ip route add "${_ip}" via "${TUN_NETWORK_PREFIX}.2"done# start badvpn_tun2socks (https://github.com/ambrop72/badvpn.git) badvpn-tun2socks --tundev "${TUN_NETWORK_DEV}" --netif-ipaddr "${TUN_NETWORK_PREFIX}.2" --netif-netmask 255.255.255.0 --socks-server-addr "${SOCKS_SERVER}:${SOCKS_PORT}"}badvpn_stop(){# delete route for _ip in ${TUN_ROUTE_IP[@]};do ip route del "${_ip}" via "${TUN_NETWORK_PREFIX}.2"done# delete network dev ip link set"${TUN_NETWORK_DEV}" down
ip addr del "${TUN_NETWORK_PREFIX}.1/24" dev "${TUN_NETWORK_DEV}" ip tuntap del dev "${TUN_NETWORK_DEV}" mode tun
}main(){case"$1" in
"start") badvpn_start
;;"stop") badvpn_stop
;; *)echo"$0 start|stop";;esac}main $@
3.4.2. socketssh-tun.service
用于管理ssh动态转发的systemd,其他方式请忽略此类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$> vim /usr/lib/systemd/system/socketssh-tun.service[Unit]Description=socketssh tunAfter=network.target[Service]Type=simplePIDFile=/run/socketssh-tun.pidExecStart=/usr/bin/ssh -4 -N -C -D 1080 <user>@<remote_ip>[Install]WantedBy=multi-user.target$> systemctl daemon-reload $> systemctl status socketssh-tun.service