chore: rebuild add adapter/inbound.Addition to simply Listener.New apis

This commit is contained in:
wwqgtxx 2022-12-05 00:20:50 +08:00
parent c7f83d3ff1
commit 2e22c712af
30 changed files with 290 additions and 225 deletions

View file

@ -0,0 +1,15 @@
package inbound
import (
C "github.com/Dreamacro/clash/constant"
)
type Addition struct {
InName string
SpecialRules string
}
func (a Addition) Apply(metadata *C.Metadata) {
metadata.InName = a.InName
metadata.SpecialRules = a.SpecialRules
}

View file

@ -9,16 +9,13 @@ import (
) )
// NewHTTP receive normal http request and return HTTPContext // NewHTTP receive normal http request and return HTTPContext
func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn) *context.ConnContext { func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn, additions ...Addition) *context.ConnContext {
return NewHTTPWithInfos(target, source, conn, "", "")
}
func NewHTTPWithInfos(target socks5.Addr, source net.Addr, conn net.Conn, inName, specialRules string) *context.ConnContext {
metadata := parseSocksAddr(target) metadata := parseSocksAddr(target)
metadata.NetWork = C.TCP metadata.NetWork = C.TCP
metadata.Type = C.HTTP metadata.Type = C.HTTP
metadata.InName = inName for _, addition := range additions {
metadata.SpecialRules = specialRules addition.Apply(metadata)
}
if ip, port, err := parseAddr(source.String()); err == nil { if ip, port, err := parseAddr(source.String()); err == nil {
metadata.SrcIP = ip metadata.SrcIP = ip
metadata.SrcPort = port metadata.SrcPort = port

View file

@ -9,15 +9,12 @@ import (
) )
// NewHTTPS receive CONNECT request and return ConnContext // NewHTTPS receive CONNECT request and return ConnContext
func NewHTTPS(request *http.Request, conn net.Conn) *context.ConnContext { func NewHTTPS(request *http.Request, conn net.Conn, additions ...Addition) *context.ConnContext {
return NewHTTPSWithInfos(request, conn, "", "")
}
func NewHTTPSWithInfos(request *http.Request, conn net.Conn, inName, specialRules string) *context.ConnContext {
metadata := parseHTTPAddr(request) metadata := parseHTTPAddr(request)
metadata.Type = C.HTTPS metadata.Type = C.HTTPS
metadata.SpecialRules = specialRules for _, addition := range additions {
metadata.InName = inName addition.Apply(metadata)
}
if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil { if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil {
metadata.SrcIP = ip metadata.SrcIP = ip
metadata.SrcPort = port metadata.SrcPort = port

View file

@ -16,12 +16,14 @@ func (s *PacketAdapter) Metadata() *C.Metadata {
return s.metadata return s.metadata
} }
func NewPacketWithInfos(target socks5.Addr, packet C.UDPPacket, source C.Type, inName, specialRules string) C.PacketAdapter { // NewPacket is PacketAdapter generator
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type, additions ...Addition) C.PacketAdapter {
metadata := parseSocksAddr(target) metadata := parseSocksAddr(target)
metadata.NetWork = C.UDP metadata.NetWork = C.UDP
metadata.Type = source metadata.Type = source
metadata.InName = inName for _, addition := range additions {
metadata.SpecialRules = specialRules addition.Apply(metadata)
}
if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil { if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil {
metadata.SrcIP = ip metadata.SrcIP = ip
metadata.SrcPort = port metadata.SrcPort = port
@ -38,8 +40,3 @@ func NewPacketWithInfos(target socks5.Addr, packet C.UDPPacket, source C.Type, i
metadata, metadata,
} }
} }
// NewPacket is PacketAdapter generator
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type) C.PacketAdapter {
return NewPacketWithInfos(target, packet, source, "", "")
}

View file

@ -9,12 +9,15 @@ import (
"github.com/Dreamacro/clash/transport/socks5" "github.com/Dreamacro/clash/transport/socks5"
) )
func NewSocketWithInfos(target socks5.Addr, conn net.Conn, source C.Type, inName, specialRules string) *context.ConnContext { // NewSocket receive TCP inbound and return ConnContext
func NewSocket(target socks5.Addr, conn net.Conn, source C.Type, additions ...Addition) *context.ConnContext {
metadata := parseSocksAddr(target) metadata := parseSocksAddr(target)
metadata.NetWork = C.TCP metadata.NetWork = C.TCP
metadata.Type = source metadata.Type = source
metadata.SpecialRules = specialRules for _, addition := range additions {
metadata.InName = inName addition.Apply(metadata)
}
remoteAddr := conn.RemoteAddr() remoteAddr := conn.RemoteAddr()
// Filter when net.Addr interface is nil // Filter when net.Addr interface is nil
@ -36,11 +39,6 @@ func NewSocketWithInfos(target socks5.Addr, conn net.Conn, source C.Type, inName
return context.NewConnContext(conn, metadata) return context.NewConnContext(conn, metadata)
} }
// NewSocket receive TCP inbound and return ConnContext
func NewSocket(target socks5.Addr, conn net.Conn, source C.Type) *context.ConnContext {
return NewSocketWithInfos(target, conn, source, "", "")
}
func NewInner(conn net.Conn, dst string, host string) *context.ConnContext { func NewInner(conn net.Conn, dst string, host string) *context.ConnContext {
metadata := &C.Metadata{} metadata := &C.Metadata{}
metadata.NetWork = C.TCP metadata.NetWork = C.TCP

View file

@ -9,8 +9,9 @@ type Listener interface {
} }
type AdvanceListener interface { type AdvanceListener interface {
Close() Close() error
Config() string Config() string
AddrList() (addrList []net.Addr)
HandleConn(conn net.Conn, in chan<- ConnContext) HandleConn(conn net.Conn, in chan<- ConnContext)
} }

View file

@ -14,8 +14,7 @@ type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
name string additions []inbound.Addition
specialRules string
lookupFunc func(netip.AddrPort) (socks5.Addr, error) lookupFunc func(netip.AddrPort) (socks5.Addr, error)
} }
@ -58,14 +57,16 @@ func (l *Listener) handleRedir(conn net.Conn, in chan<- C.ConnContext) {
_ = conn.(*net.TCPConn).SetKeepAlive(true) _ = conn.(*net.TCPConn).SetKeepAlive(true)
in <- inbound.NewSocketWithInfos(target, conn, C.REDIR, l.name, l.specialRules) in <- inbound.NewSocket(target, conn, C.REDIR, l.additions...)
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext, additions ...inbound.Addition) (*Listener, error) {
return NewWithInfos(addr, "DEFAULT-REDIR", "", in) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-REDIR",
SpecialRules: "",
}}
} }
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,8 +74,7 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
rl := &Listener{ rl := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name, additions: additions,
specialRules: specialRules,
} }
go func() { go func() {

View file

@ -12,7 +12,7 @@ import (
"github.com/Dreamacro/clash/transport/socks5" "github.com/Dreamacro/clash/transport/socks5"
) )
func newClient(source net.Addr, name, specialRules string, in chan<- C.ConnContext) *http.Client { func newClient(source net.Addr, in chan<- C.ConnContext, additions ...inbound.Addition) *http.Client {
return &http.Client{ return &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
// from http.DefaultTransport // from http.DefaultTransport
@ -32,7 +32,7 @@ func newClient(source net.Addr, name, specialRules string, in chan<- C.ConnConte
left, right := net.Pipe() left, right := net.Pipe()
in <- inbound.NewHTTPWithInfos(dstAddr, source, right, name, specialRules) in <- inbound.NewHTTP(dstAddr, source, right, additions...)
return left, nil return left, nil
}, },

View file

@ -14,8 +14,8 @@ import (
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
) )
func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) { func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool], additions ...inbound.Addition) {
client := newClient(c.RemoteAddr(), name, specialRules, in) client := newClient(c.RemoteAddr(), in, additions...)
defer client.CloseIdleConnections() defer client.CloseIdleConnections()
conn := N.NewBufferedConn(c) conn := N.NewBufferedConn(c)
@ -48,7 +48,7 @@ func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext,
break // close connection break // close connection
} }
in <- inbound.NewHTTPSWithInfos(request, conn, name, specialRules) in <- inbound.NewHTTPS(request, conn, additions...)
return // hijack connection return // hijack connection
} }
@ -61,7 +61,7 @@ func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext,
request.RequestURI = "" request.RequestURI = ""
if isUpgradeRequest(request) { if isUpgradeRequest(request) {
handleUpgrade(name, specialRules, conn, request, in) handleUpgrade(conn, request, in, additions...)
return // hijack connection return // hijack connection
} }

