-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
139 lines (113 loc) · 4.45 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
var compiler = require('./compiler'),
defaultLocation = __dirname + "/tempfiles/",
fs = require('fs'),
path = require('path'),
_ = require('underscore'),
haproxy = require('./haproxy');
function init(routeList) {
//copy the hap config template for the env
var exec = require('child_process').exec;
//Creating the tempfiles folder and copyng the proxyserver config
exec('mkdir -p '+defaultLocation+' & cp -r '+__dirname+'/hapconfigtemplate/config ' + defaultLocation, function(err, stdout, stderr) {
if(err||stdout||stderr) console.log(err, stdout, stderr);
(routeList || []).forEach(function(route){
addHttpProxy(route.name, route.targethost);
});
haproxy.init();
});
}
function addHttpProxy(name, targethost, cb) {
var hapConfig = compiler.getFiles(),
frontend = hapConfig.frontend,
backend = hapConfig.backend;
frontend.content += "" +
"\n" + SSComment(name).start +
"\n acl is" + name + " hdr_beg(host) " + name + "." +
"\n use_backend " + name + "_backend if is" + name +
"\n" + SSComment(name).end + "\n";
backend.content += "" +
"\n" + SSComment(name).start +
"\nbackend " + name + "_backend" +
"\n balance roundrobin" +
"\n server host_" + name + " " + targethost +
"\n" + SSComment(name).end + "\n";
fs.writeFileSync(path.join(defaultLocation, 'config/includes/frontend-80'), frontend.content);
fs.writeFileSync(path.join(defaultLocation, 'config/includes/backend'), backend.content);
compiler(hapConfig.files);
cb && cb();
return this;
}
function addHttpsProxy(name, targethost, cb) {
var hapConfig = compiler.getFiles(),
secureFrontend = hapConfig.secureFrontend,
backend = hapConfig.backend;
secureFrontend.content += "" +
"\n" + SSComment(name).start +
"\n acl is" + name + "secure hdr_beg(host) " + name + "." +
"\n use_backend " + name + "secure_backend if is" + name + "secure" +
"\n" + SSComment(name).end + "\n";
backend.content += "" +
"\n" + SSComment(name + "_secure").start +
"\nbackend " + name + "secure_backend" +
"\n balance roundrobin" +
"\n server host_" + name + " " + targethost + " ssl verify none" +
"\n" + SSComment(name + "_secure").end + "\n";
fs.writeFileSync(path.join(defaultLocation, 'config/includes/frontend-443'), secureFrontend.content);
fs.writeFileSync(path.join(defaultLocation, 'config/includes/backend'), backend.content);
compiler(hapConfig.files);
cb && cb();
return this;
}
function removeHttpProxy(name, port, cb) {
var hapConfig = compiler.getFiles(),
frontend = hapConfig.frontend,
backend = hapConfig.backend;
frontend.content = removeProxyConf(frontend.content, name);
backend.content = removeProxyConf(backend.content, name);
fs.writeFileSync(path.join(defaultLocation, 'config/includes/frontend-80'), frontend.content);
fs.writeFileSync(path.join(defaultLocation, 'config/includes/backend'), backend.content);
compiler(hapConfig.files);
cb && cb();
return this;
}
function removeHttpsProxy(name, port, cb) {
var hapConfig = compiler.getFiles(),
secureFrontend = hapConfig.secureFrontend,
backend = hapConfig.backend;
secureFrontend.content = removeProxyConf(secureFrontend.content, name);
backend.content = removeProxyConf(backend.content, name + "_secure");
fs.writeFileSync(path.join(defaultLocation, 'config/includes/frontend-443'), secureFrontend.content);
fs.writeFileSync(path.join(defaultLocation, 'config/includes/backend'), backend.content);
compiler(hapConfig.files);
cb && cb();
return this;
}
function restartHaproxy(cb) {
haproxy.restart(function(){
cb && cb();
});
}
function removeProxyConf(fileContent, name) {
var ssc = SSComment(name);
//#qa1-box-start([\s\S]*?)#qa1-box-end
var matchExp = new RegExp('\n' + ssc.start + '([\\s\\S]*?)' + ssc.end + '\n', 'gi');
try {
fileContent = fileContent.replace(matchExp, '');
} catch (e) {
console.log('failed to remove hap config for this box');
}
return fileContent;
}
function SSComment(name) {
return {
start : "#" + name + "-box-start",
end : "#" + name + "-box-end"
};
}
init.init = init;
init.addHttpProxy = addHttpProxy;
init.addHttpsProxy = addHttpsProxy;
init.removeHttpProxy = removeHttpProxy;
init.removeHttpsProxy = removeHttpsProxy;
init.restart = restartHaproxy;
module.exports = init;