-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathip_remote_multi_client_api.go
More file actions
256 lines (224 loc) · 6.75 KB
/
ip_remote_multi_client_api.go
File metadata and controls
256 lines (224 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package connect
import (
"context"
"errors"
"slices"
// "golang.org/x/exp/maps"
// "google.golang.org/protobuf/proto"
// "github.com/urnetwork/glog"
"github.com/urnetwork/connect/protocol"
)
type MultiClientGeneratorClientArgs struct {
ClientId Id
ClientAuth *ClientAuth
P2pOnly bool
}
func DefaultApiMultiClientGeneratorSettings() *ApiMultiClientGeneratorSettings {
return &ApiMultiClientGeneratorSettings{}
}
type ApiMultiClientGeneratorSettings struct {
}
type ApiMultiClientGenerator struct {
specs []*ProviderSpec
clientStrategy *ClientStrategy
excludeClientIds []Id
apiUrl string
byJwt string
platformUrl string
deviceDescription string
deviceSpec string
appVersion string
sourceClientId *Id
clientSettingsGenerator func() *ClientSettings
settings *ApiMultiClientGeneratorSettings
api *BringYourApi
}
func NewApiMultiClientGeneratorWithDefaults(
ctx context.Context,
specs []*ProviderSpec,
clientStrategy *ClientStrategy,
excludeClientIds []Id,
apiUrl string,
byJwt string,
platformUrl string,
deviceDescription string,
deviceSpec string,
appVersion string,
sourceClientId *Id,
) *ApiMultiClientGenerator {
return NewApiMultiClientGenerator(
ctx,
specs,
clientStrategy,
excludeClientIds,
apiUrl,
byJwt,
platformUrl,
deviceDescription,
deviceSpec,
appVersion,
sourceClientId,
DefaultClientSettings,
DefaultApiMultiClientGeneratorSettings(),
)
}
func NewApiMultiClientGenerator(
ctx context.Context,
specs []*ProviderSpec,
clientStrategy *ClientStrategy,
excludeClientIds []Id,
apiUrl string,
byJwt string,
platformUrl string,
deviceDescription string,
deviceSpec string,
appVersion string,
sourceClientId *Id,
clientSettingsGenerator func() *ClientSettings,
settings *ApiMultiClientGeneratorSettings,
) *ApiMultiClientGenerator {
api := NewBringYourApi(ctx, clientStrategy, apiUrl)
api.SetByJwt(byJwt)
return &ApiMultiClientGenerator{
specs: specs,
clientStrategy: clientStrategy,
excludeClientIds: excludeClientIds,
apiUrl: apiUrl,
byJwt: byJwt,
platformUrl: platformUrl,
deviceDescription: deviceDescription,
deviceSpec: deviceSpec,
appVersion: appVersion,
sourceClientId: sourceClientId,
clientSettingsGenerator: clientSettingsGenerator,
settings: settings,
api: api,
}
}
func (self *ApiMultiClientGenerator) NextDestinations(count int, excludeDestinations []MultiHopId, rankMode string) (map[MultiHopId]DestinationStats, error) {
excludeClientIds := slices.Clone(self.excludeClientIds)
excludeDestinationsIds := [][]Id{}
for _, excludeDestination := range excludeDestinations {
excludeDestinationsIds = append(excludeDestinationsIds, excludeDestination.Ids())
}
findProviders2 := &FindProviders2Args{
Specs: self.specs,
ExcludeClientIds: excludeClientIds,
ExcludeDestinations: excludeDestinationsIds,
Count: count,
RankMode: rankMode,
}
result, err := self.api.FindProviders2Sync(findProviders2)
if err != nil {
return nil, err
}
destinations := map[MultiHopId]DestinationStats{}
for _, provider := range result.Providers {
ids := []Id{}
if 0 < len(provider.IntermediaryIds) {
ids = append(ids, provider.IntermediaryIds...)
}
ids = append(ids, provider.ClientId)
// use the tail if the length exceeds the allowed maximum
if MaxMultihopLength < len(ids) {
ids = ids[len(ids)-MaxMultihopLength:]
}
if destination, err := NewMultiHopId(ids...); err == nil {
destinations[destination] = DestinationStats{
EstimatedBytesPerSecond: provider.EstimatedBytesPerSecond,
Tier: provider.Tier,
}
}
}
return destinations, nil
}
func (self *ApiMultiClientGenerator) NewClientArgs() (*MultiClientGeneratorClientArgs, error) {
auth := func() (string, error) {
// note the derived client id will be inferred by the api jwt
authNetworkClient := &AuthNetworkClientArgs{
SourceClientId: self.sourceClientId,
Description: self.deviceDescription,
DeviceSpec: self.deviceSpec,
}
result, err := self.api.AuthNetworkClientSync(authNetworkClient)
if err != nil {
return "", err
}
if result.Error != nil {
return "", errors.New(result.Error.Message)
}
return result.ByClientJwt, nil
}
if byJwtStr, err := auth(); err == nil {
byJwt, err := ParseByJwtUnverified(byJwtStr)
if err != nil {
// in this case we cannot clean up the client because we don't know the client id
panic(err)
}
clientAuth := &ClientAuth{
ByJwt: byJwtStr,
InstanceId: NewId(),
AppVersion: self.appVersion,
}
return &MultiClientGeneratorClientArgs{
ClientId: byJwt.ClientId,
ClientAuth: clientAuth,
}, nil
} else {
return nil, err
}
}
func (self *ApiMultiClientGenerator) RemoveClientArgs(args *MultiClientGeneratorClientArgs) {
removeNetworkClient := &RemoveNetworkClientArgs{
ClientId: args.ClientId,
}
self.api.RemoveNetworkClient(removeNetworkClient, NewApiCallback(func(result *RemoveNetworkClientResult, err error) {
}))
}
func (self *ApiMultiClientGenerator) RemoveClientWithArgs(client *Client, args *MultiClientGeneratorClientArgs) {
self.RemoveClientArgs(args)
}
func (self *ApiMultiClientGenerator) NewClientSettings() *ClientSettings {
return self.clientSettingsGenerator()
}
func (self *ApiMultiClientGenerator) NewClient(
ctx context.Context,
args *MultiClientGeneratorClientArgs,
clientSettings *ClientSettings,
) (*Client, error) {
clientOob := NewApiOutOfBandControl(ctx, self.clientStrategy, args.ClientAuth.ByJwt, self.apiUrl)
client := NewClient(ctx, args.ClientId, clientOob, clientSettings)
settings := DefaultPlatformTransportSettings()
if args.P2pOnly {
settings.TransportGenerator = func() (sendTransport Transport, receiveTransport Transport) {
// only use the platform transport for control
sendTransport = NewSendClientTransport(DestinationId(ControlId))
receiveTransport = NewReceiveGatewayTransport()
return
}
}
NewPlatformTransport(
client.Ctx(),
self.clientStrategy,
client.RouteManager(),
self.platformUrl,
args.ClientAuth,
settings,
)
// enable return traffic for this client
client.ContractManager().SetProvideModesWithReturnTrafficWithAckCallback(
map[protocol.ProvideMode]bool{},
nil,
)
return client, nil
}
func (self *ApiMultiClientGenerator) FixedDestinationSize() (int, bool) {
specClientIds := []Id{}
for _, spec := range self.specs {
if spec.ClientId != nil {
specClientIds = append(specClientIds, *spec.ClientId)
}
}
// glog.Infof("[multi]eval fixed %d/%d\n", len(specClientIds), len(self.specs))
return len(specClientIds), len(specClientIds) == len(self.specs)
}