Skip to content

Commit

Permalink
tun2socks: use configure option struct
Browse files Browse the repository at this point in the history
pass ipv6 enabled status to lwipStack

update Clash to latest dev branch

Signed-off-by: Syrone Wong <[email protected]>
  • Loading branch information
wongsyrone committed Mar 21, 2020
1 parent e3c3e29 commit 78192d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module github.com/trojan-gfw/igniter-go-libs

go 1.14

replace github.com/eycorsican/go-tun2socks => github.com/trojan-gfw/go-tun2socks v1.16.3-0.20200320162851-d51626ca9709
replace github.com/eycorsican/go-tun2socks => github.com/trojan-gfw/go-tun2socks v1.16.3-0.20200321034042-e4c02acee586

require (
github.com/Dreamacro/clash v0.18.1-0.20200319030456-230e01f07884
github.com/Dreamacro/clash v0.18.1-0.20200320043530-70a19b999da6
github.com/djherbis/buffer v1.1.0 // indirect
github.com/djherbis/nio v2.0.3+incompatible // indirect
github.com/eycorsican/go-tun2socks v0.0.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Dreamacro/clash v0.18.1-0.20200319030456-230e01f07884 h1:gdiiw4luGbBtF9Jw9dh9Vg1rVJ7l9UcdFsLt+aUf8eA=
github.com/Dreamacro/clash v0.18.1-0.20200319030456-230e01f07884/go.mod h1:awIpqp0pLm+CKtbXhrSyzzFwzTRimv1/1+xa0BfGCyI=
github.com/Dreamacro/clash v0.18.1-0.20200320043530-70a19b999da6 h1:YQwLQ7EQ93MXptK2aPX4WuDWImL/mRJMnfagJQHd47w=
github.com/Dreamacro/clash v0.18.1-0.20200320043530-70a19b999da6/go.mod h1:awIpqp0pLm+CKtbXhrSyzzFwzTRimv1/1+xa0BfGCyI=
github.com/Dreamacro/go-shadowsocks2 v0.1.5 h1:BizWSjmwzAyQoslz6YhJYMiAGT99j9cnm9zlxVr+kyI=
github.com/Dreamacro/go-shadowsocks2 v0.1.5/go.mod h1:LSXCjyHesPY3pLjhwff1mQX72ItcBT/N2xNC685cYeU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -40,8 +40,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/trojan-gfw/go-tun2socks v1.16.3-0.20200320162851-d51626ca9709 h1:vPLR9wEctK0EexGDeSPhCTtRv80HCUudrQyWD35wljs=
github.com/trojan-gfw/go-tun2socks v1.16.3-0.20200320162851-d51626ca9709/go.mod h1:iSiwGqBOvfnMVjb+LVOCNygXUTGJ9gngIMp95x1sI9c=
github.com/trojan-gfw/go-tun2socks v1.16.3-0.20200321034042-e4c02acee586 h1:Y72PvWpUGCGvNC+rf/mYU5p3mjmhczzRvxn69CLxjGo=
github.com/trojan-gfw/go-tun2socks v1.16.3-0.20200321034042-e4c02acee586/go.mod h1:iSiwGqBOvfnMVjb+LVOCNygXUTGJ9gngIMp95x1sI9c=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
23 changes: 16 additions & 7 deletions tun2socks/tun2socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ import (
"github.com/songgao/water"
)

type Tun2socksStartOptions struct {
TunFd int
Socks5Server string
FakeIPStart string
FakeIPStop string
MTU int
EnableIPv6 bool
}

var (
lwipWriter io.Writer
lwipStack core.LWIPStack
Expand Down Expand Up @@ -60,32 +69,32 @@ func openTunDevice(tunFd int) (*water.Interface, error) {
}

// Start sets up lwIP stack, starts a Tun2socks instance
func Start(tunFd int, socks5Server string, fakeIPStart string, fakeIPStop string, mtu int) int {
func Start(opt *Tun2socksStartOptions) int {

mtuUsed = mtu
mtuUsed = opt.MTU
var err error
tunDev, err = openTunDevice(tunFd)
tunDev, err = openTunDevice(opt.TunFd)
if err != nil {
log.Fatalf("failed to open tun device: %v", err)
}

if lwipStack == nil {
// Setup the lwIP stack.
lwipStack = core.NewLWIPStack()
lwipStack = core.NewLWIPStack(opt.EnableIPv6)
lwipWriter = lwipStack.(io.Writer)
}

// Register tun2socks connection handlers.
proxyAddr, err := net.ResolveTCPAddr("tcp", socks5Server)
proxyAddr, err := net.ResolveTCPAddr("tcp", opt.Socks5Server)
proxyHost := proxyAddr.IP.String()
proxyPort := uint16(proxyAddr.Port)
if err != nil {
log.Infof("invalid proxy server address: %v", err)
return -1
}
cacheDNS := cache.NewSimpleDnsCache()
if fakeIPStart != "" && fakeIPStop != "" {
fakeDNS := fakedns.NewSimpleFakeDns(fakeIPStart, fakeIPStop)
if opt.FakeIPStart != "" && opt.FakeIPStop != "" {
fakeDNS := fakedns.NewSimpleFakeDns(opt.FakeIPStart, opt.FakeIPStop)
core.RegisterTCPConnHandler(socks.NewTCPHandler(proxyHost, proxyPort, fakeDNS))
core.RegisterUDPConnHandler(socks.NewUDPHandler(proxyHost, proxyPort, 30*time.Second, cacheDNS, fakeDNS))
} else {
Expand Down

0 comments on commit 78192d6

Please sign in to comment.