-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
37 lines (29 loc) · 1.03 KB
/
app.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
var httpProxy = require('http-proxy')
var http = require('http')
var base32 = require('base32.js')
var base58 = require('base58-native')
var proxy = httpProxy.createProxyServer()
proxy.on('error', function(e) {
console.log('proxy error: '+e)
})
// sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 31337
http.createServer(function(req, res) {
var hshcaRegexFailure = false
try {
var hshca = req.headers.host.match(/^(.+)\.ipfs\.neocitiesops\.net$/i)[1]
} catch(e) {
console.log(e)
hshcaRegexFailure = true
}
if(hshcaRegexFailure) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Invalid HSHCA hash for archive lookup\n')
} else {
var decoder = new base32.Decoder({ type: "rfc4648" })
var multihash = decoder.write(hshca.toUpperCase()).finalize()
var ipfsHash = base58.encode(multihash)
console.log(ipfsHash)
proxy.web(req, res, { target: 'http://127.0.0.1:8080/ipfs/'+ipfsHash })
}
}).listen(31337, '0.0.0.0')
console.log('Server running')