Skip to content

Commit 01ee3bf

Browse files
committed
settings,ipn: local, remote, hybrid modes for auto
1 parent 7850079 commit 01ee3bf

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

intra/backend/netstat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ type RDNSInfo struct {
164164
ProxySince string
165165
Proxies string
166166

167-
AutoAlwaysRemote bool
167+
AutoMode string
168168
AutoDialsParallel bool
169169

170170
OpenConnsTCP string

intra/ipn/auto.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (h *auto) Handle() uintptr {
7676

7777
// DialerHandle implements Proxy.
7878
func (h *auto) DialerHandle() (mix uintptr) {
79-
remoteOnly := settings.AutoAlwaysRemote.Load()
79+
remoteOnly := settings.AutoAlwaysRemote()
8080
if !remoteOnly {
8181
if exit, _ := h.pxr.ProxyFor(Exit); exit != nil {
8282
mix ^= exit.DialerHandle()
@@ -132,7 +132,7 @@ func (h *auto) dial(network, laddr, raddr string) (protect.Conn, error) {
132132
}
133133
}
134134

135-
remoteOnly := settings.AutoAlwaysRemote.Load()
135+
remoteOnly := settings.AutoAlwaysRemote()
136136
parallelDial := settings.AutoDialsParallel.Load()
137137

138138
if !parallelDial {
@@ -407,7 +407,7 @@ func (h *auto) Accept(network, local string) (l protect.Listener, err error) {
407407
if h.status.Load() == END {
408408
return nil, errProxyStopped
409409
}
410-
if settings.AutoAlwaysRemote.Load() {
410+
if settings.AutoAlwaysRemote() {
411411
log.E("proxy: auto: accept(%s) on %s remote-dial unimplemented", network, local)
412412
return nil, errNotRemote
413413
}
@@ -426,7 +426,7 @@ func (h *auto) Probe(network, local string) (pc protect.PacketConn, err error) {
426426
if h.status.Load() == END {
427427
return nil, errProxyStopped
428428
}
429-
if settings.AutoAlwaysRemote.Load() {
429+
if settings.AutoAlwaysRemote() {
430430
log.E("proxy: auto: probe(%s) on %s remote-dial unimplemented", network, local)
431431
return nil, errNotRemote
432432
}

intra/ipn/proxies.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/celzero/firestack/intra/log"
2727
"github.com/celzero/firestack/intra/netstack"
2828
"github.com/celzero/firestack/intra/protect"
29+
"github.com/celzero/firestack/intra/settings"
2930
)
3031

3132
const (
@@ -175,6 +176,8 @@ type rpnProxyProvider interface {
175176
mainRpnProxyOf(provider string) (RpnProxy, error)
176177
// rpnProxyFor returns a country-specific RPN proxy from this multi-transport.
177178
rpnProxyFor(provider, cc string) (Proxy, error)
179+
// AutoActive returns true if any of the RPN proxies are in-use by ipn.Auto.
180+
AutoActive() bool
178181
}
179182

180183
type ProxyProvider interface {
@@ -653,6 +656,10 @@ func (px *proxifier) ProxyFor(id string) (Proxy, error) {
653656
return p, nil
654657
}
655658

659+
func (px *proxifier) AutoActive() bool {
660+
return settings.AutoActive()
661+
}
662+
656663
func (px *proxifier) mainRpnProxyOf(provider string) (RpnProxy, error) {
657664
if !isRPN(provider) {
658665
return nil, errNotRpnID

intra/settings/proxyopts.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,54 @@ func (p *ProxyOptions) Url() string {
108108
return p.Scheme + "://" + p.IPPort
109109
}
110110

111-
// AutoAlwaysRemote is a global variable to instruct ipn.Auto proxy
112-
// to always use remote proxies and never use local (ex: ipn.Exit) ones.
113-
var AutoAlwaysRemote atomic.Bool
114-
115-
// SetAutoAlwaysRemote puts backend.Auto in remote-only mode if y is true.
116-
// That is, backend.Auto will never use local proxies (ex: ipn.Exit).
117-
func SetAutoAlwaysRemote(y bool) (prev bool) {
118-
return AutoAlwaysRemote.Swap(y)
111+
// AutoMode is a global variable to instruct if backend.Auto proxy
112+
// is in local, remote, or hybrid mode. In local mode, backend.Auto
113+
// uses local proxies (ex: ipn.Exit) only. In remote mode,
114+
// backend.Auto uses remote proxies (ex: RPN).
115+
var AutoMode atomic.Int32
116+
117+
const (
118+
// local mode: backend.Auto uses local proxies (ex: ipn.Exit) only.
119+
AutoModeLocal = iota
120+
// remote mode: backend.Auto uses remote proxies (ex: RPN) only.
121+
AutoModeRemote
122+
// hybrid mode: backend.Auto uses local and remote proxies.
123+
AutoModeHybrid
124+
)
125+
126+
// SetAutoMode sets the global AutoMode variable to y.
127+
// Indicates if backend.Auto proxy is in local, remote, or hybrid mode.
128+
func SetAutoMode(m int32) (ok bool) {
129+
s := max(m, AutoModeLocal)
130+
s = min(m, AutoModeHybrid)
131+
if m != s {
132+
return false
133+
}
134+
AutoMode.Store(m)
135+
return true
136+
}
137+
138+
func AutoModeStr() string {
139+
switch AutoMode.Load() {
140+
case AutoModeLocal:
141+
return "local"
142+
case AutoModeRemote:
143+
return "remote"
144+
case AutoModeHybrid:
145+
return "hybrid"
146+
default:
147+
return "unknown"
148+
}
149+
}
150+
151+
// backend.Auto must use remote proxies and never use local (ex: ipn.Exit) ones.
152+
func AutoAlwaysRemote() bool {
153+
return AutoMode.Load() == AutoModeRemote
154+
}
155+
156+
// backend.Auto is effecively not active.
157+
func AutoActive() bool {
158+
return AutoMode.Load() != AutoModeLocal
119159
}
120160

121161
// AutoDialsParallel is a global variable to instruct ipn.Auto proxy

intra/tunnel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (t *rtunnel) stat() (*x.NetStat, error) {
321321
out.RDNSIn.Dialer4 = dialers.Use4()
322322
out.RDNSIn.Dialer6 = dialers.Use6()
323323
out.RDNSIn.DialerOpts = csv2ssv(settings.GetDialerOpts().String())
324-
out.RDNSIn.AutoAlwaysRemote = settings.AutoAlwaysRemote.Load()
324+
out.RDNSIn.AutoMode = settings.AutoModeStr()
325325
out.RDNSIn.AutoDialsParallel = settings.AutoDialsParallel.Load()
326326

327327
firewall := settings.BlockMode.Load()

0 commit comments

Comments
 (0)