-
Notifications
You must be signed in to change notification settings - Fork 16
/
misc.go
423 lines (386 loc) · 12.9 KB
/
misc.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
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/inhies/go-cjdns/admin"
"github.com/inhies/go-cjdns/config"
"io/ioutil"
"math/rand"
"net"
"os"
"os/user"
"regexp"
"strings"
)
const (
ipRegex = "^fc[a-f0-9]{1,2}:([a-f0-9]{0,4}:){2,6}[a-f0-9]{1,4}$"
pathRegex = "([0-9a-f]{4}\\.){3}[0-9a-f]{4}"
hostRegex = "^([a-zA-Z0-9]([a-zA-Z0-9\\-\\.]{0,}[a-zA-Z0-9]))$"
)
// gotYes will read from stdin and if it is any variation of 'y' or 'yes' then it returns true
// If defaultYes is set to true and the user presses enter without entering anything else it returns true
func gotYes(defaultYes bool) bool {
var choice string
n, _ := fmt.Scanln(&choice)
if n == 0 {
if defaultYes {
return true
} else {
return false
}
}
if strings.ToLower(choice) == "y" || strings.ToLower(choice) == "yes" {
return true
}
return false
}
// Reads the .cjdnsadmin file and returns the structured contents
func readCjdnsadmin(file string) (admin *admin.CjdnsAdminConfig, err error) {
rawFile, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
raw, err := stripComments(rawFile)
if err != nil {
return nil, err
}
err = json.Unmarshal(raw, &admin)
if err != nil {
return nil, err
}
if err != nil {
// BUG(inhies): Find a better way of dealing with these errors.
if e, ok := err.(*json.SyntaxError); ok {
// BUG(inhies): Instead of printing x amount of characters, print the previous and following 2 lines
fmt.Println("Invalid JSON") //" at byte", e.Offset, "(after stripping comments...)")
fmt.Println("----------------------------------------")
fmt.Println(string(raw[e.Offset-60 : e.Offset+60]))
fmt.Println("----------------------------------------")
} else if _, ok := err.(*json.InvalidUTF8Error); ok {
fmt.Println("Invalid UTF-8")
} else if e, ok := err.(*json.InvalidUnmarshalError); ok {
fmt.Println("Invalid unmarshall type", e.Type)
fmt.Println(err)
} else if e, ok := err.(*json.UnmarshalFieldError); ok {
fmt.Println("Invalid unmarshall field", e.Field, e.Key, e.Type)
} else if e, ok := err.(*json.UnmarshalTypeError); ok {
fmt.Println("Invalid JSON")
fmt.Println("Expected", e.Type, "but received a", e.Value)
fmt.Println("I apologize for not being more helpful")
} else if e, ok := err.(*json.UnsupportedTypeError); ok {
fmt.Println("Invalid JSON")
fmt.Println("I am unable to utilize type", e.Type)
} else if e, ok := err.(*json.UnsupportedValueError); ok {
fmt.Println("Invalid JSON")
fmt.Println("I am unable to utilize value", e.Value, e.Str)
}
return nil, err
}
return
}
// Reads the configuration file specified in global variable File
// and sets the admin credentials
func readConfig() (conf *config.Config, err error) {
conf, err = config.LoadMinConfig(File)
if err != nil || len(conf.Admin.Password) == 0 {
return
}
AdminPassword = conf.Admin.Password
AdminBind = conf.Admin.Bind
return
}
// Attempt to connect to cjdns
func adminConnect() (user *admin.Conn, err error) {
// If nothing else has already set this
var cjdnsAdmin *admin.CjdnsAdminConfig
if AdminBind == "" || AdminPassword == "" {
// If we still have no idea which configuration file to use
if File == "" {
if !userSpecifiedCjdnsadmin {
cjdnsAdmin, err = loadCjdnsadmin()
if err != nil {
fmt.Println("Unable to load configuration file:", err)
return
}
} else {
cjdnsAdmin, err = readCjdnsadmin(userCjdnsadmin)
if err != nil {
fmt.Println("Error loading cjdnsadmin file:", err)
return
}
}
// Set the admin credentials from the .cjdnsadmin file
//AdminPassword = cjdnsAdmin.Password
//AdminBind = cjdnsAdmin.Addr + ":" + strconv.Itoa(cjdnsAdmin.Port)
// If File and OutFile aren't already set, set them
// Note that they could still be empty if the "config"
// entry isnt in .cjdnsadmin
if File == "" {
File = cjdnsAdmin.Config
}
if OutFile == "" {
OutFile = cjdnsAdmin.Config
}
} else {
// File is set so use it
_, err = readConfig()
if err != nil {
err = fmt.Errorf("Unable to load configuration file:", err.Error())
return nil, err
}
}
}
user, err = admin.Connect(cjdnsAdmin)
if err != nil {
if e, ok := err.(net.Error); ok {
if e.Timeout() {
fmt.Println("\nConnection timed out")
} else if e.Temporary() {
fmt.Println("\nTemporary error (not sure what that means!)")
} else {
fmt.Println("\nUnable to connect to cjdns:", e)
}
} else {
fmt.Println("\nError:", err)
}
return
}
return
}
// Check if a file exists
func fileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return false
}
// Attempt to read the .cjdnsadmin file from the users home directory
func loadCjdnsadmin() (cjdnsAdmin *admin.CjdnsAdminConfig, err error) {
sudo_user := os.Getenv("SUDO_USER")
var read_err error
var tUser *user.User
if sudo_user != "" {
tUser, read_err = user.Lookup(sudo_user)
if !fileExists(tUser.HomeDir + "/.cjdnsadmin") {
tUser, read_err = user.Current()
}
} else {
tUser, read_err = user.Current()
}
if read_err != nil {
return
}
cjdnsAdmin, err = readCjdnsadmin(tUser.HomeDir + "/.cjdnsadmin")
if err != nil {
return
}
return
}
// Fills out an IPv6 address to the full 32 bytes
// This shouldn't be needed in newer versions of cjdns
func padIPv6(ip net.IP) string {
raw := hex.EncodeToString(ip)
parts := make([]string, len(raw)/4)
for i := range parts {
parts[i] = raw[i*4 : (i+1)*4]
}
return strings.Join(parts, ":")
}
/*
// Dumps the entire routing table and structures it
func getTable(user *cjdns.Conn) cjdns.Routes {
response, err := user.NodeStore_dumpTable()
if err != nil {
fmt.Printf("%v\n", err)
return nil
}
return response
}
*/
/*
page := 0
var more int64
table = make([]*Route, 0)
for more = 1; more != 0; page++ {
response, err := user.NodeStore_dumpTable(page)
if err != nil {
fmt.Printf("%v\n", err)
return
}
// If an error field exists, and we have an error, return it
if _, ok := response["error"]; ok {
if response["error"] != "none" {
err = fmt.Errorf(response["error"].(string))
fmt.Printf("Error: %v\n", err)
return
}
}
//Thanks again to SashaCrofter for the table parsing
rawTable := response["routingTable"].([]interface{})
for i := range rawTable {
item := rawTable[i].(map[string]interface{})
rPath := item["path"].(string)
sPath := strings.Replace(rPath, ".", "", -1)
bPath, err := hex.DecodeString(sPath)
if err != nil || len(bPath) != 8 {
//If we get an error, or the
//path is not 64 bits, discard.
//This should also prevent
//runtime errors.
continue
}
path := binary.BigEndian.Uint64(bPath)
table = append(table, &Route{
IP: item["ip"].(string),
RawPath: path,
Path: rPath,
RawLink: item["link"].(int64),
Link: float64(item["link"].(int64)) / magicalLinkConstant,
Version: item["version"].(int64),
})
}
if response["more"] != nil {
more = response["more"].(int64)
} else {
break
}
}
return
*/
type Target struct {
Target string
Supplied string
}
func validIP(input string) (result bool) {
result, _ = regexp.MatchString(ipRegex, input)
return
}
func validPath(input string) (result bool) {
result, _ = regexp.MatchString(pathRegex, input)
return
}
func validHost(input string) (result bool) {
result, _ = regexp.MatchString(hostRegex, input)
return
}
// Sets target.Target to the requried IP or cjdns path
func setTarget(data []string, usePath bool) (target Target, err error) {
if len(data) == 0 {
err = fmt.Errorf("Invalid target specified")
return
}
input := data[0]
if input != "" {
if validIP(input) {
target.Supplied = data[0]
target.Target = padIPv6(net.ParseIP(input))
return
} else if validPath(input) && usePath {
target.Target = input
target.Supplied = data[0]
return
} else if validHost(input) {
var ips []string
ips, err = resolveHost(input)
if err != nil {
return
}
// Return the first result
for _, addr := range ips {
target.Target = addr
target.Supplied = input
return
}
} else {
err = fmt.Errorf("Invalid IPv6 address, cjdns path, or hostname")
return
}
}
if usePath {
err = fmt.Errorf("You must specify an IPv6 address, hostname, or cjdns path")
return
}
err = fmt.Errorf("You must specify an IPv6 address or hostname")
return
}
func usage() {
fmt.Println("cjdcmd version ", Version)
fmt.Println("")
fmt.Println("Usage: cjdcmd command [arguments]")
fmt.Println("")
fmt.Println("The commands are:")
fmt.Println("")
fmt.Println("ping <IPv6/DNS/Path> -- Preforms a cjdns ping to a specified node")
fmt.Println("route <IPv6/DNS/Path> -- Prints all routes to a specific node")
fmt.Println("traceroute <IPv6/DNS/Path> -- Performs a traceroute on a specific node by pinging")
fmt.Println(" each known hop to the tar on all known paths")
fmt.Println("ip <cjdns public key> -- Converts a cjdns public key to its corresponding")
fmt.Println(" IPv6 address")
fmt.Println("peers [<IPv6/DNS/Path>] -- Displays a list of currently connected peers for a")
fmt.Println(" node, is no node is specified your peers are shown.")
fmt.Println("host <IPv6/DNS> -- Returns a list of all known IP addresses for a")
fmt.Println(" specified hostname or the hostname for an address")
fmt.Println("hostname [new hypedns hostname] -- Without arguments, returns your HypeDNS hostname.")
fmt.Println(" Passing a new hostname will change your HypeDNS")
fmt.Println(" record")
fmt.Println("cjdnsadmin <-file /path/to/cjdroute.conf> -- Generates a .cjdnsadmin file in your home diectory")
fmt.Println(" using the specified cjdroute.conf as input")
fmt.Println("addpeer '<json peer details>' -- Adds the peer details to your config file")
fmt.Println("addpass [password] -- Adds the password to your config file, or generates")
fmt.Println(" one and then adds that")
fmt.Println("cleanconfig [-file] [-outfile] -- Strips all comments from the config file and saves")
fmt.Println(" it at outfile")
fmt.Println("log [-l level] [-logfile file] [-line] -- Prints cjdns logs to stdout")
fmt.Println("passgen [prefix] -- Generates a random alphanumeric password between 15 and")
fmt.Println(" 50 characters. If you provide [prefix], it will be")
fmt.Println(" prepended. This is to help you keep track of your")
fmt.Println(" peering passwords")
fmt.Println("dump -- Dumps the entire routing table to stdout")
fmt.Println("kill -- Gracefully kills cjdns")
fmt.Println("memory -- Returns the bytes of memory allocated by the router")
fmt.Println("")
fmt.Println("Use `cjdcmd --help` for a list of flags.")
fmt.Println("")
}
// Returns a random alphanumeric string where length is <= max >= min
func randString(min, max int) string {
r := myRand(min, max, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
return r
}
// Returns a random character from the specified string where length is <= max >= min
func myRand(min, max int, char string) string {
var length int
if min < max {
length = min + rand.Intn(max-min)
} else {
length = min
}
buf := make([]byte, length)
for i := 0; i < length; i++ {
buf[i] = char[rand.Intn(len(char)-1)]
}
return string(buf)
}
func stripComments(b []byte) ([]byte, error) {
regComment, err := regexp.Compile("(?s)//.*?\n|/\\*.*?\\*/")
if err != nil {
return nil, err
}
out := regComment.ReplaceAllLiteral(b, nil)
return out, nil
}