fix(mobile): отказ protect возвращать ошибкой сокета

This commit is contained in:
samosvalishe committed 2026-09-22 20:04:05 +03:00
1 parent b1cbafda73
commit 5a1c6e299c
2 files changed
+35 -1

No files matched your search

+12 -1
View File
@@ -1,6 +1,8 @@
package mobile
import (
"errors"
"fmt"
"sync/atomic"
"syscall"
@@ -13,6 +15,8 @@ type Protector interface {
Protect(fd int) bool
}
var ErrProtect = errors.New("mobile: socket protect failed") //nolint:gochecknoglobals // sentinel для errors.Is
var protector atomic.Pointer[Protector]
// SetProtect устанавливает обработчик защиты сокетов хоста (nil - no-op).
@@ -24,7 +28,14 @@ func SetProtect(p Protector) {
}
protector.Store(&p)
netctl.SetControl(func(_, _ string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) { p.Protect(int(fd)) })
ok := false
if err := c.Control(func(fd uintptr) { ok = p.Protect(int(fd)) }); err != nil {
return fmt.Errorf("socket control: %w", err)
}
if !ok {
return ErrProtect
}
return nil
})
}
+23
View File
@@ -12,6 +12,7 @@ import (
"github.com/samosvalishe/free-turn-proxy/internal/config"
"github.com/samosvalishe/free-turn-proxy/internal/logx"
"github.com/samosvalishe/free-turn-proxy/internal/netctl"
"github.com/samosvalishe/free-turn-proxy/internal/tunnel"
)
@@ -369,6 +370,28 @@ func TestProtectFDReturnsStatus(t *testing.T) {
}
}
func TestSetProtectFailsDial(t *testing.T) {
p := &testProtector{}
SetProtect(p)
t.Cleanup(func() { SetProtect(nil) })
d := net.Dialer{Control: netctl.Apply}
conn, err := d.Dial("udp", "127.0.0.1:9")
if err == nil {
_ = conn.Close()
}
if !errors.Is(err, ErrProtect) {
t.Fatalf("Dial = %v, want ErrProtect", err)
}
p.ok = true
conn, err = d.Dial("udp", "127.0.0.1:9")
if err != nil {
t.Fatalf("Dial with protect ok = %v", err)
}
_ = conn.Close()
}
type stubRebinder struct {
stubBackend
rebound int