mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-12-25 11:54:25 +08:00
84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
package shadowsocks
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
|
N "github.com/Dreamacro/clash/common/net"
|
|
"github.com/Dreamacro/clash/common/sockopt"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
"github.com/Dreamacro/clash/log"
|
|
"github.com/Dreamacro/clash/transport/shadowsocks/core"
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
|
)
|
|
|
|
type UDPListener struct {
|
|
packetConn net.PacketConn
|
|
closed bool
|
|
}
|
|
|
|
func NewUDP(addr string, pickCipher core.Cipher, in chan<- C.PacketAdapter) (*UDPListener, error) {
|
|
l, err := net.ListenPacket("udp", addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = sockopt.UDPReuseaddr(l.(*net.UDPConn))
|
|
if err != nil {
|
|
log.Warnln("Failed to Reuse UDP Address: %s", err)
|
|
}
|
|
|
|
sl := &UDPListener{l, false}
|
|
conn := pickCipher.PacketConn(N.NewEnhancePacketConn(l))
|
|
go func() {
|
|
for {
|
|
data, put, remoteAddr, err := conn.WaitReadFrom()
|
|
if err != nil {
|
|
if put != nil {
|
|
put()
|
|
}
|
|
if sl.closed {
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
handleSocksUDP(conn, in, data, put, remoteAddr)
|
|
}
|
|
}()
|
|
|
|
return sl, nil
|
|
}
|
|
|
|
func (l *UDPListener) Close() error {
|
|
l.closed = true
|
|
return l.packetConn.Close()
|
|
}
|
|
|
|
func (l *UDPListener) LocalAddr() net.Addr {
|
|
return l.packetConn.LocalAddr()
|
|
}
|
|
|
|
func handleSocksUDP(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, put func(), addr net.Addr, additions ...inbound.Addition) {
|
|
tgtAddr := socks5.SplitAddr(buf)
|
|
if tgtAddr == nil {
|
|
// Unresolved UDP packet, return buffer to the pool
|
|
if put != nil {
|
|
put()
|
|
}
|
|
return
|
|
}
|
|
target := socks5.ParseAddr(tgtAddr.String())
|
|
payload := buf[len(tgtAddr):]
|
|
|
|
packet := &packet{
|
|
pc: pc,
|
|
rAddr: addr,
|
|
payload: payload,
|
|
put: put,
|
|
}
|
|
select {
|
|
case in <- inbound.NewPacket(target, packet, C.SHADOWSOCKS, additions...):
|
|
default:
|
|
}
|
|
}
|