fix: correct C.Metadata

This commit is contained in:
wwqgtxx 2022-12-04 22:08:20 +08:00
parent 62226e8b3d
commit 62474e0ed6
23 changed files with 161 additions and 161 deletions

View File

@ -13,12 +13,12 @@ func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn) *context.ConnCo
return NewHTTPWithInfos(target, source, conn, "", "") return NewHTTPWithInfos(target, source, conn, "", "")
} }
func NewHTTPWithInfos(target socks5.Addr, source net.Addr, conn net.Conn, inName, preferRulesName string) *context.ConnContext { 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 metadata.InName = inName
metadata.PreferRulesName = preferRulesName metadata.SpecialRules = specialRules
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

@ -13,10 +13,10 @@ func NewHTTPS(request *http.Request, conn net.Conn) *context.ConnContext {
return NewHTTPSWithInfos(request, conn, "", "") return NewHTTPSWithInfos(request, conn, "", "")
} }
func NewHTTPSWithInfos(request *http.Request, conn net.Conn, inName, preferRulesName string) *context.ConnContext { 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.PreferRulesName = preferRulesName metadata.SpecialRules = specialRules
metadata.InName = inName metadata.InName = inName
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

View File

@ -16,12 +16,12 @@ func (s *PacketAdapter) Metadata() *C.Metadata {
return s.metadata return s.metadata
} }
func NewPacketWithInfos(target socks5.Addr, packet C.UDPPacket, source C.Type, inName, preferRulesName string) C.PacketAdapter { func NewPacketWithInfos(target socks5.Addr, packet C.UDPPacket, source C.Type, inName, specialRules string) 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 metadata.InName = inName
metadata.PreferRulesName = preferRulesName metadata.SpecialRules = specialRules
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

View File

@ -9,11 +9,11 @@ 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 , preferRulesName string) *context.ConnContext { func NewSocketWithInfos(target socks5.Addr, conn net.Conn, source C.Type, inName, specialRules string) *context.ConnContext {
metadata := parseSocksAddr(target) metadata := parseSocksAddr(target)
metadata.NetWork = C.TCP metadata.NetWork = C.TCP
metadata.Type = source metadata.Type = source
metadata.PreferRulesName = preferRulesName metadata.SpecialRules = specialRules
metadata.InName = inName metadata.InName = inName
remoteAddr := conn.RemoteAddr() remoteAddr := conn.RemoteAddr()

View File

@ -117,23 +117,23 @@ func (t Type) MarshalJSON() ([]byte, error) {
// Metadata is used to store connection address // Metadata is used to store connection address
type Metadata struct { type Metadata struct {
NetWork NetWork `json:"network"` NetWork NetWork `json:"network"`
Type Type `json:"type"` Type Type `json:"type"`
SrcIP netip.Addr `json:"sourceIP"` SrcIP netip.Addr `json:"sourceIP"`
DstIP netip.Addr `json:"destinationIP"` DstIP netip.Addr `json:"destinationIP"`
SrcPort string `json:"sourcePort"` SrcPort string `json:"sourcePort"`
DstPort string `json:"destinationPort"` DstPort string `json:"destinationPort"`
InIP netip.Addr `json:"inboundIP"` InIP netip.Addr `json:"inboundIP"`
InPort string `json:"inboundPort"` InPort string `json:"inboundPort"`
Host string `json:"host"` Host string `json:"host"`
DNSMode DNSMode `json:"dnsMode"` DNSMode DNSMode `json:"dnsMode"`
Uid *uint32 `json:"uid"` Uid *uint32 `json:"uid"`
Process string `json:"process"` Process string `json:"process"`
ProcessPath string `json:"processPath"` ProcessPath string `json:"processPath"`
SpecialProxy string `json:"specialProxy"` SpecialProxy string `json:"specialProxy"`
RemoteDst string `json:"remoteDestination"` RemoteDst string `json:"remoteDestination"`
InName string `jsson:"-"` InName string `json:"inboundName"`
PreferRulesName string SpecialRules string `json:"specialRules"`
} }
func (m *Metadata) RemoteAddress() string { func (m *Metadata) RemoteAddress() string {

View File

@ -11,12 +11,12 @@ import (
) )
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
name string name string
preferRulesName string specialRules string
lookupFunc func(netip.AddrPort) (socks5.Addr, error) lookupFunc func(netip.AddrPort) (socks5.Addr, error)
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -58,23 +58,23 @@ 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.preferRulesName) in <- inbound.NewSocketWithInfos(target, conn, C.REDIR, l.name, l.specialRules)
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, 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, 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
} }
rl := &Listener{ rl := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name:name, name: name,
preferRulesName: preferRulesName, 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,preferRulesName string, in chan<- C.ConnContext) *http.Client { func newClient(source net.Addr, name, specialRules string, in chan<- C.ConnContext) *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,preferRulesName string, in chan<- C.ConnCont
left, right := net.Pipe() left, right := net.Pipe()
in <- inbound.NewHTTPWithInfos(dstAddr, source, right,name,preferRulesName) in <- inbound.NewHTTPWithInfos(dstAddr, source, right, name, specialRules)
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, preferRulesName string, c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) { func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) {
client := newClient(c.RemoteAddr(), name, preferRulesName, in) client := newClient(c.RemoteAddr(), name, specialRules, in)
defer client.CloseIdleConnections() defer client.CloseIdleConnections()
conn := N.NewBufferedConn(c) conn := N.NewBufferedConn(c)
@ -48,7 +48,7 @@ func HandleConn(name, preferRulesName string, c net.Conn, in chan<- C.ConnContex
break // close connection break // close connection
} }
in <- inbound.NewHTTPSWithInfos(request, conn, name, preferRulesName) in <- inbound.NewHTTPSWithInfos(request, conn, name, specialRules)
return // hijack connection return // hijack connection
} }
@ -61,7 +61,7 @@ func HandleConn(name, preferRulesName string, c net.Conn, in chan<- C.ConnContex
request.RequestURI = "" request.RequestURI = ""
if isUpgradeRequest(request) { if isUpgradeRequest(request) {
handleUpgrade(name, preferRulesName, conn, request, in) handleUpgrade(name, specialRules, conn, request, in)
return // hijack connection return // hijack connection
} }

View File

@ -9,11 +9,11 @@ import (
) )
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
name string name string
preferRulesName string specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -36,11 +36,11 @@ 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) { func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithAuthenticate(addr, name, preferRulesName, in, true) return NewWithAuthenticate(addr, name, specialRules, in, true)
} }
func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) { 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 {
@ -53,10 +53,10 @@ func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnCon
} }
hl := &Listener{ hl := &Listener{
listener: l, listener: l,
name: name, name: name,
preferRulesName: preferRulesName, specialRules: specialRules,
addr: addr, addr: addr,
} }
go func() { go func() {
for { for {
@ -67,7 +67,7 @@ func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnCon
} }
continue continue
} }
go HandleConn(hl.name, hl.preferRulesName, conn, in, c) go HandleConn(hl.name, hl.specialRules, conn, in, c)
} }
}() }()

