wndclient: register token WIP

This commit is contained in:
Vladislav Yarmak
2021-06-23 21:11:19 +03:00
parent 9a391d6e92
commit 46d9564d35
4 changed files with 57 additions and 2 deletions

21
main.go Normal file
View File

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

15
wndclient/authhash.go Normal file
View File

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

View File

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

View File

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