-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotect.go
More file actions
38 lines (34 loc) · 1.28 KB
/
protect.go
File metadata and controls
38 lines (34 loc) · 1.28 KB
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
package pseudotcp
import (
"fmt"
"syscall"
)
// ConfigureProtect sets up a socket protect function to be used by PseudoTCP.
// This function is used to prevent VPN traffic from being routed through the VPN itself,
// which would create a loop. On Android, this typically calls VpnService.protect().
func (t *PseudoTCP) ConfigureProtect(protect SocketProtector) {
t.currentProtect = protect
}
func (t *PseudoTCP) dialerControlProtect() func(network, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
err := c.Control(func(fd uintptr) {
if t.currentProtect != nil {
t.logger.Debug("Protecting FD", "fd", fd)
if err := t.currentProtect(int(fd)); err != nil {
t.logger.Error("Error calling prot", "err", err, "fd", fd)
}
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVBUF, 4194304); err != nil {
t.logger.Error("Error setting SO_RCVBUF", "err", err)
}
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, 4194304); err != nil {
t.logger.Error("Error setting SO_SNDBUF", "err", err)
}
}
})
if err != nil {
// TODO: handle possible errors in doing protect
return fmt.Errorf("error calling c.Control: %w", err)
}
return nil
}
}