Skip to content

Commit 80ee05e

Browse files
committed
ipn/yegor: retry connect
1 parent 40b74c9 commit 80ee05e

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

intra/ipn/rpn/yegor.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ const (
7272
// This error is a trigger to run a new "/init" API call to generate a new keypair,
7373
// as you're effectively in a clean slate (how all accounts start out).
7474
//
75+
// Another error 1312 - "Could not select a new WireGuard interface IP" is returned
76+
// by "/connect" if the "/init"d client IP was released by the server and assigned
77+
// to another user?
78+
//
7579
// Once local (client) has keypair, do not make the "/init" call again unless on errors
7680
// as described above.
7781
wswginitpath = "WgConfigs/init"
@@ -116,6 +120,7 @@ const (
116120
const (
117121
ekeylimit = 1313
118122
ekeyinvalid = 1311
123+
enoaddr = 1312
119124
)
120125

121126
const (
@@ -1523,6 +1528,8 @@ func genWgConfs(h *http.Client, existingCreds *WsWgCreds, existingPermaCreds *Ws
15231528
tokst := "sess-" + tokenState(bearer)
15241529

15251530
runkey := 0
1531+
runinit := 0
1532+
runconnect := 0
15261533
keyed := 0
15271534
keyagain:
15281535
useExistingCreds := existingCreds != nil && keyed == 0 && !forceInit
@@ -1550,7 +1557,6 @@ keyagain:
15501557

15511558
force := "0" // reset to 0, if force init is not needed
15521559

1553-
runinit := 0
15541560
initagain:
15551561
keyNeedsInit := !useExistingCreds || force == "1"
15561562
runinit += 1
@@ -1634,6 +1640,9 @@ initagain:
16341640
log.I("ws: wgconfs: got creds;" + details)
16351641

16361642
someEndpoint := fixedValidWsEndpoint(test)
1643+
1644+
connectagain:
1645+
runconnect += 1
16371646
// github.com/Windscribe/Android-App/blob/746d505dc69/base/src/main/java/com/windscribe/vpn/backend/utils/WindVpnController.kt#L159
16381647
/*
16391648
curl -x POST '.../WgConfigs/connect' \
@@ -1652,19 +1661,20 @@ initagain:
16521661
u := baseurl(test, cid, ent.Did).JoinPath(wswgconnectpath)
16531662
creq, err := http.NewRequest("POST", u.String(), strings.NewReader(cdata.Encode()))
16541663
if err != nil {
1655-
return nil, nil, nil, log.EE("ws: wgconfs: %s connect req err: %v", details, err)
1664+
return nil, nil, nil, log.EE("ws: wgconfs: %s connect#%d req err: %v", details, runconnect, err)
16561665
}
16571666
creq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
16581667
authHeader(creq, sess.SessionToken)
16591668
didHeader(creq, ent.DidToken)
16601669

16611670
if settings.Debug {
1662-
log.V("ws: wgconfs: %s connect req: %s tok %s", details, u.String(), tokst)
1671+
log.V("ws: wgconfs: %s connect#%d req: %s tok %s", details, runconnect, u.String(), tokst)
16631672
}
16641673

16651674
cres, err := h.Do(creq)
16661675
if err != nil || cres == nil {
1667-
return nil, nil, nil, log.EE("ws: wgconfs: %s connect res err (nil? %t / tok? %s): %v", details, cres == nil, tokst, err)
1676+
return nil, nil, nil, log.EE("ws: wgconfs: %s connect#%d res err (nil? %t / tok? %s): %v",
1677+
details, runconnect, cres == nil, tokst, err)
16681678
}
16691679
updateDidTokenIfNeeded(ent, cres)
16701680
if cres.StatusCode != http.StatusOK {
@@ -1675,6 +1685,9 @@ initagain:
16751685
keyed = 1
16761686
goto keyagain // try again with a non-default key
16771687
}
1688+
} else if wserr != nil && wserr.Code == enoaddr && runconnect < 2 {
1689+
time.Sleep(3 * time.Second) // wait a bit before retrying once
1690+
goto connectagain // retry connect
16781691
}
16791692
return nil, nil, nil, err
16801693
}
@@ -1683,15 +1696,19 @@ initagain:
16831696
_, err = wsRes(cres, &wgConnect, "wgconfs")
16841697
defer core.Close(cres.Body)
16851698
if err != nil {
1686-
return nil, nil, nil, log.EE("ws: wgconfs: %s connect res err: %v", details, err)
1699+
return nil, nil, nil, log.EE("ws: wgconfs: %s connect#%d res err: %v",
1700+
details, runconnect, err)
16871701
}
16881702

1703+
// TODO: goto connectagain if runconnect < 2?
16891704
if wgConnect.Data.Success != 1 {
1690-
return nil, nil, nil, log.EE("ws: wgconfs: %s connect success != 1; debug: %v", details, wgConnect.Data.Debug)
1705+
return nil, nil, nil, log.EE("ws: wgconfs: %s connect#%d success != 1; debug: %v",
1706+
details, runconnect, wgConnect.Data.Debug)
16911707
}
1692-
1708+
// TODO: goto connectagain if runconnect < 2?
16931709
if len(wgConnect.Data.Config.Address) <= 0 || len(wgConnect.Data.Config.DNS) <= 0 {
1694-
return nil, nil, nil, log.EE("ws: wgconfs: %s connect missing config; debug: %v", details, wgConnect.Data.Debug)
1710+
return nil, nil, nil, log.EE("ws: wgconfs: %s connect#%d missing config; debug: %v",
1711+
details, runconnect, wgConnect.Data.Debug)
16951712
}
16961713

16971714
// TODO: if wgconnect.Data.Config.Address has not changed and useExistingCreds is true,

0 commit comments

Comments
 (0)