forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable-proxy
executable file
·174 lines (134 loc) · 4.14 KB
/
enable-proxy
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
#!/usr/bin/env perl
# enable-proxy - Set Webmin configuration options to support being served
# through a proxy.
use strict;
use warnings;
BEGIN { $Pod::Usage::Formatter = 'Pod::Text::Color'; }
use 5.010; # Version in CentOS 6
use Getopt::Long qw(:config permute pass_through);
use Pod::Usage;
use Term::ANSIColor qw(:constants);
use Fcntl qw( :flock );
use Sys::Hostname;
sub main {
my %opt;
GetOptions(
'help|h' => \$opt{'help'},
'config|c=s' => \$opt{'config'},
'prefix|p=s' => \$opt{'prefix'},
'referer|r=s' => \$opt{'referer'}
);
pod2usage(0) if ( $opt{'help'} );
$opt{'config'} ||= "/etc/webmin";
enable_proxy( \%opt );
return 0;
}
exit main( \@ARGV ) if !caller(0);
sub enable_proxy {
my ($optref) = @_;
my @config_lines;
my $file = "$optref->{'config'}/config";
my $referer;
if ($optref->{'referer'}) {
$referer = $optref->{'referer'};
} else {
$referer = hostname;
}
# Setup Webmin
if ($optref->{'prefix'}) {
# Set'em up for proxying on https://domain.tld/prefix
set_config('webprefix', $optref->{'prefix'}, $file);
set_config('webprefix_noredir', '1', $file);
set_config('referer', $referer, $file);
set_config('ssl_redirect', '0', "$optref->{'config'}/miniserv.conf");
set_config('ssl', '0', "$optref->{'config'}/miniserv.conf");
} else {
# No prefix, just proxying at the root level: https://domain.tld/
set_config('referer', $referer, $file);
set_config('ssl_redirect', '0', "$optref->{'config'}/miniserv.conf");
set_config('ssl', '0', "$optref->{'config'}/miniserv.conf");
}
# Setup the local web server?
# Restart Webmin
say "Restarting Webmin to apply changes...";
system("$optref->{'config'}/restart");
exit 0;
}
sub set_config {
my ($key, $value, $file, $module, $force) = @_;
$key or die RED, "An --option must be specified", RESET;
my @config_lines;
open my $fh, '+<', $file
or die RED, "Unable to open $file", RESET;
flock($fh, LOCK_EX) or die RED, "Unable to lock $file", RESET;
chomp(@config_lines = <$fh>);
# Change'em
my $found = 0;
my $exit_code = 0;
# Validate it against the config.info if this is a module and
if ($module && !$force) {
validate_config_option($key, $value, $module);
}
for (@config_lines) {
if (/^${key}=(.*)/) {
s/^${key}=(.*)/${key}=${value}/;
$found++;
}
}
unless ($found > 0) {
push(@config_lines, "$key=$value");
$exit_code++;
}
# Write'em
seek($fh, 0, 0);
print $fh qq|$_\n| for @config_lines;
close $fh;
}
sub root {
my ($config) = @_;
open(my $CONF, "<", "$config/miniserv.conf") || die RED,
"Failed to open $config/miniserv.conf", RESET;
my $root;
while (<$CONF>) {
if (/^root=(.*)/) {
$root = $1;
}
}
close($CONF);
# Does the Webmin root exist?
if ( $root ) {
die "$root is not a directory. Is --config correct?" unless (-d $root);
} else {
# Try to guess where Webmin lives, since config file didn't know.
die "Unable to determine Webmin installation directory from $ENV{'WEBMIN_CONFIG'}";
}
return $root;
}
1;
=pod
=head1 NAME
enable-proxy
=head1 DESCRIPTION
Configure the Webmin web server to be proxied through another web server, like Apache or nginx. This is not usually recommended, and disables some security features, but can help traverse a firewall.
=head1 SYNOPSIS
enable-proxy [options]
=head1 OPTIONS
=over
=item --help, -h
Print this usage summary and exit.
=item --config, -c
Specify the full path to the Webmin configuration directory. Defaults to
C</etc/webmin>
=item --prefix, -p
To use a directory prefix for URLs (e.g. https://domain.tld/webmin) provide
it using this option.
=item --referer, -r
The hostname you'll be using in your browser to contact the server. (e.g.
domain.tld)
=back
=head1 EXIT CODES
0 on success
non-0 on error
=head1 LICENSE AND COPYRIGHT
Copyright 2018 Jamie Cameron <[email protected]>, Joe Cooper