]> git.proxmox.com Git - pve-manager.git/blob - PVE/Service/pveproxy.pm
use Template::Toolkit instead of [ExtJs|Touch|NoVnc]Index.pm
[pve-manager.git] / PVE / Service / pveproxy.pm
1 package PVE::Service::pveproxy;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Daemon;
8 use HTTP::Response;
9 use Encode;
10 use URI;
11 use URI::QueryParam;
12 use Data::Dumper;
13 use PVE::API2Tools;
14 use PVE::API2;
15 use PVE::APIServer::Formatter;
16 use PVE::APIServer::Formatter::Standard;
17 use PVE::APIServer::Formatter::HTML;
18 use PVE::APIServer::AnyEvent;
19 use PVE::HTTPServer;
20
21 use Template;
22
23 use PVE::Tools;
24
25 use base qw(PVE::Daemon);
26
27 my $cmdline = [$0, @ARGV];
28
29 my %daemon_options = (
30 max_workers => 3,
31 restart_on_error => 5,
32 stop_wait_time => 15,
33 leave_children_open_on_reload => 1,
34 setuid => 'www-data',
35 setgid => 'www-data',
36 pidfile => '/var/run/pveproxy/pveproxy.pid',
37 );
38
39 my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options);
40
41 sub add_dirs {
42 my ($result_hash, $alias, $subdir) = @_;
43
44 PVE::APIServer::AnyEvent::add_dirs($result_hash, $alias, $subdir);
45 }
46
47 my $basedirs = {
48 novnc => '/usr/share/novnc-pve',
49 extjs => '/usr/share/javascript/extjs',
50 manager => '/usr/share/pve-manager',
51 docs => '/usr/share/pve-docs',
52 vncterm => '/usr/share/vncterm',
53 };
54
55 sub init {
56 my ($self) = @_;
57
58 # we use same ALLOW/DENY/POLICY as pveproxy
59 my $proxyconf = PVE::API2Tools::read_proxy_config();
60
61 my $accept_lock_fn = "/var/lock/pveproxy.lck";
62
63 my $lockfh = IO::File->new(">>${accept_lock_fn}") ||
64 die "unable to open lock file '${accept_lock_fn}' - $!\n";
65
66 my $family = PVE::Tools::get_host_address_family($self->{nodename});
67 my $socket = $self->create_reusable_socket(8006, undef, $family);
68
69 my $dirs = {};
70
71 add_dirs($dirs, '/pve2/locale/', "$basedirs->{manager}/locale/");
72 add_dirs($dirs, '/pve2/touch/', "$basedirs->{manager}/touch/");
73 add_dirs($dirs, '/pve2/ext6/', "$basedirs->{extjs}/");
74 add_dirs($dirs, '/pve2/images/' => "$basedirs->{manager}/images/");
75 add_dirs($dirs, '/pve2/css/' => "$basedirs->{manager}/css/");
76 add_dirs($dirs, '/pve2/js/' => "$basedirs->{manager}/js/");
77 add_dirs($dirs, '/pve-docs/' => "$basedirs->{docs}/");
78 add_dirs($dirs, '/vncterm/' => "$basedirs->{vncterm}/");
79 add_dirs($dirs, '/novnc/' => "$basedirs->{novnc}/");
80
81 $self->{server_config} = {
82 title => 'Proxmox VE API',
83 keep_alive => 100,
84 max_conn => 500,
85 max_requests => 1000,
86 lockfile => $accept_lock_fn,
87 socket => $socket,
88 lockfh => $lockfh,
89 debug => $self->{debug},
90 trusted_env => 0, # not trusted, anyone can connect
91 logfile => '/var/log/pveproxy/access.log',
92 allow_from => $proxyconf->{ALLOW_FROM},
93 deny_from => $proxyconf->{DENY_FROM},
94 policy => $proxyconf->{POLICY},
95 ssl => {
96 # Note: older versions are considered insecure, for example
97 # search for "Poodle"-Attac
98 method => 'any',
99 sslv2 => 0,
100 sslv3 => 0,
101 cipher_list => $proxyconf->{CIPHERS} || 'HIGH:MEDIUM:!aNULL:!MD5',
102 key_file => '/etc/pve/local/pve-ssl.key',
103 cert_file => '/etc/pve/local/pve-ssl.pem',
104 },
105 # Note: there is no authentication for those pages and dirs!
106 pages => {
107 '/' => sub { get_index($self->{nodename}, @_) },
108 # avoid authentication when accessing favicon
109 '/favicon.ico' => {
110 file => "$basedirs->{manager}/images/favicon.ico",
111 },
112 },
113 dirs => $dirs,
114 };
115
116 if ($proxyconf->{DHPARAMS}) {
117 $self->{server_config}->{ssl}->{dh_file} = $proxyconf->{DHPARAMS};
118 } else {
119 $self->{server_config}->{ssl}->{dh} = 'skip2048';
120 }
121
122 if (-f '/etc/pve/local/pveproxy-ssl.pem' && -f '/etc/pve/local/pveproxy-ssl.key') {
123 $self->{server_config}->{ssl}->{cert_file} = '/etc/pve/local/pveproxy-ssl.pem';
124 $self->{server_config}->{ssl}->{key_file} = '/etc/pve/local/pveproxy-ssl.key';
125 syslog('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.');
126 }
127 }
128
129 sub run {
130 my ($self) = @_;
131
132 my $server = PVE::HTTPServer->new(%{$self->{server_config}});
133 $server->run();
134 }
135
136 $daemon->register_start_command();
137 $daemon->register_restart_command(1);
138 $daemon->register_stop_command();
139 $daemon->register_status_command();
140
141 our $cmddef = {
142 start => [ __PACKAGE__, 'start', []],
143 restart => [ __PACKAGE__, 'restart', []],
144 stop => [ __PACKAGE__, 'stop', []],
145 status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
146 };
147
148 sub is_phone {
149 my ($ua) = @_;
150
151 return 0 if !$ua;
152
153 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
154
155 if ($ua =~ m/Mobile(\/|\s)/) {
156 return 1 if $ua =~ m/(BlackBerry|BB)/;
157 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
158 }
159
160 return 0;
161 }
162
163 # NOTE: Requests to those pages are not authenticated
164 # so we must be very careful here
165
166 sub get_index {
167 my ($nodename, $server, $r, $args) = @_;
168
169 my $lang = 'en';
170 my $username;
171 my $token = 'null';
172
173 if (my $cookie = $r->header('Cookie')) {
174 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
175 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
176 $lang = $newlang;
177 }
178 }
179 my $ticket = PVE::APIServer::Formatter::extract_auth_cookie($cookie, $server->{cookie_name});
180 if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) {
181 $token = PVE::AccessControl::assemble_csrf_prevention_token($username);
182 }
183 }
184
185 $username = '' if !$username;
186
187 my $mobile = is_phone($r->header('User-Agent')) ? 1 : 0;
188
189 if (defined($args->{mobile})) {
190 $mobile = $args->{mobile} ? 1 : 0;
191 }
192
193 my $page = '';
194 my $template = Template->new({ABSOLUTE => 1});
195
196 my $langfile = 0;
197
198 if (-f "$basedirs->{manager}/locale/pve-lang-$lang.js") {
199 $langfile = 1;
200 }
201
202 my $vars = {
203 lang => $lang,
204 langfile => $langfile,
205 username => $username,
206 token => $token,
207 console => $args->{console},
208 nodename => $nodename,
209 debug => $server->{debug},
210 };
211
212 # by default, load the normal index
213 my $dir = $basedirs->{manager};
214
215 if (defined($args->{console}) && $args->{novnc}) {
216 $dir = $basedirs->{novnc};
217 } elsif ($mobile) {
218 $dir = "$basedirs->{manager}/touch";
219 }
220
221 $template->process("$dir/index.html.tpl", $vars, \$page)
222 || die $template->error(), "\n";
223 my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
224 my $resp = HTTP::Response->new(200, "OK", $headers, $page);
225
226 return $resp;
227 }
228
229 1;