View File

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

View File

@ -10,11 +10,11 @@ import (
) )
type Base struct { type Base struct {
config *BaseOption config *BaseOption
name string name string
preferRulesName string specialRules string
listenAddr netip.Addr listenAddr netip.Addr
port int port int
} }
func NewBase(options *BaseOption) (*Base, error) { func NewBase(options *BaseOption) (*Base, error) {
@ -26,11 +26,11 @@ func NewBase(options *BaseOption) (*Base, error) {
return nil, err return nil, err
} }
return &Base{ return &Base{
name: options.Name(), name: options.Name(),
listenAddr: addr, listenAddr: addr,
preferRulesName: options.PreferRulesName, specialRules: options.PreferRulesName,
port: options.Port, port: options.Port,
config: options, config: options,
}, nil }, nil
} }

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.preferRulesName, tcpIn) h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.specialRules, tcpIn)
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.preferRulesName, tcpIn) m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.specialRules, tcpIn)
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.preferRulesName, udpIn) m.lUDP, err = socks.NewUDPWithInfos(m.Address(), m.name, m.specialRules, udpIn)
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.preferRulesName, tcpIn) r.l, err = redir.NewWithInfos(r.Address(), r.name, r.specialRules, tcpIn)
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.preferRulesName, tcpIn); err != nil { if s.stl, err = socks.NewWithInfos(s.RawAddress(), s.name, s.specialRules, tcpIn); err != nil {
return err return err
} }
if s.udp { if s.udp {
if s.sul, err = socks.NewUDPWithInfos(s.RawAddress(), s.name, s.preferRulesName, udpIn); err != nil { if s.sul, err = socks.NewUDPWithInfos(s.RawAddress(), s.name, s.specialRules, udpIn); 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.preferRulesName, tcpIn) t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.specialRules, tcpIn)
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.preferRulesName, udpIn) t.lUDP, err = tproxy.NewUDPWithInfos(t.Address(), t.name, t.specialRules, udpIn)
if err != nil { if err != nil {
return err return err
} }

