fix: correct C.Metadata
This commit is contained in:
parent
62226e8b3d
commit
62474e0ed6
23 changed files with 161 additions and 161 deletions
|
@ -13,12 +13,12 @@ func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn) *context.ConnCo
|
|||
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.NetWork = C.TCP
|
||||
metadata.Type = C.HTTP
|
||||
metadata.InName = inName
|
||||
metadata.PreferRulesName = preferRulesName
|
||||
metadata.SpecialRules = specialRules
|
||||
if ip, port, err := parseAddr(source.String()); err == nil {
|
||||
metadata.SrcIP = ip
|
||||
metadata.SrcPort = port
|
||||
|
|
|
@ -13,10 +13,10 @@ func NewHTTPS(request *http.Request, conn net.Conn) *context.ConnContext {
|
|||
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.Type = C.HTTPS
|
||||
metadata.PreferRulesName = preferRulesName
|
||||
metadata.SpecialRules = specialRules
|
||||
metadata.InName = inName
|
||||
if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil {
|
||||
metadata.SrcIP = ip
|
||||
|
|
|
@ -16,12 +16,12 @@ func (s *PacketAdapter) Metadata() *C.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.NetWork = C.UDP
|
||||
metadata.Type = source
|
||||
metadata.InName = inName
|
||||
metadata.PreferRulesName = preferRulesName
|
||||
metadata.SpecialRules = specialRules
|
||||
if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil {
|
||||
metadata.SrcIP = ip
|
||||
metadata.SrcPort = port
|
||||
|
|
|
@ -9,11 +9,11 @@ import (
|
|||
"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.NetWork = C.TCP
|
||||
metadata.Type = source
|
||||
metadata.PreferRulesName = preferRulesName
|
||||
metadata.SpecialRules = specialRules
|
||||
metadata.InName = inName
|
||||
remoteAddr := conn.RemoteAddr()
|
||||
|
||||
|
|
|
@ -117,23 +117,23 @@ func (t Type) MarshalJSON() ([]byte, error) {
|
|||
|
||||
// Metadata is used to store connection address
|
||||
type Metadata struct {
|
||||
NetWork NetWork `json:"network"`
|
||||
Type Type `json:"type"`
|
||||
SrcIP netip.Addr `json:"sourceIP"`
|
||||
DstIP netip.Addr `json:"destinationIP"`
|
||||
SrcPort string `json:"sourcePort"`
|
||||
DstPort string `json:"destinationPort"`
|
||||
InIP netip.Addr `json:"inboundIP"`
|
||||
InPort string `json:"inboundPort"`
|
||||
Host string `json:"host"`
|
||||
DNSMode DNSMode `json:"dnsMode"`
|
||||
Uid *uint32 `json:"uid"`
|
||||
Process string `json:"process"`
|
||||
ProcessPath string `json:"processPath"`
|
||||
SpecialProxy string `json:"specialProxy"`
|
||||
RemoteDst string `json:"remoteDestination"`
|
||||
InName string `jsson:"-"`
|
||||
PreferRulesName string
|
||||
NetWork NetWork `json:"network"`
|
||||
Type Type `json:"type"`
|
||||
SrcIP netip.Addr `json:"sourceIP"`
|
||||
DstIP netip.Addr `json:"destinationIP"`
|
||||
SrcPort string `json:"sourcePort"`
|
||||
DstPort string `json:"destinationPort"`
|
||||
InIP netip.Addr `json:"inboundIP"`
|
||||
InPort string `json:"inboundPort"`
|
||||
Host string `json:"host"`
|
||||
DNSMode DNSMode `json:"dnsMode"`
|
||||
Uid *uint32 `json:"uid"`
|
||||
Process string `json:"process"`
|
||||
ProcessPath string `json:"processPath"`
|
||||
SpecialProxy string `json:"specialProxy"`
|
||||
RemoteDst string `json:"remoteDestination"`
|
||||
InName string `json:"inboundName"`
|
||||
SpecialRules string `json:"specialRules"`
|
||||
}
|
||||
|
||||
func (m *Metadata) RemoteAddress() string {
|
||||
|
|
|
@ -11,12 +11,12 @@ import (
|
|||
)
|
||||
|
||||
type Listener struct {
|
||||
listener net.Listener
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
preferRulesName string
|
||||
lookupFunc func(netip.AddrPort) (socks5.Addr, error)
|
||||
listener net.Listener
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
specialRules string
|
||||
lookupFunc func(netip.AddrPort) (socks5.Addr, error)
|
||||
}
|
||||
|
||||
// RawAddress implements C.Listener
|
||||
|
@ -58,23 +58,23 @@ func (l *Listener) handleRedir(conn net.Conn, in chan<- C.ConnContext) {
|
|||
|
||||
_ = 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) {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rl := &Listener{
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name:name,
|
||||
preferRulesName: preferRulesName,
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
specialRules: specialRules,
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
"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{
|
||||
Transport: &http.Transport{
|
||||
// from http.DefaultTransport
|
||||
|
@ -32,7 +32,7 @@ func newClient(source net.Addr,name,preferRulesName string, in chan<- C.ConnCont
|
|||
|
||||
left, right := net.Pipe()
|
||||
|
||||
in <- inbound.NewHTTPWithInfos(dstAddr, source, right,name,preferRulesName)
|
||||
in <- inbound.NewHTTPWithInfos(dstAddr, source, right, name, specialRules)
|
||||
|
||||
return left, nil
|
||||
},
|
||||
|
|
|
@ -14,8 +14,8 @@ import (
|
|||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
func HandleConn(name, preferRulesName string, c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) {
|
||||
client := newClient(c.RemoteAddr(), name, preferRulesName, in)
|
||||
func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) {
|
||||
client := newClient(c.RemoteAddr(), name, specialRules, in)
|
||||
defer client.CloseIdleConnections()
|
||||
|
||||
conn := N.NewBufferedConn(c)
|
||||
|
@ -48,7 +48,7 @@ func HandleConn(name, preferRulesName string, c net.Conn, in chan<- C.ConnContex
|
|||
break // close connection
|
||||
}
|
||||
|
||||
in <- inbound.NewHTTPSWithInfos(request, conn, name, preferRulesName)
|
||||
in <- inbound.NewHTTPSWithInfos(request, conn, name, specialRules)
|
||||
|
||||
return // hijack connection
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ func HandleConn(name, preferRulesName string, c net.Conn, in chan<- C.ConnContex
|
|||
request.RequestURI = ""
|
||||
|
||||
if isUpgradeRequest(request) {
|
||||
handleUpgrade(name, preferRulesName, conn, request, in)
|
||||
handleUpgrade(name, specialRules, conn, request, in)
|
||||
|
||||
return // hijack connection
|
||||
}
|
||||
|
|
|
@ -9,11 +9,11 @@ import (
|
|||
)
|
||||
|
||||
type Listener struct {
|
||||
listener net.Listener
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
preferRulesName string
|
||||
listener net.Listener
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
specialRules string
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) {
|
||||
return NewWithAuthenticate(addr, name, preferRulesName, in, true)
|
||||
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
|
@ -53,10 +53,10 @@ func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnCon
|
|||
}
|
||||
|
||||
hl := &Listener{
|
||||
listener: l,
|
||||
name: name,
|
||||
preferRulesName: preferRulesName,
|
||||
addr: addr,
|
||||
listener: l,
|
||||
name: name,
|
||||
specialRules: specialRules,
|
||||
addr: addr,
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
|
@ -67,7 +67,7 @@ func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnCon
|
|||
}
|
||||
continue
|
||||
}
|
||||
go HandleConn(hl.name, hl.preferRulesName, conn, in, c)
|
||||
go HandleConn(hl.name, hl.specialRules, conn, in, c)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ func isUpgradeRequest(req *http.Request) bool {
|
|||
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()
|
||||
|
||||
removeProxyHeaders(request.Header)
|
||||
|
@ -43,7 +43,7 @@ func handleUpgrade(name,preferRulesName string,conn net.Conn, request *http.Requ
|
|||
|
||||
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
|
||||
if request.TLS != nil {
|
||||
|
|
|
@ -10,11 +10,11 @@ import (
|
|||
)
|
||||
|
||||
type Base struct {
|
||||
config *BaseOption
|
||||
name string
|
||||
preferRulesName string
|
||||
listenAddr netip.Addr
|
||||
port int
|
||||
config *BaseOption
|
||||
name string
|
||||
specialRules string
|
||||
listenAddr netip.Addr
|
||||
port int
|
||||
}
|
||||
|
||||
func NewBase(options *BaseOption) (*Base, error) {
|
||||
|
@ -26,11 +26,11 @@ func NewBase(options *BaseOption) (*Base, error) {
|
|||
return nil, err
|
||||
}
|
||||
return &Base{
|
||||
name: options.Name(),
|
||||
listenAddr: addr,
|
||||
preferRulesName: options.PreferRulesName,
|
||||
port: options.Port,
|
||||
config: options,
|
||||
name: options.Name(),
|
||||
listenAddr: addr,
|
||||
specialRules: options.PreferRulesName,
|
||||
port: options.Port,
|
||||
config: options,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ func (h *HTTP) Address() string {
|
|||
// Listen implements constant.InboundListener
|
||||
func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) 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 {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -52,12 +52,12 @@ func (m *Mixed) Address() string {
|
|||
// Listen implements constant.InboundListener
|
||||
func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) 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 {
|
||||
return err
|
||||
}
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ func (r *Redir) Address() string {
|
|||
// Listen implements constant.InboundListener
|
||||
func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) 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 {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -70,11 +70,11 @@ func (s *Socks) Address() string {
|
|||
// Listen implements constant.InboundListener
|
||||
func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) 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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,13 +51,13 @@ func (t *TProxy) Address() string {
|
|||
// Listen implements constant.InboundListener
|
||||
func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) 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 {
|
||||
return err
|
||||
}
|
||||
if t.udp {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -14,12 +14,12 @@ import (
|
|||
)
|
||||
|
||||
type Listener struct {
|
||||
listener net.Listener
|
||||
addr string
|
||||
name string
|
||||
preferRulesName string
|
||||
cache *cache.LruCache[string, bool]
|
||||
closed bool
|
||||
listener net.Listener
|
||||
addr string
|
||||
name string
|
||||
specialRules string
|
||||
cache *cache.LruCache[string, bool]
|
||||
closed bool
|
||||
}
|
||||
|
||||
// RawAddress implements C.Listener
|
||||
|
@ -42,18 +42,18 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
|
|||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ml := &Listener{
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
preferRulesName: preferRulesName,
|
||||
cache: cache.New[string, bool](cache.WithAge[string, bool](30)),
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
specialRules: specialRules,
|
||||
cache: cache.New[string, bool](cache.WithAge[string, bool](30)),
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
|
@ -64,14 +64,14 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
|
|||
}
|
||||
continue
|
||||
}
|
||||
go handleConn(ml.name, ml.preferRulesName, c, in, ml.cache)
|
||||
go handleConn(ml.name, ml.specialRules, c, in, ml.cache)
|
||||
}
|
||||
}()
|
||||
|
||||
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)
|
||||
|
||||
bufConn := N.NewBufferedConn(conn)
|
||||
|
@ -82,10 +82,10 @@ func handleConn(name, preferRulesName string, conn net.Conn, in chan<- C.ConnCon
|
|||
|
||||
switch head[0] {
|
||||
case socks4.Version:
|
||||
socks.HandleSocks4(name, preferRulesName, bufConn, in)
|
||||
socks.HandleSocks4(name, specialRules, bufConn, in)
|
||||
case socks5.Version:
|
||||
socks.HandleSocks5(name, preferRulesName, bufConn, in)
|
||||
socks.HandleSocks5(name, specialRules, bufConn, in)
|
||||
default:
|
||||
http.HandleConn(name, preferRulesName, bufConn, in, cache)
|
||||
http.HandleConn(name, specialRules, bufConn, in, cache)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,11 +8,11 @@ import (
|
|||
)
|
||||
|
||||
type Listener struct {
|
||||
listener net.Listener
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
preferRulesName string
|
||||
listener net.Listener
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
specialRules string
|
||||
}
|
||||
|
||||
// RawAddress implements C.Listener
|
||||
|
@ -35,16 +35,16 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
|
|||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rl := &Listener{
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
preferRulesName: preferRulesName,
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
specialRules: specialRules,
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
@ -56,18 +56,18 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
|
|||
}
|
||||
continue
|
||||
}
|
||||
go handleRedir(rl.name, rl.preferRulesName, c, in)
|
||||
go handleRedir(rl.name, rl.specialRules, c, in)
|
||||
}
|
||||
}()
|
||||
|
||||
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)
|
||||
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, specialRules)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
specialRules string
|
||||
name string
|
||||
}
|
||||
|
||||
// RawAddress implements C.Listener
|
||||
|
@ -40,17 +40,17 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
|
|||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sl := &Listener{
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
preferRulesName: preferRulesName,
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
specialRules: specialRules,
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
|
@ -61,14 +61,14 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
|
|||
}
|
||||
continue
|
||||
}
|
||||
go handleSocks(sl.name, sl.preferRulesName, c, in)
|
||||
go handleSocks(sl.name, sl.specialRules, c, in)
|
||||
}
|
||||
}()
|
||||
|
||||
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)
|
||||
bufConn := N.NewBufferedConn(conn)
|
||||
head, err := bufConn.Peek(1)
|
||||
|
@ -79,24 +79,24 @@ func handleSocks(name, preferRulesName string, conn net.Conn, in chan<- C.ConnCo
|
|||
|
||||
switch head[0] {
|
||||
case socks4.Version:
|
||||
HandleSocks4(name, preferRulesName, bufConn, in)
|
||||
HandleSocks4(name, specialRules, bufConn, in)
|
||||
case socks5.Version:
|
||||
HandleSocks5(name, preferRulesName, bufConn, in)
|
||||
HandleSocks5(name, specialRules, bufConn, in)
|
||||
default:
|
||||
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())
|
||||
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, 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())
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
|
@ -107,5 +107,5 @@ func HandleSocks5(name, preferRulesName string, conn net.Conn, in chan<- C.ConnC
|
|||
io.Copy(io.Discard, conn)
|
||||
return
|
||||
}
|
||||
in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, preferRulesName)
|
||||
in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, specialRules)
|
||||
}
|
||||
|
|
|
@ -12,11 +12,11 @@ import (
|
|||
)
|
||||
|
||||
type UDPListener struct {
|
||||
packetConn net.PacketConn
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
preferRulesName string
|
||||
packetConn net.PacketConn
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
specialRules string
|
||||
}
|
||||
|
||||
// RawAddress implements C.Listener
|
||||
|
@ -39,7 +39,7 @@ 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, specialRules 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,
|
||||
preferRulesName: preferRulesName,
|
||||
name: name,
|
||||
packetConn: l,
|
||||
addr: addr,
|
||||
specialRules: specialRules,
|
||||
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.specialRules, 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, specialRules 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, specialRules):
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,11 +9,11 @@ import (
|
|||
)
|
||||
|
||||
type Listener struct {
|
||||
listener net.Listener
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
preferRulesName string
|
||||
listener net.Listener
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
specialRules string
|
||||
}
|
||||
|
||||
// RawAddress implements C.Listener
|
||||
|
@ -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, specialRules 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, specialRules)
|
||||
}
|
||||
|
||||
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -60,10 +60,10 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
|
|||
}
|
||||
|
||||
rl := &Listener{
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
preferRulesName: preferRulesName,
|
||||
listener: l,
|
||||
addr: addr,
|
||||
name: name,
|
||||
specialRules: specialRules,
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
@ -75,7 +75,7 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (
|
|||
}
|
||||
continue
|
||||
}
|
||||
go rl.handleTProxy(rl.name, rl.preferRulesName, c, in)
|
||||
go rl.handleTProxy(rl.name, rl.specialRules, c, in)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -11,11 +11,11 @@ import (
|
|||
)
|
||||
|
||||
type UDPListener struct {
|
||||
packetConn net.PacketConn
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
preferRulesName string
|
||||
packetConn net.PacketConn
|
||||
addr string
|
||||
closed bool
|
||||
name string
|
||||
specialRules string
|
||||
}
|
||||
|
||||
// RawAddress implements C.Listener
|
||||
|
@ -38,7 +38,7 @@ 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, specialRules string, in chan<- C.PacketAdapter) (*UDPListener, error) {
|
||||
l, err := net.ListenPacket("udp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -83,14 +83,14 @@ func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapt
|
|||
// try to unmap 4in6 address
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
pkt := &packet{
|
||||
pc: pc,
|
||||
|
@ -98,7 +98,7 @@ func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<-
|
|||
buf: buf,
|
||||
}
|
||||
select {
|
||||
case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, preferRulesName):
|
||||
case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, specialRules):
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
|
|
@ -508,7 +508,7 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
|||
}
|
||||
|
||||
func getRules(metadata *C.Metadata) []C.Rule {
|
||||
if sr, ok := subRules[metadata.PreferRulesName]; ok {
|
||||
if sr, ok := subRules[metadata.SpecialRules]; ok {
|
||||
return sr
|
||||
} else {
|
||||
return rules
|
||||
|
|
Loading…
Reference in a new issue