2021-06-13 17:23:10 +08:00
|
|
|
package tproxy
|
2020-03-08 21:58:49 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2022-06-12 19:37:27 +08:00
|
|
|
"net/netip"
|
2020-03-08 21:58:49 +08:00
|
|
|
|
2021-06-10 14:05:56 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2020-03-08 21:58:49 +08:00
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 22:18:49 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2020-03-08 21:58:49 +08:00
|
|
|
)
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
type UDPListener struct {
|
2022-12-04 13:37:14 +08:00
|
|
|
packetConn net.PacketConn
|
|
|
|
addr string
|
|
|
|
closed bool
|
|
|
|
name string
|
|
|
|
preferRulesName string
|
2020-03-08 21:58:49 +08:00
|
|
|
}
|
|
|
|
|
2021-08-01 00:35:37 +08:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) {
|
2022-12-04 13:37:14 +08:00
|
|
|
return NewUDPWithInfos(addr, "DEFAULT-TPROXY", "", in)
|
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapter) (*UDPListener, error) {
|
2020-03-08 21:58:49 +08:00
|
|
|
l, err := net.ListenPacket("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:07:51 +08:00
|
|
|
rl := &UDPListener{
|
|
|
|
packetConn: l,
|
2021-08-01 00:35:37 +08:00
|
|
|
addr: addr,
|
2021-07-19 14:07:51 +08:00
|
|
|
}
|
2020-03-08 21:58:49 +08:00
|
|
|
|
|
|
|
c := l.(*net.UDPConn)
|
|
|
|
|
2020-11-09 10:46:10 +08:00
|
|
|
rc, err := c.SyscallConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setsockopt(rc, addr)
|
2020-03-08 21:58:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
oob := make([]byte, 1024)
|
|
|
|
for {
|
2021-11-03 22:26:51 +08:00
|
|
|
buf := pool.Get(pool.UDPBufferSize)
|
2022-06-12 19:37:27 +08:00
|
|
|
n, oobn, _, lAddr, err := c.ReadMsgUDPAddrPort(buf, oob)
|
2020-03-08 21:58:49 +08:00
|
|
|
if err != nil {
|
2020-04-25 00:30:40 +08:00
|
|
|
pool.Put(buf)
|
2020-03-08 21:58:49 +08:00
|
|
|
if rl.closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-12 19:37:27 +08:00
|
|
|
rAddr, err := getOrigDst(oob[:oobn])
|
2020-03-08 21:58:49 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-29 23:36:24 +08:00
|
|
|
|
|
|
|
if rAddr.Addr().Is4() {
|
|
|
|
// try to unmap 4in6 address
|
|
|
|
lAddr = netip.AddrPortFrom(lAddr.Addr().Unmap(), lAddr.Port())
|
|
|
|
}
|
2022-12-04 13:37:14 +08:00
|
|
|
handlePacketConn(rl.name, rl.preferRulesName, l, in, buf[:n], lAddr, rAddr)
|
2020-03-08 21:58:49 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rl, nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) {
|
2022-06-12 19:37:27 +08:00
|
|
|
target := socks5.AddrFromStdAddrPort(rAddr)
|
2020-04-16 18:19:36 +08:00
|
|
|
pkt := &packet{
|
2022-11-11 23:36:06 +08:00
|
|
|
pc: pc,
|
2020-04-16 18:19:36 +08:00
|
|
|
lAddr: lAddr,
|
|
|
|
buf: buf,
|
2020-03-08 21:58:49 +08:00
|
|
|
}
|
2021-06-13 17:23:10 +08:00
|
|
|
select {
|
2022-12-04 13:37:14 +08:00
|
|
|
case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, preferRulesName):
|
2021-06-13 17:23:10 +08:00
|
|
|
default:
|
|
|
|
}
|
2020-03-08 21:58:49 +08:00
|
|
|
}
|