This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
forked from ViRb3/wgcf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (191 loc) · 4.25 KB
/
main.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"errors"
"fmt"
"log"
"net/http"
"path"
"time"
opticgo "github.com/ViRb3/optic-go"
"github.com/ViRb3/sling/v2"
"github.com/ViRb3/wgcf/cloudflare"
"github.com/ViRb3/wgcf/util"
"github.com/ViRb3/wgcf/wireguard"
)
var defaultHeaders = map[string]string{}
var defaultTransport = cloudflare.DefaultTransport
func init() {
for key, val := range cloudflare.DefaultHeaders {
defaultHeaders[key] = val
}
}
type CustomTransport struct{}
func (CustomTransport) RoundTrip(r *http.Request) (*http.Response, error) {
for key, val := range defaultHeaders {
r.Header.Set(key, val)
}
response, err := defaultTransport.RoundTrip(r)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("bad code: %d", response.StatusCode))
}
return response, nil
}
func main() {
if err := testAPI(); err != nil {
log.Fatalln(err)
}
}
func generateKeyPair() (*wireguard.Key, *wireguard.Key) {
privateKey, err := wireguard.NewPrivateKey()
if err != nil {
panic(err)
}
return privateKey, privateKey.Public()
}
func testAPI() error {
testConfig := opticgo.Config{
ApiUrl: opticgo.MustUrl(cloudflare.ApiUrl).ResolveReference(opticgo.MustUrl(cloudflare.ApiVersion)),
OpticUrl: opticgo.MustUrl("http://localhost:8889"),
ProxyListenAddr: "localhost",
DebugPrint: true,
TripFunc: func(tripper http.RoundTripper) http.RoundTripper {
return &CustomTransport{}
},
InternetCheckTimeout: 10 * time.Second,
}
tester, err := opticgo.NewTester(testConfig)
if err != nil {
return err
}
proxyErrChan, err := tester.StartProxy()
if err != nil {
return err
}
go func() {
for err := range proxyErrChan {
log.Fatalln(err)
}
}()
client := sling.New().Client(&http.Client{Transport: CustomTransport{}}).Path(testConfig.OpticUrl.String())
_, publicKey := generateKeyPair()
_, publicKey2 := generateKeyPair()
regData := struct {
PublicKey string `json:"key"`
InstallID string `json:"install_id"`
FcmToken string `json:"fcm_token"`
Tos string `json:"tos"`
Model string `json:"model"`
Type string `json:"type"`
Locale string `json:"locale"`
}{
publicKey.String(),
"", // not empty on actual client
"", // not empty on actual client
util.GetTimestamp(),
"PC",
"Android",
"en_US",
}
var regResp map[string]interface{}
regPath := path.Join(testConfig.ApiUrl.Path, "reg")
if _, err := client.New().Post(regPath).BodyJSON(regData).ReceiveSuccess(®Resp); err != nil {
return err
}
deviceId := regResp["id"].(string)
accessToken := regResp["token"].(string)
initialLicenseKey := regResp["account"].(map[string]interface{})["license"].(string)
defaultHeaders["Authorization"] = fmt.Sprintf("Bearer %s", accessToken)
var tests = []opticgo.TestDefinition{
{
"get device",
nil,
fmt.Sprintf("reg/%s", deviceId),
"GET",
},
{
"get account",
nil,
fmt.Sprintf("reg/%s/account", deviceId),
"GET",
},
{
"get account devices",
nil,
fmt.Sprintf("reg/%s/account/devices", deviceId),
"GET",
},
{
"set device active",
struct {
Active bool `json:"active"`
}{
true,
},
fmt.Sprintf("reg/%s/account/reg/%s", deviceId, deviceId),
"PATCH",
},
{
"set device name",
struct {
Name string `json:"name"`
}{
"TEST",
},
fmt.Sprintf("reg/%s/account/reg/%s", deviceId, deviceId),
"PATCH",
},
{
"get account devices, this time with the name set",
nil,
fmt.Sprintf("reg/%s/account/devices", deviceId),
"GET",
},
{
"get client config",
nil,
fmt.Sprintf("client_config"),
"GET",
},
{
"set license key",
struct {
LicenseKey string `json:"license"`
}{
initialLicenseKey, // TODO: don't use same key
},
fmt.Sprintf("reg/%s/account", deviceId),
"PUT",
},
{
"set public key",
struct {
PublicKey string `json:"key"`
}{
publicKey2.String(),
},
fmt.Sprintf("reg/%s", deviceId),
"PATCH",
},
{
"recreate license key",
nil,
fmt.Sprintf("reg/%s/account/license", deviceId),
"POST",
},
}
errChan, _, err := tester.StartAll(tests)
if err != nil {
return err
}
errText := ""
for err := range errChan {
errText += err.Error() + "\n"
}
if errText != "" {
return errors.New(errText)
}
return nil
}