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"
|
2023-06-03 13:40:09 +00:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
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 {
|
2022-12-04 16:20:50 +00:00
|
|
|
packetConn net.PacketConn
|
|
|
|
addr string
|
|
|
|
closed bool
|
2019-07-25 09:47:39 +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()
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:59:31 +00:00
|
|
|
func NewUDP(addr string, tunnel C.Tunnel, additions ...inbound.Addition) (*UDPListener, error) {
|
2022-12-04 16:20:50 +00:00
|
|
|
if len(additions) == 0 {
|
2022-12-05 02:12:53 +00:00
|
|
|
additions = []inbound.Addition{
|
|
|
|
inbound.WithInName("DEFAULT-SOCKS"),
|
|
|
|
inbound.WithSpecialRules(""),
|
|
|
|
}
|
2022-12-04 16:20:50 +00:00
|
|
|
}
|
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{
|
2022-12-04 16:20:50 +00:00
|
|
|
packetConn: l,
|
|
|
|
addr: addr,
|
2021-07-19 06:07:51 +00:00
|
|
|
}
|
2023-06-03 13:40:09 +00:00
|
|
|
conn := N.NewEnhancePacketConn(l)
|
2019-07-25 09:47:39 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2023-06-03 13:40:09 +00:00
|
|
|
data, put, remoteAddr, err := conn.WaitReadFrom()
|
2019-07-25 09:47:39 +00:00
|
|
|
if err != nil {
|
2023-06-03 13:40:09 +00:00
|
|
|
if put != nil {
|
|
|
|
put()
|
|
|
|
}
|
2019-07-25 09:47:39 +00:00
|
|
|
if sl.closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2023-09-28 10:59:31 +00:00
|
|
|
handleSocksUDP(l, tunnel, data, put, remoteAddr, additions...)
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return sl, nil
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:59:31 +00:00
|
|
|
func handleSocksUDP(pc net.PacketConn, tunnel C.Tunnel, buf []byte, put func(), addr net.Addr, additions ...inbound.Addition) {
|
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
|
2023-06-03 13:40:09 +00:00
|
|
|
if put != nil {
|
|
|
|
put()
|
|
|
|
}
|
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,
|
2023-06-03 13:40:09 +00:00
|
|
|
put: put,
|
2019-10-11 12:11:18 +00:00
|
|
|
}
|
2023-09-28 10:59:31 +00:00
|
|
|
tunnel.HandleUDPPacket(inbound.NewPacket(target, packet, C.SOCKS5, additions...))
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|