2022-11-11 20:56:08 +08:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2022-11-18 22:57:33 +08:00
|
|
|
"fmt"
|
2022-11-11 20:56:08 +08:00
|
|
|
"net"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/inbound"
|
|
|
|
"github.com/metacubex/mihomo/common/pool"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/transport/socks5"
|
2022-11-11 20:56:08 +08:00
|
|
|
)
|
|
|
|
|
2022-11-18 22:57:33 +08:00
|
|
|
type PacketConn struct {
|
|
|
|
conn net.PacketConn
|
|
|
|
addr string
|
|
|
|
target socks5.Addr
|
|
|
|
proxy string
|
|
|
|
closed bool
|
2022-11-11 20:56:08 +08:00
|
|
|
}
|
|
|
|
|
2022-11-18 22:57:33 +08:00
|
|
|
// RawAddress implements C.Listener
|
|
|
|
func (l *PacketConn) RawAddress() string {
|
|
|
|
return l.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address implements C.Listener
|
|
|
|
func (l *PacketConn) Address() string {
|
|
|
|
return l.conn.LocalAddr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements C.Listener
|
|
|
|
func (l *PacketConn) Close() error {
|
|
|
|
l.closed = true
|
|
|
|
return l.conn.Close()
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func NewUDP(addr, target, proxy string, tunnel C.Tunnel, additions ...inbound.Addition) (*PacketConn, error) {
|
2022-11-18 22:57:33 +08:00
|
|
|
l, err := net.ListenPacket("udp", addr)
|
2022-11-11 20:56:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-18 22:57:33 +08:00
|
|
|
targetAddr := socks5.ParseAddr(target)
|
|
|
|
if targetAddr == nil {
|
|
|
|
return nil, fmt.Errorf("invalid target address %s", target)
|
|
|
|
}
|
|
|
|
|
|
|
|
sl := &PacketConn{
|
|
|
|
conn: l,
|
|
|
|
target: targetAddr,
|
|
|
|
proxy: proxy,
|
|
|
|
addr: addr,
|
|
|
|
}
|
2023-10-11 22:54:19 +08:00
|
|
|
|
|
|
|
if proxy != "" {
|
|
|
|
additions = append([]inbound.Addition{inbound.WithSpecialProxy(proxy)}, additions...)
|
|
|
|
}
|
|
|
|
|
2022-11-18 22:57:33 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
buf := pool.Get(pool.UDPBufferSize)
|
|
|
|
n, remoteAddr, err := l.ReadFrom(buf)
|
2022-11-11 20:56:08 +08:00
|
|
|
if err != nil {
|
2022-11-18 22:57:33 +08:00
|
|
|
pool.Put(buf)
|
|
|
|
if sl.closed {
|
|
|
|
break
|
2022-11-11 20:56:08 +08:00
|
|
|
}
|
2022-11-18 22:57:33 +08:00
|
|
|
continue
|
2022-11-11 20:56:08 +08:00
|
|
|
}
|
2023-09-28 18:59:31 +08:00
|
|
|
sl.handleUDP(l, tunnel, buf[:n], remoteAddr, additions...)
|
2022-11-18 22:57:33 +08:00
|
|
|
}
|
|
|
|
}()
|
2022-11-11 20:56:08 +08:00
|
|
|
|
2022-11-18 22:57:33 +08:00
|
|
|
return sl, nil
|
2022-11-11 20:56:08 +08:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func (l *PacketConn) handleUDP(pc net.PacketConn, tunnel C.Tunnel, buf []byte, addr net.Addr, additions ...inbound.Addition) {
|
2023-10-11 10:55:12 +08:00
|
|
|
cPacket := &packet{
|
2022-11-18 22:57:33 +08:00
|
|
|
pc: pc,
|
|
|
|
rAddr: addr,
|
|
|
|
payload: buf,
|
2022-11-11 20:56:08 +08:00
|
|
|
}
|
|
|
|
|
2023-10-11 22:54:19 +08:00
|
|
|
tunnel.HandleUDPPacket(inbound.NewPacket(l.target, cPacket, C.TUNNEL, additions...))
|
2022-11-11 20:56:08 +08:00
|
|
|
}
|