View file

@ -12,8 +12,6 @@ type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
name string
specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -32,15 +30,17 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext, additions ...inbound.Addition) (*Listener, error) {
return NewWithAuthenticate(addr, "DEFAULT-HTTP", "", in, true) return NewWithAuthenticate(addr, in, true, additions...)
} }
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) { func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool, additions ...inbound.Addition) (*Listener, error) {
return NewWithAuthenticate(addr, name, specialRules, in, true) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-HTTP",
SpecialRules: "",
}}
} }
func NewWithAuthenticate(addr, name, specialRules string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) {
l, err := inbound.Listen("tcp", addr) l, err := inbound.Listen("tcp", addr)
if err != nil { if err != nil {
@ -54,8 +54,6 @@ func NewWithAuthenticate(addr, name, specialRules string, in chan<- C.ConnContex
hl := &Listener{ hl := &Listener{
listener: l, listener: l,
name: name,
specialRules: specialRules,
addr: addr, addr: addr,
} }
go func() { go func() {
@ -67,7 +65,7 @@ func NewWithAuthenticate(addr, name, specialRules string, in chan<- C.ConnContex
} }
continue continue
} }
go HandleConn(hl.name, hl.specialRules, conn, in, c) go HandleConn(conn, in, c, additions...)
} }
}() }()

