forked from xianlubird/etcd-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
150 lines (126 loc) · 4.09 KB
/
server.js
File metadata and controls
150 lines (126 loc) · 4.09 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
const url = require('url');
const path = require('path');
const fs = require('fs');
const http = require('http');
const socks = require('socksv5');
const ca_file = process.env.ETCDCTL_CA_FILE || false;
const key_file = process.env.ETCDCTL_KEY_FILE || false;
const cert_file = process.env.ETCDCTL_CERT_FILE || false;
const proxy_host = process.env.PROXY_HOST;
const proxy_port = process.env.PROXY_PORT;
let requester = http.request;
if (cert_file) {
// use https requests if theres a cert file
const https = require('https');
requester = https.request;
if (!fs.existsSync(cert_file)) {
console.error('CERT FILE', cert_file, 'not found!');
process.exit(1);
}
if (!fs.existsSync(key_file)) {
console.error('KEY FILE', key_file, 'not found!');
process.exit(1);
}
if (!fs.existsSync(ca_file)) {
console.error('CA FILE', ca_file, 'not found!');
process.exit(1);
}
}
const etcdHost = process.env.ETCD_HOST || 'localhost';
const etcdPort = process.env.ETCD_PORT || 4001;
const serverPort = process.env.SERVER_PORT || 8100;
const publicDir = 'frontend';
let authUser = process.env.AUTH_USER;
const authPass = process.env.AUTH_PASS;
const mimeTypes = {
'html': 'text/html',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'png': 'image/png',
'js': 'text/javascript',
'css': 'text/css'
};
http.createServer(function serverFile (req, res) {
// authenticaton
if (!auth(req, res)) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="MyRealmName"');
res.end('Unauthorized');
return;
}
auth;
if (req.url === '/') {
req.url = '/index.html';
} else if (req.url.substr(0, 3) === '/v2') {
// avoid fileExists for /v2 routes
return proxy(req, res);
}
const uri = url.parse(req.url).pathname;
const filename = path.join(process.cwd(), publicDir, uri);
fs.exists(filename, function (exists) {
// proxy if file does not exist
if (!exists) return proxy(req, res);
// serve static file if exists
res.writeHead(200, mimeTypes[path.extname(filename).split('.')[1]]);
fs.createReadStream(filename).pipe(res);
});
}).listen(serverPort, function () {
console.log('proxy /api requests to etcd on ' + etcdHost + ':' + etcdPort);
console.log('etc-browser listening on port ' + serverPort);
});
function proxy (client_req, client_res) {
const opts = {
hostname: etcdHost,
port: etcdPort,
path: client_req.url,
method: client_req.method
};
// https/certs supprt
if (cert_file) {
opts.key = fs.readFileSync(key_file);
opts.ca = fs.readFileSync(ca_file);
opts.cert = fs.readFileSync(cert_file);
if (proxy_host !== undefined && proxy_port !== undefined) {
const socksConfig = {
proxyHost: proxy_host,
proxyPort: proxy_port,
auths: [socks.auth.None()]
};
opts.agent = new socks.HttpsAgent(socksConfig);
}
}
const sockRequest = requester(opts, function (res) {
// if etcd returns that the requested page has been moved
// to a different location, indicates that the node we are
// querying is not the leader. This will redo the request
// on the leader which is reported by the Location header
if (res.statusCode === 307) {
opts.hostname = url.parse(res.headers['location']).hostname;
client_req.pipe(requester(opts, function (res) {
console.log('Got response: ' + res.statusCode);
res.pipe(client_res, { end: true });
}, { end: true }));
} else {
res.pipe(client_res, { end: true });
}
}, { end: true });
sockRequest.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
client_req.pipe(sockRequest);
}
function auth (req) {
if (!authUser) return true;
let auth = req.headers.authorization;
if (!auth) return false;
// malformed
const parts = auth.split(' ');
if ('basic' !== parts[0].toLowerCase()) return false;
if (!parts[1]) return false;
auth = parts[1];
// credentials
auth = new Buffer(auth, 'base64').toString();
auth = auth.match(/^([^:]*):(.*)$/);
if (!auth) return false;
return (auth[1] === authUser && auth[2] === authPass);
}