66 // "crypto/sha256"
77 "errors"
88 // "net"
9- // "net/netip"
9+ "net/netip"
1010 // "regexp"
1111 "strconv"
1212 // "strings"
@@ -93,14 +93,31 @@ type AuthNetworkClientArgs struct {
9393 ProxyConfig * ProxyConfig `json:"proxy_config,omitempty"`
9494}
9595
96- // FIXME
9796type ProxyConfig struct {
9897 LockCallerIp bool `json:"lock_caller_ip"`
9998 LockIpList []string `json:"lock_ip_list"`
10099
101- EnableSocks bool `json:"enable_socks"`
102- EnableHttp bool `json:"enable_http"`
103- HttpRequireAuth bool `json:"http_require_auth"`
100+ HttpsRequireAuth bool `json:"https_require_auth"`
101+
102+ InitialDeviceState * InitialDeviceState `json:"initial_device_state,omitempty"`
103+ }
104+
105+ type InitialDeviceState struct {
106+ CountryCode string `json:"country_code,omitempty"`
107+ Location * InitialDeviceStateLocation `json:"location,omitempty"`
108+ }
109+
110+ type InitialDeviceStateLocation struct {
111+ ConnectLocationId * ConnectLocationId `json:"connect_location_id"`
112+ Name string `json:"name"`
113+ LocationType LocationType `json:"location_type"`
114+ }
115+
116+ type ConnectLocationId struct {
117+ ClientId server.Id `json:"client_id,omitempty"`
118+ LocationId server.Id `json:"location_id,omitempty"`
119+ LocationGroupId server.Id `json:"location_group_id,omitempty"`
120+ BestAvailable bool `json:"best_available,omitempty"`
104121}
105122
106123type AuthNetworkClientResult struct {
@@ -116,21 +133,18 @@ type AuthNetworkClientError struct {
116133}
117134
118135type ProxyConfigResult struct {
119- ExpirationTime time.Time `json:"expiration_time"`
120136 KeepaliveSeconds int `json:"keepalive_seconds"`
121- HttpProxyUrl string `json:"http_proxy_url,omitempty"`
122- SocksProxyUrl string `json:"socks_proxy_url,omitempty"`
123-
124- HttpProxyAuth * ProxyAuthResult `json:"http_proxy_auth"`
125- SocksProxyAuth * ProxyAuthResult `json:"socks_proxy_auth"`
137+ HttpProxyUrl string `json:"http_proxy_url"`
138+ SocksProxyUrl string `json:"socks_proxy_url"`
139+ AuthToken string `json:"auth_token"`
140+ InstanceId server.Id `json:"instance_id"`
126141}
127142
128143type ProxyAuthResult struct {
129144 Username string `json:"username"`
130145 Password string `json:"password"`
131146}
132147
133- // FIXME if proxy config, call CreateProxyDeviceConfig
134148func AuthNetworkClient (
135149 authClient * AuthNetworkClientArgs ,
136150 session * session.ClientSession ,
@@ -224,13 +238,75 @@ func AuthNetworkClient(
224238 })
225239
226240 if authClientResult != nil && authClientResult .Error == nil && authClient .ProxyConfig != nil {
241+ var lockSubnets []netip.Prefix
242+ if authClient .ProxyConfig .LockCallerIp {
243+ addr , _ , err := session .ParseClientIpPort ()
244+ if err != nil {
245+ authClientError = fmt .Errorf ("Could not lock caller ip" )
246+ return
247+ }
248+ prefix , _ := addr .Prefix (addr .BitLen ())
249+ lockSubnets = append (lockSubnets , prefix )
250+ }
251+ for _ , lockIp := range authClient .ProxyConfig .LockIpList {
252+ addr , err := netip .ParseAddr (lockIp )
253+ if err == nil {
254+ prefix , _ := addr .Prefix (addr .BitLen ())
255+ lockSubnets = append (lockSubnets , prefix )
256+ } else {
257+ prefix , err := netip .ParsePrefix (lockIp )
258+ if err != nil {
259+ authClientError = fmt .Errorf ("Could not parse lock ip %s" , lockIp )
260+ return
261+ }
262+ lockSubnets = append (lockSubnets , prefix )
263+ }
264+ }
265+
266+ var proxyDeviceState * ProxyDeviceState
267+ if initialDeviceState := authClient .ProxyConfig .InitialDeviceState ; initialDeviceState != nil {
268+ proxyDeviceState = & ProxyDeviceState {}
269+
270+ if authClient .ProxyConfig .InitialDeviceState .CountryCode != "" {
271+ location := GetConnectLocationForCountryCode (session .Ctx , initialDeviceState .CountryCode )
272+ if location == nil {
273+ authClientError = fmt .Errorf ("Could not find proxy location for country code %s" , initialDeviceState .CountryCode )
274+ return
275+ }
276+ proxyDeviceState .Location = location
277+ } else {
278+ // FIXME parse location
279+ }
280+
281+ // FIXME parse performance profile
282+ }
283+
227284 proxyDeviceConfig := & ProxyDeviceConfig {
228- // FIXME
285+ ProxyDeviceConnection : ProxyDeviceConnection {
286+ ClientId : clientId ,
287+ },
288+ LockSubnets : lockSubnets ,
289+ InitialDeviceState : proxyDeviceState ,
229290 }
230291 err := CreateProxyDeviceConfig (session .Ctx , proxyDeviceConfig )
231292 if err == nil {
232- // FIXME
233- authClientResult .ProxyConfigResult = & ProxyConfigResult {}
293+ signedProxyId := SignProxyId (proxyDeviceConfig .ProxyId )
294+
295+ var httpProxyUrl string
296+ if authClient .ProxyConfig .HttpsRequireAuth {
297+ // use the encoded proxy id for the url, since the signed proxy id will be passed in auth
298+ httpProxyUrl = fmt .Sprintf ("%s.connect.bringyour.com" , EncodeProxyId (proxyDeviceConfig .ProxyId ))
299+ } else {
300+ httpProxyUrl = fmt .Sprintf ("%s.connect.bringyour.com" , signedProxyId )
301+ }
302+
303+ authClientResult .ProxyConfigResult = & ProxyConfigResult {
304+ HttpProxyUrl : httpProxyUrl ,
305+ // FIXME
306+ SocksProxyUrl : "connect.bringyour.com" ,
307+ AuthToken : signedProxyId ,
308+ InstanceId : proxyDeviceConfig .InstanceId ,
309+ }
234310 } else {
235311 authClientResult .Error = & AuthNetworkClientError {
236312 Message : "Could not create proxy device" ,
0 commit comments