forked from virtualmin/virtualmin-gpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalancer-lib.pl
executable file
·351 lines (328 loc) · 10.5 KB
/
balancer-lib.pl
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Functions for finding and changing proxy balancer blocks
# Like :
# <Proxy balancer://mongrel_cluster>
# BalancerMember http://127.0.0.1:8000
# BalancerMember http://127.0.0.1:8001
# BalancerMember http://127.0.0.1:8002
# </Proxy>
# ProxyPass / balancer://mongrel_cluster/
# or
# ProxyPass / http://127.0.0.1:8000/
# list_proxy_balancers(&domain)
# Returns a list of URL paths and backends for balancer blocks
sub list_proxy_balancers
{
local ($d) = @_;
local $p = &domain_has_website($d);
if ($p && $p ne 'web') {
return &plugin_call($p, "feature_list_web_balancers", $d);
}
&require_apache();
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $d->{'web_port'});
return ( ) if (!$virt);
local @rv;
foreach my $pp (&apache::find_directive("ProxyPass", $vconf)) {
if ($pp =~ /^(\/\S*)\s+balancer:\/\/([^\/ ]+)/) {
# Balancer proxy
push(@rv, { 'path' => $1,
'balancer' => $2 });
}
elsif ($pp =~ /^(\/\S*)\s+((http|http):\/\/\S+)/) {
# Single-host proxy
push(@rv, { 'path' => $1,
'urls' => [ $2 ] });
}
elsif ($pp =~ /^(\/\S*)\s+\!/) {
# Proxying disabled for path
push(@rv, { 'path' => $1,
'none' => 1 });
}
}
foreach my $proxy (&apache::find_directive_struct("Proxy", $vconf)) {
if ($proxy->{'value'} =~ /^balancer:\/\/([^\/ ]+)/) {
local ($rv) = grep { $_->{'balancer'} eq $1 } @rv;
if ($rv) {
$rv->{'urls'} = [ &apache::find_directive(
"BalancerMember", $proxy->{'members'}) ];
}
}
}
return @rv;
}
# create_proxy_balancer(&domain, &balancer)
# Adds the ProxyPass and Proxy directives for a new balancer. Returns an error
# message on failure, undef on success.
sub create_proxy_balancer
{
local ($d, $balancer) = @_;
local $p = &domain_has_website($d);
if ($p && $p ne 'web') {
return &plugin_call($p, "feature_create_web_balancer", $d, $balancer);
}
&require_apache();
local $conf = &apache::get_config();
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $d->{'web_port'});
return "Failed to find Apache config for $d->{'dom'}" if (!$virt);
# Check for clashes
local @pp = &apache::find_directive("ProxyPass", $vconf);
local ($clash) = grep { $_ =~ /^(\/\S*)\s+/ && $1 eq $balancer->{'path'} } @pp;
return "A ProxyPass for $balancer->{'path'} already exists" if ($clash);
if ($balancer->{'balancer'}) {
local @proxy = &apache::find_directive("Proxy", $vconf);
local ($clash) = grep { $_ =~ /balancer:\/\/([^\/ ]+)/ &&
$1 eq $balancer->{'balancer'} } @proxy;
return "A Proxy block for $balancer->{'balancer'} already exists"
if ($clash);
}
# Add the directives
local @ports = ( $d->{'web_port'},
$d->{'ssl'} ? ( $d->{'web_sslport'} ) : ( ) );
foreach my $port (@ports) {
if ($port != $d->{'web_port'}) {
($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $port);
}
next if (!$virt);
local $slash = $balancer->{'path'} eq '/' ? '/' : undef;
local $ssl = 0;
foreach my $u (@{$balancer->{'urls'}}) {
$ssl++ if ($u =~ /^https:/i);
}
if ($balancer->{'balancer'}) {
# To multiple URLs
local $lref = &read_file_lines($virt->{'file'});
local @pdirs = (map { "BalancerMember $_" } @{$balancer->{'urls'}});
if (&supports_check_peer_name() && $ssl) {
push(@pdirs, "SSLProxyCheckPeerName off");
push(@pdirs, "SSLProxyCheckPeerCN off");
push(@pdirs, "SSLProxyCheckPeerExpire off");
}
splice(@$lref, $virt->{'eline'}, 0,
"<Proxy balancer://$balancer->{'balancer'}>",
@pdirs,
"</Proxy>",
"ProxyPass $balancer->{'path'} balancer://$balancer->{'balancer'}$slash",
"ProxyPassReverse $balancer->{'path'} balancer://$balancer->{'balancer'}$slash",
);
undef(@apache::get_config_cache);
}
else {
# To just one URL - longest paths must always go first
local $url = $balancer->{'none'} ? "!" :
$balancer->{'urls'}->[0];
if ($path eq "/" && $url ne "!" &&
$url =~ /^(http|https):\/\/[a-z0-9\_\-:]+$/i) {
# If the path is just / and the URL is top-level with
# no trailing /, add one
$url .= "/";
}
foreach my $dir ("ProxyPass", "ProxyPassReverse") {
local @pp = &apache::find_directive($dir, $vconf);
@pp = &sort_proxy_paths(@pp,
"$balancer->{'path'} $url");
&apache::save_directive($dir, \@pp, $vconf, $conf);
}
}
&flush_file_lines($virt->{'file'});
}
# If proxying to SSL, turn on SSLProxyEngine
local $ssl = 0;
foreach my $url (@{$balancer->{'urls'}}) {
$ssl = 1 if ($url =~ /^https:/i);
}
if ($ssl) {
foreach my $port (@ports) {
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $port);
local @spe = &apache::find_directive("SSLProxyEngine", $vconf);
if (!@spe && lc($spe[0]) ne "on") {
&apache::save_directive("SSLProxyEngine", [ "on" ],
$vconf, $conf);
&flush_file_lines($virt->{'file'});
}
}
}
®ister_post_action(\&restart_apache);
return undef;
}
# delete_proxy_balancer(&domain, &balancer)
# Removes the ProxyPass directive and Proxy block for a balancer
sub delete_proxy_balancer
{
local ($d, $balancer) = @_;
local $p = &domain_has_website($d);
if ($p && $p ne 'web') {
return &plugin_call($p, "feature_delete_web_balancer", $d, $balancer);
}
&require_apache();
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $d->{'web_port'});
return "Failed to find Apache config for $d->{'dom'}" if (!$virt);
local @ports = ( $d->{'web_port'},
$d->{'ssl'} ? ( $d->{'web_sslport'} ) : ( ) );
local $done = 0;
foreach my $port (@ports) {
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $port);
next if (!$virt);
# Find the directives
local @pp = &apache::find_directive_struct("ProxyPass", $vconf);
local ($pp) = grep { $_->{'value'} =~ /^(\/\S*)\s+/ &&
$1 eq $balancer->{'path'} } @pp;
local @ppr = &apache::find_directive_struct("ProxyPassReverse", $vconf);
local ($ppr) = grep { $_->{'value'} =~ /^(\/\S*)\s+/ &&
$1 eq $balancer->{'path'} } @ppr;
local @proxy = &apache::find_directive_struct("Proxy", $vconf);
local ($proxy) = grep { $_->{'value'} =~ /balancer:\/\/([^\/ ]+)/ &&
$1 eq $balancer->{'balancer'} } @proxy;
# Splice them out
local $lref = &read_file_lines($virt->{'file'});
foreach my $r (sort { $b->{'line'} <=> $a->{'line'} }
grep { $_ } $pp, $ppr, $proxy) {
splice(@$lref, $r->{'line'},
$r->{'eline'} - $r->{'line'} + 1);
$done++;
}
&flush_file_lines($virt->{'file'});
undef(@apache::get_config_cache);
}
®ister_post_action(\&restart_apache);
return $done ? undef : "No proxy directives for $balancer->{'path'} found";
}
# modify_proxy_balancer(&domain, &balancer, &oldbalancer)
# Updates a balancer block - the name of which cannot change
sub modify_proxy_balancer
{
local ($d, $b, $oldb) = @_;
local $p = &domain_has_website($d);
if ($p && $p ne 'web') {
return &plugin_call($p, "feature_modify_web_balancer", $d, $b, $oldb);
}
&require_apache();
local $bn = $b->{'balancer'};
local $conf = &apache::get_config();
local $done = 0;
local @ports = ( $d->{'web_port'},
$d->{'ssl'} ? ( $d->{'web_sslport'} ) : ( ) );
foreach my $port (@ports) {
local ($virt, $vconf) = &get_apache_virtual($d->{'dom'}, $port);
next if (!$virt);
# Find and fix the ProxyPass and ProxyPassReverse
local $slash = $b->{'path'} eq '/' ? '/' : undef;
foreach my $dir ("ProxyPass", "ProxyPassReverse") {
local @npp;
foreach my $pp (&apache::find_directive($dir, $vconf)) {
local ($dirpath, $dirurl) = split(/\s+/, $pp);
if ($dirpath eq $oldb->{'path'} &&
$dirurl =~ /^(balancer:\/\/\Q$bn\E)/) {
# Balancer
$pp = "$b->{'path'} $1$slash";
$done++;
}
elsif ($dirpath eq $oldb->{'path'} &&
$dirurl =~ /^((http|https):\/\/)|\!/) {
# Single URL
if ($b->{'none'}) {
$pp = "$b->{'path'} !";
}
else {
$pp = "$b->{'path'} $b->{'urls'}->[0]";
}
$done++;
}
push(@npp, $pp);
}
if ($b->{'path'} ne $oldb->{'path'}) {
# Re-order so that new longer path is first
@npp = &sort_proxy_paths(@npp);
}
&apache::save_directive($dir, \@npp, $vconf, $conf);
}
# Find and fix the URLs in the <Proxy> block
if ($bn) {
local ($proxy) = grep
{ $_->{'value'} =~ /^balancer:\/\/\Q$bn\E/ }
&apache::find_directive_struct("Proxy", $vconf);
if ($proxy) {
&apache::save_directive("BalancerMember", $b->{'urls'},
$proxy->{'members'}, $conf);
$done++;
}
}
&flush_file_lines($virt->{'file'});
}
®ister_post_action(\&restart_apache);
return $done ? undef : "No proxy directives for $oldb->{'path'} found";
}
# sort_proxy_paths(path, ...)
# Sorts proxy paths by path length, so that the longest are first
sub sort_proxy_paths
{
return sort { my ($pa, $ua) = split(/\s+/, $a, 2);
my ($pb, $ub) = split(/\s+/, $b, 2);
return length($pb) <=> length($pa) } @_;
}
# get_balancer_usage(&domain, &scripts-used, &plugin-used)
# Fill in two hashes with maps from paths to script into and plugin usage of
# balancers
sub get_balancer_usage
{
my ($d, $used, $pused) = @_;
foreach $sinfo (&list_domain_scripts($d)) {
$used->{$sinfo->{'opts'}->{'path'}} = $sinfo;
}
foreach my $p (&list_feature_plugins(1)) {
if (&plugin_defined($p, "feature_path_desc")) {
foreach my $pd (&plugin_call($p, "feature_path_desc", $d)) {
$pd->{'plugin'} = $p;
$pused->{$pd->{'path'}} = $pd;
}
}
}
}
# allocate_proxy_port([base], [number])
# Finds ports that are not in use by any domain's script
# or server and returns a space-separated list of them
sub allocate_proxy_port
{
my ($base, $ports) = @_;
$base ||= 3000;
my %used;
foreach my $d (&list_domains()) {
foreach my $ds (&list_domain_scripts($d)) {
foreach my $p (split(/\s+/, $ds->{'opts'}->{'port'})) {
$used{$p} = 1;
}
}
}
my @rv;
while(scalar(@rv) < $ports) {
my $rport = &allocate_free_tcp_port(\%used, $base);
$rport || &error("Failed to allocate port starting from $base");
$used{$rport}++;
push(@rv, $rport);
}
return join(" ", @rv);
}
# setup_proxy(&domain, path, port, [proxy-path], [protocol])
# Adds webserver config entries to proxy some path to a local server
sub setup_proxy
{
my ($d, $path, $rport, $ppath, $proto) = @_;
$rport ||= &allocate_proxy_port(undef, 1);
my @ports = split(/\s+/, $rport);
$proto ||= "http";
my $has = &has_proxy_balancer($d);
my $balancer = { 'path' => $path };
if ($has == 2) {
# Multiple-destination balancer
$balancer->{'balancer'} = "proxy".$ports[0];
}
$balancer->{'urls'} = [ map { "$proto://127.0.0.1:$_$ppath" } @ports ];
&create_proxy_balancer($d, $balancer);
}
# delete_proxy(&domain, path)
# Delete the webserver config entries that proxy on some port
sub delete_proxy
{
my ($d, $path) = @_;
my ($balancer) = grep { $_->{'path'} eq $path } &list_proxy_balancers($d);
&delete_proxy_balancer($d, $balancer) if ($balancer);
}
1;