-
Notifications
You must be signed in to change notification settings - Fork 75
/
system_dialer.go
44 lines (36 loc) · 962 Bytes
/
system_dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package dialer
import (
"errors"
"net"
"syscall"
"time"
"github.com/xxf098/lite-proxy/log"
)
type controller func(network, address string, fd uintptr) error
var controllers []controller
func Dialer() (*net.Dialer, error) {
dialer := &net.Dialer{
Timeout: 5 * time.Second,
}
if len(controllers) > 0 {
dialer.Control = func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
for _, ctl := range controllers {
if err := ctl(network, address, fd); err != nil {
// errors.New("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx))
log.E("failed to apply external controller")
continue
}
}
})
}
}
return dialer, nil
}
func RegisterDialerController(ctl func(network, address string, fd uintptr) error) error {
if ctl == nil {
return errors.New("nil listener controller")
}
controllers = append(controllers, ctl)
return nil
}