mobile: configure SOCKS listen host

This commit is contained in:
Alexander Anisimov
2026-05-22 22:11:37 +03:00
parent 5222d8a211
commit 0f177badfc
2 changed files with 45 additions and 5 deletions

View File

@@ -8,8 +8,11 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
@@ -54,6 +57,7 @@ const (
dataTransport = "datachannel"
defaultDNSServer = "8.8.8.8:53"
defaultHTTPPingURL = "https://www.google.com/generate_204"
defaultSocksHost = "127.0.0.1"
carrierWBStream = "wbstream"
)
@@ -79,6 +83,7 @@ var (
type mobileConfig struct {
transport string
dnsServer string
socksListenHost string
vp8FPS int
vp8BatchSize int
livenessInterval time.Duration
@@ -127,6 +132,15 @@ func SetDNS(dnsServer string) {
defaults.dnsServer = dnsServer
}
// SetSocksListenHost selects the local bind host for the SOCKS5 listener.
// Use 0.0.0.0 to accept connections from other Android network interfaces.
func SetSocksListenHost(host string) {
mu.Lock()
defer mu.Unlock()
ensureDefaultConfigLocked()
defaults.socksListenHost = normalizeSocksListenHost(host)
}
// SetVP8Options configures vp8channel.
func SetVP8Options(fps, batchSize int) {
mu.Lock()
@@ -235,7 +249,7 @@ func Check(
RoomURL: buildRoomURL(carrierName, roomID),
KeyHex: keyHex,
DeviceID: clientID,
LocalAddr: fmt.Sprintf("127.0.0.1:%d", socksPort),
LocalAddr: socksListenAddr(cfg.socksListenHost, socksPort),
DNSServer: defaultDNSServer,
TransportOptions: vp8channel.Options{
FPS: clampAtLeastOne(vp8FPS, 120),
@@ -325,7 +339,7 @@ func Ping(
RoomURL: buildRoomURL(carrierName, roomID),
KeyHex: keyHex,
DeviceID: clientID,
LocalAddr: fmt.Sprintf("127.0.0.1:%d", socksPort),
LocalAddr: socksListenAddr(cfg.socksListenHost, socksPort),
DNSServer: defaultDNSServer,
TransportOptions: vp8channel.Options{
FPS: clampAtLeastOne(vp8FPS, 120),
@@ -345,7 +359,7 @@ func Ping(
case <-readyCh:
elapsed, err := httpPingThroughSocks(
ctx,
fmt.Sprintf("127.0.0.1:%d", socksPort),
socksDialAddr(cfg.socksListenHost, socksPort),
pingURL,
)
@@ -572,7 +586,7 @@ func startWithConfig(
RoomURL: roomURL,
KeyHex: keyHex,
DeviceID: clientID,
LocalAddr: fmt.Sprintf("127.0.0.1:%d", socksPort),
LocalAddr: socksListenAddr(cfg.socksListenHost, socksPort),
DNSServer: cfg.dnsServer,
SOCKSUser: socksUser,
SOCKSPass: socksPass,
@@ -693,6 +707,7 @@ func ensureDefaultConfigLocked() {
defaults = mobileConfig{
transport: defaultTransport,
dnsServer: defaultDNSServer,
socksListenHost: defaultSocksHost,
vp8FPS: 60,
vp8BatchSize: 8,
livenessInterval: control.DefaultInterval,
@@ -702,6 +717,30 @@ func ensureDefaultConfigLocked() {
})
}
func normalizeSocksListenHost(host string) string {
host = strings.TrimSpace(host)
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
host = strings.TrimSuffix(strings.TrimPrefix(host, "["), "]")
}
if host == "" {
return defaultSocksHost
}
return host
}
func socksListenAddr(host string, port int) string {
return net.JoinHostPort(normalizeSocksListenHost(host), strconv.Itoa(port))
}
func socksDialAddr(host string, port int) string {
switch normalizeSocksListenHost(host) {
case "0.0.0.0", "::":
return socksListenAddr(defaultSocksHost, port)
default:
return socksListenAddr(host, port)
}
}
func livenessConfig(cfg mobileConfig) control.Config {
interval := cfg.livenessInterval
if interval <= 0 {

View File

@@ -169,11 +169,12 @@ func TestStartWithInjectedRunnerLifecycle(t *testing.T) {
resetMobileGlobals(t)
})
SetLivenessOptions(2500, 750, 4)
SetSocksListenHost("0.0.0.0")
runClientWithReady = func(ctx context.Context, cfg client.Config, onReady func()) error {
opts, _ := cfg.TransportOptions.(vp8channel.Options)
if cfg.Transport != dataTransport || cfg.Carrier != "jitsi" ||
cfg.RoomURL != "room" || cfg.DeviceID != "client" || cfg.LocalAddr != "127.0.0.1:1080" ||
cfg.RoomURL != "room" || cfg.DeviceID != "client" || cfg.LocalAddr != "0.0.0.0:1080" ||
cfg.DNSServer != defaultDNSServer || opts.FPS != 60 || opts.BatchSize != 8 ||
cfg.Liveness.Interval != 2500*time.Millisecond ||
cfg.Liveness.Timeout != 750*time.Millisecond ||