View file

@ -25,7 +25,7 @@ func isUpgradeRequest(req *http.Request) bool {
return false return false
} }
func handleUpgrade(name, specialRules string, conn net.Conn, request *http.Request, in chan<- C.ConnContext) { func handleUpgrade(conn net.Conn, request *http.Request, in chan<- C.ConnContext, additions ...inbound.Addition) {
defer conn.Close() defer conn.Close()
removeProxyHeaders(request.Header) removeProxyHeaders(request.Header)
@ -43,7 +43,7 @@ func handleUpgrade(name, specialRules string, conn net.Conn, request *http.Reque
left, right := net.Pipe() left, right := net.Pipe()
in <- inbound.NewHTTPWithInfos(dstAddr, conn.RemoteAddr(), right, name, specialRules) in <- inbound.NewHTTP(dstAddr, conn.RemoteAddr(), right, additions...)
var bufferedLeft *N.BufferedConn var bufferedLeft *N.BufferedConn
if request.TLS != nil { if request.TLS != nil {

View file

@ -6,6 +6,7 @@ import (
"net/netip" "net/netip"
"strconv" "strconv"
"github.com/Dreamacro/clash/adapter/inbound"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
) )
@ -28,7 +29,7 @@ func NewBase(options *BaseOption) (*Base, error) {
return &Base{ return &Base{
name: options.Name(), name: options.Name(),
listenAddr: addr, listenAddr: addr,
specialRules: options.PreferRulesName, specialRules: options.SpecialRules,
port: options.Port, port: options.Port,
config: options, config: options,
}, nil }, nil
@ -64,13 +65,17 @@ func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) er
return nil return nil
} }
func (b *Base) Additions() []inbound.Addition {
return b.config.Additions()
}
var _ C.InboundListener = (*Base)(nil) var _ C.InboundListener = (*Base)(nil)
type BaseOption struct { type BaseOption struct {
NameStr string `inbound:"name"` NameStr string `inbound:"name"`
Listen string `inbound:"listen,omitempty"` Listen string `inbound:"listen,omitempty"`
Port int `inbound:"port"` Port int `inbound:"port"`
PreferRulesName string `inbound:"rule,omitempty"` SpecialRules string `inbound:"rule,omitempty"`
} }
func (o BaseOption) Name() string { func (o BaseOption) Name() string {
@ -81,6 +86,13 @@ func (o BaseOption) Equal(config C.InboundConfig) bool {
return optionToString(o) == optionToString(config) return optionToString(o) == optionToString(config)
} }
func (o BaseOption) Additions() []inbound.Addition {
return []inbound.Addition{{
InName: o.NameStr,
SpecialRules: o.SpecialRules,
}}
}
var _ C.InboundConfig = (*BaseOption)(nil) var _ C.InboundConfig = (*BaseOption)(nil)
func optionToString(option any) string { func optionToString(option any) string {

View file

@ -44,7 +44,7 @@ func (h *HTTP) Address() string {
// Listen implements constant.InboundListener // Listen implements constant.InboundListener
func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.specialRules, tcpIn) h.l, err = http.New(h.RawAddress(), tcpIn, h.Additions()...)
if err != nil { if err != nil {
return err return err
} }

View file

@ -52,12 +52,12 @@ func (m *Mixed) Address() string {
// Listen implements constant.InboundListener // Listen implements constant.InboundListener
func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.specialRules, tcpIn) m.l, err = mixed.New(m.RawAddress(), tcpIn, m.Additions()...)
if err != nil { if err != nil {
return err return err
} }
if m.udp { if m.udp {
m.lUDP, err = socks.NewUDPWithInfos(m.Address(), m.name, m.specialRules, udpIn) m.lUDP, err = socks.NewUDP(m.Address(), udpIn, m.Additions()...)
if err != nil { if err != nil {
return err return err
} }

View file

@ -44,7 +44,7 @@ func (r *Redir) Address() string {
// Listen implements constant.InboundListener // Listen implements constant.InboundListener
func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
r.l, err = redir.NewWithInfos(r.Address(), r.name, r.specialRules, tcpIn) r.l, err = redir.New(r.Address(), tcpIn, r.Additions()...)
if err != nil { if err != nil {
return err return err
} }

View file

@ -70,11 +70,11 @@ func (s *Socks) Address() string {
// Listen implements constant.InboundListener // Listen implements constant.InboundListener
func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
if s.stl, err = socks.NewWithInfos(s.RawAddress(), s.name, s.specialRules, tcpIn); err != nil { if s.stl, err = socks.New(s.RawAddress(), tcpIn, s.Additions()...); err != nil {
return err return err
} }
if s.udp { if s.udp {
if s.sul, err = socks.NewUDPWithInfos(s.RawAddress(), s.name, s.specialRules, udpIn); err != nil { if s.sul, err = socks.NewUDP(s.RawAddress(), udpIn, s.Additions()...); err != nil {
return err return err
} }
} }

View file

@ -51,13 +51,13 @@ func (t *TProxy) Address() string {
// Listen implements constant.InboundListener // Listen implements constant.InboundListener
func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.specialRules, tcpIn) t.lTCP, err = tproxy.New(t.RawAddress(), tcpIn, t.Additions()...)
if err != nil { if err != nil {
return err return err
} }
if t.udp { if t.udp {
if t.lUDP != nil { if t.lUDP != nil {
t.lUDP, err = tproxy.NewUDPWithInfos(t.Address(), t.name, t.specialRules, udpIn) t.lUDP, err = tproxy.NewUDP(t.Address(), udpIn, t.Additions()...)
if err != nil { if err != nil {
return err return err
} }

View file

@ -59,7 +59,8 @@ func (t *Tuic) Address() string {
// Listen implements constant.InboundListener // Listen implements constant.InboundListener
func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
var err error var err error
t.l, err = tuic.New(LC.TuicServer{ t.l, err = tuic.New(
LC.TuicServer{
Enable: true, Enable: true,
Listen: t.RawAddress(), Listen: t.RawAddress(),
Token: t.config.Token, Token: t.config.Token,
@ -70,7 +71,11 @@ func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter)
AuthenticationTimeout: t.config.AuthenticationTimeout, AuthenticationTimeout: t.config.AuthenticationTimeout,
ALPN: t.config.ALPN, ALPN: t.config.ALPN,
MaxUdpRelayPacketSize: t.config.MaxUdpRelayPacketSize, MaxUdpRelayPacketSize: t.config.MaxUdpRelayPacketSize,
}, tcpIn, udpIn) },
tcpIn,
udpIn,
t.Additions()...,
)
if err != nil { if err != nil {
return err return err
} }

View file

@ -16,8 +16,6 @@ import (
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
name string
specialRules string
cache *cache.LruCache[string, bool] cache *cache.LruCache[string, bool]
closed bool closed bool
} }
@ -38,11 +36,13 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext, additions ...inbound.Addition) (*Listener, error) {
return NewWithInfos(addr, "DEFAULT-MIXED", "", in) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-MIXED",
SpecialRules: "",
}}
} }
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
l, err := inbound.Listen("tcp", addr) l, err := inbound.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -51,8 +51,6 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
ml := &Listener{ ml := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name,
specialRules: specialRules,
cache: cache.New[string, bool](cache.WithAge[string, bool](30)), cache: cache.New[string, bool](cache.WithAge[string, bool](30)),
} }
go func() { go func() {
@ -64,14 +62,14 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
} }
continue continue
} }
go handleConn(ml.name, ml.specialRules, c, in, ml.cache) go handleConn(c, in, ml.cache, additions...)
} }
}() }()
return ml, nil return ml, nil
} }
func handleConn(name, specialRules string, conn net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) { func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool], additions ...inbound.Addition) {
conn.(*net.TCPConn).SetKeepAlive(true) conn.(*net.TCPConn).SetKeepAlive(true)
bufConn := N.NewBufferedConn(conn) bufConn := N.NewBufferedConn(conn)
@ -82,10 +80,10 @@ func handleConn(name, specialRules string, conn net.Conn, in chan<- C.ConnContex
switch head[0] { switch head[0] {
case socks4.Version: case socks4.Version:
socks.HandleSocks4(name, specialRules, bufConn, in) socks.HandleSocks4(bufConn, in, additions...)
case socks5.Version: case socks5.Version:
socks.HandleSocks5(name, specialRules, bufConn, in) socks.HandleSocks5(bufConn, in, additions...)
default: default:
http.HandleConn(name, specialRules, bufConn, in, cache) http.HandleConn(bufConn, in, cache, additions...)
} }
} }

