2019-07-25 17:47:39 +08:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/inbound"
|
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
"github.com/metacubex/mihomo/common/sockopt"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
|
|
|
"github.com/metacubex/mihomo/transport/socks5"
|
2019-07-25 17:47:39 +08:00
|
|
|
)
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
type UDPListener struct {
|
2022-12-05 00:20:50 +08:00
|
|
|
packetConn net.PacketConn
|
|
|
|
addr string
|
|
|
|
closed bool
|
2019-07-25 17:47:39 +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()
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func NewUDP(addr string, tunnel C.Tunnel, additions ...inbound.Addition) (*UDPListener, error) {
|
2022-12-05 00:20:50 +08:00
|
|
|
if len(additions) == 0 {
|
2022-12-05 10:12:53 +08:00
|
|
|
additions = []inbound.Addition{
|
|
|
|
inbound.WithInName("DEFAULT-SOCKS"),
|
|
|
|
inbound.WithSpecialRules(""),
|
|
|
|
}
|
2022-12-05 00:20:50 +08:00
|
|
|
}
|
2019-07-25 17:47:39 +08:00
|
|
|
l, err := net.ListenPacket("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
if err := sockopt.UDPReuseaddr(l.(*net.UDPConn)); err != nil {
|
2020-06-15 10:34:15 +08:00
|
|
|
log.Warnln("Failed to Reuse UDP Address: %s", err)
|
2020-04-11 21:45:56 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 14:07:51 +08:00
|
|
|
sl := &UDPListener{
|
2022-12-05 00:20:50 +08:00
|
|
|
packetConn: l,
|
|
|
|
addr: addr,
|
2021-07-19 14:07:51 +08:00
|
|
|
}
|
2023-06-03 21:40:09 +08:00
|
|
|
conn := N.NewEnhancePacketConn(l)
|
2019-07-25 17:47:39 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
2023-06-03 21:40:09 +08:00
|
|
|
data, put, remoteAddr, err := conn.WaitReadFrom()
|
2019-07-25 17:47:39 +08:00
|
|
|
if err != nil {
|
2023-06-03 21:40:09 +08:00
|
|
|
if put != nil {
|
|
|
|
put()
|
|
|
|
}
|
2019-07-25 17:47:39 +08:00
|
|
|
if sl.closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2023-09-28 18:59:31 +08:00
|
|
|
handleSocksUDP(l, tunnel, data, put, remoteAddr, additions...)
|
2019-07-25 17:47:39 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return sl, nil
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func handleSocksUDP(pc net.PacketConn, tunnel C.Tunnel, buf []byte, put func(), addr net.Addr, additions ...inbound.Addition) {
|
2019-10-11 20:11:18 +08:00
|
|
|
target, payload, err := socks5.DecodeUDPPacket(buf)
|
2019-07-25 17:47:39 +08:00
|
|
|
if err != nil {
|
2019-10-11 20:11:18 +08:00
|
|
|
// Unresolved UDP packet, return buffer to the pool
|
2023-06-03 21:40:09 +08:00
|
|
|
if put != nil {
|
|
|
|
put()
|
|
|
|
}
|
2019-07-25 17:47:39 +08:00
|
|
|
return
|
|
|
|
}
|
2020-04-16 18:19:36 +08:00
|
|
|
packet := &packet{
|
|
|
|
pc: pc,
|
|
|
|
rAddr: addr,
|
|
|
|
payload: payload,
|
2023-06-03 21:40:09 +08:00
|
|
|
put: put,
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
2023-09-28 18:59:31 +08:00
|
|
|
tunnel.HandleUDPPacket(inbound.NewPacket(target, packet, C.SOCKS5, additions...))
|
2019-07-25 17:47:39 +08:00
|
|
|
}
|