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

@ -13,11 +13,9 @@ import (
)
type Listener struct {
listener net.Listener
addr string
closed bool
specialRules string
name string
listener net.Listener
addr string
closed bool
}
// RawAddress implements C.Listener
@ -36,21 +34,21 @@ func (l *Listener) Close() error {
return l.listener.Close()
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithInfos(addr, "DEFAULT-SOCKS", "", 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-SOCKS",
SpecialRules: "",
}}
}
l, err := inbound.Listen("tcp", addr)
if err != nil {
return nil, err
}
sl := &Listener{
listener: l,
addr: addr,
name: name,
specialRules: specialRules,
listener: l,
addr: addr,
}
go func() {
for {
@ -61,14 +59,14 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
}
continue
}
go handleSocks(sl.name, sl.specialRules, c, in)
go handleSocks(c, in, additions...)
}
}()
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)
bufConn := N.NewBufferedConn(conn)
head, err := bufConn.Peek(1)
@ -79,24 +77,24 @@ func handleSocks(name, specialRules string, conn net.Conn, in chan<- C.ConnConte
switch head[0] {
case socks4.Version:
HandleSocks4(name, specialRules, bufConn, in)
HandleSocks4(bufConn, in, additions...)
case socks5.Version:
HandleSocks5(name, specialRules, bufConn, in)
HandleSocks5(bufConn, in, additions...)
default:
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())
if err != nil {
conn.Close()
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())
if err != nil {
conn.Close()
@ -107,5 +105,5 @@ func HandleSocks5(name, specialRules string, conn net.Conn, in chan<- C.ConnCont
io.Copy(io.Discard, conn)
return
}
in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, specialRules)
in <- inbound.NewSocket(target, conn, C.SOCKS5, additions...)
}

View File

@ -12,11 +12,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
@ -35,11 +33,13 @@ func (l *UDPListener) Close() error {
return l.packetConn.Close()
}
func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) {
return NewUDPWithInfos(addr, "DEFAULT-SOCKS", "", in)
}
func 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-SOCKS",
SpecialRules: "",
}}
}
l, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
@ -50,10 +50,8 @@ func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter)
}
sl := &UDPListener{
packetConn: l,
addr: addr,
specialRules: specialRules,
name: name,
packetConn: l,
addr: addr,
}
go func() {
for {
@ -66,14 +64,14 @@ func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter)
}
continue
}
handleSocksUDP(sl.name, sl.specialRules, l, in, buf[:n], remoteAddr)
handleSocksUDP(l, in, buf[:n], remoteAddr, additions...)
}
}()
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)
if err != nil {
// 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,
}
select {
case in <- inbound.NewPacketWithInfos(target, packet, C.SOCKS5, name, specialRules):
case in <- inbound.NewPacket(target, packet, C.SOCKS5, additions...):
default:
}
}