forked from henszey/etcd-browser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
147 lines (132 loc) · 4.69 KB
/
server.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
var yaml = require("yamljs")
var url = require('url');
var path = require('path');
var fs = require('fs');
var http = require('http');
var https = require('https');
var config = load_config(process.env.ETCD_BROWSER_CONFIG || "./config.yaml");
var serverPort = config["listen"];
var publicDir = 'frontend';
var 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) {
if(req.url === '/'){
req.url = '/index.html';
} else if(req.url == "/nodes") {
return response_etcd_nodes(config, res);
} else if(req.url.split("/")[2] === 'v2') {
// avoid fileExists for /v2 routes
return proxy(req, res);
}
var uri = url.parse(req.url).pathname;
var 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('etc-browser listening on port ' + serverPort)
});
function proxy(client_req, client_res) {
var ip = client_req.headers['x-forwarded-for'] ||
client_req.connection.remoteAddress ||
client_req.socket.remoteAddress ||
client_req.connection.socket.remoteAddress;
var opts = getServerInfo(client_req, config);
if(!opts) {
client_res.writeHead(404, {'Content-Type': 'text/plain'})
return client_res.end("Can not find this etcd host\n");
}
var time = new Date().toISOString();
console.log(time + " " + ip + " --- " + opts.method + " " + client_req.url)
console.log("proxy to: " + opts.hostname + ":" + opts.port + opts.path);
client_req.pipe(opts.requestor(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) {
client_res.statusCode = res.statusCode;
res.pipe(client_res, {end: true});
}, {end: true}));
} else {
client_res.statusCode = res.statusCode;
res.pipe(client_res, {end: true});
}
}, {end: true}));
}
function getServerInfo(client_req, config){
var path = client_req.url;
var et = path.split("/")[1];
if (!(et in config['instances'])) {
console.log("can not find etcd host: " + et);
return null;
}
config.instances[et].opts.path = path.replace("/" + et, "");
config.instances[et].opts.method = client_req.method;
return config.instances[et].opts;
}
function load_config(config) {
var conf = yaml.load(config);
for(var key in conf['instances']){
var item = conf['instances'][key];
item.opts = {}
var uri = url.parse(item['base']);
if(uri['protocol'] === "https:"){
item.opts.requestor = https.request;
if(!item.ca){
if(item.verify_ssl == true){
throw("ssl ca is not set for etcd node: " + key);
}else{
item.verify_ssl == false;
}
}else{
if(item.verify_ssl == true){
item.opts.ca = fs.readFileSync(item.ca);
}
}
if(item.cert ^ item.key){
throw("cert or key missing for etcd node: " + key);
}
if(item.cert){
item.opts.key =fs.readFileSync(item.key);
item.opts.cert =fs.readFileSync(item.cert);
}
item.opts.rejectUnauthorized = item.verify_ssl || false;
}else if(uri['protocol'] == "http:"){
item.opts.requestor = http.request;
}else{
throw("Unknow protocol: " + uri.protocol);
}
item.opts.hostname = uri.hostname;
item.opts.port = uri.port;
if(item.auth){
item.opts.auth = item.auth;
}
}
return conf;
}
function response_etcd_nodes(config, res){
// return configured etcd nodes
nodes = etcd_nodes(config);
res.writeHead(200, {'Content-Type': 'application/json'})
return res.end(JSON.stringify({"nodes": nodes}));
}
function etcd_nodes(config){
nodes = []
for (var key in config.instances) {
nodes.push({"name": key, "base": config.instances[key].base})
}
return nodes
}