diff --git a/main.go b/main.go new file mode 100644 index 0000000..0b7bb48 --- /dev/null +++ b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "context" + "fmt" + + "github.com/Snawoot/windscribe-proxy/wndclient" +) + +func main() { + wndc, err := wndclient.NewWndClient(nil) + if err != nil { + panic(err) + } + + err = wndc.RegisterToken(context.TODO()) + if err != nil { + panic(err) + } + fmt.Printf("wndc=%#v\n", wndc) +} diff --git a/wndclient/authhash.go b/wndclient/authhash.go new file mode 100644 index 0000000..2326b89 --- /dev/null +++ b/wndclient/authhash.go @@ -0,0 +1,15 @@ +package wndclient + +import ( + "crypto/md5" + "encoding/hex" + "strconv" + "time" +) + +func MakeAuthHash(secret string) (string, int64) { + iTime := time.Now().Unix() + sTime := strconv.FormatInt(iTime, 10) + h := md5.Sum([]byte(secret + sTime)) + return hex.EncodeToString(h[:]), iTime +} diff --git a/wndclient/messages.go b/wndclient/messages.go index 985ff9c..08a844c 100644 --- a/wndclient/messages.go +++ b/wndclient/messages.go @@ -1,11 +1,11 @@ package wndclient -type WndRegisterTokenRequest struct { +type RegisterTokenRequest struct { ClientAuthHash string `json:"client_auth_hash"` Time int64 `json:"time,string"` } -type WndRegisterTokenResponse struct { +type RegisterTokenResponse struct { TokenID string `json:"id"` Token string `json:"token"` TokenSignature string `json:"signature"` diff --git a/wndclient/wndclient.go b/wndclient/wndclient.go index a6944a1..097f28c 100644 --- a/wndclient/wndclient.go +++ b/wndclient/wndclient.go @@ -107,6 +107,24 @@ func (c *WndClient) resetCookies() error { func (c *WndClient) RegisterToken(ctx context.Context) error { c.Mux.Lock() defer c.Mux.Unlock() + + clientAuthHash, authTime := MakeAuthHash(c.Settings.ClientAuthSecret) + input := RegisterTokenRequest{ + ClientAuthHash: clientAuthHash, + Time: authTime, + } + var output RegisterTokenResponse + + err := c.postJSON(ctx, c.Settings.Endpoints.RegisterToken, input, &output) + if err != nil { + return err + } + + c.TokenID = output.TokenID + c.Token = output.Token + c.TokenSignature = output.TokenSignature + c.TokenSignatureTime = output.TokenTime + return nil } @@ -127,6 +145,7 @@ func (c *WndClient) postJSON(ctx context.Context, endpoint string, input, output if err != nil { return err } + c.populateRequest(req) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json")