├── README.md └── all2tun.sh /README.md: -------------------------------------------------------------------------------- 1 | all2tun 2 | ======= 3 | 4 | It is a shell script that enables rerouting all traffic (TCP and UDP) to ssh tunnel though socks5 proxy. It is just automation of use badvpn tun2socks proxifier. 5 | For more details about badvpn please go to author's homepage https://code.google.com/p/badvpn/ or github https://github.com/ambrop72/badvpn 6 | 7 | I create the script mostly for usage by myself but if it will be useful for somebody else - it'll be great ! 8 | 9 | You need to have server with "white" IP that will work as proxy (it can be OpenWRT router for example) and you need to compile badvpn-tun2socks for your system and badvpn-udpgw for your remote server. 10 | 11 | It is possible that I'll add some help and more detailed description here but no promise. 12 | 13 | I'm very beginner in shell scripting and linux so there may be much better way to get proper functionality. I'll be very grateful for any feedback, bug report or comment. Feel free to fork and contibute... 14 | -------------------------------------------------------------------------------- /all2tun.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | ###################################################################################### 4 | # version 0.05 5 | # 6 | # ToDo: 7 | # 0) create description here 8 | # 1) ssh agent for key passphrase storage 9 | # 2) ssh config for auto "yes" to "continue to connection" questions 10 | # 3) key fingerprint to check 11 | # 4) config file for the fingerprint, IP and (maybe) other stuff storing 12 | # 5) "debug mode" and "silent mode" 13 | # 6) to be ready for starting from crontab 14 | # 7) need to check if addr is absent and stop with error exit code 15 | # *) to understand - what about IPv6 ? 16 | # ?) ... 17 | # 18 | ###################################################################################### 19 | 20 | CON_FILE="/etc/NetworkManager/system-connections/Wired connection 1" 21 | SSH_KEY="keyfile" 22 | UDPGW_FILE="udpgw" 23 | TUN2SOCKS_FILE="tun2socks" 24 | DNS_SERV="8.8.8.8" 25 | TUN_DEV="tun0" 26 | TUN_IP="10.10.0.1" 27 | TUN_GW="10.10.0.2" 28 | TUN_MASK="255.255.255.0" 29 | TUN_USER="nobody" 30 | SERVER_PORT="443" 31 | SOCKS_PORT="5350" 32 | UDPGW_REMOTE_SERVER_PORT="7300" 33 | 34 | # you outside connection gateway 35 | ORIGINAL_GW="10.0.2.2" 36 | 37 | if [ -z "$1" ] || [ $(id -u) -ne 0 ] 38 | then 39 | echo "usage: sudo `basename $0` server_addr or IP [-u for udpgw upload to the sever]\n if you need to change something else - go inside and edit variables and the code :)" 40 | exit $E_BADARGS 41 | fi 42 | 43 | #echo "$(dirname $0)" 44 | cd $(dirname $0) 45 | 46 | if echo $1 | grep -E "^[0-9]{1,3}(\.[0-9]{1,3}){3}$" > /dev/null 47 | then 48 | SERVER_IP=$1 49 | else 50 | SERVER_IP=$(nslookup $1 | grep "Address: " | cut -d " " -f 2 -s) 51 | fi 52 | 53 | echo "addr=$SERVER_IP" 54 | 55 | sed -i '$G' "$CON_FILE" 56 | sed -i "/^\[ipv4\]/!b;:x;n;/^dns="$DNS_SERV";$/b;s/^dns=/dns="$DNS_SERV";/;t;/^[[:space:]]*$/s/^/dns="$DNS_SERV";\n/;t;bx" "$CON_FILE" 57 | sed -i "/^\[ipv4\]/!b;:x;n;s/^ignore-auto-dns=.*/ignore-auto-dns=true/;t;/^[[:space:]]*$/s/^/ignore-auto-dns=true\n/;t;bx" "$CON_FILE" 58 | sed -i '$d' "$CON_FILE" 59 | 60 | SERVER_CMD="/tmp/$UDPGW_FILE --listen-addr 127.0.0.1:$UDPGW_REMOTE_SERVER_PORT &" 61 | 62 | if [ "$2" = "-u" ] 63 | then 64 | echo "udpgw will be uploaded to the server and started" 65 | su -s /bin/sh $TUN_USER -c "scp -i $SSH_KEY -P $SERVER_PORT $UDPGW_FILE root@$SERVER_IP:/tmp/" 66 | su -s /bin/sh $TUN_USER -c "ssh -i $SSH_KEY root@$SERVER_IP -p $SERVER_PORT $SERVER_CMD" 67 | fi 68 | 69 | 70 | if ip tuntap show | grep "$TUN_DEV: tun" 71 | # > /dev/null 72 | then 73 | echo "tun device $TUN_DEV already exist" 74 | else 75 | ip tuntap add dev $TUN_DEV mode tun user $TUN_USER 76 | fi 77 | 78 | ifconfig $TUN_DEV $TUN_IP netmask $TUN_MASK 79 | 80 | SSH_CMDLN="ssh -i $SSH_KEY -fNC -D localhost:$SOCKS_PORT root@$SERVER_IP -p $SERVER_PORT -o ServerAliveInterval=5 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -o StrictHostKeyChecking=no" 81 | #SSH_CMDLN="ssh -fNC -D localhost:$SOCKS_PORT root@$SERVER_IP -p $SERVER_PORT -o ServerAliveInterval=5 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes" 82 | if pgrep -xf "$SSH_CMDLN" 83 | then 84 | echo "ssh tunnel already exist" 85 | else 86 | echo "ssh tunnel will be started" 87 | su -s /bin/sh $TUN_USER -c "$SSH_CMDLN" 88 | fi 89 | echo "$? ****" 90 | 91 | TUN2SOCKS_CMDLN="$TUN2SOCKS_FILE --tundev $TUN_DEV --netif-ipaddr $TUN_GW --netif-netmask $TUN_MASK --socks-server-addr 127.0.0.1:$SOCKS_PORT --udpgw-remote-server-addr 127.0.0.1:$UDPGW_REMOTE_SERVER_PORT 1>/dev/null &" 92 | 93 | #2>&1 &" 94 | 95 | pkill -x $TUN2SOCKS_FILE 96 | #ps -AFww | grep $TUN2SOCKS_FILE 97 | 98 | su -s /bin/sh $TUN_USER -c "./$TUN2SOCKS_CMDLN" 99 | echo "$? *****" 100 | 101 | ip route replace $SERVER_IP via $ORIGINAL_GW metric 5 102 | ip route del default 103 | ip route add default via $TUN_GW metric 6 104 | 105 | cd - >/dev/null 106 | 107 | --------------------------------------------------------------------------------