2023-08-13 16:19:23 -04:00
|
|
|
package windscribe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Auth struct {
|
|
|
|
User string `json:"user"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Code string `json:"code"`
|
|
|
|
|
|
|
|
isAuthed bool
|
|
|
|
}
|
|
|
|
|
2023-08-13 23:22:48 -04:00
|
|
|
func (c *client) Auth(ctx context.Context, a Auth) error {
|
2023-08-13 16:19:23 -04:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", "https://windscribe.com/login", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fillReq(req)
|
|
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
|
2023-08-13 23:22:48 -04:00
|
|
|
resp, err := c.hc.Do(req)
|
2023-08-13 16:19:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-13 23:22:48 -04:00
|
|
|
cleanupBody(resp.Body)
|
|
|
|
|
|
|
|
c.csrf, err = c.getCSRF(ctx)
|
2023-08-13 16:19:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.doInit(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
values := url.Values{
|
2023-08-13 23:22:48 -04:00
|
|
|
"login": []string{"1"},
|
|
|
|
"upgrade": []string{"0"},
|
|
|
|
"csrf_time": []string{fmt.Sprint(c.csrf.Time)},
|
|
|
|
"csrf_token": []string{c.csrf.Token},
|
|
|
|
"username": []string{c.auth.User},
|
|
|
|
"password": []string{c.auth.Password},
|
|
|
|
"code": []string{c.auth.Code},
|
2023-08-13 16:19:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequestWithContext(ctx, "POST", "https://windscribe.com/login", strings.NewReader(values.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fillReq(req)
|
|
|
|
|
2023-08-13 23:22:48 -04:00
|
|
|
resp, err = c.hc.Do(req)
|
2023-08-13 16:19:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 302 {
|
|
|
|
return fmt.Errorf("login failed %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2023-08-13 23:22:48 -04:00
|
|
|
c.csrf, err = c.getCSRFfromBody(ctx, resp.Body)
|
2023-08-13 16:19:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupBody(resp.Body)
|
|
|
|
|
|
|
|
c.auth.isAuthed = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-13 23:22:48 -04:00
|
|
|
func (c *client) doInit(ctx context.Context) error {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", "https://res.windscribe.com/res/init", strings.NewReader(url.Values{"wsref": []string{"https://windscribe.com/"}}.Encode()))
|
2023-08-13 16:19:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
fillReq(req)
|
|
|
|
|
|
|
|
resp, err := c.hc.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupBody(resp.Body)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|