From 52478f056ec7022ee3a1fb703d2d8d002c26732d Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 27 Apr 2020 20:17:40 +0800 Subject: [PATCH] Update Docker --- Dockerfile | 28 +++++++++++++++++++++------- tun2socks.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 tun2socks.sh diff --git a/Dockerfile b/Dockerfile index 256d1ee..f3f5cc7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,16 +3,30 @@ FROM golang:alpine as builder WORKDIR /tun2socks-src COPY . /tun2socks-src -RUN apk add --no-cache \ - git \ - make \ - gcc \ - musl-dev \ +RUN apk add --update --no-cache \ + gcc git make musl-dev \ && go mod download \ && make build \ && mv ./bin/tun2socks /tun2socks FROM alpine:latest -COPY --from=builder /tun2socks / -ENTRYPOINT ["/tun2socks"] +COPY ./tun2socks.sh / +COPY --from=builder /tun2socks /usr/local/bin + +RUN apk add --update --no-cache \ + curl lsof iptables iproute2 bind-tools \ + && chmod +x /tun2socks.sh + +ENV TUN tun0 +ENV ETH eth0 +ENV ETHGW 172.16.1.1 +ENV TUNGW 240.0.0.1 +ENV PROXY 172.16.1.2:1080 +ENV MONITOR 0.0.0.0:80 +ENV EXCLUDED 172.16.1.2/32 +ENV LOGLEVEL warning +ENV BACKENDDNS 8.8.8.8:53 +ENV HOSTS localhost=127.0.0.1 + +ENTRYPOINT ["/tun2socks.sh"] diff --git a/tun2socks.sh b/tun2socks.sh new file mode 100644 index 0000000..466329f --- /dev/null +++ b/tun2socks.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +TUN="${TUN:-utun0}" +ETH="${ETH:-eth0}" +ETHGW="${ETHGW:-172.16.1.1}" +TUNGW="${TUNGW:-240.0.0.1}" +PROXY="${PROXY:-172.16.1.2:1080}" +MONITOR="${MONITOR:-0.0.0.0:80}" +EXCLUDED="${EXCLUDED:-172.16.1.2/32}" +LOGLEVEL="${LOGLEVEL:-warning}" +BACKENDDNS="${BACKENDDNS:-8.8.8.8:53}" +HOSTS="${HOSTS:-localhost=127.0.0.1}" + +# enable ip_forward +sysctl -w net.ipv4.ip_forward=1 &> /dev/null + +# create tun device +ip tuntap add mode tun dev $TUN +ip addr add $TUNGW/24 dev $TUN +ip link set dev $TUN up +echo "tun device created: $TUN" + +# change default gateway +ip route del default &> /dev/null +ip route add default via $TUNGW dev $TUN + +# add to ip route +for ip in $(echo $EXCLUDED | tr ',' '\n') +do + ip route add $ip via $ETHGW +done + +# DNS settings +echo "nameserver $TUNGW" > /etc/resolv.conf +echo "DNS settings updated" + +tun2socks -loglevel $LOGLEVEL \ + -tunName $TUN -proxyServer $PROXY \ + -monitor -monitorAddr $MONITOR \ + -fakeDNS -hosts $HOSTS \ + -backendDNS $BACKENDDNS