mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-06 03:44:00 +00:00
73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
package redir
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
)
|
|
|
|
type Listener struct {
|
|
listener net.Listener
|
|
addr string
|
|
closed bool
|
|
name string
|
|
specialRules string
|
|
}
|
|
|
|
// 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, in chan<- C.ConnContext) (*Listener, error) {
|
|
return NewWithInfos(addr, "DEFAULT-REDIR", "", in)
|
|
}
|
|
|
|
func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) {
|
|
l, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rl := &Listener{
|
|
listener: l,
|
|
addr: addr,
|
|
name: name,
|
|
specialRules: specialRules,
|
|
}
|
|
|
|
go func() {
|
|
for {
|
|
c, err := l.Accept()
|
|
if err != nil {
|
|
if rl.closed {
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
go handleRedir(rl.name, rl.specialRules, c, in)
|
|
}
|
|
}()
|
|
|
|
return rl, nil
|
|
}
|
|
func handleRedir(name, specialRules string, conn net.Conn, in chan<- C.ConnContext) {
|
|
target, err := parserPacket(conn)
|
|
if err != nil {
|
|
conn.Close()
|
|
return
|
|
}
|
|
conn.(*net.TCPConn).SetKeepAlive(true)
|
|
in <- inbound.NewSocketWithInfos(target, conn, C.REDIR, name, specialRules)
|
|
}
|