-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_test.go
428 lines (350 loc) · 10.8 KB
/
main_test.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"
)
var proxyUrl string
const GATESENTRY_CERTIFICATE_COMMON_NAME = "GateSentryFilter"
const BLOCKED_URLS_FILTER = "Blocked URLs"
const HTTPS_EXCEPTION_SITE = "https://www.github.com"
const HTTPS_BUMP_SITE = "https://www.google.com"
const HTTP_BLOCKED_SITE = "http://www.snapads.com"
const HTTPS_BLOCKED_SITE = "https://www.snapads.com"
const GATESENTRY_ADMIN_USERNAME = "admin"
const GATESENTRY_ADMIN_PASSWORD = "admin"
var GATESENTRY_WEBSERVER_BASE_ENDPOINT = "http://localhost:" + GSWEBADMINPORT + "/api"
func TestMain(m *testing.M) {
// Start your proxy server here
go main() // Assume startProxyServer starts your proxy
proxyUrl = "http://localhost:" + GSPROXYPORT
// Run tests
code := m.Run()
// Shutdown code if needed
os.Exit(code)
}
func redirectLogs() {
// set log to dev null
f, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0644)
log.SetOutput(f)
}
func disableDNSBlacklistDownloads() {
// Disable DNS blacklist downloads
R.GSSettings.Update("dns_custom_entries", "[]")
time.Sleep(1 * time.Second)
R.Init()
time.Sleep(1 * time.Second)
}
func TestProxyServer(t *testing.T) {
fmt.Println("Starting tests...")
time.Sleep(2 * time.Second)
fmt.Println("Disabling DNS blacklist downloads")
disableDNSBlacklistDownloads()
time.Sleep(5 * time.Second)
t.Run("Test if the url block filter works", func(t *testing.T) {
proxyURL, err := url.Parse(proxyUrl)
if err != nil {
t.Fatal(err)
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 10 * time.Second,
}
url := ""
for _, filter := range R.Filters {
if filter.FilterName == BLOCKED_URLS_FILTER && len(filter.FileContents) > 0 {
url = filter.FileContents[0].Content
}
}
if url == "" {
t.Fatal("No blocked URLs found")
}
fmt.Println("Checking if url = " + HTTP_BLOCKED_SITE + " is blocked")
resp, err := httpClient.Get(HTTP_BLOCKED_SITE)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
time.Sleep(1 * time.Second)
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
bodyStr := string(body)
if !strings.Contains(bodyStr, "blocked URL") {
t.Fatalf("Expected body to contain 'URL Blocked', but got %s", bodyStr)
}
fmt.Println("Checking if url = " + HTTPS_BLOCKED_SITE + " is blocked")
resp, err = httpClient.Get(HTTPS_BLOCKED_SITE)
if err != nil {
fmt.Println("Error doing a GET for HTTPS blocked site")
t.Fatal(err)
}
defer resp.Body.Close()
time.Sleep(1 * time.Second)
body, err = io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
bodyStr = string(body)
if !strings.Contains(bodyStr, "blocked URL") {
t.Fatalf("Expected body to contain 'URL Blocked', but got %s", bodyStr)
}
})
t.Run("Test if enabling https bumping actually bumps traffic", func(t *testing.T) {
redirectLogs()
enable_filtering := R.GSSettings.Get("enable_https_filtering")
fmt.Println("Enable filtering = " + enable_filtering)
R.GSSettings.Update("enable_https_filtering", "true")
fmt.Println("Updated settings for https filtering")
time.Sleep(1 * time.Second)
enable_filtering = R.GSSettings.Get("enable_https_filtering")
fmt.Println("Enable filtering = " + enable_filtering)
R.Init()
time.Sleep(1 * time.Second)
proxyURL, err := url.Parse(proxyUrl)
if err != nil {
t.Fatal(err)
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // Don't check certificate
},
},
}
resp, err := httpClient.Get(HTTPS_BUMP_SITE)
if err != nil {
t.Fatalf("Traffic was not bumped. Got error: %s", err.Error())
}
defer resp.Body.Close()
realCertSubject := "Some expected subject"
proxyCertSubject := resp.TLS.PeerCertificates[0].Subject.CommonName
isBumped := false
for _, cert := range resp.TLS.PeerCertificates {
if cert.Issuer.CommonName == GATESENTRY_CERTIFICATE_COMMON_NAME {
isBumped = true
break
}
}
if !isBumped {
t.Fatalf("Traffic was not bumped. Got cert subject: %s", proxyCertSubject)
} else {
t.Logf("Traffic was bumped. Expected %s but got %s", realCertSubject, proxyCertSubject)
}
})
t.Run("Test if exception https site is not bumped", func(t *testing.T) {
enable_filtering := R.GSSettings.Get("enable_https_filtering")
fmt.Println("Enable filtering = " + enable_filtering)
proxyURL, err := url.Parse(proxyUrl)
if err != nil {
t.Fatal(err)
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // Don't check certificate
},
},
}
resp, err := httpClient.Get(HTTPS_EXCEPTION_SITE)
if err != nil {
t.Fatalf("Got error: %s", err.Error())
}
defer resp.Body.Close()
realCertSubject := "Some expected subject"
proxyCertSubject := resp.TLS.PeerCertificates[0].Subject.CommonName
isBumped := false
for _, cert := range resp.TLS.PeerCertificates {
if cert.Issuer.CommonName == GATESENTRY_CERTIFICATE_COMMON_NAME {
isBumped = true
break
}
}
if isBumped {
t.Fatalf("Traffic was not bumped. Got cert subject: %s", proxyCertSubject)
} else {
t.Logf("Traffic was bumped. Expected %s but got %s", realCertSubject, proxyCertSubject)
}
})
t.Run("Test if disabling https bumping works", func(t *testing.T) {
redirectLogs()
R.GSSettings.Update("enable_https_filtering", "false")
fmt.Println("Updated settings for https filtering")
time.Sleep(1 * time.Second)
enable_filtering := R.GSSettings.Get("enable_https_filtering")
fmt.Println("Enable filtering = " + enable_filtering)
R.Init()
time.Sleep(1 * time.Second)
proxyURL, err := url.Parse(proxyUrl)
if err != nil {
t.Fatal(err)
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false, // Don't check certificate
},
},
}
resp, err := httpClient.Get("https://www.google.com")
if err != nil {
// this is the actual test
t.Fatal(err)
}
defer resp.Body.Close()
})
t.Run("Test if webserver login works with the default user", func(t *testing.T) {
username := GATESENTRY_ADMIN_USERNAME
password := GATESENTRY_ADMIN_PASSWORD
payload := map[string]string{"username": username, "pass": password}
jsonData, err := json.Marshal(payload)
if err != nil {
t.Fatal("Failed to marshal JSON for sending:", err)
}
resp, err := http.Post(GATESENTRY_WEBSERVER_BASE_ENDPOINT+"/auth/token", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
t.Fatal("Failed to get token:", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal("Failed to read body:", err)
}
// Extract token from response
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
t.Fatal("Failed to unmarshal response:", err)
}
token, ok := result["Jwtoken"].(string)
if !ok {
t.Fatal("Token not found in response")
}
// Make GET request to /filters using the token
req, err := http.NewRequest("GET", GATESENTRY_WEBSERVER_BASE_ENDPOINT+"/filters", nil)
if err != nil {
t.Fatal("Failed to create request:", err)
}
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
resp, err = client.Do(req)
time.Sleep(2 * time.Second)
if err != nil {
t.Fatal("Failed to get filters:", err)
}
defer resp.Body.Close()
// Check for 200 status code
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status 200, got %d", resp.StatusCode)
}
jsonDataString := `
[{"Content":"google","Score":10000}]
`
req, err = http.NewRequest("POST", GATESENTRY_WEBSERVER_BASE_ENDPOINT+"/filters/bVxTPTOXiqGRbhF", bytes.NewBuffer([]byte(jsonDataString)))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
if err != nil {
t.Fatal("Failed to create request:", err)
}
// get response body
resp, err = client.Do(req)
if err != nil {
t.Fatal("Failed to post filters:", err)
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
if err != nil {
t.Fatal("Failed to read body:", err)
}
fmt.Println("Response body after post = " + string(body))
fmt.Println("Waiting for the server to reload")
// time.Sleep(4 * time.Second)
for _, filter := range R.Filters {
fmt.Println("Filter name = " + filter.FilterName)
for _, line := range filter.FileContents {
fmt.Println("Line = " + line.Content)
}
}
req, err = http.NewRequest("GET", GATESENTRY_WEBSERVER_BASE_ENDPOINT+"/filters/bVxTPTOXiqGRbhF", nil)
req.Header.Set("Authorization", "Bearer "+token)
if err != nil {
t.Fatal("Failed to create request:", err)
}
// get response body
resp, err = client.Do(req)
time.Sleep(2 * time.Second)
if err != nil {
t.Fatal("Failed to get filters:", err)
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
if err != nil {
t.Fatal("Failed to read body:", err)
}
fmt.Println("Response body = " + string(body))
})
t.Run("Test if keyword blocking works by adding the keyword google and visiting Google", func(t *testing.T) {
redirectLogs()
enable_filtering := R.GSSettings.Get("enable_https_filtering")
fmt.Println("Enable filtering = " + enable_filtering)
R.GSSettings.Update("enable_https_filtering", "true")
fmt.Println("Updated settings for https filtering")
time.Sleep(1 * time.Second)
enable_filtering = R.GSSettings.Get("enable_https_filtering")
fmt.Println("Enable filtering = " + enable_filtering)
R.Init()
time.Sleep(2 * time.Second)
proxyURL, err := url.Parse(proxyUrl)
if err != nil {
t.Fatal(err)
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // Don't check certificate
},
},
}
resp, err := httpClient.Get("https://www.google.com")
time.Sleep(4 * time.Second)
if err != nil {
t.Fatalf("Traffic was not bumped. Got error: %s", err.Error())
}
defer resp.Body.Close()
proxyCertSubject := resp.TLS.PeerCertificates[0].Subject.CommonName
isBumped := false
for _, cert := range resp.TLS.PeerCertificates {
if cert.Issuer.CommonName == GATESENTRY_CERTIFICATE_COMMON_NAME {
isBumped = true
break
}
}
if !isBumped {
t.Fatalf("Traffic was not bumped. Got cert subject: %s", proxyCertSubject)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal("Failed to read body:", err)
}
if !strings.Contains(string(body), "<title>Blocked</title>") {
t.Fatal("Traffic was not blocked")
}
})
}