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

@ -14,12 +14,10 @@ import (
)
type Listener struct {
listener net.Listener
addr string
name string
specialRules string
cache *cache.LruCache[string, bool]
closed bool
listener net.Listener
addr string
cache *cache.LruCache[string, bool]
closed bool
}
// RawAddress implements C.Listener
@ -38,22 +36,22 @@ func (l *Listener) Close() error {
return l.listener.Close()
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithInfos(addr, "DEFAULT-MIXED", "", 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-MIXED",
SpecialRules: "",
}}
}
l, err := inbound.Listen("tcp", addr)
if err != nil {
return nil, err
}
ml := &Listener{
listener: l,
addr: addr,
name: name,
specialRules: specialRules,
cache: cache.New[string, bool](cache.WithAge[string, bool](30)),
listener: l,
addr: addr,
cache: cache.New[string, bool](cache.WithAge[string, bool](30)),
}
go func() {
for {
@ -64,14 +62,14 @@ func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Li
}
continue
}
go handleConn(ml.name, ml.specialRules, c, in, ml.cache)
go handleConn(c, in, ml.cache, additions...)
}
}()
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)
bufConn := N.NewBufferedConn(conn)
@ -82,10 +80,10 @@ func handleConn(name, specialRules string, conn net.Conn, in chan<- C.ConnContex
switch head[0] {
case socks4.Version:
socks.HandleSocks4(name, specialRules, bufConn, in)
socks.HandleSocks4(bufConn, in, additions...)
case socks5.Version:
socks.HandleSocks5(name, specialRules, bufConn, in)
socks.HandleSocks5(bufConn, in, additions...)
default:
http.HandleConn(name, specialRules, bufConn, in, cache)
http.HandleConn(bufConn, in, cache, additions...)
}
}