/
db.go
96 lines (84 loc) · 2.67 KB
/
db.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
package db
import (
"context"
"encoding/json"
"fmt"
"github.com/pywc/shawshank_intel/config"
"github.com/pywc/shawshank_intel/testers/dns_tester"
"github.com/pywc/shawshank_intel/testers/http_tester"
"github.com/pywc/shawshank_intel/testers/https_tester"
"github.com/pywc/shawshank_intel/testers/ip_tester"
"github.com/pywc/shawshank_intel/testers/quic_tester"
"github.com/pywc/shawshank_intel/util"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
type TestReport struct {
Country string `json:"country"`
ProxyIP string `json:"proxy_ip"`
ProxyISP string `json:"proxy_isp"`
TestTime string `json:"test_time"`
HostDomain string `json:"host_domain"`
HostIP string `json:"host_ip"`
Residual []util.ResidualDetected `json:"residual,omitempty"`
DNS dns_tester.DNSResult `json:"dns"`
IP ip_tester.IPResult `json:"ip"`
HTTP http_tester.HTTPResult `json:"http"`
HTTPS https_tester.HTTPSResult `json:"https"`
QUIC quic_tester.QUICResult `json:"quic"`
}
func TestDomain(country string, domain string, ip string) {
util.PrintInfo(domain, "initiating tests...")
util.ResetResidualDetector()
report := TestReport{
Country: country,
ProxyIP: config.ProxyIP,
ProxyISP: config.ProxyISP,
HostDomain: domain,
HostIP: ip,
DNS: dns_tester.TestDNS(ip, domain),
IP: ip_tester.TestIP(ip),
HTTP: http_tester.TestHTTP(ip, domain),
HTTPS: https_tester.TestHTTPS(ip, domain),
QUIC: quic_tester.TestQUIC(ip, domain),
Residual: util.AllResidualDetected,
TestTime: time.Now().String(),
}
err := saveToDB(report)
if err != nil {
return
}
}
func saveToDB(report TestReport) error {
util.PrintInfo(report.HostDomain, "saving results...")
j, _ := json.MarshalIndent(report, "", " ")
fmt.Println(string(j))
client, err := connectDB()
if err != nil {
util.PrintError("", err)
return err
}
defer func(client *mongo.Client, ctx context.Context) {
err := client.Disconnect(ctx)
if err != nil {
util.PrintError("", err)
}
}(client, context.Background())
collection := client.Database("shawshank").Collection("results")
_, err = collection.InsertOne(context.Background(), report)
if err != nil {
util.PrintError("", err)
return err
}
return nil
}
func connectDB() (*mongo.Client, error) {
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
util.PrintError("", err)
return nil, err
}
return client, nil
}