View file

@ -11,8 +11,6 @@ type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
name string
specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -31,11 +29,13 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext, additions ...inbound.Addition) (*Listener, error) {
return NewWithInfos(addr, "DEFAULT-REDIR", "", in) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-REDIR",
SpecialRules: "",
}}
} }
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -43,8 +43,6 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
rl := &Listener{ rl := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name,
specialRules: specialRules,
} }
go func() { go func() {
@ -56,18 +54,18 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
} }
continue continue
} }
go handleRedir(rl.name, rl.specialRules, c, in) go handleRedir(c, in, additions...)
} }
}() }()
return rl, nil return rl, nil
} }
func handleRedir(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) { func handleRedir(conn net.Conn, in chan<- C.ConnContext, additions ...inbound.Addition) {
target, err := parserPacket(conn) target, err := parserPacket(conn)
if err != nil { if err != nil {
conn.Close() conn.Close()
return return
} }
conn.(*net.TCPConn).SetKeepAlive(true) conn.(*net.TCPConn).SetKeepAlive(true)
in <- inbound.NewSocketWithInfos(target, conn, C.REDIR, name, specialRules) in <- inbound.NewSocket(target, conn, C.REDIR, additions...)
} }

View file

@ -71,20 +71,37 @@ func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter
return sl, nil return sl, nil
} }
func (l *Listener) Close() { func (l *Listener) Close() error {
l.closed = true var retErr error
for _, lis := range l.listeners { for _, lis := range l.listeners {
_ = lis.Close() err := lis.Close()
if err != nil {
retErr = err
}
} }
for _, lis := range l.udpListeners { for _, lis := range l.udpListeners {
_ = lis.Close() err := lis.Close()
if err != nil {
retErr = err
} }
} }
return retErr
}
func (l *Listener) Config() string { func (l *Listener) Config() string {
return l.config return l.config
} }
func (l *Listener) AddrList() (addrList []net.Addr) {
for _, lis := range l.listeners {
addrList = append(addrList, lis.Addr())
}
for _, lis := range l.udpListeners {
addrList = append(addrList, lis.LocalAddr())
}
return
}
func (l *Listener) HandleConn(conn net.Conn, in chan<- C.ConnContext) { func (l *Listener) HandleConn(conn net.Conn, in chan<- C.ConnContext) {
conn = l.pickCipher.StreamConn(conn) conn = l.pickCipher.StreamConn(conn)

View file

@ -53,6 +53,10 @@ func (l *UDPListener) Close() error {
return l.packetConn.Close() return l.packetConn.Close()
} }
func (l *UDPListener) LocalAddr() net.Addr {
return l.packetConn.LocalAddr()
}
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) tgtAddr := socks5.SplitAddr(buf)
if tgtAddr == nil { if tgtAddr == nil {

View file

@ -126,20 +126,38 @@ func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter
return sl, nil return sl, nil
} }
func (l *Listener) Close() { func (l *Listener) Close() error {
l.closed = true l.closed = true
var retErr error
for _, lis := range l.listeners { for _, lis := range l.listeners {
_ = lis.Close() err := lis.Close()
if err != nil {
retErr = err
}
} }
for _, lis := range l.udpListeners { for _, lis := range l.udpListeners {
_ = lis.Close() err := lis.Close()
if err != nil {
retErr = err
} }
} }
return retErr
}
func (l *Listener) Config() string { func (l *Listener) Config() string {
return l.config return l.config
} }
func (l *Listener) AddrList() (addrList []net.Addr) {
for _, lis := range l.listeners {
addrList = append(addrList, lis.Addr())
}
for _, lis := range l.udpListeners {
addrList = append(addrList, lis.LocalAddr())
}
return
}
func (l *Listener) HandleConn(conn net.Conn, in chan<- C.ConnContext) { func (l *Listener) HandleConn(conn net.Conn, in chan<- C.ConnContext) {
err := l.service.NewConnection(context.TODO(), conn, metadata.Metadata{ err := l.service.NewConnection(context.TODO(), conn, metadata.Metadata{
Protocol: "shadowsocks", Protocol: "shadowsocks",

View file

@ -272,9 +272,9 @@ func parseRange(uidRanges []ranges.Range[uint32], rangeList []string) ([]ranges.
return uidRanges, nil return uidRanges, nil
} }
func (l *Listener) Close() { func (l *Listener) Close() error {
l.closed = true l.closed = true
_ = common.Close( return common.Close(
l.tunStack, l.tunStack,
l.tunIf, l.tunIf,
l.defaultInterfaceMonitor, l.defaultInterfaceMonitor,

View file

@ -80,18 +80,33 @@ func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter
return sl, nil return sl, nil
} }
func (l *Listener) Close() { func (l *Listener) Close() error {
l.closed = true l.closed = true
var retErr error
for _, lis := range l.listeners { for _, lis := range l.listeners {
_ = lis.Close() err := lis.Close()
if err != nil {
retErr = err
} }
_ = l.service.Close() }
err := l.service.Close()
if err != nil {
retErr = err
}
return retErr
} }
func (l *Listener) Config() string { func (l *Listener) Config() string {
return l.config return l.config
} }
func (l *Listener) AddrList() (addrList []net.Addr) {
for _, lis := range l.listeners {
addrList = append(addrList, lis.Addr())
}
return
}
func (l *Listener) HandleConn(conn net.Conn, in chan<- C.ConnContext) { func (l *Listener) HandleConn(conn net.Conn, in chan<- C.ConnContext) {
err := l.service.NewConnection(context.TODO(), conn, metadata.Metadata{ err := l.service.NewConnection(context.TODO(), conn, metadata.Metadata{
Protocol: "vmess", Protocol: "vmess",

View file

@ -16,8 +16,6 @@ type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
specialRules string
name string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -36,11 +34,13 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext, additions ...inbound.Addition) (*Listener, error) {
return NewWithInfos(addr, "DEFAULT-SOCKS", "", in) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-SOCKS",
SpecialRules: "",
}}
} }
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
l, err := inbound.Listen("tcp", addr) l, err := inbound.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -49,8 +49,6 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
sl := &Listener{ sl := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name,
specialRules: specialRules,
} }
go func() { go func() {
for { for {
@ -61,14 +59,14 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
} }
continue continue
} }
go handleSocks(sl.name, sl.specialRules, c, in) go handleSocks(c, in, additions...)
} }
}() }()
return sl, nil return sl, nil
} }
func handleSocks(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) { func handleSocks(conn net.Conn, in chan<- C.ConnContext, additions ...inbound.Addition) {
conn.(*net.TCPConn).SetKeepAlive(true) conn.(*net.TCPConn).SetKeepAlive(true)
bufConn := N.NewBufferedConn(conn) bufConn := N.NewBufferedConn(conn)
head, err := bufConn.Peek(1) head, err := bufConn.Peek(1)
@ -79,24 +77,24 @@ func handleSocks(name, specialRules string, conn net.Conn, in chan<- C.ConnConte
switch head[0] { switch head[0] {
case socks4.Version: case socks4.Version:
HandleSocks4(name, specialRules, bufConn, in) HandleSocks4(bufConn, in, additions...)
case socks5.Version: case socks5.Version:
HandleSocks5(name, specialRules, bufConn, in) HandleSocks5(bufConn, in, additions...)
default: default:
conn.Close() conn.Close()
} }
} }
func HandleSocks4(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) { func HandleSocks4(conn net.Conn, in chan<- C.ConnContext, additions ...inbound.Addition) {
addr, _, err := socks4.ServerHandshake(conn, authStore.Authenticator()) addr, _, err := socks4.ServerHandshake(conn, authStore.Authenticator())
if err != nil { if err != nil {
conn.Close() conn.Close()
return return
} }
in <- inbound.NewSocketWithInfos(socks5.ParseAddr(addr), conn, C.SOCKS4, name, specialRules) in <- inbound.NewSocket(socks5.ParseAddr(addr), conn, C.SOCKS4, additions...)
} }
func HandleSocks5(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) { func HandleSocks5(conn net.Conn, in chan<- C.ConnContext, additions ...inbound.Addition) {
target, command, err := socks5.ServerHandshake(conn, authStore.Authenticator()) target, command, err := socks5.ServerHandshake(conn, authStore.Authenticator())
if err != nil { if err != nil {
conn.Close() conn.Close()
@ -107,5 +105,5 @@ func HandleSocks5(name, specialRules string, conn net.Conn, in chan<- C.ConnCont
io.Copy(io.Discard, conn) io.Copy(io.Discard, conn)
return return
} }
in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, specialRules) in <- inbound.NewSocket(target, conn, C.SOCKS5, additions...)
} }

View file

@ -15,8 +15,6 @@ type UDPListener struct {
packetConn net.PacketConn packetConn net.PacketConn
addr string addr string
closed bool closed bool
name string
specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -35,11 +33,13 @@ func (l *UDPListener) Close() error {
return l.packetConn.Close() return l.packetConn.Close()
} }
func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) { func NewUDP(addr string, in chan<- C.PacketAdapter, additions ...inbound.Addition) (*UDPListener, error) {
return NewUDPWithInfos(addr, "DEFAULT-SOCKS", "", in) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-SOCKS",
SpecialRules: "",
}}
} }
func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr) l, err := net.ListenPacket("udp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -52,8 +52,6 @@ func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter)
sl := &UDPListener{ sl := &UDPListener{
packetConn: l, packetConn: l,
addr: addr, addr: addr,
specialRules: specialRules,
name: name,
} }
go func() { go func() {
for { for {
@ -66,14 +64,14 @@ func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter)
} }
continue continue
} }
handleSocksUDP(sl.name, sl.specialRules, l, in, buf[:n], remoteAddr) handleSocksUDP(l, in, buf[:n], remoteAddr, additions...)
} }
}() }()
return sl, nil return sl, nil
} }
func handleSocksUDP(name, specialRules string, 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, additions ...inbound.Addition) {
target, payload, err := socks5.DecodeUDPPacket(buf) target, payload, err := socks5.DecodeUDPPacket(buf)
if err != nil { if err != nil {
// Unresolved UDP packet, return buffer to the pool // Unresolved UDP packet, return buffer to the pool
@ -87,7 +85,7 @@ func handleSocksUDP(name, specialRules string, pc net.PacketConn, in chan<- C.Pa
bufRef: buf, bufRef: buf,
} }
select { select {
case in <- inbound.NewPacketWithInfos(target, packet, C.SOCKS5, name, specialRules): case in <- inbound.NewPacket(target, packet, C.SOCKS5, additions...):
default: default:
} }
} }

