Git Product home page Git Product logo

win-tun2socks's Introduction

tun2socks

A tun2socks implementation written in Go.

forked & modified from eycorsican/go-tun2socks

作者更新后,再编译,win下报错。这是中间使用过程中一个可以编译且比较稳的一个版本。

ubuntu下交叉编译命令

编译64位windows程序

export GOOS=windows
export GOARCH=amd64
export CC=x86_64-w64-mingw32-gcc
export CXX=x86_64-w64-mingw32-g++
export CGO_ENABLED=1
cd tun2socks
make

我使用的脚本

route delete 0.0.0.0 mask 0.0.0.0
route add 114.114.114.114 192.168.8.128 metric 5
route add 8.8.8.8 192.168.8.128 metric 5
route add ssrIP 网关IP metric 5
# 这里查询自己的ssrip和网关ip替换即可
start cmd /k tun2socks.exe -proxyServer 127.0.0.1:1080 -tunAddr 10.0.236.2 -tunMask 255.255.255.0 -tunGw 10.0.236.1 -tunName "SSTAP" -tunDNS 114.114.114.114,8.8.8.8
timeout /t 4 /nobreak >nul
route add 0.0.0.0 mask 0.0.0.0 10.0.236.1

最后,享受win下全局代理

What's the difference with the original project

  • Add new features (listed below)
  • Optimize handlers (e.g. new TCP/UDP proxy handler)
  • Rewrite and remove some implementations

Main Features

Previous Features

  • TCP and UDP support
  • IPv4 gateway support
  • Support proxy handler: SOCKS5
  • ICMP echoing

New Features

  • Fake DNS (Fake IP range: 198.18.0.0/15)
  • Backend DNS (resolve non-TypeA query)
  • Hijack DNS (force the specific DNS to get a fake address)
  • Hosts mapping (e.g. localhost->127.0.0.1)
  • Web statistics monitor

How to Build

go-tun2socks is using cgo and go modules, thus a C compiler and GO version >= 1.13 are required.

git clone https://github.com/xjasonlyu/tun2socks.git
cd tun2socks && go mod download
make clean && make build
./bin/tun2socks -h

Or build via Docker

docker build -t tun2socks .

Running in Docker (My Daily Workaround)

Create a macvlan network called switch

docker network create -d macvlan \
  --subnet=10.0.0.0/24 \
  --gateway=10.0.0.1 \
  -o parent=eth0 \
  switch

Pull tun2socks docker image

docker pull xjasonlyu/tun2socks

Run tun2socks as a gateway (e.g. 10.0.0.2)

docker run -d \
  --network switch \
  --name tun2socks \
  --ip 10.0.0.2 \
  --privileged \
  --restart always \
  --sysctl net.ipv4.ip_forward=1 \
  xjasonlyu/tun2socks

PS:

  • Works on Synology NAS
  • Add custom environment variables if needed

Alpine VM Demo

This project is running on my server as a second gateway, so my Apple TV and other devices could access the full internet and AD block function without complex configuration.

Here is my Running Environment

  • Linux alpine 4.19.79-0-virt (VM)
  • Proxy Server: 10.0.0.3
  • Alpine Address: 10.0.0.2
  • Router Gateway: 10.0.0.1
  • Apple TV Address: 10.0.0.120
This is my alpine tun2socks service file
#!/sbin/openrc-run

### tun2socks options
TUN="utun0"
ETH="eth0"
ETHGW="10.0.0.1"
TUNGW="240.0.0.1"
SOCKS="10.0.0.3:1080"
MONITOR="0.0.0.0:80"
HIJACKDNS=""
BACKENDDNS="1.2.4.8:53,1.1.1.1:53"
HOSTS="localhost=127.0.0.1"
OPTIONS="-loglevel warning -tunName $TUN -proxyServer $SOCKS -monitor -monitorAddr $MONITOR -fakeDNS -hosts $HOSTS -backendDNS $BACKENDDNS"

### openrc options
description="the tun2socks route process"
name=$RC_SVCNAME
pidfile="/var/run/$RC_SVCNAME.pid"
logfile="/var/log/$RC_SVCNAME.log"
command="/usr/local/bin/tun2socks"
command_args=$OPTIONS
command_user="root"
command_background="yes"

depend() {
    #after *
    before chronyd
    after firewall
    use dns
}

_set_dns() {
    option=$1
    case $option in
        0)
        einfo "DNS settings updated"
        [ -f /etc/resolv.conf.copy ] || mv /etc/resolv.conf /etc/resolv.conf.copy
        cat > /etc/resolv.conf << EOF
nameserver $TUNGW
EOF
        ;;
        1)
        einfo "DNS settings restored"
        cp -f /etc/resolv.conf.copy /etc/resolv.conf
        ;;
        *)
        return 0
        ;;
    esac
    eend $?
}

start_pre() {
    if [ "${RC_CMD}" = "restart" ]; then
        ip link delete $TUN 2> /dev/null
    fi

    # enable ip_forward
    einfo $(sysctl -w net.ipv4.ip_forward=1) && eend $?

    # create tun device
    ip tuntap add mode tun dev $TUN
    ip addr add $TUNGW/24 dev $TUN
    ip link set dev $TUN up
    einfo "tun device created: $TUN" && eend $?

    # change default gateway
    ip route del default 2> /dev/null
    ip route add default via $TUNGW dev $TUN

    # add to ip route
    #ip route add 1.1.1.1/32 via $ETHGW

    # DNS settings
    _set_dns 0
}

