chore: better tfo inbound code

This commit is contained in:
gVisor bot 2022-11-16 10:43:16 +08:00
parent e991a7d557
commit 24cfa48eef
11 changed files with 55 additions and 64 deletions

26
adapter/inbound/listen.go Normal file
View file

@ -0,0 +1,26 @@
package inbound
import (
"context"
"net"
"github.com/database64128/tfo-go/v2"
)
var (
lc = tfo.ListenConfig{
DisableTFO: true,
}
)
func SetTfo(open bool) {
lc.DisableTFO = !open
}
func ListenContext(ctx context.Context, network, address string) (net.Listener, error) {
return lc.Listen(ctx, network, address)
}
func Listen(network, address string) (net.Listener, error) {
return ListenContext(context.Background(), network, address)
}

View file

@ -2,14 +2,13 @@ package executor
import ( import (
"fmt" "fmt"
"github.com/Dreamacro/clash/component/tls"
"github.com/Dreamacro/clash/listener/inner"
"net/netip" "net/netip"
"os" "os"
"runtime" "runtime"
"sync" "sync"
"github.com/Dreamacro/clash/adapter" "github.com/Dreamacro/clash/adapter"
"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/adapter/outboundgroup" "github.com/Dreamacro/clash/adapter/outboundgroup"
"github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/auth"
"github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/dialer"
@ -19,6 +18,7 @@ import (
"github.com/Dreamacro/clash/component/profile/cachefile" "github.com/Dreamacro/clash/component/profile/cachefile"
"github.com/Dreamacro/clash/component/resolver" "github.com/Dreamacro/clash/component/resolver"
SNI "github.com/Dreamacro/clash/component/sniffer" SNI "github.com/Dreamacro/clash/component/sniffer"
"github.com/Dreamacro/clash/component/tls"
"github.com/Dreamacro/clash/component/trie" "github.com/Dreamacro/clash/component/trie"
"github.com/Dreamacro/clash/config" "github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
@ -26,6 +26,7 @@ import (
"github.com/Dreamacro/clash/dns" "github.com/Dreamacro/clash/dns"
P "github.com/Dreamacro/clash/listener" P "github.com/Dreamacro/clash/listener"
authStore "github.com/Dreamacro/clash/listener/auth" authStore "github.com/Dreamacro/clash/listener/auth"
"github.com/Dreamacro/clash/listener/inner"
"github.com/Dreamacro/clash/listener/tproxy" "github.com/Dreamacro/clash/listener/tproxy"
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel" "github.com/Dreamacro/clash/tunnel"
@ -335,7 +336,7 @@ func updateGeneral(general *config.General, force bool) {
bindAddress := general.BindAddress bindAddress := general.BindAddress
P.SetBindAddress(bindAddress) P.SetBindAddress(bindAddress)
P.SetInboundTfo(general.InboundTfo) inbound.SetTfo(general.InboundTfo)
tcpIn := tunnel.TCPIn() tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn() udpIn := tunnel.UDPIn()

View file

@ -3,11 +3,11 @@ package route
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"net"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"github.com/Dreamacro/clash/adapter/inbound"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel/statistic" "github.com/Dreamacro/clash/tunnel/statistic"
@ -85,7 +85,7 @@ func Start(addr string, secret string) {
}) })
} }
l, err := net.Listen("tcp", addr) l, err := inbound.Listen("tcp", addr)
if err != nil { if err != nil {
log.Errorln("External controller listen error: %s", err) log.Errorln("External controller listen error: %s", err)
return return

View file

@ -1,10 +1,9 @@
package http package http
import ( import (
"context"
"github.com/database64128/tfo-go/v2"
"net" "net"
"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/common/cache" "github.com/Dreamacro/clash/common/cache"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
) )
@ -31,15 +30,12 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func New(addr string, inboundTfo bool, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithAuthenticate(addr, in, true, inboundTfo) return NewWithAuthenticate(addr, in, true)
} }
func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool, inboundTfo bool) (*Listener, error) { func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) {
lc := tfo.ListenConfig{ l, err := inbound.Listen("tcp", addr)
DisableTFO: !inboundTfo,
}
l, err := lc.Listen(context.Background(), "tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -14,7 +14,6 @@ import (
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/autoredir" "github.com/Dreamacro/clash/listener/autoredir"
"github.com/Dreamacro/clash/listener/http" "github.com/Dreamacro/clash/listener/http"
"github.com/Dreamacro/clash/listener/inner"
"github.com/Dreamacro/clash/listener/mixed" "github.com/Dreamacro/clash/listener/mixed"
"github.com/Dreamacro/clash/listener/redir" "github.com/Dreamacro/clash/listener/redir"
"github.com/Dreamacro/clash/listener/sing_shadowsocks" "github.com/Dreamacro/clash/listener/sing_shadowsocks"
@ -29,7 +28,6 @@ import (
var ( var (
allowLan = false allowLan = false
bindAddress = "*" bindAddress = "*"
inboundTfo = false
socksListener *socks.Listener socksListener *socks.Listener
socksUDPListener *socks.UDPListener socksUDPListener *socks.UDPListener
@ -103,14 +101,6 @@ func SetBindAddress(host string) {
bindAddress = host bindAddress = host
} }
func SetInboundTfo(itfo bool) {
inboundTfo = itfo
}
func NewInner(tcpIn chan<- C.ConnContext) {
inner.New(tcpIn)
}
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) { func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
httpMux.Lock() httpMux.Lock()
defer httpMux.Unlock() defer httpMux.Unlock()
@ -136,7 +126,7 @@ func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
return return
} }
httpListener, err = http.New(addr, inboundTfo, tcpIn) httpListener, err = http.New(addr, tcpIn)
if err != nil { if err != nil {
log.Errorln("Start HTTP server error: %s", err.Error()) log.Errorln("Start HTTP server error: %s", err.Error())
return return
@ -187,7 +177,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
return return
} }
tcpListener, err := socks.New(addr, inboundTfo, tcpIn) tcpListener, err := socks.New(addr, tcpIn)
if err != nil { if err != nil {
return return
} }
@ -280,7 +270,7 @@ func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, u
return return
} }
listener, err := sing_shadowsocks.New(shadowSocksConfig, inboundTfo, tcpIn, udpIn) listener, err := sing_shadowsocks.New(shadowSocksConfig, tcpIn, udpIn)
if err != nil { if err != nil {
return return
} }
@ -320,7 +310,7 @@ func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<-
return return
} }
listener, err := sing_vmess.New(vmessConfig, inboundTfo, tcpIn, udpIn) listener, err := sing_vmess.New(vmessConfig, tcpIn, udpIn)
if err != nil { if err != nil {
return return
} }
@ -487,7 +477,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
return return
} }
mixedListener, err = mixed.New(addr, inboundTfo, tcpIn) mixedListener, err = mixed.New(addr, tcpIn)
if err != nil { if err != nil {
return return
} }