View file

@ -12,8 +12,6 @@ type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
name string
specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -32,17 +30,19 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func (l *Listener) handleTProxy(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) { func (l *Listener) handleTProxy(conn net.Conn, in chan<- C.ConnContext, additions ...inbound.Addition) {
target := socks5.ParseAddrToSocksAddr(conn.LocalAddr()) target := socks5.ParseAddrToSocksAddr(conn.LocalAddr())
conn.(*net.TCPConn).SetKeepAlive(true) conn.(*net.TCPConn).SetKeepAlive(true)
in <- inbound.NewSocketWithInfos(target, conn, C.TPROXY, name, specialRules) in <- inbound.NewSocket(target, conn, C.TPROXY, additions...)
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { func New(addr string, in chan<- C.ConnContext, additions ...inbound.Addition) (*Listener, error) {
return NewWithInfos(addr, "DEFAULT-TPROXY", "", in) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-TPROXY",
SpecialRules: "",
}}
} }
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -62,8 +62,6 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
rl := &Listener{ rl := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name,
specialRules: specialRules,
} }
go func() { go func() {
@ -75,7 +73,7 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
} }
continue continue
} }
go rl.handleTProxy(rl.name, rl.specialRules, c, in) go rl.handleTProxy(c, in, additions...)
} }
}() }()

