chore: cleanup natTable's api
This commit is contained in:
parent
00f554fad3
commit
aa9da5dcb0
24 changed files with 47 additions and 85 deletions
|
@ -217,10 +217,6 @@ type UDPPacket interface {
|
||||||
|
|
||||||
// LocalAddr returns the source IP/Port of packet
|
// LocalAddr returns the source IP/Port of packet
|
||||||
LocalAddr() net.Addr
|
LocalAddr() net.Addr
|
||||||
|
|
||||||
SetNatTable(natTable NatTable)
|
|
||||||
|
|
||||||
SetUdpInChan(in chan<- PacketAdapter)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UDPPacketInAddr interface {
|
type UDPPacketInAddr interface {
|
||||||
|
|
|
@ -16,7 +16,7 @@ type MultiAddrListener interface {
|
||||||
|
|
||||||
type InboundListener interface {
|
type InboundListener interface {
|
||||||
Name() string
|
Name() string
|
||||||
Listen(tcpIn chan<- ConnContext, udpIn chan<- PacketAdapter) error
|
Listen(tcpIn chan<- ConnContext, udpIn chan<- PacketAdapter, natTable NatTable) error
|
||||||
Close() error
|
Close() error
|
||||||
Address() string
|
Address() string
|
||||||
RawAddress() string
|
RawAddress() string
|
||||||
|
|
|
@ -137,8 +137,9 @@ func GetGeneral() *config.General {
|
||||||
func updateListeners(listeners map[string]C.InboundListener) {
|
func updateListeners(listeners map[string]C.InboundListener) {
|
||||||
tcpIn := tunnel.TCPIn()
|
tcpIn := tunnel.TCPIn()
|
||||||
udpIn := tunnel.UDPIn()
|
udpIn := tunnel.UDPIn()
|
||||||
|
natTable := tunnel.NatTable()
|
||||||
|
|
||||||
listener.PatchInboundListeners(listeners, tcpIn, udpIn, true)
|
listener.PatchInboundListeners(listeners, tcpIn, udpIn, natTable, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateExperimental(c *config.Config) {
|
func updateExperimental(c *config.Config) {
|
||||||
|
@ -348,12 +349,13 @@ func updateGeneral(general *config.General, force bool) {
|
||||||
|
|
||||||
tcpIn := tunnel.TCPIn()
|
tcpIn := tunnel.TCPIn()
|
||||||
udpIn := tunnel.UDPIn()
|
udpIn := tunnel.UDPIn()
|
||||||
|
natTable := tunnel.NatTable()
|
||||||
|
|
||||||
listener.ReCreateHTTP(general.Port, tcpIn)
|
listener.ReCreateHTTP(general.Port, tcpIn)
|
||||||
listener.ReCreateSocks(general.SocksPort, tcpIn, udpIn)
|
listener.ReCreateSocks(general.SocksPort, tcpIn, udpIn)
|
||||||
listener.ReCreateRedir(general.RedirPort, tcpIn, udpIn)
|
listener.ReCreateRedir(general.RedirPort, tcpIn, udpIn, natTable)
|
||||||
listener.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn)
|
listener.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn)
|
||||||
listener.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn)
|
listener.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn, natTable)
|
||||||
listener.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
|
listener.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
|
||||||
listener.ReCreateShadowSocks(general.ShadowSocksConfig, tcpIn, udpIn)
|
listener.ReCreateShadowSocks(general.ShadowSocksConfig, tcpIn, udpIn)
|
||||||
listener.ReCreateVmess(general.VmessConfig, tcpIn, udpIn)
|
listener.ReCreateVmess(general.VmessConfig, tcpIn, udpIn)
|
||||||
|
|
|
@ -239,11 +239,12 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
tcpIn := tunnel.TCPIn()
|
tcpIn := tunnel.TCPIn()
|
||||||
udpIn := tunnel.UDPIn()
|
udpIn := tunnel.UDPIn()
|
||||||
|
natTable := tunnel.NatTable()
|
||||||
|
|
||||||
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port), tcpIn)
|
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port), tcpIn)
|
||||||
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort), tcpIn, udpIn)
|
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort), tcpIn, udpIn)
|
||||||
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tcpIn, udpIn)
|
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tcpIn, udpIn, natTable)
|
||||||
P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tcpIn, udpIn)
|
P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tcpIn, udpIn, natTable)
|
||||||
P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort), tcpIn, udpIn)
|
P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort), tcpIn, udpIn)
|
||||||
P.ReCreateTun(pointerOrDefaultTun(general.Tun, P.LastTunConf), tcpIn, udpIn)
|
P.ReCreateTun(pointerOrDefaultTun(general.Tun, P.LastTunConf), tcpIn, udpIn)
|
||||||
P.ReCreateShadowSocks(pointerOrDefaultString(general.ShadowSocksConfig, ports.ShadowSocksConfig), tcpIn, udpIn)
|
P.ReCreateShadowSocks(pointerOrDefaultString(general.ShadowSocksConfig, ports.ShadowSocksConfig), tcpIn, udpIn)
|
||||||
|
|
|
@ -61,7 +61,7 @@ func (b *Base) RawAddress() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ func (h *HTTP) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
h.l, err = http.New(h.RawAddress(), tcpIn, h.Additions()...)
|
h.l, err = http.New(h.RawAddress(), tcpIn, h.Additions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -50,7 +50,7 @@ func (m *Mixed) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
m.l, err = mixed.New(m.RawAddress(), tcpIn, m.Additions()...)
|
m.l, err = mixed.New(m.RawAddress(), tcpIn, m.Additions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -42,7 +42,7 @@ func (r *Redir) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
r.l, err = redir.New(r.RawAddress(), tcpIn, r.Additions()...)
|
r.l, err = redir.New(r.RawAddress(), tcpIn, r.Additions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -57,7 +57,7 @@ func (s *ShadowSocks) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (s *ShadowSocks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (s *ShadowSocks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
s.l, err = sing_shadowsocks.New(s.ss, tcpIn, udpIn, s.Additions()...)
|
s.l, err = sing_shadowsocks.New(s.ss, tcpIn, udpIn, s.Additions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -68,7 +68,7 @@ func (s *Socks) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
if s.stl, err = socks.New(s.RawAddress(), tcpIn, s.Additions()...); err != nil {
|
if s.stl, err = socks.New(s.RawAddress(), tcpIn, s.Additions()...); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -49,7 +49,7 @@ func (t *TProxy) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
t.lTCP, err = tproxy.New(t.RawAddress(), tcpIn, t.Additions()...)
|
t.lTCP, err = tproxy.New(t.RawAddress(), tcpIn, t.Additions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -57,7 +57,7 @@ func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter
|
||||||
}
|
}
|
||||||
if t.udp {
|
if t.udp {
|
||||||
if t.lUDP != nil {
|
if t.lUDP != nil {
|
||||||
t.lUDP, err = tproxy.NewUDP(t.RawAddress(), udpIn, t.Additions()...)
|
t.lUDP, err = tproxy.NewUDP(t.RawAddress(), udpIn, natTable, t.Additions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ func (t *Tuic) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
t.l, err = tuic.New(t.ts, tcpIn, udpIn, t.Additions()...)
|
t.l, err = tuic.New(t.ts, tcpIn, udpIn, t.Additions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -111,7 +111,7 @@ func (t *Tun) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (t *Tun) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (t *Tun) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
t.l, err = sing_tun.New(t.tun, tcpIn, udpIn, t.Additions()...)
|
t.l, err = sing_tun.New(t.tun, tcpIn, udpIn, t.Additions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -74,7 +74,7 @@ func (t *Tunnel) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (t *Tunnel) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (t *Tunnel) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
for _, network := range t.config.Network {
|
for _, network := range t.config.Network {
|
||||||
switch network {
|
switch network {
|
||||||
|
|
|
@ -69,7 +69,7 @@ func (v *Vmess) Address() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen implements constant.InboundListener
|
// Listen implements constant.InboundListener
|
||||||
func (v *Vmess) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
func (v *Vmess) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
|
||||||
var err error
|
var err error
|
||||||
users := make([]LC.VmessUser, len(v.config.Users))
|
users := make([]LC.VmessUser, len(v.config.Users))
|
||||||
for i, v := range v.config.Users {
|
for i, v := range v.config.Users {
|
||||||
|
|
|
@ -207,7 +207,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd
|
||||||
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
|
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) {
|
||||||
redirMux.Lock()
|
redirMux.Lock()
|
||||||
defer redirMux.Unlock()
|
defer redirMux.Unlock()
|
||||||
|
|
||||||
|
@ -245,7 +245,7 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
redirUDPListener, err = tproxy.NewUDP(addr, udpIn)
|
redirUDPListener, err = tproxy.NewUDP(addr, udpIn, natTable)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnln("Failed to start Redir UDP Listener: %s", err)
|
log.Warnln("Failed to start Redir UDP Listener: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -403,7 +403,7 @@ func ReCreateTuic(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<-
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) {
|
||||||
tproxyMux.Lock()
|
tproxyMux.Lock()
|
||||||
defer tproxyMux.Unlock()
|
defer tproxyMux.Unlock()
|
||||||
|
|
||||||
|
@ -441,7 +441,7 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketA
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn)
|
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn, natTable)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnln("Failed to start TProxy UDP Listener: %s", err)
|
log.Warnln("Failed to start TProxy UDP Listener: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -719,7 +719,7 @@ func PatchTunnel(tunnels []LC.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, dropOld bool) {
|
func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable, dropOld bool) {
|
||||||
inboundMux.Lock()
|
inboundMux.Lock()
|
||||||
defer inboundMux.Unlock()
|
defer inboundMux.Unlock()
|
||||||
|
|
||||||
|
@ -731,7 +731,7 @@ func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn ch
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := newListener.Listen(tcpIn, udpIn); err != nil {
|
if err := newListener.Listen(tcpIn, udpIn, natTable); err != nil {
|
||||||
log.Errorln("Listener %s listen err: %s", name, err.Error())
|
log.Errorln("Listener %s listen err: %s", name, err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,13 +44,6 @@ func (c *packet) InAddr() net.Addr {
|
||||||
return c.pc.LocalAddr()
|
return c.pc.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packet) SetNatTable(natTable C.NatTable) {
|
|
||||||
// no need
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) {
|
|
||||||
// no need
|
|
||||||
}
|
|
||||||
func ParseSSURL(s string) (addr, cipher, password string, err error) {
|
func ParseSSURL(s string) (addr, cipher, password string, err error) {
|
||||||
u, err := url.Parse(s)
|
u, err := url.Parse(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,11 +39,3 @@ func (c *packet) Drop() {
|
||||||
func (c *packet) InAddr() net.Addr {
|
func (c *packet) InAddr() net.Addr {
|
||||||
return c.pc.LocalAddr()
|
return c.pc.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packet) SetNatTable(natTable C.NatTable) {
|
|
||||||
// no need
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) {
|
|
||||||
// no need
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,8 +15,8 @@ type packet struct {
|
||||||
pc net.PacketConn
|
pc net.PacketConn
|
||||||
lAddr netip.AddrPort
|
lAddr netip.AddrPort
|
||||||
buf []byte
|
buf []byte
|
||||||
natTable C.NatTable
|
|
||||||
in chan<- C.PacketAdapter
|
in chan<- C.PacketAdapter
|
||||||
|
natTable C.NatTable
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packet) Data() []byte {
|
func (c *packet) Data() []byte {
|
||||||
|
@ -25,7 +25,7 @@ func (c *packet) Data() []byte {
|
||||||
|
|
||||||
// WriteBack opens a new socket binding `addr` to write UDP packet back
|
// WriteBack opens a new socket binding `addr` to write UDP packet back
|
||||||
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
||||||
tc, err := createOrGetLocalConn(addr, c.LocalAddr(), c.natTable, c.in)
|
tc, err := createOrGetLocalConn(addr, c.LocalAddr(), c.in, c.natTable)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
n = 0
|
n = 0
|
||||||
return
|
return
|
||||||
|
@ -47,18 +47,10 @@ func (c *packet) InAddr() net.Addr {
|
||||||
return c.pc.LocalAddr()
|
return c.pc.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packet) SetNatTable(natTable C.NatTable) {
|
|
||||||
c.natTable = natTable
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) {
|
|
||||||
c.in = in
|
|
||||||
}
|
|
||||||
|
|
||||||
// this function listen at rAddr and write to lAddr
|
// this function listen at rAddr and write to lAddr
|
||||||
// for here, rAddr is the ip/port client want to access
|
// for here, rAddr is the ip/port client want to access
|
||||||
// lAddr is the ip/port client opened
|
// lAddr is the ip/port client opened
|
||||||
func createOrGetLocalConn(rAddr, lAddr net.Addr, natTable C.NatTable, in chan<- C.PacketAdapter) (*net.UDPConn, error) {
|
func createOrGetLocalConn(rAddr, lAddr net.Addr, in chan<- C.PacketAdapter, natTable C.NatTable) (*net.UDPConn, error) {
|
||||||
remote := rAddr.String()
|
remote := rAddr.String()
|
||||||
local := lAddr.String()
|
local := lAddr.String()
|
||||||
localConn := natTable.GetLocalConn(local, remote)
|
localConn := natTable.GetLocalConn(local, remote)
|
||||||
|
@ -83,7 +75,7 @@ func createOrGetLocalConn(rAddr, lAddr net.Addr, natTable C.NatTable, in chan<-
|
||||||
natTable.DeleteLocalConnMap(local, lockKey)
|
natTable.DeleteLocalConnMap(local, lockKey)
|
||||||
cond.Broadcast()
|
cond.Broadcast()
|
||||||
}()
|
}()
|
||||||
conn, err := listenLocalConn(rAddr, lAddr, in)
|
conn, err := listenLocalConn(rAddr, lAddr, in, natTable)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln("listenLocalConn failed with error: %s, packet loss", err.Error())
|
log.Errorln("listenLocalConn failed with error: %s, packet loss", err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -97,7 +89,7 @@ func createOrGetLocalConn(rAddr, lAddr net.Addr, natTable C.NatTable, in chan<-
|
||||||
|
|
||||||
// this function listen at rAddr
|
// this function listen at rAddr
|
||||||
// and send what received to program itself, then send to real remote
|
// and send what received to program itself, then send to real remote
|
||||||
func listenLocalConn(rAddr, lAddr net.Addr, in chan<- C.PacketAdapter) (*net.UDPConn, error) {
|
func listenLocalConn(rAddr, lAddr net.Addr, in chan<- C.PacketAdapter, natTable C.NatTable) (*net.UDPConn, error) {
|
||||||
additions := []inbound.Addition{
|
additions := []inbound.Addition{
|
||||||
inbound.WithInName("DEFAULT-TPROXY"),
|
inbound.WithInName("DEFAULT-TPROXY"),
|
||||||
inbound.WithSpecialRules(""),
|
inbound.WithSpecialRules(""),
|
||||||
|
@ -120,7 +112,7 @@ func listenLocalConn(rAddr, lAddr net.Addr, in chan<- C.PacketAdapter) (*net.UDP
|
||||||
}
|
}
|
||||||
// since following localPackets are pass through this socket which listen rAddr
|
// since following localPackets are pass through this socket which listen rAddr
|
||||||
// I choose current listener as packet's packet conn
|
// I choose current listener as packet's packet conn
|
||||||
handlePacketConn(lc, in, buf[:br], lAddr.(*net.UDPAddr).AddrPort(), rAddr.(*net.UDPAddr).AddrPort(), additions...)
|
handlePacketConn(lc, in, natTable, buf[:br], lAddr.(*net.UDPAddr).AddrPort(), rAddr.(*net.UDPAddr).AddrPort(), additions...)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return lc, nil
|
return lc, nil
|
||||||
|
|
|
@ -32,7 +32,7 @@ func (l *UDPListener) Close() error {
|
||||||
return l.packetConn.Close()
|
return l.packetConn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUDP(addr string, in chan<- C.PacketAdapter, additions ...inbound.Addition) (*UDPListener, error) {
|
func NewUDP(addr string, in chan<- C.PacketAdapter, natTable C.NatTable, additions ...inbound.Addition) (*UDPListener, error) {
|
||||||
if len(additions) == 0 {
|
if len(additions) == 0 {
|
||||||
additions = []inbound.Addition{
|
additions = []inbound.Addition{
|
||||||
inbound.WithInName("DEFAULT-TPROXY"),
|
inbound.WithInName("DEFAULT-TPROXY"),
|
||||||
|
@ -83,19 +83,21 @@ func NewUDP(addr string, in chan<- C.PacketAdapter, additions ...inbound.Additio
|
||||||
// try to unmap 4in6 address
|
// try to unmap 4in6 address
|
||||||
lAddr = netip.AddrPortFrom(lAddr.Addr().Unmap(), lAddr.Port())
|
lAddr = netip.AddrPortFrom(lAddr.Addr().Unmap(), lAddr.Port())
|
||||||
}
|
}
|
||||||
handlePacketConn(l, in, buf[:n], lAddr, rAddr, additions...)
|
handlePacketConn(l, in, natTable, buf[:n], lAddr, rAddr, additions...)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return rl, nil
|
return rl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePacketConn(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort, additions ...inbound.Addition) {
|
func handlePacketConn(pc net.PacketConn, in chan<- C.PacketAdapter, natTable C.NatTable, buf []byte, lAddr, rAddr netip.AddrPort, additions ...inbound.Addition) {
|
||||||
target := socks5.AddrFromStdAddrPort(rAddr)
|
target := socks5.AddrFromStdAddrPort(rAddr)
|
||||||
pkt := &packet{
|
pkt := &packet{
|
||||||
pc: pc,
|
pc: pc,
|
||||||
lAddr: lAddr,
|
lAddr: lAddr,
|
||||||
buf: buf,
|
buf: buf,
|
||||||
|
in: in,
|
||||||
|
natTable: natTable,
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case in <- inbound.NewPacket(target, pkt, C.TPROXY, additions...):
|
case in <- inbound.NewPacket(target, pkt, C.TPROXY, additions...):
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type packet struct {
|
type packet struct {
|
||||||
|
@ -34,11 +33,3 @@ func (c *packet) Drop() {
|
||||||
func (c *packet) InAddr() net.Addr {
|
func (c *packet) InAddr() net.Addr {
|
||||||
return c.pc.LocalAddr()
|
return c.pc.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packet) SetNatTable(natTable C.NatTable) {
|
|
||||||
// no need
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) {
|
|
||||||
// no need
|
|
||||||
}
|
|
||||||
|
|
|
@ -316,13 +316,5 @@ func (s *serverUDPPacket) Drop() {
|
||||||
s.packet.DATA = nil
|
s.packet.DATA = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *serverUDPPacket) SetNatTable(natTable C.NatTable) {
|
|
||||||
// no need
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *serverUDPPacket) SetUdpInChan(in chan<- C.PacketAdapter) {
|
|
||||||
// no need
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ C.UDPPacket = &serverUDPPacket{}
|
var _ C.UDPPacket = &serverUDPPacket{}
|
||||||
var _ C.UDPPacketInAddr = &serverUDPPacket{}
|
var _ C.UDPPacketInAddr = &serverUDPPacket{}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package tunnel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/Dreamacro/clash/log"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
|
@ -10,6 +9,7 @@ import (
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) error {
|
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) error {
|
||||||
|
|
|
@ -82,6 +82,11 @@ func UDPIn() chan<- C.PacketAdapter {
|
||||||
return udpQueue
|
return udpQueue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NatTable return nat table
|
||||||
|
func NatTable() C.NatTable {
|
||||||
|
return natTable
|
||||||
|
}
|
||||||
|
|
||||||
// Rules return all rules
|
// Rules return all rules
|
||||||
func Rules() []C.Rule {
|
func Rules() []C.Rule {
|
||||||
return rules
|
return rules
|
||||||
|
@ -338,8 +343,6 @@ func handleUDPConn(packet C.PacketAdapter) {
|
||||||
|
|
||||||
oAddr := metadata.DstIP
|
oAddr := metadata.DstIP
|
||||||
natTable.Set(key, pc)
|
natTable.Set(key, pc)
|
||||||
packet.SetNatTable(natTable)
|
|
||||||
packet.SetUdpInChan(udpQueue)
|
|
||||||
|
|
||||||
go handleUDPToLocal(packet, pc, key, oAddr, fAddr)
|
go handleUDPToLocal(packet, pc, key, oAddr, fAddr)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue