]> git.proxmox.com Git - pve-manager.git/blob - PVE/Service/pveproxy.pm
change to debian font-awesome
[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 fontawesome => '/usr/share/fonts-font-awesome',
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, '/pve2/fa/fonts/' => "$basedirs->{fontawesome}/fonts/");
78 add_dirs($dirs, '/pve2/fa/css/' => "$basedirs->{fontawesome}/css/");
79 add_dirs($dirs, '/pve-docs/' => "$basedirs->{docs}/");
80 add_dirs($dirs, '/novnc/' => "$basedirs->{novnc}/");
81
82 $self->{server_config} = {
83 title => 'Proxmox VE API',
84 keep_alive => 100,
85 max_conn => 500,
86 max_requests => 1000,
87 lockfile => $accept_lock_fn,
88 socket => $socket,
89 lockfh => $lockfh,
90 debug => $self->{debug},
91 trusted_env => 0, # not trusted, anyone can connect
92 logfile => '/var/log/pveproxy/access.log',
93 allow_from => $proxyconf->{ALLOW_FROM},
94 deny_from => $proxyconf->{DENY_FROM},
95 policy => $proxyconf->{POLICY},
96 ssl => {
97 # Note: older versions are considered insecure, for example
98 # search for "Poodle"-Attac
99 method => 'any',
100 sslv2 => 0,
101 sslv3 => 0,
102 cipher_list => $proxyconf->{CIPHERS} || 'HIGH:MEDIUM:!aNULL:!MD5',
103 key_file => '/etc/pve/local/pve-ssl.key',
104 cert_file => '/etc/pve/local/pve-ssl.pem',
105 },
106 # Note: there is no authentication for those pages and dirs!
107 pages => {
108 '/' => sub { get_index($self->{nodename}, @_) },
109 # avoid authentication when accessing favicon
110 '/favicon.ico' => {
111 file => "$basedirs->{manager}/images/favicon.ico",
112 },
113 },
114 dirs => $dirs,
115 };
116
117 if ($proxyconf->{DHPARAMS}) {
118 $self->{server_config}->{ssl}->{dh_file} = $proxyconf->{DHPARAMS};
119 } else {
120 $self->{server_config}->{ssl}->{dh} = 'skip2048';
121 }
122
123 if (-f '/etc/pve/local/pveproxy-ssl.pem' && -f '/etc/pve/local/pveproxy-ssl.key') {
124 $self->{server_config}->{ssl}->{cert_file} = '/etc/pve/local/pveproxy-ssl.pem';
125 $self->{server_config}->{ssl}->{key_file} = '/etc/pve/local/pveproxy-ssl.key';
126 syslog('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.');
127 }
128 }
129
130 sub run {
131 my ($self) = @_;
132
133 my $server = PVE::HTTPServer->new(%{$self->{server_config}});
134 $server->run();
135 }
136
137 $daemon->register_start_command();
138 $daemon->register_restart_command(1);
139 $daemon->register_stop_command();
140 $daemon->register_status_command();
141
142 our $cmddef = {
143 start => [ __PACKAGE__, 'start', []],
144 restart => [ __PACKAGE__, 'restart', []],
145 stop => [ __PACKAGE__, 'stop', []],
146 status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
147 };
148
149 sub is_phone {
150 my ($ua) = @_;
151
152 return 0 if !$ua;
153
154 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
155
156 if ($ua =~ m/Mobile(\/|\s)/) {
157 return 1 if $ua =~ m/(BlackBerry|BB)/;
158 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
159 }
160
161 return 0;
162 }
163
164 # NOTE: Requests to those pages are not authenticated
165 # so we must be very careful here
166
167 sub get_index {
168 my ($nodename, $server, $r, $args) = @_;
169
170 my $lang = 'en';
171 my $username;
172 my $token = 'null';
173
174 if (my $cookie = $r->header('Cookie')) {
175 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
176 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
177 $lang = $newlang;
178 }
179 }
180 my $ticket = PVE::APIServer::Formatter::extract_auth_cookie($cookie, $server->{cookie_name});
181 if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) {
182 $token = PVE::AccessControl::assemble_csrf_prevention_token($username);
183 }
184 }
185
186 $username = '' if !$username;
187
188 my $mobile = is_phone($r->header('User-Agent')) ? 1 : 0;
189
190 if (defined($args->{mobile})) {
191 $mobile = $args->{mobile} ? 1 : 0;
192 }
193
194 my $page = '';
195 my $template = Template->new({ABSOLUTE => 1});
196
197 my $langfile = 0;
198
199 if (-f "$basedirs->{manager}/locale/pve-lang-$lang.js") {
200 $langfile = 1;
201 }
202
203 my $vars = {
204 lang => $lang,
205 langfile => $langfile,
206 username => $username,
207 token => $token,
208 console => $args->{console},
209 nodename => $nodename,
210 debug => $server->{debug},
211 };
212
213 # by default, load the normal index
214 my $dir = $basedirs->{manager};
215
216 if (defined($args->{console}) && $args->{novnc}) {
217 $dir = $basedirs->{novnc};
218 } elsif ($mobile) {
219 $dir = "$basedirs->{manager}/touch";
220 }
221
222 $template->process("$dir/index.html.tpl", $vars, \$page)
223 || die $template->error(), "\n";
224 my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
225 my $resp = HTTP::Response->new(200, "OK", $headers, $page);
226
227 return $resp;
228 }
229
230 1;