View file

@ -14,8 +14,6 @@ type UDPListener struct {
packetConn net.PacketConn packetConn net.PacketConn
addr string addr string
closed bool closed bool
name string
specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -34,11 +32,13 @@ func (l *UDPListener) Close() error {
return l.packetConn.Close() return l.packetConn.Close()
} }
func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) { func NewUDP(addr string, in chan<- C.PacketAdapter, additions ...inbound.Addition) (*UDPListener, error) {
return NewUDPWithInfos(addr, "DEFAULT-TPROXY", "", in) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-TPROXY",
SpecialRules: "",
}}
} }
func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr) l, err := net.ListenPacket("udp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -83,14 +83,14 @@ func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter)
// try to unmap 4in6 address // try to unmap 4in6 address
lAddr = netip.AddrPortFrom(lAddr.Addr().Unmap(), lAddr.Port()) lAddr = netip.AddrPortFrom(lAddr.Addr().Unmap(), lAddr.Port())
} }
handlePacketConn(rl.name, rl.specialRules, l, in, buf[:n], lAddr, rAddr) handlePacketConn(l, in, buf[:n], lAddr, rAddr, additions...)
} }
}() }()
return rl, nil return rl, nil
} }
func handlePacketConn(name, specialRules string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) { func handlePacketConn(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort, additions ...inbound.Addition) {
target := socks5.AddrFromStdAddrPort(rAddr) target := socks5.AddrFromStdAddrPort(rAddr)
pkt := &packet{ pkt := &packet{
pc: pc, pc: pc,
@ -98,7 +98,7 @@ func handlePacketConn(name, specialRules string, pc net.PacketConn, in chan<- C.
buf: buf, buf: buf,
} }
select { select {
case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, specialRules): case in <- inbound.NewPacket(target, pkt, C.TPROXY, additions...):
default: default:
} }
} }

