This commit is contained in:
Daniel Ponte 2023-08-13 23:22:48 -04:00
parent a117e48183
commit a9f46fddc4
7 changed files with 104 additions and 93 deletions

View file

@ -1,3 +1,2 @@
all: wsp
wsp: main.go
all:
go build -o wsp main.go

2
go.mod
View file

@ -1,3 +1,3 @@
module dynatron.me/x/windport
go 1.20
go 1.21

52
main.go
View file

@ -1,15 +1,10 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
"dynatron.me/x/windport/pkg/windscribe"
)
@ -21,53 +16,16 @@ func main() {
log.Fatal(err)
}
var PTransport http.RoundTripper = &http.Transport{Proxy: http.ProxyFromEnvironment}
c := windscribe.New()
ctx := context.Background()
cli := client{
hc: http.DefaultClient,
auth: a,
}
cli.hc.Transport = PTransport
cli.hc.Jar, err = cookiejar.New(nil)
err = c.Auth(ctx, a)
if err != nil {
log.Fatal(err)
}
err = cli.matchingPort()
err = c.MatchingPort(ctx)
if err != nil {
log.Fatal(err)
}
}
type client struct {
hc *http.Client
auth Auth
}
func (c *client) matchingPort() error {
ephVals := url.Values{
"port": []string{""},
"ctime": []string{fmt.Sprint(csrf.Time)},
"ctoken": []string{csrf.Token},
}
req, err = http.NewRequestWithContext(ctx, "POST", "https://windscribe.com/staticips/postEphPort", strings.NewReader(ephVals.Encode()))
if err != nil {
return err
}
fillReq(req)
req.Header.Set("referer", "https://windscribe.com/myaccount")
resp, err = c.hc.Do(req)
if err != nil {
return err
}
io.Copy(os.Stderr, resp.Body)
cleanupBody(resp.Body)
return nil
}

View file

@ -16,8 +16,7 @@ type Auth struct {
isAuthed bool
}
func (c *client) Auth(a Auth) error {
ctx := context.Background()
func (c *client) Auth(ctx context.Context, a Auth) error {
req, err := http.NewRequestWithContext(ctx, "GET", "https://windscribe.com/login", nil)
if err != nil {
return err
@ -26,12 +25,14 @@ func (c *client) Auth(a Auth) error {
fillReq(req)
req.Header.Set("Accept", "*/*")
_, err = c.hc.Do(req)
resp, err := c.hc.Do(req)
if err != nil {
return err
}
csrf, err := c.getCSRF(ctx)
cleanupBody(resp.Body)
c.csrf, err = c.getCSRF(ctx)
if err != nil {
return err
}
@ -42,13 +43,13 @@ func (c *client) Auth(a Auth) error {
}
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},
"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},
}
req, err = http.NewRequestWithContext(ctx, "POST", "https://windscribe.com/login", strings.NewReader(values.Encode()))
@ -58,7 +59,7 @@ func (c *client) Auth(a Auth) error {
fillReq(req)
resp, err := c.hc.Do(req)
resp, err = c.hc.Do(req)
if err != nil {
return err
}
@ -67,7 +68,7 @@ func (c *client) Auth(a Auth) error {
return fmt.Errorf("login failed %d", resp.StatusCode)
}
csrf, err = c.getCSRFfromBody(ctx, resp.Body)
c.csrf, err = c.getCSRFfromBody(ctx, resp.Body)
if err != nil {
return err
}
@ -79,8 +80,8 @@ func (c *client) Auth(a Auth) error {
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()))
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
}
@ -97,5 +98,3 @@ func (c *client) doInit(ctx context.Context) (error) {
return nil
}

View file

@ -1,6 +1,7 @@
package windscribe
import (
"context"
"io"
"net/http"
"net/http/cookiejar"
@ -11,12 +12,14 @@ const (
)
type Client interface {
Auth(Auth) error
Auth(context.Context, Auth) error
MatchingPort(context.Context) error
}
type client struct {
hc *http.Client
hc *http.Client
auth Auth
csrf *CSRF
}
func New() Client {
@ -41,23 +44,23 @@ 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`,
"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`,
"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 {

View file

@ -10,14 +10,31 @@ import (
"regexp"
"strconv"
"strings"
"time"
)
type csrf struct {
const (
csrfExpire time.Duration = time.Second * 4
)
type CSRF struct {
Token string `json:"csrf_token"`
Time int `json:"csrf_time"`
Time int `json:"csrf_time"`
}
func (c *client) getCSRF(ctx context.Context) (*csrf, error) {
func (c *client) CSRF(ctx context.Context) *CSRF {
if c.csrf == nil || time.Now().Sub(time.Unix(int64(c.csrf.Time), 0)) > csrfExpire {
var err error
c.csrf, err = c.getCSRF(ctx)
if err != nil {
panic(err)
}
}
return c.csrf
}
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
@ -30,7 +47,7 @@ func (c *client) getCSRF(ctx context.Context) (*csrf, error) {
return nil, err
}
var csrf csrf
var csrf CSRF
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&csrf)
@ -44,8 +61,8 @@ func (c *client) getCSRF(ctx context.Context) (*csrf, error) {
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{}
func (c *client) getCSRFfromBody(ctx context.Context, body io.Reader) (*CSRF, error) {
csrf := CSRF{}
scan := bufio.NewScanner(body)
for scan.Scan() {
@ -75,5 +92,3 @@ func (c *client) getCSRFfromBody(ctx context.Context, body io.Reader) (*csrf, er
return &csrf, nil
}

37
pkg/windscribe/port.go Normal file
View file

@ -0,0 +1,37 @@
package windscribe
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
)
func (c *client) MatchingPort(ctx context.Context) error {
ephVals := url.Values{
"port": []string{""},
"ctime": []string{fmt.Sprint(c.csrf.Time)},
"ctoken": []string{c.csrf.Token},
}
req, err := http.NewRequestWithContext(ctx, "POST", "https://windscribe.com/staticips/postEphPort", strings.NewReader(ephVals.Encode()))
if err != nil {
return err
}
fillReq(req)
req.Header.Set("referer", "https://windscribe.com/myaccount")
resp, err := c.hc.Do(req)
if err != nil {
return err
}
io.Copy(os.Stderr, resp.Body)
cleanupBody(resp.Body)
return nil
}