View file

@ -1,8 +1,7 @@
package mixed package mixed
import ( import (
"context" "github.com/Dreamacro/clash/adapter/inbound"
"github.com/database64128/tfo-go/v2"
"net" "net"
"github.com/Dreamacro/clash/common/cache" "github.com/Dreamacro/clash/common/cache"
@ -38,11 +37,8 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func New(addr string, inboundTfo bool, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
lc := tfo.ListenConfig{ l, err := inbound.Listen("tcp", addr)
DisableTFO: !inboundTfo,
}
l, err := lc.Listen(context.Background(), "tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,8 +1,6 @@
package shadowsocks package shadowsocks
import ( import (
"context"
"github.com/database64128/tfo-go/v2"
"net" "net"
"strings" "strings"
@ -23,7 +21,7 @@ type Listener struct {
var _listener *Listener var _listener *Listener
func New(config string, inboundTfo bool, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (*Listener, error) { func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (*Listener, error) {
addr, cipher, password, err := ParseSSURL(config) addr, cipher, password, err := ParseSSURL(config)
if err != nil { if err != nil {
return nil, err return nil, err
@ -48,10 +46,7 @@ func New(config string, inboundTfo bool, tcpIn chan<- C.ConnContext, udpIn chan<
sl.udpListeners = append(sl.udpListeners, ul) sl.udpListeners = append(sl.udpListeners, ul)
//TCP //TCP
lc := tfo.ListenConfig{ l, err := inbound.Listen("tcp", addr)
DisableTFO: !inboundTfo,
}
l, err := lc.Listen(context.Background(), "tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,7 +3,6 @@ package sing_shadowsocks
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/database64128/tfo-go/v2"
"net" "net"
"strings" "strings"
@ -33,7 +32,7 @@ type Listener struct {
var _listener *Listener var _listener *Listener
func New(config string, inboundTfo bool, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (C.AdvanceListener, error) { func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (C.AdvanceListener, error) {
addr, cipher, password, err := embedSS.ParseSSURL(config) addr, cipher, password, err := embedSS.ParseSSURL(config)
if err != nil { if err != nil {
return nil, err return nil, err
@ -57,7 +56,7 @@ func New(config string, inboundTfo bool, tcpIn chan<- C.ConnContext, udpIn chan<
sl.service, err = shadowaead_2022.NewServiceWithPassword(cipher, password, udpTimeout, h) sl.service, err = shadowaead_2022.NewServiceWithPassword(cipher, password, udpTimeout, h)
default: default:
err = fmt.Errorf("shadowsocks: unsupported method: %s", cipher) err = fmt.Errorf("shadowsocks: unsupported method: %s", cipher)
return embedSS.New(config, inboundTfo, tcpIn, udpIn) return embedSS.New(config, tcpIn, udpIn)
} }
if err != nil { if err != nil {
return nil, err return nil, err
@ -101,10 +100,7 @@ func New(config string, inboundTfo bool, tcpIn chan<- C.ConnContext, udpIn chan<
}() }()
//TCP //TCP
lc := tfo.ListenConfig{ l, err := inbound.Listen("tcp", addr)
DisableTFO: !inboundTfo,
}
l, err := lc.Listen(context.Background(), "tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -2,7 +2,6 @@ package sing_vmess
import ( import (
"context" "context"
"github.com/database64128/tfo-go/v2"
"net" "net"
"net/url" "net/url"
"strings" "strings"
@ -25,7 +24,7 @@ type Listener struct {
var _listener *Listener var _listener *Listener
func New(config string, inboundTfo bool, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (*Listener, error) { func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (*Listener, error) {
addr, username, password, err := parseVmessURL(config) addr, username, password, err := parseVmessURL(config)
if err != nil { if err != nil {
return nil, err return nil, err
@ -55,10 +54,7 @@ func New(config string, inboundTfo bool, tcpIn chan<- C.ConnContext, udpIn chan<
addr := addr addr := addr
//TCP //TCP
lc := tfo.ListenConfig{ l, err := inbound.Listen("tcp", addr)
DisableTFO: !inboundTfo,
}
l, err := lc.Listen(context.Background(), "tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,8 +1,6 @@
package socks package socks
import ( import (
"context"
"github.com/database64128/tfo-go/v2"
"io" "io"
"net" "net"
@ -36,11 +34,8 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func New(addr string, inboundTfo bool, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
lc := tfo.ListenConfig{ l, err := inbound.Listen("tcp", addr)
DisableTFO: !inboundTfo,
}
l, err := lc.Listen(context.Background(), "tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -32,7 +32,7 @@ func New(config string, in chan<- C.ConnContext) (*Listener, error) {
log.Errorln("invalid target address %q", target) log.Errorln("invalid target address %q", target)
return return
} }
l, err := net.Listen("tcp", addr) l, err := inbound.Listen("tcp", addr)
if err != nil { if err != nil {
return return
} }