forked from ZeroNetJS/zeronet-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zeronet.js
executable file
·207 lines (181 loc) · 5.05 KB
/
zeronet.js
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
#!/usr/bin/env node
"use strict"
require("colors")
let node
let dwait = require("./lib/hacky-logs.js")
const fs = require("fs")
const path = require("path")
const mkdirp = require("mkdirp")
const MergeRecursive = require("merge-recursive").recursive
const ZeroNet = require("zeronet-node")
const FS = require("zeronet-storage-fs")
const Common = require("./lib/common")
let dir = require("./lib/storage-dir")()
mkdirp.sync(dir)
mkdirp.sync(path.join(dir, "logs"))
let cm
const TCP = require('libp2p-tcp')
const WS = require("libp2p-websockets")
const defaults = {
id_expire: 1000 * 60 * 60 * 24 * 7, //approx 1 week
modules: {
uiserver: require("zeronet-uiserver"),
nat: require("zeronet-swarm/lib/zero/nat")
},
swarm: {
zero: {
listen: [
"/ip4/0.0.0.0/tcp/15543"
],
transports: [
new TCP()
],
trackers: [
//"zero://boot3rdez4rzn36x.onion:15441",
//"zero://boot.zeronet.io#f36ca555bee6ba216b14d10f38c16f7769ff064e0e37d887603548cc2e64191d:15441",
"udp://tracker.coppersurfer.tk:6969",
"udp://tracker.leechers-paradise.org:6969",
"udp://9.rarbg.com:2710",
"http://tracker.opentrackr.org:1337/announce",
"http://explodie.org:6969/announce",
"http://tracker1.wasabii.com.tw:6969/announce"
//"http://localhost:25534/announce"
],
crypto: [
require("zeronet-crypto/secio"),
//require("zeronet-crypto/tls").tls_rsa
],
nat: true
},
libp2p: {
listen: [
"/ip4/0.0.0.0/tcp/15542",
"/ip6/::/tcp/15542",
"/p2p-websocket-star",
"/ip4/0.0.0.0/tcp/15540/ws",
"/ip6/::/tcp/15540/ws"
],
transports: [
new TCP(),
new WS()
],
bootstrap: require("./bootstrappers").libp2p,
mdns: true,
dht: false,
wstar: require("./bootstrappers").wstar,
wstar_ignore: true
}
},
uiserver: {
listen: {
host: "127.0.0.1",
port: 15544
}
},
common: cm = new Common({
debug_file: path.resolve(dir, path.join("logs", "debug.log")),
debug_shift_file: path.resolve(dir, path.join("logs", "debug-last.log")),
debug: !!process.env.DEBUG
}),
storage: new FS(path.join(dir, "data"))
}
cm.logger("node")("Starting...")
cm.title("ZeroNetJS - Starting...")
const errCB = err => {
if (!err && process.env.TESTOK) process.emit("SIGINT")
if (!err) {
cm.title("ZeroNetJS")
return node.logger("node")("Started successfully")
}
cm.logger("node").fatal("The node failed to start")
cm.logger("node").fatal(err)
process.exit(2)
}
process.on('uncaughtException', err => {
console.error(err.stack)
cm.logger("node").fatal("FATAL ERROR")
cm.logger("node").fatal(err)
process.nextTick(() => process.exit(2))
})
const confpath = path.resolve(dir, process.env.CONFIG_FILE || "config.json")
const idpath = path.resolve(dir, process.env.ID_FILE || "id.json")
const readJSON = path => JSON.parse(fs.readFileSync(path).toString())
const writeJSON = (path, data) => fs.writeFileSync(path, new Buffer(JSON.stringify(data)))
let config
if (fs.existsSync(confpath)) {
const config_data = readJSON(confpath)
config = MergeRecursive(defaults, config_data)
} else config = defaults
let exiting
function exit(code) {
/**
@namespace exit
@constructor
@private
*/
if (!node) {
cm.logger("node").error("Stopping before started!")
exiting = true;
["error", "warn"].forEach(k => console.error[k] = console.error)
node = {
logger: () => console.error
}
exit(2)
}
if (exiting) {
node.logger("node").warn("Force stop!")
return process.nextTick(() => process.exit(2))
}
exiting = true
node.logger("node")("Stopping...")
node.logger("node")("Press ^C to force stop")
node.stop(err => {
if (err) {
cm.logger("node").error(err)
cm.logger("node").error("FAILED TO QUIT GRACEFULLY")
throw err
}
node.logger("node")("Stopped")
process.exit(code || 0)
})
}
if (!process.env.IGNORE_SIG)["SIGTERM", "SIGINT", "SIGUSR2"].forEach(sig => process.on(sig, exit))
const Id = require("peer-id")
const liftoff = (err, id) => {
if (err) return errCB(err)
config.id = id
node = new ZeroNet(config)
dwait.map(d => d())
dwait = null
node.start(errCB)
}
const createAndSaveID = r => {
cm.logger("id")("%s ID... This may take a few seconds...", r ? "Changing" : "Creating")
if (r) cm.logger("id")("(The ID is changed every %s seconds to improve anonymity)", config.id_expire)
Id.create({
bits: 2048
}, (err, id) => {
if (err) return errCB(err)
cm.logger("id")("Created ID %s!", id.toB58String())
try {
writeJSON(idpath, {
id,
created_at: new Date().getTime()
})
liftoff(null, id)
} catch (e) {
liftoff(e)
}
})
}
try {
if (fs.existsSync(idpath)) {
const id = readJSON(idpath)
if ((id.created_at || 0) + config.id_expire < new Date().getTime())
createAndSaveID(true)
else
Id.createFromJSON(id.id, liftoff)
} else createAndSaveID()
} catch (e) {
liftoff(e)
}