-
Notifications
You must be signed in to change notification settings - Fork 3
/
edit_proxy.cgi
188 lines (151 loc) · 4.59 KB
/
edit_proxy.cgi
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
#!/usr/bin/perl
require './tomcat-lib.pl';
foreign_require('apache', 'apache-lib.pl');
sub get_proxy_file{
my $domain_user = $_[0];
my $proxy_file = '';
if( -d '/etc/httpd/conf.d' ){
$proxy_file = '/etc/httpd/conf.d/tomcat.conf';
}elsif( -d '/etc/apache2/conf-enabled/'){ #ubuntu or debian
$proxy_file = '/etc/apache2/conf-enabled/tomcat.conf';
}
return $proxy_file;
}
sub load_proxy_maps{
my $proxy_file = $_[0];
my %maps;
open(my $fh, '<', $proxy_file) or return %maps;
while(my $line = <$fh>){
if($line =~ /^ProxyPass ([\/a-z0-9_\-\.]+) ([a-z:\/0-9\.\-]+)/i){
$maps{$2} = $1;
}
}
close $fh;
return %maps;
}
sub add_proxy{
my $proxy_file = $_[0];
my $app_name = $_[1];
my $default = $_[2];
my $ssl_port = $_[3];
my $wildcard = $_[4];
my %idata = ('port_http'=>'8080', 'port_https'=>'8443');
my $app_path = '/';
if($default == 0){ #if app is not default
$app_path .= $app_name;
}
if($wildcard == 1){
$app_name = '';
}
my $lref = &read_file_lines($proxy_file);
my $lnum = 0;
my $proxy_header_section = 0;
my $user_domain = 'localhost';
foreach my $line (@$lref) {
if($line =~ /^ProxyPass \/ |^ProxyPassReverse \/ /){ #another default app
if($default == 1){ #if new app is default
delete @{$lref}[$lnum]; #remove old default app
}
}elsif($line =~ /^ProxyPass.*\/$app_name$/){ #if its a line with our app
delete @{$lref}[$lnum];
}elsif($line =~ /^ProxyRequests Off$/){
$proxy_header_section = 1; #proxy settings are found
}elsif($line =~ /^ServerName (.*)$/){
$user_domain = $1;
}
$lnum++;
}
my $conf_tail;
if(@$lref[$lnum] eq '</VirtualHost>'){
$conf_tail = pop @{$lref};
}
if($proxy_header_section == 0){
my $line = "ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>";
push(@$lref, $line);
}
my $proto = '';
if($ssl_port == 1){
$proto = 's';
}
push(@$lref, "ProxyPass $app_path http".$proto."://$user_domain:".$idata{'port_http'.$proto}."/$app_name");
push(@$lref, "ProxyPassReverse $app_path http".$proto."://$user_domain:".$idata{'port_http'.$proto}."/$app_name");
if($conf_tail){
push(@$lref, $conf_tail); #Restore conf end
}
flush_file_lines($proxy_file);
}
sub remove_proxy{
my ($proxy_file, $app_path) = @_;
my $lref = &read_file_lines($proxy_file);
my $lnum = 0;
foreach my $line (@$lref) {
if($line =~ /^ProxyPass $app_path |^ProxyPassReverse $app_path /){ #if its a line with our app
delete @{$lref}[$lnum];
}
$lnum++;
}
flush_file_lines($proxy_file);
}
&ReadParse();
my $form_submit = 0;
if ($in{'submit_flag'}) {
$form_submit = 1;
}
&ui_print_header(undef, $text{'proxy_title'}, "");
my $proxy_file = get_proxy_file($remote_user);
if($proxy_file eq ''){
print "Error: Failed to find proxy file for user $remote_user</b>"
}
if($form_submit == 1){
if($in{'app_path'}){
remove_proxy($proxy_file, $in{'app_path'});
}else{
add_proxy($proxy_file, $in{'app_name'}, $in{'app_default'}, $in{'proxy_ssl'}, $in{'app_wildcard'});
}
apache::restart_apache();
}
my %maps = load_proxy_maps($proxy_file);
my @tds = ();
print &ui_columns_start(['Path','URL'], undef, 0, \@tds, 'Apps with proxy ('.$proxy_file.')');
foreach my $url (keys %maps) {
my @cols;
push(@cols, $maps{$url});
push(@cols, "<a href=\"$url\">$url</a>");
print &ui_columns_row(\@cols, \@tds);
}
print &ui_columns_end();
print &ui_form_start("edit_proxy.cgi", "post");
print &ui_hidden('submit_flag', 1);
print &ui_table_start($text{'proxy_add_options'}, undef, 2);
my @apps = get_all_war_infos();
@opt_apps = ( );
foreach $app (@apps) {
push(@opt_apps, [ $app, $app ]);
}
print &ui_table_row($text{'proxy_wildcard'},
&ui_checkbox("app_wildcard", 1, $text{'proxy_wildcard_info'}, 0), 2);
print &ui_table_hr();
print &ui_table_row($text{'wars_installed'}, &ui_select("app_name", undef, \@opt_apps, 10, 1), 2);
print &ui_table_row($text{'proxy_default_app'},
&ui_checkbox("app_default", 1, $text{'proxy_app_default_warning'}, 0), 2);
print &ui_table_row($text{'proxy_ssl'},
&ui_checkbox("proxy_ssl", 1, $text{'proxy_ssl_info'}, 0), 2);
print &ui_table_end();
print &ui_form_end([ [ "", $text{'proxy_installok'} ] ]);
print &ui_form_start("edit_proxy.cgi", "post");
print &ui_hidden('submit_flag', 1);
print &ui_table_start($text{'proxy_remove_options'}, undef, 2);
@opt_paths = ( );
foreach $path (keys %maps) {
my $val = $maps{$path};
push(@opt_paths, [ $val, $val ]);
}
print &ui_table_row($text{'proxy_list'}, &ui_select("app_path", undef, \@opt_paths, 10, 1), 2);
print &ui_table_end();
print &ui_form_end([ [ "", $text{'proxy_removeok'} ] ]);
&ui_print_footer("", $text{'index_return'});