-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathindex.js
executable file
·188 lines (162 loc) · 4.66 KB
/
index.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
// Example of the server https is taken from here: https://engineering.circle.com/https-authorized-certs-with-node-js-315e548354a2
// Conversion of client1-crt.pem to certificate.pfx: https://stackoverflow.com/a/38408666/4637638
const express = require('express')
const https = require('https')
const cors = require('cors')
const auth = require('basic-auth')
const app = express()
const appHttps = express()
const appAuthBasic = express()
const fs = require('fs')
const path = require('path')
var options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-crt.pem'),
ca: fs.readFileSync('ca-crt.pem'),
requestCert: true,
rejectUnauthorized: false
};
appHttps.get('/', (req, res) => {
console.log(JSON.stringify(req.headers))
const cert = req.connection.getPeerCertificate()
// The `req.client.authorized` flag will be true if the certificate is valid and was issued by a CA we white-listed
// earlier in `opts.ca`. We display the name of our user (CN = Common Name) and the name of the issuer, which is
// `localhost`.
if (req.client.authorized) {
res.send(`
<html>
<head>
</head>
<body>
<h1>Authorized</h1>
</body>
</html>
`);
// They can still provide a certificate which is not accepted by us. Unfortunately, the `cert` object will be an empty
// object instead of `null` if there is no certificate at all, so we have to check for a known field rather than
// truthiness.
} else if (cert.subject) {
console.log(`Sorry ${cert.subject.CN}, certificates from ${cert.issuer.CN} are not welcome here.`);
res.status(403).send(`
<html>
<head>
</head>
<body>
<h1>Forbidden</h1>
</body>
</html>
`);
// And last, they can come to us with no certificate at all:
} else {
console.log(`Sorry, but you need to provide a client certificate to continue.`)
res.status(401).send(`
<html>
<head>
</head>
<body>
<h1>Unauthorized</h1>
</body>
</html>
`);
}
res.end()
})
// Let's create our HTTPS server and we're ready to go.
https.createServer(options, appHttps).listen(4433)
// Ensure this is before any other middleware or routes
appAuthBasic.use((req, res, next) => {
let user = auth(req)
if (user === undefined || user['name'] !== 'USERNAME' || user['pass'] !== 'PASSWORD') {
res.statusCode = 401
res.setHeader('WWW-Authenticate', 'Basic realm="Node"')
res.send(`
<html>
<head>
</head>
<body>
<h1>Unauthorized</h1>
</body>
</html>
`);
res.end()
} else {
next()
}
});
appAuthBasic.use(express.static(__dirname + '/public'));
appAuthBasic.get("/", (req, res) => {
console.log(JSON.stringify(req.headers))
res.send(`
<html>
<head>
</head>
<body>
<h1>Authorized</h1>
</body>
</html>
`);
res.end()
});
appAuthBasic.get('/test-index', (req, res) => {
res.sendFile(__dirname + '/public/test-index.html');
});
appAuthBasic.listen(8081);
// Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded());
app.use(cors());
// Parse JSON bodies (as sent by API clients)
app.use(express.json());
app.use(express.static(__dirname + '/public'));
app.get("/", (req, res) => {
console.log(JSON.stringify(req.headers))
res.send(`
<html>
<head>
</head>
<body>
<p>HELLO</p>
</body>
</html>
`);
res.end()
})
app.get('/test-index', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
})
app.post("/test-post", (req, res) => {
console.log(JSON.stringify(req.headers))
console.log(JSON.stringify(req.body))
res.send(`
<html>
<head>
</head>
<body>
<p>HELLO ${req.body.name}!</p>
</body>
</html>
`);
res.end()
})
app.post("/test-ajax-post", (req, res) => {
console.log(JSON.stringify(req.headers))
console.log(JSON.stringify(req.body))
res.set("Content-Type", "application/json")
res.send(JSON.stringify({
"firstname": req.body.firstname,
"lastname": req.body.lastname,
"fullname": req.body.firstname + " " + req.body.lastname,
}))
res.end()
})
app.get("/test-download-file", (req, res) => {
console.log(JSON.stringify(req.headers))
const filePath = path.join(__dirname, 'assets', 'flutter_logo.png');
const stat = fs.statSync(filePath);
const file = fs.readFileSync(filePath, 'binary');
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'image/png');
res.setHeader('Content-Disposition', 'attachment; filename=flutter_logo.png');
res.write(file, 'binary');
res.end();
})
app.listen(8082)