This commit is contained in:
Daniel 2023-08-13 16:19:23 -04:00
parent bf737c3d5c
commit 68c4e0622d
5 changed files with 263 additions and 199 deletions

2
go.mod
View file

@ -1,3 +1,3 @@
module dynatron.me/amigan/wndport module dynatron.me/amigan/windport
go 1.20 go 1.20

206
main.go
View file

@ -1,33 +1,21 @@
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"os"
"fmt" "fmt"
"strings"
"bufio"
"regexp"
"io" "io"
"log" "log"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"net/url" "net/url"
"strconv" "os"
) "strings"
const ( "dynatron.me/x/windport/pkg/windscribe"
READ_LIMIT int64 = 128 * 1024
) )
type Auth struct {
User string `json:"user"`
Password string `json:"password"`
Code string `json:"code"`
}
func main() { func main() {
a := Auth{} a := windscribe.Auth{}
err := json.NewDecoder(os.Stdin).Decode(&a) err := json.NewDecoder(os.Stdin).Decode(&a)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -36,7 +24,7 @@ func main() {
var PTransport http.RoundTripper = &http.Transport{Proxy: http.ProxyFromEnvironment} var PTransport http.RoundTripper = &http.Transport{Proxy: http.ProxyFromEnvironment}
cli := client{ cli := client{
hc: http.DefaultClient, hc: http.DefaultClient,
auth: a, auth: a,
} }
@ -54,71 +42,14 @@ func main() {
} }
type client struct { type client struct {
hc *http.Client hc *http.Client
auth Auth auth Auth
} }
func (c *client) matchingPort() error { func (c *client) matchingPort() error {
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, "GET", "https://windscribe.com/login", nil)
if err != nil {
return err
}
fillReq(req)
req.Header.Set("Accept", "*/*")
_, err = c.hc.Do(req)
if err != nil {
return err
}
csrf, err := c.getCSRF(ctx)
if err != nil {
return err
}
err = c.doInit(ctx)
if err != nil {
return err
}
values := url.Values{
"login": []string{"1"},
"upgrade": []string{"0"},
"csrf_time": []string{fmt.Sprint(csrf.Time)},
"csrf_token": []string{csrf.Token},
"username": []string{c.auth.User},
"password": []string{c.auth.Password},
"code": []string{c.auth.Code},
}
req, err = http.NewRequestWithContext(ctx, "POST", "https://windscribe.com/login", strings.NewReader(values.Encode()))
if err != nil {
return err
}
fillReq(req)
resp, err := c.hc.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 302 {
return fmt.Errorf("login failed %d", resp.StatusCode)
}
csrf, err = c.getCSRFfromBody(ctx, resp.Body)
if err != nil {
return err
}
cleanupBody(resp.Body)
ephVals := url.Values{ ephVals := url.Values{
"port": []string{""}, "port": []string{""},
"ctime": []string{fmt.Sprint(csrf.Time)}, "ctime": []string{fmt.Sprint(csrf.Time)},
"ctoken": []string{csrf.Token}, "ctoken": []string{csrf.Token},
} }
@ -140,124 +71,3 @@ func (c *client) matchingPort() error {
return nil return nil
} }
func fillReq(r *http.Request) {
r.Header.Set("Connection", "keep-alive")
hdr := map[string]string{
"cache-control": `max-age=0`,
"sec-ch-ua": `"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"`,
"sec-ch-ua-mobile": `?0`,
"sec-ch-ua-platform": `"macOS"`,
"origin": `https://windscribe.com`,
"dnt": `1`,
"upgrade-insecure-requests": `1`,
"content-type": `application/x-www-form-urlencoded`,
"user-agent": `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36`,
"accept": `text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7`,
"sec-fetch-site": `same-origin`,
"sec-fetch-mode": `navigate`,
"sec-fetch-user": `?1`,
"sec-fetch-dest": `document`,
"referer": `https://windscribe.com/login`,
"accept-language": `en-US,en;q=0.9,fr;q=0.8`,
"sec-gpc": `1`,
}
for k, v := range hdr {
r.Header.Set(k, v)
}
}
type CSRF struct {
Token string `json:"csrf_token"`
Time int `json:"csrf_time"`
}
func (c *client) getCSRF(ctx context.Context) (*CSRF, error) {
req, err := http.NewRequestWithContext(ctx, "POST", "https://res.windscribe.com/res/logintoken", strings.NewReader(""))
if err != nil {
return nil, err
}
fillReq(req)
resp, err := c.hc.Do(req)
if err != nil {
return nil, err
}
var csrf CSRF
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&csrf)
if err != nil {
return nil, err
}
cleanupBody(resp.Body)
return &csrf, nil
}
var csrfRE = regexp.MustCompile(`^\s*csrf_(time|token) = '?([0-9a-f]+)'?;\s*$`)
func (c *client) getCSRFfromBody(ctx context.Context, body io.Reader) (*CSRF, error) {
csrf := CSRF{}
scan := bufio.NewScanner(body)
for scan.Scan() {
ln := scan.Text()
m := csrfRE.FindStringSubmatch(ln)
if len(m) == 3 {
var err error
switch m[1] {
case "time":
csrf.Time, err = strconv.Atoi(m[2])
if err != nil {
return nil, err
}
case "token":
csrf.Token = m[2]
}
}
}
if err := scan.Err(); err != nil {
return nil, err
}
if csrf.Token == "" || csrf.Time == 0 {
return nil, fmt.Errorf("getCSRFfromBody: csrf is %+v", csrf)
}
return &csrf, nil
}
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()))
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
}
func cleanupBody(body io.ReadCloser) {
io.Copy(io.Discard, &io.LimitedReader{
R: body,
N: READ_LIMIT,
})
body.Close()
}

