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