github.com/metacubex/mihomo@v1.18.5/component/dialer/control.go (about) 1 package dialer 2 3 import ( 4 "context" 5 "net" 6 "syscall" 7 ) 8 9 type controlFn = func(ctx context.Context, network, address string, c syscall.RawConn) error 10 11 func addControlToListenConfig(lc *net.ListenConfig, fn controlFn) { 12 llc := *lc 13 lc.Control = func(network, address string, c syscall.RawConn) (err error) { 14 switch { 15 case llc.Control != nil: 16 if err = llc.Control(network, address, c); err != nil { 17 return 18 } 19 } 20 return fn(context.Background(), network, address, c) 21 } 22 } 23 24 func addControlToDialer(d *net.Dialer, fn controlFn) { 25 ld := *d 26 d.ControlContext = func(ctx context.Context, network, address string, c syscall.RawConn) (err error) { 27 switch { 28 case ld.ControlContext != nil: 29 if err = ld.ControlContext(ctx, network, address, c); err != nil { 30 return 31 } 32 case ld.Control != nil: 33 if err = ld.Control(network, address, c); err != nil { 34 return 35 } 36 } 37 return fn(ctx, network, address, c) 38 } 39 }