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