-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (132 loc) · 4.01 KB
/
index.js
File metadata and controls
158 lines (132 loc) · 4.01 KB
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
var express = require("express");
var app = express();
var http = require('http');
var https = require('https');
const fs = require('fs')
var server;
if(!process.env.PORT){
process.env.PORT = 3000;
var cert;
try{
server = https.createServer({
key: fs.readFileSync('_._server.key'),
cert: cert = fs.readFileSync('_._server.cert')
}, app);
}catch(e){}
if(!cert){
console.log("need to run this command in terminal in project dir", __dirname);
console.log("' $ openssl req -nodes -new -x509 -keyout _._server.key -out _._server.cert '");
server = http.createServer(app);
}
}else{
server = http.createServer(app);
}
var dirname = __dirname + "/docs";
app.use((req, res, next) => {
var u = req.url.split("?")[0];
var q = req.url.split("?")[1];
switch (u) {
case "/":
req.url = getFile("/index");
break;
case "/beta7/":
res.redirect("/past_releases/beta7/");
break;
case "/search":
case "/encrypt":
case "/decrypt":
case "/encrypt-file":
case "/decrypt-file":
case "/password-generator":
res.redirect("/app"+u+ (q && q.length ? "?"+req.url.split("?")[1] : ""));
break;
default:
req.url = getFile(u)+(q && q.length ? "?"+req.url.split("?")[1] : "");
break;
}
next();
});
function getFile(filePath, prev) { // /filename = /filename || /filename.html || /filename-dev.html
try {
if (fs.existsSync(dirname + filePath)) {
return filePath;
}
else throw new Error("Not Found");
}
catch (e) {
try {
if (fs.existsSync(dirname + filePath + ".html")) {
return filePath + ".html";
}
else throw new Error("Not Found");
}
catch (e) {
try {
if (fs.existsSync(dirname + filePath + "-dev.html")) {
return filePath + "-dev.html";
}
else throw new Error("Not Found");
}
catch (e) {
// if(!prev)
// return getFile("/index", filePath);
// else return prev;
return filePath;
}
}
}
}
app.use(express.static(dirname));
app.get('/protonmail/:action/:address_id*', (req, res, next) => {
var address_id = req.params.address_id;
var action = req.params.action;
var url;
switch (action) {
case "get":
url = 'https://api.protonmail.ch/pks/lookup?op=' + action + '&search=' + address_id;
break;
case "index":
url = 'https://api.protonmail.ch/pks/lookup?op=' + action + '&search=' + address_id;
break;
default:
return next();
}
res.set("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.set('Content-Type', 'text/plain');
//https://api.protonmail.ch/pks/lookup?op=index&search=bmatusiak@protonmail.com
//https://api.protonmail.ch/pks/lookup?op=get&search=bmatusiak@protonmail.com
//https://api.protonmail.ch/pks/lookup?op=index&search=0x0acad543e682989d <--keyid
//https://api.protonmail.ch/pks/lookup?op=get&search=0x0acad543e682989d <--keyid
//https://api.protonmail.ch/pks/lookup?op=index&search=0x0396d70ff10f3c9175a76f0a149a3dc46e2e6313
//https://api.protonmail.ch/pks/lookup?op=get&search=0x0396d70ff10f3c9175a76f0a149a3dc46e2e6313
https.get(url, (resp) => {
var data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
if (action == "get" && data.slice(0, 10) != '-----BEGIN') {
return res.status(404).send('Not found');
}
res.send(data);
});
}).on("error", () => {
next();
});
});
var $server = server.listen(process.env.PORT, () => {
console.log('listening on *:' + process.env.PORT);
});
var Gun = require('gun');
Gun.log = function() {};
Gun.log.once = function() {};
require('gun/axe'); // is there a GUN BUG with this?
require('gun/nts'); // is there a GUN BUG with this?
var gunOptions = {
file: 'radata',
web: $server,
super: false,
stats: true
};
var gun = Gun(gunOptions);