diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..2fed27a --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,48 @@ +name: Publish Docker Image + +on: + push: + branches: + - master + paths-ignore: + - '.github/**' + - 'assets/**' + - '.gitignore' + - 'README.md' + +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Get Version + id: version + uses: oprypin/find-latest-tag@v1 + with: + repository: xjasonlyu/tun2socks + + - name: Build and Push + uses: docker/build-push-action@v2 + with: + push: true + platforms: linux/amd64,linux/arm64 + tags: | + xjasonlyu/tun2socks:latest + xjasonlyu/tun2socks:${{ steps.version.outputs.tag }} diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9a1fbb7..ad7168e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -6,6 +6,11 @@ on: - master tags: - '*' + paths-ignore: + - '.github/**' + - 'assets/**' + - '.gitignore' + - 'README.md' jobs: diff --git a/Makefile b/Makefile index 870861e..922b840 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ NAME = "tun2socks" TAGS = "" BUILD_FLAGS = "-v" -VERSION = $(shell git describe --tags) +VERSION = $(shell git describe --tags --abbrev=0) BUILD_TIME = $(shell date -u '+%FT%TZ') LDFLAGS += -w -s -buildid= @@ -17,7 +17,7 @@ PLATFORM_LIST = \ linux-amd64 \ linux-arm64 \ -.PHONY: $(PLATFORM_LIST) +.PHONY: all docker $(PLATFORM_LIST) all: $(PLATFORM_LIST) diff --git a/README.md b/README.md index c8a22eb..13cc916 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,83 @@ # tun2socks -![build](https://img.shields.io/github/workflow/status/xjasonlyu/tun2socks/Go?style=flat-square) -![go report](https://goreportcard.com/badge/github.com/xjasonlyu/tun2socks?style=flat-square) -![license](https://img.shields.io/github/license/xjasonlyu/tun2socks?style=flat-square) -![release](https://img.shields.io/github/v/release/xjasonlyu/tun2socks.svg?include_prereleases&style=flat-square) +![GitHub Workflow Status](https://img.shields.io/github/workflow/status/xjasonlyu/tun2socks/Go/master?style=flat-square) +![Go report](https://goreportcard.com/badge/github.com/xjasonlyu/tun2socks?style=flat-square) +![GitHub License](https://img.shields.io/github/license/xjasonlyu/tun2socks?style=flat-square) +![Lines of code](https://img.shields.io/tokei/lines/github/xjasonlyu/tun2socks?style=flat-square) +![Release](https://img.shields.io/github/v/release/xjasonlyu/tun2socks.svg?include_prereleases&style=flat-square) A tun2socks implementation written in Go. ## Features -- External RESTful API support -- Fake DNS with manual hosts support -- IPv4/IPv6 support +- IPv6 support - Optimized UDP transmission for game acceleration - Pure Go implementation, no CGO required - Socks5, Shadowsocks protocol support for remote connections - TCP/IP stack powered by [gVisor](https://github.com/google/gvisor) - Up to 2.5Gbps throughput (10x faster than [v1](https://github.com/xjasonlyu/tun2socks/tree/v1)) -### Requirements +## Requirements | Target | Minimum | Recommended | -| --- | --- | --- | +| :----- | :-----: | :---------: | | System | linux darwin | linux | -| Memory | >20MB | +∞ | -| CPU | ANY | amd64 arm64 | +| Memory | >20MB | >128MB | +| CPU | amd64 arm64 | amd64 | ## QuickStart Download from precompiled [Releases](https://github.com/xjasonlyu/tun2socks/releases). +
+ With Docker + +> Since Go 1.12, the runtime now uses MADV_FREE to release unused memory on **linux**. This is more efficient but may result in higher reported RSS. The kernel will reclaim the unused data when it is needed. To revert to the Go 1.11 behavior (MADV_DONTNEED), set the environment variable GODEBUG=madvdontneed=1. + +docker-compose.yml + +```yaml +version: '2.4' + +services: + tun2socks: + image: xjasonlyu/tun2socks:latest + cap_add: + - NET_ADMIN + devices: + - '/dev/net/tun:/dev/net/tun' + environment: + # - GODEBUG=madvdontneed=1 + - PROXY=socks5://server:port + - LOGLEVEL=warning + - API=:8080 + - DNS=:53 + - HOSTS= + - EXCLUDED= + - EXTRACMD= + networks: + switch: + ipv4_address: 172.20.1.2 + restart: always + container_name: tun2socks + +networks: + switch: + name: switch + ipam: + driver: default + config: + - subnet: '172.20.1.0/25' + gateway: 172.20.1.1 + driver: macvlan + driver_opts: + parent: eth0 +``` +
+ +
+ With Linux + create tun ```shell script @@ -38,17 +86,27 @@ ip addr add 198.18.0.1/15 dev tun0 ip link set dev tun0 up ``` +add route + +```shell script +ip route del default +ip route add default via 198.18.0.1 dev tun0 +``` + run ```shell script ./tun2socks --loglevel WARN --device tun://tun0 --proxy socks5://server:port --interface eth0 ``` +
-or just +
+ With Script ```shell script PROXY=socks5://server:port LOGLEVEL=WARN sh ./scripts/entrypoint.sh ``` +
## Build from source @@ -58,7 +116,32 @@ $ cd tun2socks $ make ``` -## Issues +## Performance + +![iperf3 test](assets/iperf3.png) + +## Usage + +```text +NAME: + tun2socks - A tun2socks implementation written in Go. + +USAGE: + tun2socks [global options] [arguments...] + +GLOBAL OPTIONS: + --api value URL of external API to listen + --device value, -d value URL of device to open + --dns value URL of fake DNS to listen + --hosts value Extra hosts mapping + --interface value, -i value Bind interface to dial + --loglevel value, -l value Set logging level (default: "INFO") + --proxy value, -p value URL of proxy to dial + --version, -v Print current version (default: false) + --help, -h show help (default: false) +``` + +## Known Issues Due to the implementation of pure Go, the memory usage is higher than the previous version. If you are memory sensitive, please go back to [v1](https://github.com/xjasonlyu/tun2socks/tree/v1). diff --git a/assets/iperf3.png b/assets/iperf3.png new file mode 100644 index 0000000..c44182d Binary files /dev/null and b/assets/iperf3.png differ diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 6c0e950..5797820 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -2,7 +2,7 @@ version: '2.4' services: tun2socks: - image: xjasonlyu/tun2socks:arm64-dev + image: xjasonlyu/tun2socks:latest cap_add: - NET_ADMIN devices: @@ -10,15 +10,15 @@ services: environment: - GODEBUG=madvdontneed=1 - PROXY= - - LOGLEVEL=warning - - API=:8080 + - LOGLEVEL= + - API= - DNS= - HOSTS= - EXCLUDED= - EXTRACMD= networks: switch: - ipv4_address: 172.20.1.20 + ipv4_address: 172.20.1.2 restart: always container_name: tun2socks diff --git a/go.mod b/go.mod index aacf6d7..e3658e7 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/gorilla/websocket v1.4.2 github.com/sirupsen/logrus v1.7.0 github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 - github.com/urfave/cli/v2 v2.2.0 + github.com/urfave/cli/v2 v2.3.0 github.com/xjasonlyu/clash v0.15.1-0.20201105074459-aa45c8b56cf6 go.uber.org/atomic v1.7.0 golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 // indirect diff --git a/go.sum b/go.sum index 301a497..23a6c2f 100644 --- a/go.sum +++ b/go.sum @@ -227,8 +227,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= -github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= -github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= +github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= github.com/vishvananda/netlink v1.0.1-0.20190930145447-2ec5bdc52b86/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk= github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/xjasonlyu/clash v0.15.1-0.20201105074459-aa45c8b56cf6 h1:iQsLkjayjJs29VOeXaeznpy1jddiuRjstj6MfFbKVoM= @@ -423,6 +423,7 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=