Skip to content

Commit 7561f86

Browse files
committed
rpn: gobind does not like array-types
1 parent c5a2986 commit 7561f86

5 files changed

Lines changed: 57 additions & 15 deletions

File tree

intra/backend/ipn_proxies.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ type RpnAcc interface {
178178
Created() int64
179179
// Expires returns the time (unix millis) currently active account expires.
180180
Expires() int64
181-
// Locations returns a list of servers worldwide. May be nil.
182-
Locations() []RpnServer
181+
// Locations returns RpnServers encapsulating this proxy's worldwide server presence.
182+
Locations() (RpnServers, error)
183183
// Update updates the account creating new state.
184184
Update() (newstate *Gobyte, err error)
185185
}
@@ -270,6 +270,15 @@ type RouterStats struct {
270270
Since int64
271271
}
272272

273+
type RpnServers interface {
274+
// Get returns the RpnServer at index i; errors if i is out of bounds.
275+
Get(i int) (*RpnServer, error)
276+
// Len returns the number of RpnServers.
277+
Len() int
278+
// Json returns the RpnServer struct as JSON bytes.
279+
Json() (*Gobyte, error)
280+
}
281+
273282
type RpnServer struct {
274283
// Name of the server, if any.
275284
Name string

intra/dns53/dot_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,13 @@ func TestProtonReaches(t *testing.T) {
366366

367367
const maxVisited = 10
368368
visited := make(map[string]struct{}, 0)
369-
for _, c := range win.Locations() {
369+
locs, err := win.Locations()
370+
ko(t, err)
371+
for i := 0; i < locs.Len(); i++ {
372+
c, err := locs.Get(i)
373+
if err != nil {
374+
continue
375+
}
370376
if _, ok := visited[c.CC]; !ok {
371377
// _, _ = pxr.AddProxy(ipn.RpnPro+c.CC, c.UapiConfig())
372378
visited[c.CC] = struct{}{}

intra/ipn/rpn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,6 @@ func (r *rpnp) Update() (newState *x.Gobyte, err error) {
192192
return r.RpnAcc.Update()
193193
}
194194

195-
func (r *rpnp) Locations() []x.RpnServer {
195+
func (r *rpnp) Locations() (x.RpnServers, error) {
196196
return r.RpnAcc.Locations()
197197
}

intra/ipn/warp/api.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ import (
3333
)
3434

3535
var (
36-
errRpnStateless = errors.New("rpn has no state or config")
37-
errRpnUpdateless = errors.New("rpn cannot be updated only registered")
36+
errRpnCountryless = errors.New("rpn is not multi-country")
37+
errRpnStateless = errors.New("rpn has no state or config")
38+
errRpnUpdateless = errors.New("rpn cannot be updated only registered")
3839
)
3940

4041
type RpnAcc interface {
@@ -67,8 +68,8 @@ func (RpnMultiCountry) MultiCountry() bool { return true }
6768

6869
type RpnCountryless struct{}
6970

70-
func (c RpnCountryless) MultiCountry() bool { return false }
71-
func (c RpnCountryless) Locations() []x.RpnServer { return nil }
71+
func (c RpnCountryless) MultiCountry() bool { return false }
72+
func (c RpnCountryless) Locations() (x.RpnServers, error) { return nil, errRpnCountryless }
7273

7374
type RpnStateless struct {
7475
RpnUpdateless
@@ -81,6 +82,34 @@ type RpnUpdateless struct{}
8182

8283
func (RpnUpdateless) Update() (*x.Gobyte, error) { return nil, errRpnUpdateless }
8384

85+
type RpnMultiCountryServers struct {
86+
all []x.RpnServer
87+
}
88+
89+
var _ x.RpnServers = (*RpnMultiCountryServers)(nil)
90+
91+
func (s *RpnMultiCountryServers) Get(i int) (*x.RpnServer, error) {
92+
if i < 0 || i >= len(s.all) {
93+
return nil, fmt.Errorf("rpn: %d out of range [0, %d)", i, len(s.all))
94+
}
95+
return &s.all[i], nil
96+
}
97+
98+
func (s *RpnMultiCountryServers) Len() int {
99+
return len(s.all)
100+
}
101+
102+
func (s *RpnMultiCountryServers) Json() (*x.Gobyte, error) {
103+
if s == nil || len(s.all) <= 0 {
104+
return nil, fmt.Errorf("rpn: no servers")
105+
}
106+
b, err := json.Marshal(s.all)
107+
if err != nil {
108+
return nil, fmt.Errorf("rpn: marshal servers: %w", err)
109+
}
110+
return x.BytesOf(b), nil
111+
}
112+
84113
type WarpClient struct {
85114
RpnCountryless
86115
RpnUpdateless

intra/ipn/warp/yegor.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -792,18 +792,16 @@ func (a *WsClient) Expires() int64 {
792792
return refreshAt.UnixMilli()
793793
}
794794

795-
func (a *WsClient) Locations() []x.RpnServer {
795+
func (a *WsClient) Locations() (x.RpnServers, error) {
796796
if a == nil {
797-
return nil
797+
return nil, errWsNoClient
798798
}
799799
c := a.config()
800800
if c == nil {
801-
log.W("ws: locations: no session")
802-
return nil
801+
return nil, errWsNoConfig
803802
}
804803
if len(c.Configs) <= 0 {
805-
log.W("ws: locations: no configs")
806-
return nil
804+
return nil, errWsNoCcConfig
807805
}
808806
visited := make(map[string]bool, len(c.Configs))
809807
s := make([]x.RpnServer, 0, len(c.Configs)/maxPerRegionWgConfs)
@@ -829,7 +827,7 @@ func (a *WsClient) Locations() []x.RpnServer {
829827
}
830828
visited[rc.CC] = true
831829
}
832-
return s
830+
return &RpnMultiCountryServers{s}, nil
833831
}
834832

835833
// Update implements x.RpnAcc.

0 commit comments

Comments
 (0)