first working version

This commit is contained in:
Vladislav Yarmak
2021-06-25 04:16:41 +03:00
parent c96b7ca763
commit abb10a90d6
3 changed files with 39 additions and 28 deletions

39
main.go
View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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) {