chore: change C.PacketAdapter from a struct to an interface

This commit is contained in:
wwqgtxx
2022-12-04 14:37:52 +08:00
parent 4f75201a98
commit 6fc62da7ae
32 changed files with 267 additions and 251 deletions

23
listener/config/tuic.go Normal file
View File

@ -0,0 +1,23 @@
package config
import (
"encoding/json"
)
type TuicServer struct {
Enable bool
Listen string
Token []string
Certificate string
PrivateKey string
CongestionController string
MaxIdleTime int
AuthenticationTimeout int
ALPN []string
MaxUdpRelayPacketSize int
}
func (t TuicServer) String() string {
b, _ := json.Marshal(t)
return string(b)
}

View File

@ -1,4 +1,4 @@
package sing_tun
package config
import (
"encoding/json"

View File

@ -9,10 +9,10 @@ import (
)
type Listener struct {
listener net.Listener
addr string
closed bool
name string
listener net.Listener
addr string
closed bool
name string
preferRulesName string
}
@ -33,14 +33,14 @@ func (l *Listener) Close() error {
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithAuthenticate(addr,"DEFAULT-HTTP","", in, true)
return NewWithAuthenticate(addr, "DEFAULT-HTTP", "", in, true)
}
func NewWithInfos(addr ,name ,preferRulesName string,in chan<-C.ConnContext)(*Listener,error){
return NewWithAuthenticate(addr,name,preferRulesName,in,true)
func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithAuthenticate(addr, name, preferRulesName, in, true)
}
func NewWithAuthenticate(addr,name,preferRulesName string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) {
func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) {
l, err := inbound.Listen("tcp", addr)
if err != nil {
@ -53,10 +53,10 @@ func NewWithAuthenticate(addr,name,preferRulesName string, in chan<- C.ConnConte
}
hl := &Listener{
listener: l,
name: name,
listener: l,
name: name,
preferRulesName: preferRulesName,
addr: addr,
addr: addr,
}
go func() {
for {
@ -67,7 +67,7 @@ func NewWithAuthenticate(addr,name,preferRulesName string, in chan<- C.ConnConte
}
continue
}
go HandleConn(hl.name,hl.preferRulesName,conn, in, c)
go HandleConn(hl.name, hl.preferRulesName, conn, in, c)
}
}()

View File

@ -52,7 +52,7 @@ func (b *Base) RawAddress() string {
}
// ReCreate implements constant.NewListener
func (*Base) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error {
func (*Base) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
return nil
}

View File

@ -30,7 +30,7 @@ func (h *HTTP) Address() string {
}
// ReCreate implements constant.NewListener
func (h *HTTP) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error {
func (h *HTTP) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
_ = h.Close()
h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.preferRulesName, tcpIn)

View File

@ -39,7 +39,7 @@ func (m *Mixed) Address() string {
}
// ReCreate implements constant.NewListener
func (m *Mixed) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error {
func (m *Mixed) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
_ = m.Close()
m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.preferRulesName, tcpIn)

View File

@ -31,7 +31,7 @@ func (r *Redir) Address() string {
}
// ReCreate implements constant.NewListener
func (r *Redir) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error {
func (r *Redir) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
_ = r.Close()
r.l, err = redir.NewWithInfos(r.Address(), r.name, r.preferRulesName, tcpIn)

View File

@ -60,7 +60,7 @@ func (s *Socks) Address() string {
}
// ReCreate implements constant.NewListener
func (s *Socks) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error {
func (s *Socks) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
s.mux.Lock()
defer s.mux.Unlock()
var err error

View File

@ -38,7 +38,7 @@ func (t *TProxy) Address() string {
}
// ReCreate implements constant.NewListener
func (t *TProxy) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error {
func (t *TProxy) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error
_ = t.Close()
t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.preferRulesName, tcpIn)

View File

@ -12,6 +12,7 @@ import (
"github.com/Dreamacro/clash/component/ebpf"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/autoredir"
LC "github.com/Dreamacro/clash/listener/config"
"github.com/Dreamacro/clash/listener/http"
"github.com/Dreamacro/clash/listener/mixed"
"github.com/Dreamacro/clash/listener/redir"
@ -64,8 +65,8 @@ var (
autoRedirMux sync.Mutex
tcMux sync.Mutex
LastTunConf sing_tun.Tun
LastTuicConf tuic.TuicServer
LastTunConf LC.Tun
LastTuicConf LC.TuicServer
)
type Ports struct {
@ -78,18 +79,18 @@ type Ports struct {
VmessConfig string `json:"vmess-config"`
}
func GetTunConf() sing_tun.Tun {
func GetTunConf() LC.Tun {
if tunLister == nil {
return sing_tun.Tun{
return LC.Tun{
Enable: false,
}
}
return tunLister.Config()
}
func GetTuicConf() tuic.TuicServer {
func GetTuicConf() LC.TuicServer {
if tuicListener == nil {
return tuic.TuicServer{Enable: false}
return LC.TuicServer{Enable: false}
}
return tuicListener.Config()
}
@ -144,7 +145,7 @@ func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
log.Infoln("HTTP proxy listening at: %s", httpListener.Address())
}
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
socksMux.Lock()
defer socksMux.Unlock()
@ -203,7 +204,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketA
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
}
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
redirMux.Lock()
defer redirMux.Unlock()
@ -249,7 +250,7 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketA
log.Infoln("Redirect proxy listening at: %s", redirListener.Address())
}
func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
ssMux.Lock()
defer ssMux.Unlock()
@ -289,7 +290,7 @@ func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, u
return
}
func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
vmessMux.Lock()
defer vmessMux.Unlock()
@ -329,7 +330,7 @@ func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<-
return
}
func ReCreateTuic(config tuic.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func ReCreateTuic(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
tuicMux.Lock()
defer func() {
LastTuicConf = config
@ -371,7 +372,7 @@ func ReCreateTuic(config tuic.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan
return
}
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
tproxyMux.Lock()
defer tproxyMux.Unlock()
@ -417,7 +418,7 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.Packet
log.Infoln("TProxy server listening at: %s", tproxyListener.Address())
}
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
mixedMux.Lock()
defer mixedMux.Unlock()
@ -472,7 +473,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketA
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
}
func ReCreateTun(tunConf sing_tun.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func ReCreateTun(tunConf LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
tunMux.Lock()
defer func() {
LastTunConf = tunConf
@ -536,7 +537,7 @@ func ReCreateRedirToTun(ifaceNames []string) {
log.Infoln("Attached tc ebpf program to interfaces %v", tcProgram.RawNICs())
}
func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- *C.PacketAdapter) {
func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- C.PacketAdapter) {
autoRedirMux.Lock()
defer autoRedirMux.Unlock()
@ -592,7 +593,7 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<-
log.Infoln("Auto redirect proxy listening at: %s, attached tc ebpf program to interfaces %v", autoRedirListener.Address(), autoRedirProgram.RawNICs())
}
func PatchTunnel(tunnels []tunnel.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) {
func PatchTunnel(tunnels []tunnel.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
tunnelMux.Lock()
defer tunnelMux.Unlock()
@ -745,7 +746,7 @@ func genAddr(host string, port int, allowLan bool) string {
return fmt.Sprintf("127.0.0.1:%d", port)
}
func hasTunConfigChange(tunConf *sing_tun.Tun) bool {
func hasTunConfigChange(tunConf *LC.Tun) bool {
if LastTunConf.Enable != tunConf.Enable ||
LastTunConf.Device != tunConf.Device ||
LastTunConf.Stack != tunConf.Stack ||
@ -833,5 +834,5 @@ func Cleanup(wait bool) {
tunLister.Close()
tunLister = nil
}
LastTunConf = sing_tun.Tun{}
LastTunConf = LC.Tun{}
}

View File

@ -25,23 +25,38 @@ func ParseListener(mapping map[string]any) (C.NewListener, error) {
switch proxyType {
case "socks":
socksOption := &IN.SocksOption{}
decoder.Decode(mapping, socksOption)
err = decoder.Decode(mapping, socksOption)
if err != nil {
return nil, err
}
listener, err = IN.NewSocks(socksOption)
case "http":
httpOption := &IN.HTTPOption{}
decoder.Decode(mapping, httpOption)
err = decoder.Decode(mapping, httpOption)
if err != nil {
return nil, err
}
listener, err = IN.NewHTTP(httpOption)
case "tproxy":
tproxyOption := &IN.TProxyOption{}
decoder.Decode(mapping, tproxyOption)
err = decoder.Decode(mapping, tproxyOption)
if err != nil {
return nil, err
}
listener, err = IN.NewTProxy(tproxyOption)
case "redir":
redirOption := &IN.RedirOption{}
decoder.Decode(mapping, redirOption)
err = decoder.Decode(mapping, redirOption)
if err != nil {
return nil, err
}
listener, err = IN.NewRedir(redirOption)
case "mixed":
mixedOption := &IN.MixedOption{}
decoder.Decode(mapping, mixedOption)
err = decoder.Decode(mapping, mixedOption)
if err != nil {
return nil, err
}
listener, err = IN.NewMixed(mixedOption)
default:
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)

View File

@ -8,10 +8,10 @@ import (
)
type Listener struct {
listener net.Listener
addr string
closed bool
name string
listener net.Listener
addr string
closed bool
name string
preferRulesName string
}
@ -32,18 +32,18 @@ func (l *Listener) Close() error {
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithInfos(addr,"DEFAULT-REDIR","",in)
return NewWithInfos(addr, "DEFAULT-REDIR", "", in)
}
func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
rl := &Listener{
listener: l,
addr: addr,
name: name,
listener: l,
addr: addr,
name: name,
preferRulesName: preferRulesName,
}
@ -56,18 +56,18 @@ func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*L
}
continue
}
go handleRedir(rl.name,rl.preferRulesName,c, in)
go handleRedir(rl.name, rl.preferRulesName, c, in)
}
}()
return rl, nil
}
func handleRedir(name,preferRulesName string,conn net.Conn, in chan<- C.ConnContext) {
func handleRedir(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) {
target, err := parserPacket(conn)
if err != nil {
conn.Close()
return
}
conn.(*net.TCPConn).SetKeepAlive(true)
in <- inbound.NewSocketWithInfos(target, conn, C.REDIR,name,preferRulesName)
in <- inbound.NewSocketWithInfos(target, conn, C.REDIR, name, preferRulesName)
}

View File

@ -21,7 +21,7 @@ type Listener struct {
var _listener *Listener
func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (*Listener, error) {
func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (*Listener, error) {
addr, cipher, password, err := ParseSSURL(config)
if err != nil {
return nil, err

View File

@ -17,7 +17,7 @@ type UDPListener struct {
closed bool
}
func NewUDP(addr string, pickCipher core.Cipher, in chan<- *C.PacketAdapter) (*UDPListener, error) {
func NewUDP(addr string, pickCipher core.Cipher, in chan<- C.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
@ -53,7 +53,7 @@ func (l *UDPListener) Close() error {
return l.packetConn.Close()
}
func handleSocksUDP(pc net.PacketConn, in chan<- *C.PacketAdapter, buf []byte, addr net.Addr) {
func handleSocksUDP(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) {
tgtAddr := socks5.SplitAddr(buf)
if tgtAddr == nil {
// Unresolved UDP packet, return buffer to the pool

View File

@ -24,7 +24,7 @@ const UDPTimeout = 5 * time.Minute
type ListenerHandler struct {
TcpIn chan<- C.ConnContext
UdpIn chan<- *C.PacketAdapter
UdpIn chan<- C.PacketAdapter
Type C.Type
}

View File

@ -32,7 +32,7 @@ type Listener struct {
var _listener *Listener
func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (C.AdvanceListener, error) {
func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (C.AdvanceListener, error) {
addr, cipher, password, err := embedSS.ParseSSURL(config)
if err != nil {
return nil, err

View File

@ -11,6 +11,7 @@ import (
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/iface"
C "github.com/Dreamacro/clash/constant"
LC "github.com/Dreamacro/clash/listener/config"
"github.com/Dreamacro/clash/listener/sing"
"github.com/Dreamacro/clash/log"
@ -25,7 +26,7 @@ var InterfaceName = "Meta"
type Listener struct {
closed bool
options Tun
options LC.Tun
handler *ListenerHandler
tunName string
@ -63,7 +64,7 @@ func CalculateInterfaceName(name string) (tunName string) {
return
}
func New(options Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (l *Listener, err error) {
func New(options LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (l *Listener, err error) {
tunName := options.Device
if tunName == "" {
tunName = CalculateInterfaceName(InterfaceName)
@ -161,12 +162,12 @@ func New(options Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter)
tunOptions := tun.Options{
Name: tunName,
MTU: tunMTU,
Inet4Address: common.Map(options.Inet4Address, ListenPrefix.Build),
Inet6Address: common.Map(options.Inet6Address, ListenPrefix.Build),
Inet4Address: common.Map(options.Inet4Address, LC.ListenPrefix.Build),
Inet6Address: common.Map(options.Inet6Address, LC.ListenPrefix.Build),
AutoRoute: options.AutoRoute,
StrictRoute: options.StrictRoute,
Inet4RouteAddress: common.Map(options.Inet4RouteAddress, ListenPrefix.Build),
Inet6RouteAddress: common.Map(options.Inet6RouteAddress, ListenPrefix.Build),
Inet4RouteAddress: common.Map(options.Inet4RouteAddress, LC.ListenPrefix.Build),
Inet6RouteAddress: common.Map(options.Inet6RouteAddress, LC.ListenPrefix.Build),
IncludeUID: includeUID,
ExcludeUID: excludeUID,
IncludeAndroidUser: options.IncludeAndroidUser,
@ -282,6 +283,6 @@ func (l *Listener) Close() {
)
}
func (l *Listener) Config() Tun {
func (l *Listener) Config() LC.Tun {
return l.options
}

View File

@ -24,7 +24,7 @@ type Listener struct {
var _listener *Listener
func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (*Listener, error) {
func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (*Listener, error) {
addr, username, password, err := parseVmessURL(config)
if err != nil {
return nil, err

View File

@ -13,11 +13,11 @@ import (
)
type Listener struct {
listener net.Listener
addr string
closed bool
preferRulesName string
name string
listener net.Listener
addr string
closed bool
preferRulesName string
name string
}
// RawAddress implements C.Listener
@ -37,19 +37,19 @@ func (l *Listener) Close() error {
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithInfos(addr,"DEFAULT-SOCKS","",in)
return NewWithInfos(addr, "DEFAULT-SOCKS", "", in)
}
func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
l, err := inbound.Listen("tcp", addr)
if err != nil {
return nil, err
}
sl := &Listener{
listener: l,
addr: addr,
name: name,
listener: l,
addr: addr,
name: name,
preferRulesName: preferRulesName,
}
go func() {
@ -61,14 +61,14 @@ func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*L
}
continue
}
go handleSocks(sl.name,sl.preferRulesName,c, in)
go handleSocks(sl.name, sl.preferRulesName, c, in)
}
}()
return sl, nil
}
func handleSocks(name,preferRulesName string,conn net.Conn, in chan<- C.ConnContext) {
func handleSocks(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) {
conn.(*net.TCPConn).SetKeepAlive(true)
bufConn := N.NewBufferedConn(conn)
head, err := bufConn.Peek(1)
@ -79,24 +79,24 @@ func handleSocks(name,preferRulesName string,conn net.Conn, in chan<- C.ConnCont
switch head[0] {
case socks4.Version:
HandleSocks4(name,preferRulesName,bufConn, in)
HandleSocks4(name, preferRulesName, bufConn, in)
case socks5.Version:
HandleSocks5(name,preferRulesName,bufConn, in)
HandleSocks5(name, preferRulesName, bufConn, in)
default:
conn.Close()
}
}
func HandleSocks4(name,preferRulesName string, conn net.Conn, in chan<- C.ConnContext) {
func HandleSocks4(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) {
addr, _, err := socks4.ServerHandshake(conn, authStore.Authenticator())
if err != nil {
conn.Close()
return
}
in <- inbound.NewSocketWithInfos(socks5.ParseAddr(addr), conn, C.SOCKS4,name,preferRulesName)
in <- inbound.NewSocketWithInfos(socks5.ParseAddr(addr), conn, C.SOCKS4, name, preferRulesName)
}
func HandleSocks5(name,preferRulesName string,conn net.Conn, in chan<- C.ConnContext) {
func HandleSocks5(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) {
target, command, err := socks5.ServerHandshake(conn, authStore.Authenticator())
if err != nil {
conn.Close()
@ -107,5 +107,5 @@ func HandleSocks5(name,preferRulesName string,conn net.Conn, in chan<- C.ConnCon
io.Copy(io.Discard, conn)
return
}
in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5,name,preferRulesName)
in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, preferRulesName)
}

View File

@ -12,10 +12,10 @@ import (
)
type UDPListener struct {
packetConn net.PacketConn
addr string
closed bool
name string
packetConn net.PacketConn
addr string
closed bool
name string
preferRulesName string
}
@ -35,11 +35,11 @@ func (l *UDPListener) Close() error {
return l.packetConn.Close()
}
func NewUDP(addr string, in chan<- *C.PacketAdapter) (*UDPListener, error) {
return NewUDPWithInfos(addr,"DEFAULT-SOCKS","",in)
func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) {
return NewUDPWithInfos(addr, "DEFAULT-SOCKS", "", in)
}
func NewUDPWithInfos(addr,name ,preferRulesName string, in chan<- *C.PacketAdapter) (*UDPListener, error) {
func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
@ -50,10 +50,10 @@ func NewUDPWithInfos(addr,name ,preferRulesName string, in chan<- *C.PacketAdapt
}
sl := &UDPListener{
packetConn: l,
addr: addr,
packetConn: l,
addr: addr,
preferRulesName: preferRulesName,
name: name,
name: name,
}
go func() {
for {
@ -66,14 +66,14 @@ func NewUDPWithInfos(addr,name ,preferRulesName string, in chan<- *C.PacketAdapt
}
continue
}
handleSocksUDP(sl.name,sl.preferRulesName,l, in, buf[:n], remoteAddr)
handleSocksUDP(sl.name, sl.preferRulesName, l, in, buf[:n], remoteAddr)
}
}()
return sl, nil
}
func handleSocksUDP(name,preferRulesName string,pc net.PacketConn, in chan<- *C.PacketAdapter, buf []byte, addr net.Addr) {
func handleSocksUDP(name, preferRulesName string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) {
target, payload, err := socks5.DecodeUDPPacket(buf)
if err != nil {
// Unresolved UDP packet, return buffer to the pool
@ -87,7 +87,7 @@ func handleSocksUDP(name,preferRulesName string,pc net.PacketConn, in chan<- *C.
bufRef: buf,
}
select {
case in <- inbound.NewPacketWithInfos(target, packet, C.SOCKS5,name,preferRulesName):
case in <- inbound.NewPacketWithInfos(target, packet, C.SOCKS5, name, preferRulesName):
default:
}
}

View File

@ -9,10 +9,10 @@ import (
)
type Listener struct {
listener net.Listener
addr string
closed bool
name string
listener net.Listener
addr string
closed bool
name string
preferRulesName string
}
@ -32,17 +32,17 @@ func (l *Listener) Close() error {
return l.listener.Close()
}
func (l *Listener) handleTProxy(name,preferRulesName string ,conn net.Conn, in chan<- C.ConnContext) {
func (l *Listener) handleTProxy(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) {
target := socks5.ParseAddrToSocksAddr(conn.LocalAddr())
conn.(*net.TCPConn).SetKeepAlive(true)
in <- inbound.NewSocketWithInfos(target, conn, C.TPROXY,name,preferRulesName)
in <- inbound.NewSocketWithInfos(target, conn, C.TPROXY, name, preferRulesName)
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithInfos(addr,"DEFAULT-TPROXY","",in)
return NewWithInfos(addr, "DEFAULT-TPROXY", "", in)
}
func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
@ -60,9 +60,9 @@ func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*L
}
rl := &Listener{
listener: l,
addr: addr,
name: name,
listener: l,
addr: addr,
name: name,
preferRulesName: preferRulesName,
}
@ -75,7 +75,7 @@ func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*L
}
continue
}
go rl.handleTProxy(rl.name,rl.preferRulesName,c, in)
go rl.handleTProxy(rl.name, rl.preferRulesName, c, in)
}
}()

View File

@ -34,11 +34,11 @@ func (l *UDPListener) Close() error {
return l.packetConn.Close()
}
func NewUDP(addr string, in chan<- *C.PacketAdapter) (*UDPListener, error) {
func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) {
return NewUDPWithInfos(addr, "DEFAULT-TPROXY", "", in)
}
func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- *C.PacketAdapter) (*UDPListener, error) {
func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
@ -90,7 +90,7 @@ func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- *C.PacketAdap
return rl, nil
}
func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<- *C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) {
func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) {
target := socks5.AddrFromStdAddrPort(rAddr)
pkt := &packet{
pc: pc,

View File

@ -2,47 +2,29 @@ package tuic
import (
"crypto/tls"
"encoding/json"
"net"
"strings"
"time"
"github.com/metacubex/quic-go"
"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/common/sockopt"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/adapter/inbound"
LC "github.com/Dreamacro/clash/listener/config"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/transport/socks5"
"github.com/Dreamacro/clash/transport/tuic"
)
type TuicServer struct {
Enable bool
Listen string
Token []string
Certificate string
PrivateKey string
CongestionController string
MaxIdleTime int
AuthenticationTimeout int
ALPN []string
MaxUdpRelayPacketSize int
}
func (t TuicServer) String() string {
b, _ := json.Marshal(t)
return string(b)
}
type Listener struct {
closed bool
config TuicServer
config LC.TuicServer
udpListeners []net.PacketConn
servers []*tuic.Server
}
func New(config TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (*Listener, error) {
func New(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (*Listener, error) {
cert, err := tls.LoadX509KeyPair(config.Certificate, config.PrivateKey)
if err != nil {
return nil, err
@ -140,6 +122,6 @@ func (l *Listener) Close() {
}
}
func (l *Listener) Config() TuicServer {
func (l *Listener) Config() LC.TuicServer {
return l.config
}

View File

@ -34,7 +34,7 @@ func (l *PacketConn) Close() error {
return l.conn.Close()
}
func NewUDP(addr, target, proxy string, in chan<- *C.PacketAdapter) (*PacketConn, error) {
func NewUDP(addr, target, proxy string, in chan<- C.PacketAdapter) (*PacketConn, error) {
l, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
@ -69,7 +69,7 @@ func NewUDP(addr, target, proxy string, in chan<- *C.PacketAdapter) (*PacketConn
return sl, nil
}
func (l *PacketConn) handleUDP(pc net.PacketConn, in chan<- *C.PacketAdapter, buf []byte, addr net.Addr) {
func (l *PacketConn) handleUDP(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) {
packet := &packet{
pc: pc,
rAddr: addr,