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

@ -9,11 +9,9 @@ import (
)
type Listener struct {
listener net.Listener
addr string
closed bool
name string
specialRules string
listener net.Listener
addr string
closed bool
}
// RawAddress implements C.Listener
@ -32,17 +30,19 @@ func (l *Listener) Close() error {
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())
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) {
return NewWithInfos(addr, "DEFAULT-TPROXY", "", in)
}
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
func New(addr string, in chan<- C.ConnContext, additions ...inbound.Addition) (*Listener, error) {
if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-TPROXY",
SpecialRules: "",
}}
}
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
@ -60,10 +60,8 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
}
rl := &Listener{
listener: l,
addr: addr,
name: name,
specialRules: specialRules,
listener: l,
addr: addr,
}
go func() {
@ -75,7 +73,7 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
}
continue
}
go rl.handleTProxy(rl.name, rl.specialRules, c, in)
go rl.handleTProxy(c, in, additions...)
}
}()

View File

@ -11,11 +11,9 @@ import (
)
type UDPListener struct {
packetConn net.PacketConn
addr string
closed bool
name string
specialRules string
packetConn net.PacketConn
addr string
closed bool
}
// RawAddress implements C.Listener
@ -34,11 +32,13 @@ func (l *UDPListener) Close() error {
return l.packetConn.Close()
}
func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) {
return NewUDPWithInfos(addr, "DEFAULT-TPROXY", "", in)
}
func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter) (*UDPListener, error) {
func NewUDP(addr string, in chan<- C.PacketAdapter, additions ...inbound.Addition) (*UDPListener, error) {
if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-TPROXY",
SpecialRules: "",
}}
}
l, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
@ -83,14 +83,14 @@ func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter)
// try to unmap 4in6 address
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
}
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)
pkt := &packet{
pc: pc,
@ -98,7 +98,7 @@ func handlePacketConn(name, specialRules string, pc net.PacketConn, in chan<- C.
buf: buf,
}
select {
case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, specialRules):
case in <- inbound.NewPacket(target, pkt, C.TPROXY, additions...):
default:
}
}