View File

@ -14,12 +14,12 @@ import (
) )
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
name string name string
preferRulesName string specialRules string
cache *cache.LruCache[string, bool] cache *cache.LruCache[string, bool]
closed bool closed bool
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -42,18 +42,18 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithInfos(addr, "DEFAULT-MIXED", "", in) return NewWithInfos(addr, "DEFAULT-MIXED", "", in)
} }
func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { 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
} }
ml := &Listener{ ml := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name, name: name,
preferRulesName: preferRulesName, 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() {
for { for {
@ -64,14 +64,14 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
} }
continue continue
} }
go handleConn(ml.name, ml.preferRulesName, c, in, ml.cache) go handleConn(ml.name, ml.specialRules, c, in, ml.cache)
} }
}() }()
return ml, nil return ml, nil
} }
func handleConn(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) { func handleConn(name, specialRules string, conn net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) {
conn.(*net.TCPConn).SetKeepAlive(true) conn.(*net.TCPConn).SetKeepAlive(true)
bufConn := N.NewBufferedConn(conn) bufConn := N.NewBufferedConn(conn)
@ -82,10 +82,10 @@ func handleConn(name, preferRulesName string, conn net.Conn, in chan<- C.ConnCon
switch head[0] { switch head[0] {
case socks4.Version: case socks4.Version:
socks.HandleSocks4(name, preferRulesName, bufConn, in) socks.HandleSocks4(name, specialRules, bufConn, in)
case socks5.Version: case socks5.Version:
socks.HandleSocks5(name, preferRulesName, bufConn, in) socks.HandleSocks5(name, specialRules, bufConn, in)
default: default:
http.HandleConn(name, preferRulesName, bufConn, in, cache) http.HandleConn(name, specialRules, bufConn, in, cache)
} }
} }

View File

@ -8,11 +8,11 @@ import (
) )
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
name string name string
preferRulesName string specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -35,16 +35,16 @@ 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, 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
} }
rl := &Listener{ rl := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name, name: name,
preferRulesName: preferRulesName, specialRules: specialRules,
} }
go func() { go func() {
@ -56,18 +56,18 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
} }
continue continue
} }
go handleRedir(rl.name, rl.preferRulesName, c, in) go handleRedir(rl.name, rl.specialRules, c, in)
} }
}() }()
return rl, nil return rl, nil
} }
func handleRedir(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { func handleRedir(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) {
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, preferRulesName) in <- inbound.NewSocketWithInfos(target, conn, C.REDIR, name, specialRules)
} }

View File

@ -13,11 +13,11 @@ import (
) )
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
preferRulesName string specialRules string
name string name string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -40,17 +40,17 @@ 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, 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
} }
sl := &Listener{ sl := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name, name: name,
preferRulesName: preferRulesName, specialRules: specialRules,
} }
go func() { go func() {
for { for {
@ -61,14 +61,14 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
} }
continue continue
} }
go handleSocks(sl.name, sl.preferRulesName, c, in) go handleSocks(sl.name, sl.specialRules, c, in)
} }
}() }()
return sl, nil return sl, nil
} }
func handleSocks(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { func handleSocks(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) {
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 +79,24 @@ func handleSocks(name, preferRulesName string, conn net.Conn, in chan<- C.ConnCo
switch head[0] { switch head[0] {
case socks4.Version: case socks4.Version:
HandleSocks4(name, preferRulesName, bufConn, in) HandleSocks4(name, specialRules, bufConn, in)
case socks5.Version: case socks5.Version:
HandleSocks5(name, preferRulesName, bufConn, in) HandleSocks5(name, specialRules, bufConn, in)
default: default:
conn.Close() conn.Close()
} }
} }
func HandleSocks4(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { func HandleSocks4(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) {
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, preferRulesName) in <- inbound.NewSocketWithInfos(socks5.ParseAddr(addr), conn, C.SOCKS4, name, specialRules)
} }
func HandleSocks5(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { func HandleSocks5(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) {
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 +107,5 @@ func HandleSocks5(name, preferRulesName string, conn net.Conn, in chan<- C.ConnC
io.Copy(io.Discard, conn) io.Copy(io.Discard, conn)
return return
} }
in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, preferRulesName) in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, specialRules)
} }

View File

