forked from inhies/cjdcmd
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cjdcmd.go
222 lines (190 loc) · 5.43 KB
/
cjdcmd.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
/*
* 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 (
"fmt"
"github.com/fc00/go-cjdns/admin"
"github.com/spf13/cobra"
"os"
"regexp"
)
const Version = "0.8.0"
func die(format string, msg ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", msg...)
os.Exit(1)
}
var (
ipRegex = regexp.MustCompile("^fc[a-f0-9]{1,2}:([a-f0-9]{0,4}:){2,6}[a-f0-9]{1,4}$")
pathRegex = regexp.MustCompile("([0-9a-f]{4}\\.){3}[0-9a-f]{4}")
hostRegex = regexp.MustCompile("^([a-zA-Z0-9]([a-zA-Z0-9\\-\\.]{0,}[a-zA-Z0-9]))$")
keyRegex = regexp.MustCompile("^[0-9a-z]*\\.k$")
)
var (
ConfFileIn, ConfFileOut string
AdminFileIn, AdminFileOut string
NmapOutput, Verbose, ReverseLookup bool
)
var (
rootCmd = &cobra.Command{Use: "cjdcmd"}
PingCmd = &cobra.Command{
Use: "ping <IPv6/DNS>",
Short: "Preforms a cjdns ping to a specified address.",
Run: pingCmd,
}
RouteCmd = &cobra.Command{
Use: "route <IPv6/DNS/Path>",
Short: "Prints all routes to a specific node",
Run: routeCmd,
}
TracerouteCmd = &cobra.Command{
Use: "traceroute <IPv6/DNS/Path>",
Short: "Performs a traceroute on a specific node by pinging each known hop to the target on all known paths",
Run: tracerouteCmd,
}
PubKeyToIPCmd = &cobra.Command{
Use: "ip <cjdns public key>",
Short: "Converts a cjdns public key to its corresponding IPv6 address.",
Run: pubKeyToIPCmd,
}
PeersCmd = &cobra.Command{
Use: "peers [<IPv6/DNS/Path>]",
Short: "Displays a list of currently connected peers for a node, if no node is specified your peers are shown.",
Run: peersCmd,
}
HostCmd = &cobra.Command{
Use: "host <IPv6/DNS>",
Short: "Returns a list of all known IP addresses for a specified hostname or the hostname for an address.",
Run: hostCmd,
}
CjdnsAdminCmd = &cobra.Command{
Use: "cjdnsadmin <-file /path/to/cjdroute.conf>",
Short: "Generates a .cjdnsadmin file in your home diectory using the specified cjdroute.conf as input",
Run: cjdnsAdminCmd,
}
AddPeerCmd = &cobra.Command{
Use: "addpeer '<json peer details>'",
Short: "Adds the peer details to your config file",
Long: "You must enter the peering details surrounded by single qoutes '<peer details>'",
Run: addPeerCmd,
}
AddPasswordCmd = &cobra.Command{
Use: "addpass [password]",
Short: "Adds the password to your config file, or generates one and then adds that",
Run: addPasswordCmd,
}
ListPasswordCmd = &cobra.Command{
Use: "listpass",
Short: "ALPHA FEATURE - List currently loaded peering passwords.",
Run: listPasswordCmd,
}
CleanConfigCmd = &cobra.Command{
Use: "cleanconfig [-file] [-outfile]",
Short: "Strips all comments from the config file and saves it at outfile",
Run: cleanConfigCmd,
}
LogCmd = &cobra.Command{
Use: "log [--level level] [--file file] [--line]",
Short: "Prints cjdns logs to stdout",
Run: logCmd,
}
PassGenCmd = &cobra.Command{
Use: "passgen [prefix]",
Short: "Generates a random alphanumeric password between 15 and 50 characters. If you provide [prefix], it will be prepended. This is to help you keep track of your peering passwords",
Run: passGenCmd,
}
DumpCmd = &cobra.Command{
Use: "dump",
Short: "Dumps the entire routing table to stdout.",
Run: dumpCmd,
}
KillCmd = &cobra.Command{
Use: "kill",
Short: "Gracefully kills cjdns",
}
NickCmd = &cobra.Command{
Use: "nick <IPv6/DNS>",
Short: "Scrape HyperIRC for nicks using host",
Run: nickCmd,
}
ConnectCmd = &cobra.Command{
Use: "connect PUBLIC_KEY HOST:PORT [ PASSWORD ] [ INTERFACE_INDEX ]",
Short: "Connect directly to another node over UDP",
Run: connectCmd,
}
/*
ListenCmd = &cobra.Command {
Use: "listen [ADDRESS:]PORT",
Short: "Create a new UDP interface",
Run: listenCmd,
}
*/
InfoCmd = &cobra.Command{
Use: "info HOST [ HOST ... ]",
Short: "Show node information",
Run: infoCmd,
}
TrafficCmd = &cobra.Command{
Use: "traffic",
Short: "Show traffic statistics",
Run: trafficCmd,
}
)
func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolVarP(&ReverseLookup, "resolve", "r", true, "reverse resolve IP addresses")
rootCmd.AddCommand(
PingCmd,
RouteCmd,
TracerouteCmd,
PubKeyToIPCmd,
PeersCmd,
HostCmd,
CjdnsAdminCmd,
AddPeerCmd,
AddPasswordCmd,
ListPasswordCmd,
CleanConfigCmd,
LogCmd,
PassGenCmd,
DumpCmd,
KillCmd,
NickCmd,
ConvertCmd,
ConnectCmd,
//ListenCmd,
InfoCmd,
TrafficCmd,
FingerprintCmd,
)
}
func main() {
rootCmd.Execute()
/*
c, _, err := rootCmd.Find(os.Args[1:])
if err != nil {
infoCmd(InfoCmd, os.Args[1:])
} else {
c.Execute()
}
*/
}
// Connect connects to
func Connect() *admin.Conn {
admin, err := admin.Connect(nil)
if err != nil {
fmt.Fprintln(os.Stderr, "Could not connect to cjdns:", err)
os.Exit(1)
}
return admin
}