-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd.go
240 lines (210 loc) · 5.63 KB
/
cmd.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
package cmd
import (
"encoding/hex"
"fmt"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/ryogrid/nostrp2p/api_server"
"github.com/ryogrid/nostrp2p/core"
"github.com/ryogrid/nostrp2p/glo_val"
"github.com/ryogrid/nostrp2p/np2p_util"
"github.com/ryogrid/nostrp2p/transport"
"github.com/spf13/cobra"
"github.com/weaveworks/mesh"
"io/ioutil"
"log"
"net"
"os"
"strconv"
"strings"
)
var listenAddrPort = "127.0.0.1:20000"
var bootPeerAddrPort = ""
var publicKey = ""
var writable = true
var debug = false
var isEnabledSSL = false
var rootCmd = &cobra.Command{
Use: "nostrp2p",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("nostrp2p v0.0.1")
},
}
var serverCmd = &cobra.Command{
Use: "server",
Short: "Startup server.",
Run: func(cmd *cobra.Command, args []string) {
if !writable {
glo_val.DenyWriteMode = true
}
if debug {
np2p_util.DebugMode = true
}
peers := &np2p_util.Stringset{}
if bootPeerAddrPort != "" {
peers.Set(bootPeerAddrPort)
}
logger := log.New(os.Stderr, "> ", log.LstdFlags)
host, portStr, err := net.SplitHostPort(listenAddrPort)
if err != nil {
logger.Fatalf("SplitHostPort error: %s: %v", listenAddrPort, err)
}
port, err := strconv.Atoi(portStr)
if err != nil {
logger.Fatalf("port sting coversion error: %s: %v", listenAddrPort, err)
}
fmt.Println("public key: ", publicKey)
var hexPubKeyStr string
var pubKeyBytes []byte
var err2 error
if strings.HasPrefix(publicKey, "npub") {
_, tmpPubKeyVal, err2_ := nip19.Decode(publicKey)
if err2_ != nil {
logger.Fatalf("public key: %s: %v", publicKey, err)
}
hexPubKeyStr = tmpPubKeyVal.(string)
pubKeyBytes, err2 = hex.DecodeString(hexPubKeyStr)
} else {
hexPubKeyStr = publicKey
pubKeyBytes, err2 = hex.DecodeString(publicKey)
if err2 != nil {
logger.Fatalf("public key: %s: %v", publicKey, err)
}
}
var tmpArr [32]byte
copy(tmpArr[:], pubKeyBytes[:32])
glo_val.SelfPubkeyStr = hexPubKeyStr
glo_val.SelfPubkey = &tmpArr
glo_val.SelfPubkey64bit = np2p_util.GetUint64FromHexPubKeyStr(hexPubKeyStr)
if isEnabledSSL {
glo_val.IsEnabledSSL = true
fmt.Println("REST I/F is offered over SSL")
}
// initialize rand generator
np2p_util.InitializeRandGen(-1 * int64(glo_val.SelfPubkey64bit))
// mesh library's peer ID is 6 bytes uint64 (MeshTransport only restriction)
peerId := np2p_util.Get6ByteUint64FromHexPubKeyStr(hexPubKeyStr)
peer := core.NewPeer(peerId, logger)
fmt.Println(fmt.Sprintf("%x", *glo_val.SelfPubkey), fmt.Sprintf("%x", peerId))
setupMeshTransport := func() *mesh.Router {
router, err := mesh.NewRouter(mesh.Config{
Host: host,
Port: port,
ProtocolMinVersion: mesh.ProtocolMaxVersion,
Password: nil,
ConnLimit: 64,
PeerDiscovery: true,
TrustedSubnets: []*net.IPNet{},
}, mesh.PeerName(peerId), "", mesh.NullOverlay{}, log.New(ioutil.Discard, "", 0))
if err != nil {
logger.Fatalf("Could not create router: %v", err)
}
tport := transport.NewMeshTransport(peer)
gossip, err := router.NewGossip("nostrp2p", tport)
tport.Register(gossip)
peer.MessageMan.SetTransport(tport)
if err != nil {
logger.Fatalf("Could not create gossip: %v", err)
}
//peer.Register(gossip)
go func() {
logger.Printf("mesh router starting (%s)", listenAddrPort)
router.Start()
}()
router.ConnectionMaker.InitiateConnections(peers.Slice(), true)
tport.SetRouter(router)
return router
}
router := setupMeshTransport()
defer func() {
logger.Printf("mesh router stopping")
router.Stop()
}()
apiServ := api_server.NewApiServer(peer)
go apiServ.LaunchAPIServer(host + ":" + strconv.Itoa(port+1))
np2p_util.OSInterrupt()
},
}
var genkeyCmd = &cobra.Command{
Use: "genkey",
Short: "Generate new key pair.",
Run: func(cmd *cobra.Command, args []string) {
sk := nostr.GeneratePrivateKey()
pk, _ := nostr.GetPublicKey(sk)
nsec, _ := nip19.EncodePrivateKey(sk)
npub, _ := nip19.EncodePublicKey(pk)
fmt.Println("Secret Key:")
fmt.Println(nsec)
fmt.Println("Secret Key (In Hex Representation): ")
fmt.Println(sk)
fmt.Println("Public Key:")
fmt.Println(npub)
fmt.Println("Public key (In Hex Representation): ")
fmt.Println(pk)
fmt.Println()
fmt.Println("Please keep the secret key secret.")
fmt.Println("The key is used only at NostrP2P client as your identity.")
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize()
serverCmd.Flags().StringVarP(
&listenAddrPort,
"listen-addr-port",
"l",
"127.0.0.1:20000",
"Address and port to bind to (optional)",
)
serverCmd.Flags().StringVarP(
&bootPeerAddrPort,
"boot-peer-addr-port",
"b",
"",
"Address and port of a server which already joined nostrp2p network (optional)",
)
serverCmd.Flags().StringVarP(
&publicKey,
"public-key",
"p",
"",
"Your public key (required)",
)
serverCmd.MarkFlagRequired("public-key")
//serverCmd.Flags().StringVarP(
// &nickname,
// "Your nickname on nostrp2p (required)",
// "n",
// "",
// "Port to forward",
//)
//serverCmd.MarkFlagRequired("nickname")
serverCmd.Flags().BoolVarP(
&writable,
"writable",
"w",
true,
"Whether handle write request (default: true)",
)
serverCmd.Flags().BoolVarP(
&debug,
"debug",
"d",
false,
"If true, debug log is output to stderr (default: false)",
)
serverCmd.Flags().BoolVarP(
&isEnabledSSL,
"ssl",
"s",
false,
"If true, REST I/F is offered over SSL (default: false)",
)
rootCmd.AddCommand(serverCmd)
rootCmd.AddCommand(genkeyCmd)
}