mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 21:51:23 +08:00
74 lines
1.3 KiB
Go
74 lines
1.3 KiB
Go
package redir
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
|
N "github.com/Dreamacro/clash/common/net"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
)
|
|
|
|
type Listener struct {
|
|
listener net.Listener
|
|
addr string
|
|
closed bool
|
|
}
|
|
|
|
// RawAddress implements C.Listener
|
|
func (l *Listener) RawAddress() string {
|
|
return l.addr
|
|
}
|
|
|
|
// Address implements C.Listener
|
|
func (l *Listener) Address() string {
|
|
return l.listener.Addr().String()
|
|
}
|
|
|
|
// Close implements C.Listener
|
|
func (l *Listener) Close() error {
|
|
l.closed = true
|
|
return l.listener.Close()
|
|
}
|
|
|
|
func New(addr string, tunnel C.Tunnel, additions ...inbound.Addition) (*Listener, error) {
|
|
if len(additions) == 0 {
|
|
additions = []inbound.Addition{
|
|
inbound.WithInName("DEFAULT-REDIR"),
|
|
inbound.WithSpecialRules(""),
|
|
}
|
|
}
|
|
l, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rl := &Listener{
|
|
listener: l,
|
|
addr: addr,
|
|
}
|
|
|
|
go func() {
|
|
for {
|
|
c, err := l.Accept()
|
|
if err != nil {
|
|
if rl.closed {
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
go handleRedir(c, tunnel, additions...)
|
|
}
|
|
}()
|
|
|
|
return rl, nil
|
|
}
|
|
|
|
func handleRedir(conn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) {
|
|
target, err := parserPacket(conn)
|
|
if err != nil {
|
|
conn.Close()
|
|
return
|
|
}
|
|
N.TCPKeepAlive(conn)
|
|
tunnel.HandleTCPConn(inbound.NewSocket(target, conn, C.REDIR, additions...))
|
|
}
|