|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/urnetwork/glog" |
| 12 | + "github.com/urnetwork/server" |
| 13 | + "github.com/urnetwork/server/jwt" |
| 14 | + "github.com/urnetwork/server/model" |
| 15 | + "github.com/urnetwork/server/session" |
| 16 | +) |
| 17 | + |
| 18 | +type watchdog struct { |
| 19 | + ctx context.Context |
| 20 | + pollTimeout time.Duration |
| 21 | +} |
| 22 | + |
| 23 | +func newWatchdog(ctx context.Context, pollTimeout time.Duration) *watchdog { |
| 24 | + w := &watchdog{ |
| 25 | + ctx: ctx, |
| 26 | + pollTimeout: pollTimeout, |
| 27 | + } |
| 28 | + go server.HandleError(w.run, func() { |
| 29 | + os.Exit(1) |
| 30 | + }) |
| 31 | + return w |
| 32 | +} |
| 33 | + |
| 34 | +func (self *watchdog) run() { |
| 35 | + for { |
| 36 | + select { |
| 37 | + case <-self.ctx.Done(): |
| 38 | + case <-time.After(self.pollTimeout): |
| 39 | + } |
| 40 | + |
| 41 | + testNetworkId := server.RequireParseId("018c224b-909e-d3f3-b0bf-f40b7e11c5d7") |
| 42 | + testUserId := server.RequireParseId("018c224b-909e-d3f3-b0bf-f40b2f8f8cfd") |
| 43 | + |
| 44 | + success := func() bool { |
| 45 | + testCtx, testCancel := context.WithCancel(self.ctx) |
| 46 | + defer testCancel() |
| 47 | + |
| 48 | + testSession := &session.ClientSession{ |
| 49 | + Ctx: testCtx, |
| 50 | + Cancel: testCancel, |
| 51 | + ClientAddress: "0.0.0.0:0", |
| 52 | + Header: map[string][]string{}, |
| 53 | + ByJwt: &jwt.ByJwt{ |
| 54 | + NetworkId: testNetworkId, |
| 55 | + UserId: testUserId, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + result, err := model.AuthNetworkClient( |
| 60 | + &model.AuthNetworkClientArgs{ |
| 61 | + Description: "proxy watchdog", |
| 62 | + DeviceSpec: "proxy watchdog", |
| 63 | + }, |
| 64 | + testSession, |
| 65 | + ) |
| 66 | + if err != nil { |
| 67 | + panic(err) |
| 68 | + } |
| 69 | + |
| 70 | + clientId := *result.ClientId |
| 71 | + defer model.RemoveNetworkClient( |
| 72 | + &model.RemoveNetworkClientArgs{ |
| 73 | + ClientId: clientId, |
| 74 | + }, |
| 75 | + testSession, |
| 76 | + ) |
| 77 | + |
| 78 | + proxyDeviceConfig := &model.ProxyDeviceConfig{ |
| 79 | + InitialDeviceState: &model.ProxyDeviceState{ |
| 80 | + Location: model.GetConnectLocationForCountryCode(self.ctx, "us"), |
| 81 | + }, |
| 82 | + } |
| 83 | + proxyDeviceConfig.ClientId = clientId |
| 84 | + model.CreateProxyDeviceConfig(self.ctx, proxyDeviceConfig) |
| 85 | + defer model.RemoveProxyDeviceConfig(testCtx, proxyDeviceConfig.ProxyId) |
| 86 | + signedProxyId := model.SignProxyId(proxyDeviceConfig.ProxyId) |
| 87 | + |
| 88 | + httpProxyUrl, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", ListenHttpPort)) |
| 89 | + if err != nil { |
| 90 | + panic(err) |
| 91 | + } |
| 92 | + |
| 93 | + proxyConnectHeader := http.Header{} |
| 94 | + proxyConnectHeader.Add("Proxy-Authorization", fmt.Sprintf("Bearer %s", signedProxyId)) |
| 95 | + |
| 96 | + proxyHttpClient := &http.Client{ |
| 97 | + Timeout: 120 * time.Second, |
| 98 | + Transport: &http.Transport{ |
| 99 | + Proxy: http.ProxyURL(httpProxyUrl), |
| 100 | + ProxyConnectHeader: proxyConnectHeader, |
| 101 | + DisableKeepAlives: true, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + n := 5 |
| 106 | + success := false |
| 107 | + for i := range n { |
| 108 | + r, err := http.NewRequestWithContext(self.ctx, "GET", "https://api.bringyour.com/hello", nil) |
| 109 | + if err != nil { |
| 110 | + panic(err) |
| 111 | + } |
| 112 | + response, err := proxyHttpClient.Do(r) |
| 113 | + if err == nil && response.StatusCode == http.StatusOK { |
| 114 | + glog.Infof("[proxy][%d/%d]watchdog poll success\n", i+1, n) |
| 115 | + success = true |
| 116 | + break |
| 117 | + } |
| 118 | + if i+1 < n { |
| 119 | + glog.Infof("[proxy][%d/%d]watchdog poll failed err=%s. Will retry ...\n", i+1, n, err) |
| 120 | + } else { |
| 121 | + glog.Infof("[proxy][%d/%d]watchdog poll failed err=%s. Exiting.\n", i+1, n, err) |
| 122 | + } |
| 123 | + } |
| 124 | + return success |
| 125 | + }() |
| 126 | + select { |
| 127 | + case <-self.ctx.Done(): |
| 128 | + return |
| 129 | + default: |
| 130 | + } |
| 131 | + if !success { |
| 132 | + os.Exit(1) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments