mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-12-27 12:35:39 +08:00
105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
package tproxy
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
|
)
|
|
|
|
type UDPListener struct {
|
|
packetConn net.PacketConn
|
|
addr string
|
|
closed bool
|
|
name string
|
|
specialRules string
|
|
}
|
|
|
|
// RawAddress implements C.Listener
|
|
func (l *UDPListener) RawAddress() string {
|
|
return l.addr
|
|
}
|
|
|
|
// Address implements C.Listener
|
|
func (l *UDPListener) Address() string {
|
|
return l.packetConn.LocalAddr().String()
|
|
}
|
|
|
|
// Close implements C.Listener
|
|
func (l *UDPListener) Close() error {
|
|
l.closed = true
|
|
return l.packetConn.Close()
|
|
}
|
|
|
|
func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) {
|
|
return NewUDPWithInfos(addr, "DEFAULT-TPROXY", "", in)
|
|
}
|
|
|
|
func NewUDPWithInfos(addr, name, specialRules string, in chan<- C.PacketAdapter) (*UDPListener, error) {
|
|
l, err := net.ListenPacket("udp", addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rl := &UDPListener{
|
|
packetConn: l,
|
|
addr: addr,
|
|
}
|
|
|
|
c := l.(*net.UDPConn)
|
|
|
|
rc, err := c.SyscallConn()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = setsockopt(rc, addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
go func() {
|
|
oob := make([]byte, 1024)
|
|
for {
|
|
buf := pool.Get(pool.UDPBufferSize)
|
|
n, oobn, _, lAddr, err := c.ReadMsgUDPAddrPort(buf, oob)
|
|
if err != nil {
|
|
pool.Put(buf)
|
|
if rl.closed {
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
|
|
rAddr, err := getOrigDst(oob[:oobn])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
if rAddr.Addr().Is4() {
|
|
// try to unmap 4in6 address
|
|
lAddr = netip.AddrPortFrom(lAddr.Addr().Unmap(), lAddr.Port())
|
|
}
|
|
handlePacketConn(rl.name, rl.specialRules, l, in, buf[:n], lAddr, rAddr)
|
|
}
|
|
}()
|
|
|
|
return rl, nil
|
|
}
|
|
|
|
func handlePacketConn(name, specialRules string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) {
|
|
target := socks5.AddrFromStdAddrPort(rAddr)
|
|
pkt := &packet{
|
|
pc: pc,
|
|
lAddr: lAddr,
|
|
buf: buf,
|
|
}
|
|
select {
|
|
case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, specialRules):
|
|
default:
|
|
}
|
|
}
|