]> git.proxmox.com Git - pve-manager.git/blame - PVE/Service/pveproxy.pm
add 'auto' mode for noVNC scaling
[pve-manager.git] / PVE / Service / pveproxy.pm
CommitLineData
4a17e72e
DM
1package PVE::Service::pveproxy;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
7use PVE::Daemon;
8use HTTP::Response;
9use Encode;
10use URI;
11use URI::QueryParam;
4a17e72e 12use Data::Dumper;
f4aa76c5 13use PVE::Cluster;
3ac3653e 14use PVE::DataCenterConfig;
a642f8a0 15use PVE::APIServer::Utils;
4a17e72e 16use PVE::API2;
b996e6c0
DM
17use PVE::APIServer::Formatter;
18use PVE::APIServer::Formatter::Standard;
19use PVE::APIServer::Formatter::HTML;
20use PVE::APIServer::AnyEvent;
4a17e72e 21use PVE::HTTPServer;
54165ad3 22use PVE::pvecfg;
4a17e72e 23
184825e1 24use Template;
4a17e72e
DM
25
26use PVE::Tools;
27
28use base qw(PVE::Daemon);
29
30my $cmdline = [$0, @ARGV];
31
32my %daemon_options = (
33 max_workers => 3,
34 restart_on_error => 5,
35 stop_wait_time => 15,
36 leave_children_open_on_reload => 1,
37 setuid => 'www-data',
38 setgid => 'www-data',
39 pidfile => '/var/run/pveproxy/pveproxy.pid',
40);
41
42my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options);
43
44sub add_dirs {
45 my ($result_hash, $alias, $subdir) = @_;
46
e4697709 47 PVE::APIServer::AnyEvent::add_dirs($result_hash, $alias, $subdir);
4a17e72e
DM
48}
49
245e567e
DC
50my $basedirs = {
51 novnc => '/usr/share/novnc-pve',
52 extjs => '/usr/share/javascript/extjs',
53 manager => '/usr/share/pve-manager',
411967db 54 i18n => '/usr/share/pve-i18n',
245e567e 55 docs => '/usr/share/pve-docs',
7f3b89a0 56 fontawesome => '/usr/share/fonts-font-awesome',
03f09f9a 57 xtermjs => '/usr/share/pve-xtermjs',
f90908cb 58 widgettoolkit => '/usr/share/javascript/proxmox-widget-toolkit',
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
411967db 77 add_dirs($dirs, '/pve2/locale/', "$basedirs->{i18n}/");
245e567e
DC
78 add_dirs($dirs, '/pve2/touch/', "$basedirs->{manager}/touch/");
79 add_dirs($dirs, '/pve2/ext6/', "$basedirs->{extjs}/");
80 add_dirs($dirs, '/pve2/images/' => "$basedirs->{manager}/images/");
81 add_dirs($dirs, '/pve2/css/' => "$basedirs->{manager}/css/");
82 add_dirs($dirs, '/pve2/js/' => "$basedirs->{manager}/js/");
7f3b89a0
DC
83 add_dirs($dirs, '/pve2/fa/fonts/' => "$basedirs->{fontawesome}/fonts/");
84 add_dirs($dirs, '/pve2/fa/css/' => "$basedirs->{fontawesome}/css/");
245e567e 85 add_dirs($dirs, '/pve-docs/' => "$basedirs->{docs}/");
b2682f33 86 add_dirs($dirs, '/pve-docs/api-viewer/extjs/' => "$basedirs->{extjs}/");
245e567e 87 add_dirs($dirs, '/novnc/' => "$basedirs->{novnc}/");
03f09f9a 88 add_dirs($dirs, '/xtermjs/' => "$basedirs->{xtermjs}/");
cc8c253f
DC
89 add_dirs($dirs, '/pwt/images/' => "$basedirs->{widgettoolkit}/images/");
90 add_dirs($dirs, '/pwt/css/' => "$basedirs->{widgettoolkit}/css/");
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},
4a17e72e
DM
108 key_file => '/etc/pve/local/pve-ssl.key',
109 cert_file => '/etc/pve/local/pve-ssl.pem',
95035118 110 honor_cipher_order => $proxyconf->{HONOR_CIPHER_ORDER},
4a17e72e 111 },
a33abad1 112 compression => $proxyconf->{COMPRESSION},
4a17e72e
DM
113 # Note: there is no authentication for those pages and dirs!
114 pages => {
c7f32808 115 '/' => sub { get_index($self->{nodename}, @_) },
4a17e72e
DM
116 # avoid authentication when accessing favicon
117 '/favicon.ico' => {
245e567e 118 file => "$basedirs->{manager}/images/favicon.ico",
4a17e72e 119 },
f90908cb
DC
120 '/proxmoxlib.js' => {
121 file => "$basedirs->{widgettoolkit}/proxmoxlib.js",
122 },
6b2028cb
WB
123 '/qrcode.min.js' => {
124 file => '/usr/share/javascript/qrcodejs/qrcode.min.js',
125 },
4a17e72e
DM
126 },
127 dirs => $dirs,
128 };
41196653 129
95035118 130 if (defined($proxyconf->{DHPARAMS})) {
41196653 131 $self->{server_config}->{ssl}->{dh_file} = $proxyconf->{DHPARAMS};
41196653 132 }
299d290c
FG
133 if (-f '/etc/pve/local/pveproxy-ssl.pem' && -f '/etc/pve/local/pveproxy-ssl.key') {
134 $self->{server_config}->{ssl}->{cert_file} = '/etc/pve/local/pveproxy-ssl.pem';
135 $self->{server_config}->{ssl}->{key_file} = '/etc/pve/local/pveproxy-ssl.key';
136 syslog('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.');
137 }
4a17e72e
DM
138}
139
140sub run {
141 my ($self) = @_;
142
143 my $server = PVE::HTTPServer->new(%{$self->{server_config}});
144 $server->run();
145}
146
147$daemon->register_start_command();
148$daemon->register_restart_command(1);
149$daemon->register_stop_command();
150$daemon->register_status_command();
151
152our $cmddef = {
153 start => [ __PACKAGE__, 'start', []],
154 restart => [ __PACKAGE__, 'restart', []],
155 stop => [ __PACKAGE__, 'stop', []],
156 status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
157};
158
159sub is_phone {
160 my ($ua) = @_;
161
162 return 0 if !$ua;
163
164 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
165
166 if ($ua =~ m/Mobile(\/|\s)/) {
167 return 1 if $ua =~ m/(BlackBerry|BB)/;
168 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
169 }
170
171 return 0;
172}
173
174# NOTE: Requests to those pages are not authenticated
175# so we must be very careful here
176
177sub get_index {
c7f32808 178 my ($nodename, $server, $r, $args) = @_;
4a17e72e 179
f4aa76c5 180 my $lang;
4a17e72e
DM
181 my $username;
182 my $token = 'null';
183
184 if (my $cookie = $r->header('Cookie')) {
185 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
186 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
187 $lang = $newlang;
188 }
189 }
9a5a1655 190 my $ticket = PVE::APIServer::Formatter::extract_auth_value($cookie, $server->{cookie_name});
4a17e72e
DM
191 if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) {
192 $token = PVE::AccessControl::assemble_csrf_prevention_token($username);
193 }
194 }
195
f4aa76c5
DC
196 if (!$lang) {
197 my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
198 $lang = $dc_conf->{language} // 'en';
199 }
200
4a17e72e
DM
201 $username = '' if !$username;
202
203 my $mobile = is_phone($r->header('User-Agent')) ? 1 : 0;
204
205 if (defined($args->{mobile})) {
206 $mobile = $args->{mobile} ? 1 : 0;
207 }
208
03f09f9a
DC
209 my $novnc = defined($args->{console}) && $args->{novnc};
210 my $xtermjs = defined($args->{console}) && $args->{xtermjs};
211
184825e1
DC
212 my $page = '';
213 my $template = Template->new({ABSOLUTE => 1});
214
215 my $langfile = 0;
216
411967db 217 if (-f "$basedirs->{i18n}/pve-lang-$lang.js") {
184825e1
DC
218 $langfile = 1;
219 }
220
180a86d3 221 my $version = PVE::pvecfg::version();
54165ad3 222
f90908cb
DC
223 my $wtversionraw = PVE::Tools::file_read_firstline("$basedirs->{widgettoolkit}/proxmoxlib.js");
224 my $wtversion;
225 if ($wtversionraw =~ m|^// (.*)$|) {
226 $wtversion = $1;
227 };
228
c195a3c2
TL
229 my $debug = $server->{debug};
230 if (exists $args->{debug}) {
231 $debug = !defined($args->{debug}) || $args->{debug};
232 }
233
184825e1
DC
234 my $vars = {
235 lang => $lang,
236 langfile => $langfile,
237 username => $username,
238 token => $token,
239 console => $args->{console},
240 nodename => $nodename,
c195a3c2 241 debug => $debug,
180a86d3 242 version => "$version",
f90908cb 243 wtversion => $wtversion,
184825e1
DC
244 };
245
246 # by default, load the normal index
247 my $dir = $basedirs->{manager};
4a17e72e 248
03f09f9a 249 if ($novnc) {
184825e1 250 $dir = $basedirs->{novnc};
03f09f9a
DC
251 } elsif ($xtermjs) {
252 $dir = $basedirs->{xtermjs};
4a17e72e 253 } elsif ($mobile) {
184825e1 254 $dir = "$basedirs->{manager}/touch";
2ebf4aec 255 }
184825e1
DC
256
257 $template->process("$dir/index.html.tpl", $vars, \$page)
258 || die $template->error(), "\n";
4a17e72e
DM
259 my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
260 my $resp = HTTP::Response->new(200, "OK", $headers, $page);
261
262 return $resp;
263}
264
2651;