Skip to content

Commit 76bcb35

Browse files
committed
ipn,rpn: impl Has(ccOrId) bool
1 parent 5cffc67 commit 76bcb35

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

intra/backend/ipn_proxies.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ type RpnProxy interface {
255255
PingAll() (csvpids string, err error)
256256
// Purge removes proxy for country code, cc.
257257
Purge(cc string) bool
258+
// Has returns true if a proxy for country code, cc, has been forked.
259+
Has(cc string) bool
258260
// Get returns proxy for country code, cc.
259261
Get(cc string) (Proxy, error)
260262
// Kids returns RpnServers describing all forked kids, excluding the main proxy.
@@ -321,6 +323,8 @@ type Proxies interface {
321323
AddProxy(id, url string) (Proxy, error)
322324
// Remove removes a transport from this multi-transport.
323325
RemoveProxy(id string) bool
326+
// HasProxy returns true if a proxy with the given id is registered.
327+
HasProxy(id string) bool
324328
// GetProxy returns a transport from this multi-transport.
325329
GetProxy(id string) (Proxy, error)
326330
// TestHop returns empty diag if origin can hop to via,

intra/ipn/proxies.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,13 @@ func (px *proxifier) GetProxy(id string) (x.Proxy, error) {
848848
return px.ProxyFor(id)
849849
}
850850

851-
// TestHop implements Proxies.
851+
// Has implements x.Proxies.
852+
func (px *proxifier) HasProxy(id string) bool {
853+
p, err := px.proxyFor(id)
854+
return err == nil && p != nil && core.IsNotNil(p)
855+
}
856+
857+
// TestHop implements x.Proxies.
852858
func (px *proxifier) TestHop(via, origin string) string {
853859
defer core.Recover(core.Exit11, "pxr.TestHop."+via+">>"+origin)
854860
if err := px.hop(via, origin, true /*dryrun*/); err != nil {

intra/ipn/rpn.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ func (r *rpnp) Probe(network, local string) (protect.PacketConn, error) {
272272
// Emplace implements RpnProxy.
273273
func (r *rpnp) Emplace(new Proxy) (err error) {
274274
if new == nil {
275+
// TODO: main RpnServer information must be reverted to prev?
275276
log.W("proxy: rpn: emplace: no-op as new proxy nil")
276277
return errRpnBadEmplace
277278
}
@@ -329,13 +330,14 @@ func (r *rpnp) fork(cc string) (x.Proxy, error) {
329330
// re-forking main proxy (which may not be multi-country acc) via Update() => forkAll()
330331
log.I("proxy: rpn: fork: %s main cc %s; re-adding...", provider, cc)
331332
// expect Emplace to be called
332-
kid, srv, err := r.pxr.addRpnProxy(acc, cc) // re-generates conf and re-adds
333-
if kid != nil && core.IsNotNil(kid) {
333+
mp, ms, err := r.pxr.addRpnProxy(acc, cc) // re-generates conf and re-adds
334+
// see: Emplace (and errBadEmplace)
335+
if mp != nil && core.IsNotNil(mp) {
334336
r.mu.Lock()
335-
r.s = srv
337+
r.s = ms
336338
r.mu.Unlock()
337339
}
338-
return kid, err
340+
return mp, err
339341
}
340342

341343
// check main's status if not forking main
@@ -586,6 +588,32 @@ func (r *rpnp) get(cc string) (Proxy, error) {
586588
return r.pxr.rpnProxyFor(provider, cc)
587589
}
588590

591+
// Has implements RpnProxy.
592+
func (r *rpnp) Has(cc string) bool {
593+
acc := r.RpnAcc
594+
provider := acc.ProviderID()
595+
596+
// strip ProviderID prefix if present (see get)
597+
cc, _ = strings.CutPrefix(cc, provider)
598+
cc = strings.ToUpper(cc)
599+
600+
r.mu.RLock()
601+
main := r.p
602+
_, gotCC := r.kids[cc]
603+
r.mu.RUnlock()
604+
605+
mainpid := idstr(main)
606+
607+
if provider+cc == mainpid ||
608+
cc == mainpid ||
609+
(cc == noCountryForOldMen && !acc.MultiCountry()) ||
610+
(cc == anyCountryCode && acc.MultiCountry()) {
611+
return main != nil && core.IsNotNil(main)
612+
}
613+
614+
return gotCC
615+
}
616+
589617
// Kids implements x.RpnProxy.
590618
func (r *rpnp) Kids() x.RpnServers {
591619
r.mu.RLock()

intra/ipn/rpn/yegor.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,8 @@ func (a *WsClient) Conf(cc string) (string, *x.RpnServer, error) {
11701170
cc = cccsv[1]
11711171
}
11721172
// in sync with anyCountryCode / noCountryForOldMen vars in proxy.go
1173-
chooseAny := cc == "**" || len(cc) <= 0
1173+
anycc := "**"
1174+
chooseAny := cc == anycc || len(cc) <= 0
11741175
hasCity := len(city) > 0
11751176
cc = strings.ToUpper(cc)
11761177

@@ -1267,6 +1268,10 @@ reconf:
12671268
r := rand.IntN(len(out))
12681269
log.I("ws: conf: cc %s(%s): %d/%d => chosen (any? %t): %d[%s/%s] (port: %s)",
12691270
cc, city, c, len(out), chooseAny, r, srvs[r].City, srvs[r].CC, portstr)
1271+
// change key to "any"
1272+
if chooseAny {
1273+
srvs[r].Key = anycc
1274+
}
12701275
return out[r], &srvs[r], nil
12711276
}
12721277
if xl > 0 && (tot == 0 || v <= xl) { // fail open if all CCs excluded

0 commit comments

Comments
 (0)