Files
free-turn-proxy/mobile/protect.go
T
smediainfo 3481de79a3 feat(mobile): add host socket protector (SetProtect) for tun/VPN hosts
Checked type assertions (errcheck) and Close() error returns to satisfy golangci-lint.
2026-06-28 15:31:04 +03:00

31 lines
1012 B
Go

package mobile
import (
"syscall"
"github.com/samosvalishe/free-turn-proxy/internal/netctl"
)
// Protector is implemented by the host to keep the client's own sockets out of
// a VPN tunnel it is feeding. On Android that is VpnService.protect(fd); on
// other tun-based hosts the equivalent. Without it the client's TURN / VK API /
// DNS traffic is routed back into the tunnel and the connection deadlocks.
type Protector interface {
// Protect is invoked once for every outbound socket the client opens, with
// the socket's raw file descriptor, before it connects.
Protect(fd int) bool
}
// SetProtect installs the host socket protector. Pass nil to clear it (the
// default is a no-op, so desktop builds are unaffected). May be called before
// or after Start; it is read at dial time.
func SetProtect(p Protector) {
if p == nil {
netctl.SetControl(nil)
return
}
netctl.SetControl(func(_, _ string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) { p.Protect(int(fd)) })
})
}