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

@ -12,7 +12,7 @@ import (
"github.com/Dreamacro/clash/transport/socks5"
)
func newClient(source net.Addr, name, specialRules string, in chan<- C.ConnContext) *http.Client {
func newClient(source net.Addr, in chan<- C.ConnContext, additions ...inbound.Addition) *http.Client {
return &http.Client{
Transport: &http.Transport{
// from http.DefaultTransport
@ -32,7 +32,7 @@ func newClient(source net.Addr, name, specialRules string, in chan<- C.ConnConte
left, right := net.Pipe()
in <- inbound.NewHTTPWithInfos(dstAddr, source, right, name, specialRules)
in <- inbound.NewHTTP(dstAddr, source, right, additions...)
return left, nil
},

View File

@ -14,8 +14,8 @@ import (
"github.com/Dreamacro/clash/log"
)
func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) {
client := newClient(c.RemoteAddr(), name, specialRules, in)
func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool], additions ...inbound.Addition) {
client := newClient(c.RemoteAddr(), in, additions...)
defer client.CloseIdleConnections()
conn := N.NewBufferedConn(c)
@ -48,7 +48,7 @@ func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext,
break // close connection
}
in <- inbound.NewHTTPSWithInfos(request, conn, name, specialRules)
in <- inbound.NewHTTPS(request, conn, additions...)
return // hijack connection
}
@ -61,7 +61,7 @@ func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext,
request.RequestURI = ""
if isUpgradeRequest(request) {
handleUpgrade(name, specialRules, conn, request, in)
handleUpgrade(conn, request, in, additions...)
return // hijack connection
}

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,15 +30,17 @@ func (l *Listener) Close() error {
return l.listener.Close()
}
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithAuthenticate(addr, "DEFAULT-HTTP", "", in, true)
func New(addr string, in chan<- C.ConnContext, additions ...inbound.Addition) (*Listener, error) {
return NewWithAuthenticate(addr, in, true, additions...)
}
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
return NewWithAuthenticate(addr, name, specialRules, in, true)
}
func NewWithAuthenticate(addr, name, specialRules string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) {
func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool, additions ...inbound.Addition) (*Listener, error) {
if len(additions) == 0 {
additions = []inbound.Addition{{
InName: "DEFAULT-HTTP",
SpecialRules: "",
}}
}
l, err := inbound.Listen("tcp", addr)
if err != nil {
@ -53,10 +53,8 @@ func NewWithAuthenticate(addr, name, specialRules string, in chan<- C.ConnContex
}
hl := &Listener{
listener: l,
name: name,
specialRules: specialRules,
addr: addr,
listener: l,
addr: addr,
}
go func() {
for {
@ -67,7 +65,7 @@ func NewWithAuthenticate(addr, name, specialRules string, in chan<- C.ConnContex
}
continue
}
go HandleConn(hl.name, hl.specialRules, conn, in, c)
go HandleConn(conn, in, c, additions...)
}
}()

View File

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