View file

@ -25,11 +25,13 @@ type Listener struct {
servers []*tuic.Server servers []*tuic.Server
} }
func New(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (*Listener, error) { func New(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, additions ...inbound.Addition) (*Listener, error) {
return NewWithInfos("DEFAULT-TUIC", "", config, tcpIn, udpIn) if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-TUIC",
SpecialRules: "",
}}
} }
func NewWithInfos(name, specialRules string, config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (*Listener, error) {
cert, err := CN.ParseCert(config.Certificate, config.PrivateKey) cert, err := CN.ParseCert(config.Certificate, config.PrivateKey)
if err != nil { if err != nil {
return nil, err return nil, err
@ -61,12 +63,12 @@ func NewWithInfos(name, specialRules string, config LC.TuicServer, tcpIn chan<-
option := &tuic.ServerOption{ option := &tuic.ServerOption{
HandleTcpFn: func(conn net.Conn, addr socks5.Addr) error { HandleTcpFn: func(conn net.Conn, addr socks5.Addr) error {
tcpIn <- inbound.NewSocketWithInfos(addr, conn, C.TUIC, name, specialRules) tcpIn <- inbound.NewSocket(addr, conn, C.TUIC, additions...)
return nil return nil
}, },
HandleUdpFn: func(addr socks5.Addr, packet C.UDPPacket) error { HandleUdpFn: func(addr socks5.Addr, packet C.UDPPacket) error {
select { select {
case udpIn <- inbound.NewPacketWithInfos(addr, packet, C.TUIC, name, specialRules): case udpIn <- inbound.NewPacket(addr, packet, C.TUIC, additions...):
default: default:
} }
return nil return nil
@ -116,7 +118,6 @@ func NewWithInfos(name, specialRules string, config LC.TuicServer, tcpIn chan<-
return sl, nil return sl, nil
} }
// Close implements C.Listener
func (l *Listener) Close() error { func (l *Listener) Close() error {
l.closed = true l.closed = true
var retErr error var retErr error