@ -12,11 +12,11 @@ import (
) )
type UDPListener struct { type UDPListener struct {
packetConn net.PacketConn packetConn net.PacketConn
addr string addr string
closed bool closed bool
name string name string
preferRulesName string specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -39,7 +39,7 @@ func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) {
return NewUDPWithInfos(addr, "DEFAULT-SOCKS", "", in) return NewUDPWithInfos(addr, "DEFAULT-SOCKS", "", in)
} }
func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapter) (*UDPListener, error) { 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
@ -50,10 +50,10 @@ func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapt
} }
sl := &UDPListener{ sl := &UDPListener{
packetConn: l, packetConn: l,
addr: addr, addr: addr,
preferRulesName: preferRulesName, specialRules: specialRules,
name: name, name: name,
} }
go func() { go func() {
for { for {
@ -66,14 +66,14 @@ func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapt
} }
continue continue
} }
handleSocksUDP(sl.name, sl.preferRulesName, l, in, buf[:n], remoteAddr) handleSocksUDP(sl.name, sl.specialRules, l, in, buf[:n], remoteAddr)
} }
}() }()
return sl, nil return sl, nil
} }
func handleSocksUDP(name, preferRulesName string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) { func handleSocksUDP(name, specialRules string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) {
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 +87,7 @@ func handleSocksUDP(name, preferRulesName string, pc net.PacketConn, in chan<- C
bufRef: buf, bufRef: buf,
} }
select { select {
case in <- inbound.NewPacketWithInfos(target, packet, C.SOCKS5, name, preferRulesName): case in <- inbound.NewPacketWithInfos(target, packet, C.SOCKS5, name, specialRules):
default: default:
} }
} }

View File

@ -9,11 +9,11 @@ import (
) )
type Listener struct { type Listener struct {
listener net.Listener listener net.Listener
addr string addr string
closed bool closed bool
name string name string
preferRulesName string specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -32,17 +32,17 @@ func (l *Listener) Close() error {
return l.listener.Close() return l.listener.Close()
} }
func (l *Listener) handleTProxy(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { func (l *Listener) handleTProxy(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) {
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, preferRulesName) in <- inbound.NewSocketWithInfos(target, conn, C.TPROXY, name, specialRules)
} }
func New(addr string, in chan<- C.ConnContext) (*Listener, error) { 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, 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
@ -60,10 +60,10 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
} }
rl := &Listener{ rl := &Listener{
listener: l, listener: l,
addr: addr, addr: addr,
name: name, name: name,
preferRulesName: preferRulesName, specialRules: specialRules,
} }
go func() { go func() {
@ -75,7 +75,7 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
} }
continue continue
} }
go rl.handleTProxy(rl.name, rl.preferRulesName, c, in) go rl.handleTProxy(rl.name, rl.specialRules, c, in)
} }
}() }()

View File

@ -11,11 +11,11 @@ import (
) )
type UDPListener struct { type UDPListener struct {
packetConn net.PacketConn packetConn net.PacketConn
addr string addr string
closed bool closed bool
name string name string
preferRulesName string specialRules string
} }
// RawAddress implements C.Listener // RawAddress implements C.Listener
@ -38,7 +38,7 @@ func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) {
return NewUDPWithInfos(addr, "DEFAULT-TPROXY", "", in) return NewUDPWithInfos(addr, "DEFAULT-TPROXY", "", in)
} }
func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapter) (*UDPListener, error) { 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, preferRulesName string, in chan<- C.PacketAdapt
// 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.preferRulesName, l, in, buf[:n], lAddr, rAddr) handlePacketConn(rl.name, rl.specialRules, l, in, buf[:n], lAddr, rAddr)
} }
}() }()
return rl, nil return rl, nil
} }
func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) { func handlePacketConn(name, specialRules string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) {
target := socks5.AddrFromStdAddrPort(rAddr) target := socks5.AddrFromStdAddrPort(rAddr)
pkt := &packet{ pkt := &packet{
pc: pc, pc: pc,
@ -98,7 +98,7 @@ func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<-
buf: buf, buf: buf,
} }
select { select {
case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, preferRulesName): case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, specialRules):
default: default:
} }
} }

View File

@ -508,7 +508,7 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
} }
func getRules(metadata *C.Metadata) []C.Rule { func getRules(metadata *C.Metadata) []C.Rule {
if sr, ok := subRules[metadata.PreferRulesName]; ok { if sr, ok := subRules[metadata.SpecialRules]; ok {
return sr return sr
} else { } else {
return rules return rules