forked from freedomofpress/sdstatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (146 loc) · 3.01 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/urfave/cli"
"golang.org/x/net/proxy"
)
const (
// proxyAddr points to local SOCKS proxy from Tor
proxyAddr = "127.0.0.1:9050"
)
// Information is used in channels
type Information interface {
msg() string
}
// SDMetadata stores JSON metadata from SD instances
type SDMetadata struct {
Version string `json:"sd_version"`
Fingerprint string `json:"gpg_fpr"`
}
// SDInfo stores metadata and Onion URL
type SDInfo struct {
Info SDMetadata
Url string
Available bool
}
func (sd SDInfo) msg() string {
msgstr := fmt.Sprintf("%s,%s,%s", sd.Url, sd.Info.Version, sd.Info.Fingerprint)
return msgstr
}
func check(e error) {
if e != nil {
panic(e)
}
}
func checkStatus(ch chan Information, client *http.Client, url string) {
var result SDInfo
result.Url = url
metadataURL := fmt.Sprintf("http://%s/metadata", url)
// Create the request
req, err := http.NewRequest("GET", metadataURL, nil)
if err != nil {
result.Available = false
ch <- result
return
}
resp, err := client.Do(req)
if err != nil {
result.Available = false
ch <- result
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
result.Available = false
ch <- result
return
}
var info SDMetadata
json.Unmarshal(body, &info)
result.Info = info
result.Available = true
ch <- result
}
func runScan(csv bool) {
i := 0
results := make([]SDInfo, 0)
// create a SOCKS5 dialer
dialer, err := proxy.SOCKS5("tcp", proxyAddr, nil, proxy.Direct)
if err != nil {
fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err)
os.Exit(1)
}
// setup the http client
httpTransport := &http.Transport{}
c := &http.Client{Transport: httpTransport}
// Add the dialer
httpTransport.Dial = dialer.Dial
ch := make(chan Information)
// Now let us find the onion addresses
data, err := ioutil.ReadFile("sdonion.txt")
check(err)
lines := strings.Split(string(data), "\n")
// For each address we are creating a goroutine
for _, v := range lines {
url := strings.TrimSpace(v)
if url != "" {
go checkStatus(ch, c, v)
i = i + 1
}
}
// Now wait for all the results
for {
result := <-ch
if result != nil {
if csv {
fmt.Println(result.msg())
}
results = append(results, result.(SDInfo))
i = i - 1
}
if i == 0 {
break
}
}
if !csv {
bits, err := json.MarshalIndent(results, "", "\t")
if err == nil {
fmt.Println(string(bits))
}
}
}
func createApp() *cli.App {
app := cli.NewApp()
app.EnableBashCompletion = true
app.Name = "sdstatus"
app.Version = "0.1.0"
app.Usage = "To scan SecureDrop instances"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "csv",
Usage: "Prints output in CSV format",
},
}
app.Action = func(c *cli.Context) error {
csv := c.GlobalBool("csv")
if csv {
runScan(csv)
} else {
runScan(false)
}
return nil
}
return app
}
func main() {
app := createApp()
if err := app.Run(os.Args); err != nil {
check(err)
}
}