-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
129 lines (108 loc) · 2.83 KB
/
client.go
File metadata and controls
129 lines (108 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package pia
import (
"bufio"
"context"
"encoding/json"
"fmt"
"time"
"resty.dev/v3"
)
func (c *Client) client(ctx context.Context) *resty.Client {
return resty.NewWithClient(c.http).
AddRequestMiddleware(c.AuthHandler).
SetContext(ctx)
}
type DipRequest struct {
Tokens []string
}
type DipResponse struct {
Status string
Ip string
Cn string
DipExpire string
Id string
}
func (c *Client) GetDIP(ctx context.Context, request *DipRequest) ([]DipResponse, error) {
var result []DipResponse
rest := c.client(ctx)
defer c.close(rest)
res, err := rest.R().
SetContentType("application/json").
SetBody(request).
SetResult(&result).
Post("https://www.privateinternetaccess.com/api/client/v2/dedicated_ip")
if err != nil {
return nil, err
}
if res.IsError() {
return nil, fmt.Errorf("dedicated ip request failed: %s", res.Status())
}
return result, nil
}
type Server struct {
Ip string `json:"ip"`
Cn string `json:"cn"`
Van bool `json:"van,omitempty"`
}
type Region struct {
Id string `json:"id"`
Name string `json:"name"`
Country string `json:"country"`
AutoRegion bool `json:"auto_region"`
Dns string `json:"dns"`
PortForward bool `json:"port_forward"`
Geo bool `json:"geo"`
Offline bool `json:"offline"`
Servers map[string][]Server `json:"servers"`
}
type Group struct {
Name string `json:"name"`
Ports []int `json:"ports"`
}
type ServersResponse struct {
Groups map[string][]Group `json:"groups"`
Regions []Region `json:"regions"`
}
func (c *Client) Servers(ctx context.Context) (*ServersResponse, error) {
rest := resty.NewWithClient(c.http).SetContext(ctx)
defer c.close(rest)
res, err := rest.R().
Get("https://serverlist.piaservers.net/vpninfo/servers/v6")
if err != nil {
return nil, err
}
if res.IsError() {
return nil, fmt.Errorf("servers request failed: %s", res.Status())
}
// Skip the weird b64 thing at the end of the response
r := bufio.NewReader(res.Body)
data, err := r.ReadBytes('\n')
if err != nil {
return nil, err
}
var result ServersResponse
if err = json.Unmarshal(data, &result); err != nil {
return nil, err
}
return &result, err
}
func (c *Client) ConnTime(ctx context.Context, ip string) (time.Duration, error) {
rest := resty.New().SetContext(ctx)
defer c.close(rest)
res, err := rest.R().
EnableTrace().
Get(fmt.Sprintf("http://%s:443", ip))
if err != nil {
return 0, err
}
if res.IsError() {
return 0, fmt.Errorf("latency request failed: %s", res.Status())
}
trace := res.Request.TraceInfo()
return trace.ConnTime, nil
}
func (c *Client) close(rest *resty.Client) {
if err := rest.Close(); err != nil {
c.log.Warn("error closing client", "err", err)
}
}