-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
422 lines (336 loc) · 11.6 KB
/
index.ts
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
import express from "express"
import cors from "cors"
import fs from "fs"
import path from "path"
import https from "https"
import http from "http"
import tls from "tls"
import ths from "ths"
import web3 from "web3"
import greenlock from "greenlock"
import enshash from "@ensdomains/content-hash"
import rateLimiter from "express-rate-limit"
import * as ipfs from "ipfs-http-client"
import * as tarfs from "tar-fs"
import { CID } from 'multiformats/cid'
import { Readable } from 'node:stream'
import config from './constants/config.js'
import message from './constants/msgs.js'
import * as types from './constants/types.js'
const NODE_ENDPOINT = new URL(config.IFPS_NODE_ENDPOINT)
const SSL_REGISTRY = greenlock.create(config.GLOCK_CONFIG)
const IPFS_NODE = ipfs.create({ url: NODE_ENDPOINT })
const ONION_SERVER = express()
const TOR_NODE = new ths()
const SERVER = express()
const PUBLIC = express()
async function startResolver() {
const cachePath = path.join(path.resolve(), 'cache')
if(!fs.existsSync(cachePath)) {
fs.mkdirSync(cachePath)
}
await SSL_REGISTRY.manager.defaults(config.GLOCK_DEFAULTS)
await TOR_NODE.setTorCommand(config.TOR_PATH)
await TOR_NODE.setBridges(config.TOR_BRIDGES)
await TOR_NODE.start()
await TOR_NODE.loadConfig()
}
async function getContentHash(label: string): Promise<types.ContentHash> {
const provider = new web3(config.RPC_PROVIDER)
const ensContentHash = await provider.eth.ens.getContenthash(label)
const { cidV0ToV1Base32} = enshash.helpers
const contentHashPath = ensContentHash.decoded
if(ensContentHash.protocolType) {
return {
type: 'ipfs',
payload: CID.parse(cidV0ToV1Base32(contentHashPath))
}
} else {
const ipnsNameHash = enshash.decode(contentHashPath)
const ipnsRecord = await IPFS_NODE.name.resolve(ipnsNameHash)
let ipnsFields: Array<string> = []
for await(const e of ipnsRecord) {
ipnsFields = ipnsFields.concat(e)
}
const ipnsIpfsv0Hash = ipnsFields[0].split('/ipfs/')[1]
return {
payload: CID.parse(cidV0ToV1Base32(ipnsIpfsv0Hash)),
type: 'ipns'
}
}
}
async function createWildcardCertificate(domainPath: string) {
const wildcardAddress = `*.${domainPath}`
await SSL_REGISTRY.add({ subject: wildcardAddress, altnames: [ wildcardAddress ],
challenges: {
"dns-01": {
module: "acme-dns-01-namecheap",
propogationDelay: 200000,
...config.DNS_CHALLENGE_PARAMS
}
}
})
}
async function createCertificate(onionPath: string) {
await SSL_REGISTRY.add({ subject: onionPath, altnames: [ onionPath ],
challenges: {
"http-01": {
module: "acme-http-01-webroot",
webroot: "/var/www/.well-known/acme-challenge"
}
}
})
}
async function getCertificate(domainPath: string): Promise<types.CertKeypair | undefined> {
try {
const matchingRequest = await SSL_REGISTRY.get({ servername: domainPath })
if (matchingRequest?.pems) {
return {
key: matchingRequest.pems.privkey,
cert: matchingRequest.pems.cert
}
}
} catch (e) {
try {
let wildcardPaths = domainPath.split('.')
wildcardPaths.shift()
const wildcardRootPath = wildcardPaths.join('.')
const wildcardAddress = wildcardRootPath || domainPath
const secondaryRequest = await SSL_REGISTRY.get({ servername: `*.${wildcardAddress}` })
if(secondaryRequest?.pems) {
return {
key: secondaryRequest.pems.privkey,
cert: secondaryRequest.pems.cert
}
}
} catch (e) { }
}
return undefined
}
async function pinIPFSContent(contentHash: types.CIDContentHash) {
await IPFS_NODE.pin.add(contentHash.toString())
}
async function unpinIPFSContent(contentHash: types.CIDContentHash) {
await IPFS_NODE.pin.rm(contentHash.toString())
}
function contentHashToCID(contentHash: string): types.CIDContentHash {
const ipfsCIDv1 = enshash.helpers.cidV0ToV1Base32(contentHash)
return CID.parse(ipfsCIDv1)
}
async function getIPFSContent(contentHash: types.CIDContentHash): Promise<Buffer> {
const stream = await IPFS_NODE.get(contentHash.toString())
const chunks = []
for await(const x of stream) { chunks.push(x) }
return Buffer.concat(chunks)
}
async function cacheIPFSContent(
contentPath: string,
contentBuffer: Buffer
) {
const completeCallback = () => {
const doesExist = fs.existsSync(contentPath)
if (doesExist) {
const isDir = fs.lstatSync(contentPath).isDirectory()
if (!isDir) {
fs.renameSync(contentPath, contentPath + '.html')
}
}
}
const bufferStream = Readable.from(contentBuffer)
bufferStream.pipe(tarfs.extract('./cache/', {
finalize: true, finish: completeCallback
},
completeCallback
))
}
async function deleteCacheContent(contentPath: string) {
try {
await fs.rmSync(contentPath, { recursive: true, force: true })
} catch (e) {}
try {
await fs.rmSync(contentPath + '.html')
} catch (e) {}
}
async function isPinnedContent(contentHash: types.CIDContentHash): Promise<boolean> {
const clientRequest = await IPFS_NODE.pin.ls()
let pinnedContent: Array<{ cid: types.CIDContentHash, type: string }> = []
for await(const e of clientRequest) { pinnedContent = pinnedContent.concat(e) }
const isPinned = pinnedContent.find((e) => e.cid.toString() === contentHash.toString())
return isPinned !== undefined
}
async function createHiddenService(contentHash: types.CIDContentHash) {
await TOR_NODE.createHiddenService(contentHash.toString(), [ config.ONION_PORT ], true)
await TOR_NODE.saveConfig()
}
async function getHiddenService(contentHash: types.CIDContentHash): Promise<string> {
const targetContentHash = contentHash.toString().toLowerCase()
let onionAddress
try {
await TOR_NODE.loadConfig()
let activeServices = await TOR_NODE.getServices()
let matchingService = activeServices.find((e:any) => e.name == targetContentHash)
if(!matchingService) {
await createHiddenService(targetContentHash)
await TOR_NODE.loadConfig()
activeServices = await TOR_NODE.getServices()
matchingService = activeServices.find((e:any) => e.name == targetContentHash)
}
onionAddress = matchingService.hostname
} catch (e) { }
return onionAddress
}
function parseENSDomain(hostname: string): string {
let ensLabel = hostname.split('.3th.ws')[0]
if (hostname.includes('onion.')) {
ensLabel = ensLabel.split('onion.')[1]
}
return ensLabel
}
async function getContentHashFromOnionAddress(onionAddress: string): Promise<string | undefined> {
try {
await TOR_NODE.loadConfig()
const activeServices = await TOR_NODE.getServices()
const matchingService = activeServices.find((e:any) => e.hostname == onionAddress.toLowerCase())
return matchingService.name
} catch (e) {
return undefined
}
}
async function reverseOnionPropogation(
req: types.Request,
res: types.Response
) {
const ipfsHash = await getContentHashFromOnionAddress(req.hostname)
if(ipfsHash) {
const onionPath = path.join(path.resolve(), 'cache', ipfsHash)
const isStaticDir = fs.existsSync(onionPath + '/index.html')
const isStatic = fs.existsSync(onionPath + '.html')
const hasStaticContent = isStaticDir || isStatic
const servePath = isStaticDir ? onionPath : `${path.resolve()}/cache/`
const fileName = isStaticDir ? 'index' : ipfsHash
ONION_SERVER.use(express.static(servePath))
res.sendFile(`${fileName}.html`, { root: servePath })
} else {
res.send(message.ERR_REVERSE_ONION)
}
return
}
async function wildcardPropogation(
req: types.Request,
res: types.Response
) {
const hostName = req.hostname
const serveOnions = hostName?.includes('onion.')
try {
const ensLabel = parseENSDomain(hostName)
const ensDomain = ensLabel + '.eth'
const { type, payload } = await getContentHash(ensDomain)
const ipfsContentHash = payload
if (!(type === 'ipfs' || type === 'ipns')) {
res.send(message.ERR_UNSUPPORTED_TYPE)
return
}
const isCached = await isPinnedContent(ipfsContentHash)
const contentPath = path.join(
path.resolve(), 'cache', ipfsContentHash.toString()
)
let isStaticDir = fs.existsSync(contentPath + '/index.html')
let isStatic = fs.existsSync(contentPath + '.html')
let hasStaticContent = isStaticDir || isStatic
if (!isCached && !hasStaticContent) {
const ipfsContent = await getIPFSContent(ipfsContentHash)
if (ipfsContent.length === 0) {
res.send(message.ERR_FAILED_FETCH)
return
} else {
await cacheIPFSContent(contentPath, ipfsContent)
isStaticDir = fs.existsSync(contentPath + '/index.html')
isStatic = fs.existsSync(contentPath + '.html')
hasStaticContent = isStaticDir || isStatic
}
}
if (!hasStaticContent) {
await deleteCacheContent(contentPath)
res.send(message.ERR_NO_STATIC_CONTENT)
return
} else if (!isCached) {
await pinIPFSContent(ipfsContentHash)
}
if (serveOnions) {
const onionAddress = await getHiddenService(ipfsContentHash)
if(onionAddress) {
res.redirect(301, `https://${onionAddress}`)
} else {
res.send(message.ERR_NO_ONION_SERVICE)
}
return
} else {
const cachePath = path.resolve() + '/cache/'
const servePath = isStaticDir ? contentPath : cachePath
const fileName = isStaticDir ? 'index' : ipfsContentHash.toString()
SERVER.use(express.static(servePath))
res.sendFile(`${fileName}.html`, { root: servePath })
return
}
} catch (e) {
res.send(message.ERR_RESOLVE_CONFLICT)
console.log(e)
return
}
}
async function handleCertificate(hostName: string, ctx: Function) {
const hostPaths = hostName.split('.')
const isFirstLevelDomain = hostPaths[2] == "ws" || hostName[1] == "ws"
const isRootDomain = hostPaths[1] == "ws" || isFirstLevelDomain
const isOnionAddress = hostPaths[hostPaths.length - 1] == 'onion'
const isDomain = hostName.includes('3th.ws')
const defaultKeypair = {
cert: fs.readFileSync(config.PATH_SSL_CERT),
key: fs.readFileSync(config.PATH_SSL_KEY)
}
let sslKeypair = defaultKeypair
if (!isRootDomain && isDomain) {
let cachedCertificate = await getCertificate(hostName)
if (!cachedCertificate) {
await createCertificate(hostName)
cachedCertificate = await getCertificate(hostName)
}
if (cachedCertificate) {
sslKeypair = cachedCertificate
}
}
const selectedContext = tls.createSecureContext({ ...sslKeypair })
ctx(null, selectedContext)
}
const RATE_LIMITER = rateLimiter({
windowMs: 10 * 60 * 1000,
max: 50,
message: "TOO MANY REQUESTS",
standardHeaders: true,
legacyHeaders: false
})
ONION_SERVER.use(RATE_LIMITER)
SERVER.use(RATE_LIMITER)
SERVER.use(express.urlencoded({ extended: true }))
SERVER.use(express.json())
SERVER.use(express.raw())
SERVER.use(cors())
ONION_SERVER.get('/', reverseOnionPropogation)
SERVER.get('/', wildcardPropogation)
const RESOLVER_CONFIG = { SNICallback: handleCertificate }
const HTTPS_CONFIG = {
key: fs.readFileSync(config.PATH_SSL_KEY),
cert: fs.readFileSync(config.PATH_SSL_CERT)
}
const HIDDEN_SERVICE = https.createServer(HTTPS_CONFIG, ONION_SERVER)
const RESOLVER = https.createServer(RESOLVER_CONFIG, SERVER)
RESOLVER.listen(443, async() => {
try {
await HIDDEN_SERVICE.listen(config.ONION_PORT)
await startResolver()
console.log(message.SERVER_INIT)
} catch (e) {
console.log(message.SERVER_FAIL)
console.log(e)
}
})