From abb10a90d61befe169e53d609c53b30b301dc5cb Mon Sep 17 00:00:00 2001 From: Vladislav Yarmak Date: Fri, 25 Jun 2021 04:16:41 +0300 Subject: [PATCH] first working version --- main.go | 39 ++++++++++++++++++++++++--------------- wndclient/messages.go | 22 ++++++++++++---------- wndclient/wndclient.go | 6 +++--- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/main.go b/main.go index a899347..659c1d5 100644 --- a/main.go +++ b/main.go @@ -183,7 +183,9 @@ func run() int { mainLogger.Error("Unable to save state file! Error: %v", err) } } else { + wndc.Mux.Lock() wndc.State = *state + wndc.Mux.Unlock() } var serverList wndclient.ServerList @@ -206,10 +208,19 @@ func run() int { return printLocations(serverList) } - //if len(ips) == 0 { - // mainLogger.Critical("Empty endpoint list!") - // return 13 - //} + var proxyHostname string + if args.location == "" { + ctx, cl := context.WithTimeout(context.Background(), args.timeout) + bestLocation, err := wndc.BestLocation(ctx) + cl() + if err != nil { + mainLogger.Critical("Unable to get best location endpoint: %v", err) + return 13 + } + proxyHostname = bestLocation.Hostname + } else { + return 0 + } //runTicker(context.Background(), args.refresh, args.refreshRetry, func(ctx context.Context) error { // mainLogger.Info("Refreshing login...") @@ -234,7 +245,6 @@ func run() int { // return nil //}) - //endpoint := ips[0] auth := func() string { return basic_auth_header(wndc.GetProxyCredentials()) } @@ -254,16 +264,15 @@ func run() int { } // TODO: set servername - //handlerDialer := NewProxyDialer(endpoint.NetAddr(), "", auth, caPool, dialer) - //mainLogger.Info("Endpoint: %s", endpoint.NetAddr()) - //mainLogger.Info("Starting proxy server...") - //handler := NewProxyHandler(handlerDialer, proxyLogger) - //mainLogger.Info("Init complete.") - //err = http.ListenAndServe(args.bindAddress, handler) - //mainLogger.Critical("Server terminated with a reason: %v", err) - //mainLogger.Info("Shutting down...") - _ = proxyLogger - _ = auth + proxyNetAddr := net.JoinHostPort(proxyHostname, strconv.FormatUint(uint64(ASSUMED_PROXY_PORT), 10)) + handlerDialer := NewProxyDialer(proxyNetAddr, proxyHostname, auth, caPool, dialer) + mainLogger.Info("Endpoint: %s", proxyNetAddr) + mainLogger.Info("Starting proxy server...") + handler := NewProxyHandler(handlerDialer, proxyLogger) + mainLogger.Info("Init complete.") + err = http.ListenAndServe(args.bindAddress, handler) + mainLogger.Critical("Server terminated with a reason: %v", err) + mainLogger.Info("Shutting down...") return 0 } diff --git a/wndclient/messages.go b/wndclient/messages.go index 487fab4..b81aada 100644 --- a/wndclient/messages.go +++ b/wndclient/messages.go @@ -83,14 +83,16 @@ type ServerListGroupHost struct { Weight float64 `json:"weight"` } -type BestLocationResponse struct { - Data *struct { - CountryCode string `json:"country_code"` - ShortName string `json:"short_name"` - LocationName string `json:"location_name"` - CityName string `json:"city_name"` - DCID int `json:"dc_id"` - ServerID int `json:"server_id"` - Hostname string `json:"hostname"` - } `json:"data"` +type BestLocation struct { + CountryCode string `json:"country_code"` + ShortName string `json:"short_name"` + LocationName string `json:"location_name"` + CityName string `json:"city_name"` + DCID int `json:"dc_id"` + ServerID int `json:"server_id"` + Hostname string `json:"hostname"` +} + +type BestLocationResponse struct { + Data *BestLocation `json:"data"` } diff --git a/wndclient/wndclient.go b/wndclient/wndclient.go index 306bdbf..66e680d 100644 --- a/wndclient/wndclient.go +++ b/wndclient/wndclient.go @@ -195,13 +195,13 @@ func (c *WndClient) ServerCredentials(ctx context.Context) error { return nil } -func (c *WndClient) BestLocation(ctx context.Context) (*BestLocationResponse, error) { +func (c *WndClient) BestLocation(ctx context.Context) (*BestLocation, error) { c.Mux.Lock() defer c.Mux.Unlock() clientAuthHash, authTime := MakeAuthHash(c.State.Settings.ClientAuthSecret) - requestUrl, err := url.Parse(c.State.Settings.Endpoints.ServerCredentials) + requestUrl, err := url.Parse(c.State.Settings.Endpoints.BestLocation) if err != nil { return nil, err } @@ -221,7 +221,7 @@ func (c *WndClient) BestLocation(ctx context.Context) (*BestLocationResponse, er return nil, ErrNoDataInResponse } - return &output, nil + return output.Data, nil } func (c *WndClient) ServerList(ctx context.Context) (ServerList, error) {