]>
Commit | Line | Data |
---|---|---|
4a17e72e DM |
1 | package PVE::Service::pveproxy; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
bc917275 | 6 | use Data::Dumper; |
4a17e72e | 7 | use Encode; |
bc917275 TL |
8 | use HTTP::Response; |
9 | use Template; | |
4a17e72e | 10 | use URI::QueryParam; |
bc917275 TL |
11 | use URI; |
12 | ||
4a17e72e | 13 | use PVE::API2; |
b996e6c0 | 14 | use PVE::APIServer::AnyEvent; |
bc917275 TL |
15 | use PVE::APIServer::Formatter::HTML; |
16 | use PVE::APIServer::Formatter::Standard; | |
17 | use PVE::APIServer::Formatter; | |
18 | use PVE::APIServer::Utils; | |
19 | use PVE::Cluster; | |
20 | use PVE::Daemon; | |
21 | use PVE::DataCenterConfig; | |
4a17e72e | 22 | use PVE::HTTPServer; |
bc917275 | 23 | use PVE::SafeSyslog; |
54165ad3 | 24 | use PVE::pvecfg; |
4a17e72e DM |
25 | use PVE::Tools; |
26 | ||
27 | use base qw(PVE::Daemon); | |
28 | ||
29 | my $cmdline = [$0, @ARGV]; | |
30 | ||
31 | my %daemon_options = ( | |
32 | max_workers => 3, | |
33 | restart_on_error => 5, | |
34 | stop_wait_time => 15, | |
35 | leave_children_open_on_reload => 1, | |
36 | setuid => 'www-data', | |
37 | setgid => 'www-data', | |
38 | pidfile => '/var/run/pveproxy/pveproxy.pid', | |
39 | ); | |
40 | ||
41 | my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options); | |
42 | ||
43 | sub add_dirs { | |
44 | my ($result_hash, $alias, $subdir) = @_; | |
45 | ||
e4697709 | 46 | PVE::APIServer::AnyEvent::add_dirs($result_hash, $alias, $subdir); |
4a17e72e DM |
47 | } |
48 | ||
245e567e DC |
49 | my $basedirs = { |
50 | novnc => '/usr/share/novnc-pve', | |
51 | extjs => '/usr/share/javascript/extjs', | |
52 | manager => '/usr/share/pve-manager', | |
411967db | 53 | i18n => '/usr/share/pve-i18n', |
245e567e | 54 | docs => '/usr/share/pve-docs', |
7f3b89a0 | 55 | fontawesome => '/usr/share/fonts-font-awesome', |
03f09f9a | 56 | xtermjs => '/usr/share/pve-xtermjs', |
f90908cb | 57 | widgettoolkit => '/usr/share/javascript/proxmox-widget-toolkit', |
245e567e DC |
58 | }; |
59 | ||
4a17e72e DM |
60 | sub init { |
61 | my ($self) = @_; | |
62 | ||
63 | # we use same ALLOW/DENY/POLICY as pveproxy | |
a642f8a0 | 64 | my $proxyconf = PVE::APIServer::Utils::read_proxy_config($self->{name}); |
4a17e72e DM |
65 | |
66 | my $accept_lock_fn = "/var/lock/pveproxy.lck"; | |
67 | ||
68 | my $lockfh = IO::File->new(">>${accept_lock_fn}") || | |
69 | die "unable to open lock file '${accept_lock_fn}' - $!\n"; | |
70 | ||
36ad2b3c | 71 | my $listen_ip = $proxyconf->{LISTEN_IP}; |
e224b7d2 | 72 | my $socket = $self->create_reusable_socket(8006, $listen_ip); |
4a17e72e DM |
73 | |
74 | my $dirs = {}; | |
75 | ||
919cfa29 TL |
76 | add_dirs($dirs, '/novnc/' => "$basedirs->{novnc}/"); |
77 | add_dirs($dirs, '/pve-docs/' => "$basedirs->{docs}/"); | |
78 | add_dirs($dirs, '/pve-docs/api-viewer/extjs/' => "$basedirs->{extjs}/"); | |
79 | add_dirs($dirs, '/pve2/css/' => "$basedirs->{manager}/css/"); | |
245e567e | 80 | add_dirs($dirs, '/pve2/ext6/', "$basedirs->{extjs}/"); |
919cfa29 TL |
81 | add_dirs($dirs, '/pve2/fa/css/' => "$basedirs->{fontawesome}/css/"); |
82 | add_dirs($dirs, '/pve2/fa/fonts/' => "$basedirs->{fontawesome}/fonts/"); | |
245e567e | 83 | add_dirs($dirs, '/pve2/images/' => "$basedirs->{manager}/images/"); |
245e567e | 84 | add_dirs($dirs, '/pve2/js/' => "$basedirs->{manager}/js/"); |
919cfa29 TL |
85 | add_dirs($dirs, '/pve2/locale/', "$basedirs->{i18n}/"); |
86 | add_dirs($dirs, '/pve2/touch/', "$basedirs->{manager}/touch/"); | |
cc8c253f | 87 | add_dirs($dirs, '/pwt/css/' => "$basedirs->{widgettoolkit}/css/"); |
919cfa29 | 88 | add_dirs($dirs, '/pwt/images/' => "$basedirs->{widgettoolkit}/images/"); |
5137b16f | 89 | add_dirs($dirs, '/pwt/themes/' => "$basedirs->{widgettoolkit}/themes/"); |
919cfa29 | 90 | add_dirs($dirs, '/xtermjs/' => "$basedirs->{xtermjs}/"); |
4a17e72e DM |
91 | |
92 | $self->{server_config} = { | |
a9de2d44 | 93 | title => 'Proxmox VE API', |
4a17e72e DM |
94 | keep_alive => 100, |
95 | max_conn => 500, | |
96 | max_requests => 1000, | |
97 | lockfile => $accept_lock_fn, | |
98 | socket => $socket, | |
99 | lockfh => $lockfh, | |
100 | debug => $self->{debug}, | |
101 | trusted_env => 0, # not trusted, anyone can connect | |
102 | logfile => '/var/log/pveproxy/access.log', | |
103 | allow_from => $proxyconf->{ALLOW_FROM}, | |
104 | deny_from => $proxyconf->{DENY_FROM}, | |
105 | policy => $proxyconf->{POLICY}, | |
106 | ssl => { | |
95035118 | 107 | cipher_list => $proxyconf->{CIPHERS}, |
ff65c929 | 108 | ciphersuites => $proxyconf->{CIPHERSUITES}, |
4a17e72e DM |
109 | key_file => '/etc/pve/local/pve-ssl.key', |
110 | cert_file => '/etc/pve/local/pve-ssl.pem', | |
95035118 | 111 | honor_cipher_order => $proxyconf->{HONOR_CIPHER_ORDER}, |
4a17e72e | 112 | }, |
a33abad1 | 113 | compression => $proxyconf->{COMPRESSION}, |
4a17e72e DM |
114 | # Note: there is no authentication for those pages and dirs! |
115 | pages => { | |
c7f32808 | 116 | '/' => sub { get_index($self->{nodename}, @_) }, |
4a17e72e DM |
117 | # avoid authentication when accessing favicon |
118 | '/favicon.ico' => { | |
245e567e | 119 | file => "$basedirs->{manager}/images/favicon.ico", |
4a17e72e | 120 | }, |
f90908cb DC |
121 | '/proxmoxlib.js' => { |
122 | file => "$basedirs->{widgettoolkit}/proxmoxlib.js", | |
123 | }, | |
6b2028cb WB |
124 | '/qrcode.min.js' => { |
125 | file => '/usr/share/javascript/qrcodejs/qrcode.min.js', | |
126 | }, | |
4a17e72e DM |
127 | }, |
128 | dirs => $dirs, | |
129 | }; | |
41196653 | 130 | |
95035118 | 131 | if (defined($proxyconf->{DHPARAMS})) { |
41196653 | 132 | $self->{server_config}->{ssl}->{dh_file} = $proxyconf->{DHPARAMS}; |
41196653 | 133 | } |
ed59fcff FG |
134 | if (defined($proxyconf->{DISABLE_TLS_1_2})) { |
135 | $self->{server_config}->{ssl}->{tlsv1_2} = !$proxyconf->{DISABLE_TLS_1_2}; | |
136 | } | |
137 | if (defined($proxyconf->{DISABLE_TLS_1_3})) { | |
138 | $self->{server_config}->{ssl}->{tlsv1_3} = !$proxyconf->{DISABLE_TLS_1_3}; | |
139 | } | |
64672c28 FG |
140 | my $custom_key_path = '/etc/pve/local/pveproxy-ssl.key'; |
141 | if (defined($proxyconf->{TLS_KEY_FILE})) { | |
142 | $custom_key_path = $proxyconf->{TLS_KEY_FILE}; | |
143 | } | |
144 | if (-f '/etc/pve/local/pveproxy-ssl.pem' && -f $custom_key_path) { | |
299d290c | 145 | $self->{server_config}->{ssl}->{cert_file} = '/etc/pve/local/pveproxy-ssl.pem'; |
64672c28 | 146 | $self->{server_config}->{ssl}->{key_file} = $custom_key_path; |
299d290c FG |
147 | syslog('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.'); |
148 | } | |
4a17e72e DM |
149 | } |
150 | ||
151 | sub run { | |
152 | my ($self) = @_; | |
153 | ||
154 | my $server = PVE::HTTPServer->new(%{$self->{server_config}}); | |
155 | $server->run(); | |
156 | } | |
157 | ||
158 | $daemon->register_start_command(); | |
159 | $daemon->register_restart_command(1); | |
160 | $daemon->register_stop_command(); | |
161 | $daemon->register_status_command(); | |
162 | ||
163 | our $cmddef = { | |
164 | start => [ __PACKAGE__, 'start', []], | |
165 | restart => [ __PACKAGE__, 'restart', []], | |
166 | stop => [ __PACKAGE__, 'stop', []], | |
167 | status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ], | |
168 | }; | |
169 | ||
170 | sub is_phone { | |
171 | my ($ua) = @_; | |
172 | ||
173 | return 0 if !$ua; | |
174 | ||
175 | return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/; | |
176 | ||
177 | if ($ua =~ m/Mobile(\/|\s)/) { | |
178 | return 1 if $ua =~ m/(BlackBerry|BB)/; | |
179 | return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/); | |
180 | } | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
185 | # NOTE: Requests to those pages are not authenticated | |
186 | # so we must be very careful here | |
187 | ||
188 | sub get_index { | |
c7f32808 | 189 | my ($nodename, $server, $r, $args) = @_; |
4a17e72e | 190 | |
f4aa76c5 | 191 | my $lang; |
4a17e72e DM |
192 | my $username; |
193 | my $token = 'null'; | |
5137b16f | 194 | my $theme = ""; |
4a17e72e DM |
195 | |
196 | if (my $cookie = $r->header('Cookie')) { | |
197 | if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) { | |
198 | if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) { | |
199 | $lang = $newlang; | |
200 | } | |
201 | } | |
5137b16f SS |
202 | |
203 | if (my $newtheme = ($cookie =~ /(?:^|\s)PVEThemeCookie=([^;]*)/)[0]) { | |
204 | # theme names need to be kebab case, with each segment a maximum of 10 characters long | |
205 | # and at most 6 segments | |
206 | if ($newtheme =~ m/^[a-z]{1,10}(-[a-z]{1,10}){0,5}$/) { | |
207 | $theme = $newtheme; | |
208 | } | |
209 | } | |
210 | ||
9a5a1655 | 211 | my $ticket = PVE::APIServer::Formatter::extract_auth_value($cookie, $server->{cookie_name}); |
4a17e72e DM |
212 | if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) { |
213 | $token = PVE::AccessControl::assemble_csrf_prevention_token($username); | |
214 | } | |
215 | } | |
216 | ||
28a93d3e SS |
217 | if ($theme eq "") { |
218 | $theme = "auto" | |
219 | } | |
220 | ||
f4aa76c5 DC |
221 | if (!$lang) { |
222 | my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
223 | $lang = $dc_conf->{language} // 'en'; | |
224 | } | |
225 | ||
4a17e72e DM |
226 | $username = '' if !$username; |
227 | ||
228 | my $mobile = is_phone($r->header('User-Agent')) ? 1 : 0; | |
229 | ||
230 | if (defined($args->{mobile})) { | |
231 | $mobile = $args->{mobile} ? 1 : 0; | |
232 | } | |
233 | ||
03f09f9a DC |
234 | my $novnc = defined($args->{console}) && $args->{novnc}; |
235 | my $xtermjs = defined($args->{console}) && $args->{xtermjs}; | |
236 | ||
184825e1 DC |
237 | my $page = ''; |
238 | my $template = Template->new({ABSOLUTE => 1}); | |
239 | ||
240 | my $langfile = 0; | |
241 | ||
411967db | 242 | if (-f "$basedirs->{i18n}/pve-lang-$lang.js") { |
184825e1 DC |
243 | $langfile = 1; |
244 | } | |
245 | ||
180a86d3 | 246 | my $version = PVE::pvecfg::version(); |
54165ad3 | 247 | |
f90908cb DC |
248 | my $wtversionraw = PVE::Tools::file_read_firstline("$basedirs->{widgettoolkit}/proxmoxlib.js"); |
249 | my $wtversion; | |
250 | if ($wtversionraw =~ m|^// (.*)$|) { | |
251 | $wtversion = $1; | |
252 | }; | |
253 | ||
c195a3c2 TL |
254 | my $debug = $server->{debug}; |
255 | if (exists $args->{debug}) { | |
256 | $debug = !defined($args->{debug}) || $args->{debug}; | |
257 | } | |
258 | ||
184825e1 DC |
259 | my $vars = { |
260 | lang => $lang, | |
261 | langfile => $langfile, | |
262 | username => $username, | |
263 | token => $token, | |
264 | console => $args->{console}, | |
265 | nodename => $nodename, | |
c195a3c2 | 266 | debug => $debug, |
180a86d3 | 267 | version => "$version", |
f90908cb | 268 | wtversion => $wtversion, |
5137b16f | 269 | theme => $theme, |
184825e1 DC |
270 | }; |
271 | ||
272 | # by default, load the normal index | |
273 | my $dir = $basedirs->{manager}; | |
4a17e72e | 274 | |
03f09f9a | 275 | if ($novnc) { |
184825e1 | 276 | $dir = $basedirs->{novnc}; |
03f09f9a DC |
277 | } elsif ($xtermjs) { |
278 | $dir = $basedirs->{xtermjs}; | |
4a17e72e | 279 | } elsif ($mobile) { |
184825e1 | 280 | $dir = "$basedirs->{manager}/touch"; |
2ebf4aec | 281 | } |
184825e1 DC |
282 | |
283 | $template->process("$dir/index.html.tpl", $vars, \$page) | |
284 | || die $template->error(), "\n"; | |
4a17e72e DM |
285 | my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8"); |
286 | my $resp = HTTP::Response->new(200, "OK", $headers, $page); | |
287 | ||
288 | return $resp; | |
289 | } | |
290 | ||
291 | 1; |