2019-07-25 09:47:39 +00:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2019-07-25 09:47:39 +00:00
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
2020-04-11 13:45:56 +00:00
|
|
|
"github.com/Dreamacro/clash/common/sockopt"
|
2019-07-25 09:47:39 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2020-06-15 02:34:15 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2019-07-25 09:47:39 +00:00
|
|
|
)
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
type UDPListener struct {
|
2021-06-13 15:05:22 +00:00
|
|
|
packetConn net.PacketConn
|
|
|
|
closed bool
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) {
|
2019-07-25 09:47:39 +00:00
|
|
|
l, err := net.ListenPacket("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
if err := sockopt.UDPReuseaddr(l.(*net.UDPConn)); err != nil {
|
2020-06-15 02:34:15 +00:00
|
|
|
log.Warnln("Failed to Reuse UDP Address: %s", err)
|
2020-04-11 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 06:07:51 +00:00
|
|
|
sl := &UDPListener{
|
|
|
|
packetConn: l,
|
|
|
|
}
|
2019-07-25 09:47:39 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2020-04-24 16:30:40 +00:00
|
|
|
buf := pool.Get(pool.RelayBufferSize)
|
2019-07-25 09:47:39 +00:00
|
|
|
n, remoteAddr, err := l.ReadFrom(buf)
|
|
|
|
if err != nil {
|
2020-04-24 16:30:40 +00:00
|
|
|
pool.Put(buf)
|
2019-07-25 09:47:39 +00:00
|
|
|
if sl.closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2021-06-13 09:23:10 +00:00
|
|
|
handleSocksUDP(l, in, buf[:n], remoteAddr)
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return sl, nil
|
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
func (l *UDPListener) Close() error {
|
2019-07-25 09:47:39 +00:00
|
|
|
l.closed = true
|
2021-06-13 15:05:22 +00:00
|
|
|
return l.packetConn.Close()
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
func (l *UDPListener) Address() string {
|
2021-07-19 06:07:51 +00:00
|
|
|
return l.packetConn.LocalAddr().String()
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
func handleSocksUDP(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, addr net.Addr) {
|
2019-10-11 12:11:18 +00:00
|
|
|
target, payload, err := socks5.DecodeUDPPacket(buf)
|
2019-07-25 09:47:39 +00:00
|
|
|
if err != nil {
|
2019-10-11 12:11:18 +00:00
|
|
|
// Unresolved UDP packet, return buffer to the pool
|
2020-04-24 16:30:40 +00:00
|
|
|
pool.Put(buf)
|
2019-07-25 09:47:39 +00:00
|
|
|
return
|
|
|
|
}
|
2020-04-16 10:19:36 +00:00
|
|
|
packet := &packet{
|
|
|
|
pc: pc,
|
|
|
|
rAddr: addr,
|
|
|
|
payload: payload,
|
|
|
|
bufRef: buf,
|
2019-10-11 12:11:18 +00:00
|
|
|
}
|
2021-06-13 09:23:10 +00:00
|
|
|
select {
|
2021-07-21 15:08:52 +00:00
|
|
|
case in <- inbound.NewPacket(target, packet, C.SOCKS):
|
2021-06-13 09:23:10 +00:00
|
|
|
default:
|
|
|
|
}
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|