github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/environment/systemnetworkimpl/systemnetwork.go (about) 1 package systemnetworkimpl 2 3 import ( 4 "context" 5 6 "github.com/v2fly/v2ray-core/v5/common/environment" 7 "github.com/v2fly/v2ray-core/v5/common/net" 8 "github.com/v2fly/v2ray-core/v5/transport/internet" 9 ) 10 11 func NewSystemNetworkImpl(listener internet.SystemListener, dialer internet.SystemDialer) environment.SystemNetworkCapabilitySet { 12 return &systemNetworkImpl{dialer: dialer, listener: listener} 13 } 14 15 type systemDefaultDialer struct{} 16 17 func (s systemDefaultDialer) Listen(ctx context.Context, addr net.Addr, sockopt *internet.SocketConfig) (net.Listener, error) { 18 return internet.ListenSystem(ctx, addr, sockopt) 19 } 20 21 func (s systemDefaultDialer) ListenPacket(ctx context.Context, addr net.Addr, sockopt *internet.SocketConfig) (net.PacketConn, error) { 22 return internet.ListenSystemPacket(ctx, addr, sockopt) 23 } 24 25 func (s systemDefaultDialer) Dial(ctx context.Context, source net.Address, destination net.Destination, sockopt *internet.SocketConfig) (net.Conn, error) { 26 return internet.DialSystem(ctx, destination, sockopt) 27 } 28 29 func NewSystemNetworkDefault() environment.SystemNetworkCapabilitySet { 30 systemDefault := systemDefaultDialer{} 31 return &systemNetworkImpl{dialer: systemDefault, listener: systemDefault} 32 } 33 34 type systemNetworkImpl struct { 35 listener internet.SystemListener 36 dialer internet.SystemDialer 37 } 38 39 func (s systemNetworkImpl) Dialer() internet.SystemDialer { 40 return s.dialer 41 } 42 43 func (s systemNetworkImpl) Listener() internet.SystemListener { 44 return s.listener 45 } 46 47 func NewSystemListenerWithDefaultOpt(listener internet.SystemListener, opt *internet.SocketConfig) internet.SystemListener { 48 return systemListenerWithDefaultOpt{SystemListener: listener, opt: opt} 49 } 50 51 type systemListenerWithDefaultOpt struct { 52 internet.SystemListener 53 opt *internet.SocketConfig 54 } 55 56 func (s systemListenerWithDefaultOpt) Listen(ctx context.Context, addr net.Addr, sockopt *internet.SocketConfig) (net.Listener, error) { 57 if sockopt == nil { 58 return s.Listen(ctx, addr, s.opt) 59 } 60 return s.Listen(ctx, addr, sockopt) 61 } 62 63 func (s systemListenerWithDefaultOpt) ListenPacket(ctx context.Context, addr net.Addr, sockopt *internet.SocketConfig) (net.PacketConn, error) { 64 if sockopt == nil { 65 return s.ListenPacket(ctx, addr, s.opt) 66 } 67 return s.ListenPacket(ctx, addr, sockopt) 68 } 69 70 func NewSystemDialerWithDefaultOpt(listener internet.SystemDialer, opt *internet.SocketConfig) internet.SystemDialer { 71 return systemDialerWithDefaultOpt{SystemDialer: listener, opt: opt} 72 } 73 74 type systemDialerWithDefaultOpt struct { 75 internet.SystemDialer 76 opt *internet.SocketConfig 77 } 78 79 func (s systemDialerWithDefaultOpt) Dial(ctx context.Context, source net.Address, destination net.Destination, sockopt *internet.SocketConfig) (net.Conn, error) { 80 if sockopt == nil { 81 return s.Dial(ctx, source, destination, s.opt) 82 } 83 return s.Dial(ctx, source, destination, sockopt) 84 }