github.com/metacubex/mihomo@v1.18.5/component/proxydialer/sing.go (about) 1 package proxydialer 2 3 import ( 4 "context" 5 "net" 6 7 C "github.com/metacubex/mihomo/constant" 8 9 M "github.com/sagernet/sing/common/metadata" 10 N "github.com/sagernet/sing/common/network" 11 ) 12 13 type SingDialer interface { 14 N.Dialer 15 SetDialer(dialer C.Dialer) 16 } 17 18 type singDialer proxyDialer 19 20 var _ N.Dialer = (*singDialer)(nil) 21 22 func (d *singDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { 23 return (*proxyDialer)(d).DialContext(ctx, network, destination.String()) 24 } 25 26 func (d *singDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { 27 return (*proxyDialer)(d).ListenPacket(ctx, "udp", "", destination.AddrPort()) 28 } 29 30 func (d *singDialer) SetDialer(dialer C.Dialer) { 31 (*proxyDialer)(d).dialer = dialer 32 } 33 34 func NewSingDialer(proxy C.ProxyAdapter, dialer C.Dialer, statistic bool) SingDialer { 35 return (*singDialer)(&proxyDialer{ 36 proxy: proxy, 37 dialer: dialer, 38 statistic: statistic, 39 }) 40 } 41 42 type byNameSingDialer struct { 43 dialer C.Dialer 44 proxyName string 45 } 46 47 var _ N.Dialer = (*byNameSingDialer)(nil) 48 49 func (d *byNameSingDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { 50 var cDialer C.Dialer = d.dialer 51 if len(d.proxyName) > 0 { 52 pd, err := NewByName(d.proxyName, d.dialer) 53 if err != nil { 54 return nil, err 55 } 56 cDialer = pd 57 } 58 return cDialer.DialContext(ctx, network, destination.String()) 59 } 60 61 func (d *byNameSingDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { 62 var cDialer C.Dialer = d.dialer 63 if len(d.proxyName) > 0 { 64 pd, err := NewByName(d.proxyName, d.dialer) 65 if err != nil { 66 return nil, err 67 } 68 cDialer = pd 69 } 70 return cDialer.ListenPacket(ctx, "udp", "", destination.AddrPort()) 71 } 72 73 func (d *byNameSingDialer) SetDialer(dialer C.Dialer) { 74 d.dialer = dialer 75 } 76 77 func NewByNameSingDialer(proxyName string, dialer C.Dialer) SingDialer { 78 return &byNameSingDialer{ 79 dialer: dialer, 80 proxyName: proxyName, 81 } 82 }