forked from dyne/tor-dam
/
main.go
338 lines (297 loc) · 8.83 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
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
package main
/*
* Copyright (c) 2017-2018 Dyne.org Foundation
* tor-dam is written and maintained by Ivan Jelincic <parazyd@dyne.org>
*
* This file is part of tor-dam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"bufio"
"bytes"
"compress/gzip"
"crypto/rand"
"encoding/base64"
"encoding/json"
"flag"
"io/ioutil"
"log"
"math/big"
"os"
"os/exec"
"strings"
"sync"
"time"
"golang.org/x/crypto/ed25519"
lib "github.com/adam-burns/tor-dam/pkg/damlib"
)
type msgStruct struct {
Secret string
}
var (
noremote = flag.Bool("noremote", false, "Don't fetch remote entrypoints.")
gen = flag.Bool("gen", false, "Only (re)generate keypairs and exit cleanly.")
annint = flag.Int("ai", 5, "Announce interval (in minutes)")
remoteentry = flag.String("remoteentry", "https://dam.decodeproject.eu/dirs.txt", "Remote list of entrypoints. (comma-separated)")
portmap = flag.String("portmap", "13010:13010,13011:13011,5000:5000", "Map of ports forwarded to/from Tor.")
)
func clientInit(gen bool) error {
pub, priv, err := lib.GenEd25519()
if err != nil {
return err
}
if err := lib.SavePrivEd25519(lib.PrivKeyPath, priv); err != nil {
return err
}
if err := lib.SaveSeedEd25519(lib.SeedPath, priv.Seed()); err != nil {
return err
}
if err := os.Chmod(lib.PrivKeyPath, 0600); err != nil {
return err
}
if err := os.Chmod(lib.SeedPath, 0600); err != nil {
return err
}
onionaddr := lib.OnionFromPubkeyEd25519(pub)
if err := ioutil.WriteFile("hostname", onionaddr, 0600); err != nil {
return err
}
if gen {
log.Println("Our hostname is:", string(onionaddr))
os.Exit(0)
}
return nil
}
func fetchNodeList(epLists []string, remote bool) ([]string, error) {
var nodeslice, nodelist []string
log.Println("Fetching a list of nodes.")
// Remote network entrypoints
if !(remote) {
for _, i := range epLists {
log.Println("Fetching", i)
n, err := lib.HTTPDownload(i)
if err != nil {
return nil, err
}
nodeslice = lib.ParseDirs(nodeslice, n)
}
}
// Local ~/.dam/directories.txt
if _, err := os.Stat("directories.txt"); err == nil {
ln, err := ioutil.ReadFile("directories.txt")
if err != nil {
return nil, err
}
nodeslice = lib.ParseDirs(nodeslice, ln)
}
// Local nodes known to Redis
nodes, _ := lib.RedisCli.Keys(lib.Rctx, "*.onion").Result()
for _, i := range nodes {
valid, err := lib.RedisCli.HGet(lib.Rctx, i, "valid").Result()
if err != nil {
// Possible RedisCli bug, possible Redis bug. To be investigated.
// Sometimes it returns err, but it's nil and does not say what's
// happening exactly.
continue
}
if valid == "1" {
nodeslice = append(nodeslice, i)
}
}
// Remove possible duplicates. Duplicates can cause race conditions and are
// redundant to the entire logic.
encounter := map[string]bool{}
for i := range nodeslice {
encounter[nodeslice[i]] = true
}
nodeslice = []string{}
for key := range encounter {
nodeslice = append(nodeslice, key)
}
if len(nodeslice) < 1 {
log.Fatalln("Couldn't fetch any nodes to announce to. Exiting.")
} else if len(nodeslice) <= 6 {
log.Printf("Found only %d nodes.\n", len(nodeslice))
nodelist = nodeslice
} else {
log.Println("Found enough directories. Picking out 6 random ones.")
for i := 0; i <= 5; i++ {
n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(nodeslice))))
nodelist = append(nodelist, nodeslice[n.Int64()])
nodeslice[n.Int64()] = nodeslice[len(nodeslice)-1]
nodeslice = nodeslice[:len(nodeslice)-1]
}
}
return nodelist, nil
}
func announce(node string, vals map[string]string, privkey ed25519.PrivateKey) (bool, error) {
msg, _ := json.Marshal(vals)
log.Println("Announcing keypair to:", node)
resp, err := lib.HTTPPost("http://"+node+"/announce", msg)
if err != nil {
return false, err
}
// Parse server's reply
var m msgStruct
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&m); err != nil {
return false, err
}
if resp.StatusCode == 400 {
log.Printf("%s fail. Reply: %s\n", node, m.Secret)
return false, nil
}
if resp.StatusCode == 200 {
log.Printf("%s success. 1/2 handshake valid.", node)
sig, err := lib.SignMsgEd25519([]byte(m.Secret), privkey)
if err != nil {
return false, err
}
encodedSig := base64.StdEncoding.EncodeToString(sig)
vals["secret"] = m.Secret
vals["message"] = m.Secret
vals["signature"] = encodedSig
msg, _ := json.Marshal(vals)
log.Printf("%s: success. Sending back signed secret.\n", node)
resp, err := lib.HTTPPost("http://"+node+"/announce", msg)
if err != nil {
return false, err
}
decoder = json.NewDecoder(resp.Body)
if err := decoder.Decode(&m); err != nil {
return false, err
}
if resp.StatusCode == 200 {
log.Printf("%s success. 2/2 handshake valid.\n", node)
data, err := base64.StdEncoding.DecodeString(m.Secret)
if err != nil {
// Not a list of nodes.
log.Printf("%s replied: %s\n", node, m.Secret)
return true, nil
}
log.Println("Got node data. Processing...")
b := bytes.NewReader(data)
r, _ := gzip.NewReader(b)
nodes := make(map[string]map[string]interface{})
decoder = json.NewDecoder(r)
if err = decoder.Decode(&nodes); err != nil {
return false, err
}
for k, v := range nodes {
log.Printf("Adding %s to Redis.\n", k)
_, err = lib.RedisCli.HMSet(lib.Rctx, k, v).Result()
lib.CheckError(err)
}
return true, nil
}
log.Printf("%s fail. Reply: %s\n", node, m.Secret)
return false, nil
}
return false, nil
}
func main() {
flag.Parse()
// Network entrypoints. These files hold the lists of nodes we can announce
// to initially. Format is "DIR:unlikelynamefora.onion", other lines are
// ignored and can be used as comments or similar.
epLists := strings.Split(*remoteentry, ",")
if _, err := os.Stat(lib.Workdir); os.IsNotExist(err) {
err := os.Mkdir(lib.Workdir, 0700)
lib.CheckError(err)
}
err := os.Chdir(lib.Workdir)
lib.CheckError(err)
if _, err = os.Stat(lib.PrivKeyPath); os.IsNotExist(err) || *gen {
err = clientInit(*gen)
lib.CheckError(err)
}
// Map it to the flag
lib.TorPortMap = "80:49371," + *portmap
log.Println("Starting up the hidden service.")
cmd := exec.Command("damhs.py", "-k", lib.PrivKeyPath, "-p", lib.TorPortMap)
defer cmd.Process.Kill()
stdout, err := cmd.StdoutPipe()
lib.CheckError(err)
err = cmd.Start()
lib.CheckError(err)
scanner := bufio.NewScanner(stdout)
ok := false
go func() {
// If we do not manage to publish our descriptor, we shall exit.
t1 := time.Now().Unix()
for !(ok) {
t2 := time.Now().Unix()
if t2-t1 > 90 {
log.Fatalln("Too much time has passed for publishing descriptor.")
}
time.Sleep(1000 * time.Millisecond)
}
}()
for !(ok) {
scanner.Scan()
status := scanner.Text()
if status == "OK" {
log.Println("Hidden service is now running.")
ok = true
}
}
onionaddr, err := ioutil.ReadFile("hostname")
lib.CheckError(err)
log.Println("Our hostname is:", string(onionaddr))
for {
log.Println("Announcing to nodes...")
var ann = 0 // Track of successful authentications.
var wg sync.WaitGroup
nodes, err := fetchNodeList(epLists, *noremote)
if err != nil {
// No route to host, or failed download. Try later.
log.Println("Failed to fetch any nodes. Retrying in a minute.")
time.Sleep(60 * time.Second)
continue
}
privkey, err := lib.LoadEd25519KeyFromSeed(lib.SeedPath)
lib.CheckError(err)
pubkey := privkey.Public().(ed25519.PublicKey)
onionaddr := lib.OnionFromPubkeyEd25519(pubkey)
encodedPub := base64.StdEncoding.EncodeToString([]byte(pubkey))
sig, err := lib.SignMsgEd25519([]byte(lib.PostMsg), privkey)
lib.CheckError(err)
encodedSig := base64.StdEncoding.EncodeToString(sig)
nodevals := map[string]string{
"address": string(onionaddr),
"pubkey": encodedPub,
"message": lib.PostMsg,
"signature": encodedSig,
"secret": "",
}
for _, i := range nodes {
wg.Add(1)
go func(x string) {
valid, err := announce(x, nodevals, privkey)
if err != nil {
log.Printf("%s: %s\n", x, err.Error())
}
if valid {
ann++
}
wg.Done()
}(i)
}
wg.Wait()
log.Printf("%d successful authentications.\n", ann)
log.Printf("Waiting %d min before next announce.\n", *annint)
time.Sleep(time.Duration(*annint) * time.Minute)
}
}