101
pkg/windscribe/auth.go Normal file
View file

@ -0,0 +1,101 @@
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
}
func (c *client) Auth(a Auth) error {
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, "GET", "https://windscribe.com/login", nil)
if err != nil {
return err
}
fillReq(req)
req.Header.Set("Accept", "*/*")
_, err = c.hc.Do(req)
if err != nil {
return err
}
csrf, err := c.getCSRF(ctx)
if err != nil {
return err
}
err = c.doInit(ctx)
if err != nil {
return err
}
values := url.Values{
"login": []string{"1"},
"upgrade": []string{"0"},
"csrf_time": []string{fmt.Sprint(csrf.Time)},
"csrf_token": []string{csrf.Token},
"username": []string{c.auth.User},
"password": []string{c.auth.Password},
"code": []string{c.auth.Code},
}
req, err = http.NewRequestWithContext(ctx, "POST", "https://windscribe.com/login", strings.NewReader(values.Encode()))
if err != nil {
return err
}
fillReq(req)
resp, err := c.hc.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 302 {
return fmt.Errorf("login failed %d", resp.StatusCode)
}
csrf, err = c.getCSRFfromBody(ctx, resp.Body)
if err != nil {
return err
}
cleanupBody(resp.Body)
c.auth.isAuthed = true
return nil
}
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()))
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
}

74
pkg/windscribe/client.go Normal file
View file

@ -0,0 +1,74 @@
package windscribe
import (
"io"
"net/http"
"net/http/cookiejar"
)
const (
ReadLimit int64 = 128 * 1024
)
type Client interface {
Auth(Auth) error
}
type client struct {
hc *http.Client
auth Auth
}
func New() Client {
jar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
c := &client{
hc: &http.Client{
Jar: jar,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
},
}
return c
}
func fillReq(r *http.Request) {
r.Header.Set("Connection", "keep-alive")
hdr := map[string]string{
"cache-control": `max-age=0`,
"sec-ch-ua": `"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"`,
"sec-ch-ua-mobile": `?0`,
"sec-ch-ua-platform": `"macOS"`,
"origin": `https://windscribe.com`,
"dnt": `1`,
"upgrade-insecure-requests": `1`,
"content-type": `application/x-www-form-urlencoded`,
"user-agent": `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36`,
"accept": `text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7`,
"sec-fetch-site": `same-origin`,
"sec-fetch-mode": `navigate`,
"sec-fetch-user": `?1`,
"sec-fetch-dest": `document`,
"referer": `https://windscribe.com/login`,
"accept-language": `en-US,en;q=0.9,fr;q=0.8`,
"sec-gpc": `1`,
}
for k, v := range hdr {
r.Header.Set(k, v)
}
}
func cleanupBody(body io.ReadCloser) {
io.Copy(io.Discard, &io.LimitedReader{
R: body,
N: ReadLimit,
})
body.Close()
}

79
pkg/windscribe/csrf.go Normal file
View file

@ -0,0 +1,79 @@
package windscribe
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
)
type csrf struct {
Token string `json:"csrf_token"`
Time int `json:"csrf_time"`
}
func (c *client) getCSRF(ctx context.Context) (*csrf, error) {
req, err := http.NewRequestWithContext(ctx, "POST", "https://res.windscribe.com/res/logintoken", strings.NewReader(""))
if err != nil {
return nil, err
}
fillReq(req)
resp, err := c.hc.Do(req)
if err != nil {
return nil, err
}
var csrf csrf
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&csrf)
if err != nil {
return nil, err
}
cleanupBody(resp.Body)
return &csrf, nil
}
var csrfRE = regexp.MustCompile(`^\s*csrf_(time|token) = '?([0-9a-f]+)'?;\s*$`)
func (c *client) getCSRFfromBody(ctx context.Context, body io.Reader) (*csrf, error) {
csrf := csrf{}
scan := bufio.NewScanner(body)
for scan.Scan() {
ln := scan.Text()
m := csrfRE.FindStringSubmatch(ln)
if len(m) == 3 {
var err error
switch m[1] {
case "time":
csrf.Time, err = strconv.Atoi(m[2])
if err != nil {
return nil, err
}
case "token":
csrf.Token = m[2]
}
}
}
if err := scan.Err(); err != nil {
return nil, err
}
if csrf.Token == "" || csrf.Time == 0 {
return nil, fmt.Errorf("getCSRFfromBody: csrf is %+v", csrf)
}
return &csrf, nil
}