stop_post() {
    # disable ip_forward
    einfo $(sysctl -w net.ipv4.ip_forward=0) && eend $?

    # delete from ip route
    #ip route del 1.1.1.1/32 via $ETHGW

    # change default gateway
    # ip route del default
    ip route add default via $ETHGW dev $ETH 2> /dev/null

    # delete tun device
    #ip link set dev $TUN down
    #ip addr del $TUNGW/24 dev $TUN
    #ip tuntap del mode tun dev $TUN
    ip link delete $TUN 2> /dev/null
    einfo "tun device deleted: $TUN" && eend $?

    # DNS settings
    _set_dns 1
}

start() {
    ebegin "Starting $RC_SVCNAME"
    start-stop-daemon --start --quiet \
        --background --exec $command \
        --user $command_user \
        --make-pidfile --pidfile $pidfile \
        --stdout $logfile --stderr $logfile \
        -- $command_args
    eend $?
}

stop() {
    if [ "${RC_CMD}" = "restart" ]; then
        _pid=$(pgrep $RC_SVCNAME)
        if [ "$_pid" != "$(cat $pidfile)" ]; then
            echo $_pid > $pidfile
        fi
    fi

    ebegin "Stopping $RC_SVCNAME"
    start-stop-daemon --stop --quiet --exec "$command" \
        --pidfile "$pidfile"
    eend $?

    if [ "$RC_RUNLEVEL" = "shutdown" ]; then
        _pid=$(pgrep $RC_SVCNAME)
        if [ -n $_pid ]; then
            kill -9 $_pid > /dev/null 2>&1
        fi
        rm -rf $pidfile
    fi
}

Follow 3 Steps

  • Simply put this config in /etc/init.d/
  • Give it executable permission chmod +x tun2socks
  • Launch the service rc-service tun2socks start

Finally, all you need to do is modify your internet settings.

In this case, I just need configure my Apple TV internet settings from DHCP to Static, and change my gateway and DNS to 10.0.0.2.

Done!

Tun2socks Usage

Usage of tun2socks:
  -backendDNS string
        Backend DNS to resolve non-TypeA or non-ClassINET query (must support tcp) (default "8.8.8.8:53,8.8.4.4:53")
  -fakeDNS
        Enable fake DNS
  -fakeDNSAddr string
        Listen address of fake DNS (default ":53")
  -hijackDNS string
        Hijack the specific DNS query to get a fake ip, e.g. '*:53', '8.8.8.8:53,8.8.4.4:53'
  -hosts string
        DNS hosts mapping, e.g. 'example.com=1.1.1.1,example.net=2.2.2.2'
  -loglevel string
        Logging level [info, warning, error, debug, silent] (default "info")
  -monitor
        Enable session statistics monitor
  -monitorAddr string
        Listen address of session monitor, open in your browser to view statistics (default "localhost:6001")
  -proxyServer string
        Proxy server address
  -tunAddr string
        TUN interface address (default "240.0.0.2")
  -tunDNS string
        DNS resolvers for TUN interface (Windows Only) (default "8.8.8.8,8.8.4.4")
  -tunGw string
        TUN interface gateway (default "240.0.0.1")
  -tunMask string
        TUN interface netmask (default "255.255.255.0")
  -tunName string
        TUN interface name (default "utun0")
  -tunPersist
        Persist TUN interface after the program exits or the last open file descriptor is closed (Linux only)
  -udpTimeout duration
        UDP session timeout (default 30s)
  -version
        Show current version of tun2socks

Run

tun2socks -loglevel warning -tunName utun0 -proxyServer 1.2.3.4:1080 -monitor -monitorAddr 0.0.0.0:80 -fakeDNS -hosts localhost=127.0.0.1 -backendDNS 1.1.1.1:53,8.8.8.8:53

Note that the TUN device may have a different name, and it should be a different name on Windows unless you have renamed it, so make sure use ifconfig, ipconfig or ip addr to check it out.

Create TUN device and Configure Routing Table

Suppose your original gateway is 192.168.0.1. The proxy server address is 1.2.3.4.

The following commands will need root permissions.

macOS

The program will automatically create a TUN device for you on macOS. To show the created TUN device, use ifconfig.

Delete original gateway:

route delete default

Add our TUN interface as the default gateway:

route add default 240.0.0.1

Add a route for your proxy server to bypass the TUN interface:

route add 1.2.3.4/32 192.168.0.1

Linux

The program will not create the TUN device for you on Linux. You need to create the TUN device by yourself:

ip tuntap add mode tun dev tun1
ip addr add 240.0.0.1 dev tun1
ip link set dev tun1 up

Delete original gateway:

ip route del default

Add our TUN interface as the default gateway:

ip route add default via 240.0.0.1

Add a route for your proxy server to bypass the TUN interface:

ip route add 1.2.3.4/32 via 192.168.0.1

Windows

使用教程 - Tutorial in Chinese

To create a TUN device on Windows, you need Tap-windows, refer here for more information.

Add our TUN interface as the default gateway:

# Using 240.0.0.1 is not allowed on Windows, we use 10.0.0.1 instead
route add 0.0.0.0 mask 0.0.0.0 10.0.0.1 metric 6

Add a route for your proxy server to bypass the TUN interface:

route add 1.2.3.4 192.168.0.1 metric 5

This project is using lwIP

This project is using a modified version of lwIP, you can checkout this repo to find out what are the changes: https://github.com/eycorsican/lwip (original author)

Many thanks to the following projects

win-tun2socks's People

Contributors

bb33bb avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.