github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/control/interface.go (about) 1 package control 2 3 import ( 4 "syscall" 5 6 E "github.com/sagernet/sing/common/exceptions" 7 ) 8 9 type Func = func(network, address string, conn syscall.RawConn) error 10 11 func Append(oldFunc Func, newFunc Func) Func { 12 if oldFunc == nil { 13 return newFunc 14 } else if newFunc == nil { 15 return oldFunc 16 } 17 return func(network, address string, conn syscall.RawConn) error { 18 if err := oldFunc(network, address, conn); err != nil { 19 return err 20 } 21 return newFunc(network, address, conn) 22 } 23 } 24 25 func Conn(conn syscall.Conn, block func(fd uintptr) error) error { 26 rawConn, err := conn.SyscallConn() 27 if err != nil { 28 return err 29 } 30 return Raw(rawConn, block) 31 } 32 33 func Raw(rawConn syscall.RawConn, block func(fd uintptr) error) error { 34 var innerErr error 35 err := rawConn.Control(func(fd uintptr) { 36 innerErr = block(fd) 37 }) 38 return E.Errors(innerErr, err) 39 }