2021-06-13 09:23:10 +00:00
|
|
|
package tproxy
|
2020-03-08 13:58:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2020-03-08 13:58:49 +00:00
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2020-03-08 13:58:49 +00:00
|
|
|
)
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
type UDPListener struct {
|
2021-06-13 15:05:22 +00:00
|
|
|
packetConn net.PacketConn
|
2021-07-31 16:35:37 +00:00
|
|
|
addr string
|
2021-06-13 15:05:22 +00:00
|
|
|
closed bool
|
2020-03-08 13:58:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 16:35:37 +00: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()
|
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) {
|
2020-03-08 13:58:49 +00:00
|
|
|
l, err := net.ListenPacket("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-19 06:07:51 +00:00
|
|
|
rl := &UDPListener{
|
|
|
|
packetConn: l,
|
2021-07-31 16:35:37 +00:00
|
|
|
addr: addr,
|
2021-07-19 06:07:51 +00:00
|
|
|
}
|
2020-03-08 13:58:49 +00:00
|
|
|
|
|
|
|
c := l.(*net.UDPConn)
|
|
|
|
|
2020-11-09 02:46:10 +00:00
|
|
|
rc, err := c.SyscallConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setsockopt(rc, addr)
|
2020-03-08 13:58:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
oob := make([]byte, 1024)
|
|
|
|
for {
|
2021-11-03 14:26:51 +00:00
|
|
|
buf := pool.Get(pool.UDPBufferSize)
|
2020-03-10 12:36:24 +00:00
|
|
|
n, oobn, _, lAddr, err := c.ReadMsgUDP(buf, oob)
|
2020-03-08 13:58:49 +00:00
|
|
|
if err != nil {
|
2020-04-24 16:30:40 +00:00
|
|
|
pool.Put(buf)
|
2020-03-08 13:58:49 +00:00
|
|
|
if rl.closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:36:24 +00:00
|
|
|
rAddr, err := getOrigDst(oob, oobn)
|
2020-03-08 13:58:49 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-13 09:23:10 +00:00
|
|
|
handlePacketConn(l, in, buf[:n], lAddr, rAddr)
|
2020-03-08 13:58:49 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rl, nil
|
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
func handlePacketConn(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {
|
2020-03-10 12:36:24 +00:00
|
|
|
target := socks5.ParseAddrToSocksAddr(rAddr)
|
2020-04-16 10:19:36 +00:00
|
|
|
pkt := &packet{
|
|
|
|
lAddr: lAddr,
|
|
|
|
buf: buf,
|
2020-03-08 13:58:49 +00:00
|
|
|
}
|
2021-06-13 09:23:10 +00:00
|
|
|
select {
|
|
|
|
case in <- inbound.NewPacket(target, pkt, C.TPROXY):
|
|
|
|
default:
|
|
|
|
}
|
2020-03-08 13:58:49 +00:00
|
|
|
}
|