]> git.proxmox.com Git - pve-manager.git/blob - PVE/Service/pveproxy.pm
specify basedirs instead of hardcoding them several times
[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 PVE::ExtJSIndex;
22 use PVE::NoVncIndex;
23 use PVE::TouchIndex;
24
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 novnc => '/usr/share/novnc-pve',
51 extjs => '/usr/share/javascript/extjs',
52 manager => '/usr/share/pve-manager',
53 docs => '/usr/share/pve-docs',
54 vncterm => '/usr/share/vncterm',
55 };
56
57 sub init {
58 my ($self) = @_;
59
60 # we use same ALLOW/DENY/POLICY as pveproxy
61 my $proxyconf = PVE::API2Tools::read_proxy_config();
62
63 my $accept_lock_fn = "/var/lock/pveproxy.lck";
64
65 my $lockfh = IO::File->new(">>${accept_lock_fn}") ||
66 die "unable to open lock file '${accept_lock_fn}' - $!\n";
67
68 my $family = PVE::Tools::get_host_address_family($self->{nodename});
69 my $socket = $self->create_reusable_socket(8006, undef, $family);
70
71 my $dirs = {};
72
73 add_dirs($dirs, '/pve2/locale/', "$basedirs->{manager}/locale/");
74 add_dirs($dirs, '/pve2/touch/', "$basedirs->{manager}/touch/");
75 add_dirs($dirs, '/pve2/ext6/', "$basedirs->{extjs}/");
76 add_dirs($dirs, '/pve2/images/' => "$basedirs->{manager}/images/");
77 add_dirs($dirs, '/pve2/css/' => "$basedirs->{manager}/css/");
78 add_dirs($dirs, '/pve2/js/' => "$basedirs->{manager}/js/");
79 add_dirs($dirs, '/pve-docs/' => "$basedirs->{docs}/");
80 add_dirs($dirs, '/vncterm/' => "$basedirs->{vncterm}/");
81 add_dirs($dirs, '/novnc/' => "$basedirs->{novnc}/");
82
83 $self->{server_config} = {
84 title => 'Proxmox VE API',
85 keep_alive => 100,
86 max_conn => 500,
87 max_requests => 1000,
88 lockfile => $accept_lock_fn,
89 socket => $socket,
90 lockfh => $lockfh,
91 debug => $self->{debug},
92 trusted_env => 0, # not trusted, anyone can connect
93 logfile => '/var/log/pveproxy/access.log',
94 allow_from => $proxyconf->{ALLOW_FROM},
95 deny_from => $proxyconf->{DENY_FROM},
96 policy => $proxyconf->{POLICY},
97 ssl => {
98 # Note: older versions are considered insecure, for example
99 # search for "Poodle"-Attac
100 method => 'any',
101 sslv2 => 0,
102 sslv3 => 0,
103 cipher_list => $proxyconf->{CIPHERS} || 'HIGH:MEDIUM:!aNULL:!MD5',
104 key_file => '/etc/pve/local/pve-ssl.key',
105 cert_file => '/etc/pve/local/pve-ssl.pem',
106 },
107 # Note: there is no authentication for those pages and dirs!
108 pages => {
109 '/' => sub { get_index($self->{nodename}, @_) },
110 # avoid authentication when accessing favicon
111 '/favicon.ico' => {
112 file => "$basedirs->{manager}/images/favicon.ico",
113 },
114 },
115 dirs => $dirs,
116 };
117
118 if ($proxyconf->{DHPARAMS}) {
119 $self->{server_config}->{ssl}->{dh_file} = $proxyconf->{DHPARAMS};
120 } else {
121 $self->{server_config}->{ssl}->{dh} = 'skip2048';
122 }
123
124 if (-f '/etc/pve/local/pveproxy-ssl.pem' && -f '/etc/pve/local/pveproxy-ssl.key') {
125 $self->{server_config}->{ssl}->{cert_file} = '/etc/pve/local/pveproxy-ssl.pem';
126 $self->{server_config}->{ssl}->{key_file} = '/etc/pve/local/pveproxy-ssl.key';
127 syslog('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.');
128 }
129 }
130
131 sub run {
132 my ($self) = @_;
133
134 my $server = PVE::HTTPServer->new(%{$self->{server_config}});
135 $server->run();
136 }
137
138 $daemon->register_start_command();
139 $daemon->register_restart_command(1);
140 $daemon->register_stop_command();
141 $daemon->register_status_command();
142
143 our $cmddef = {
144 start => [ __PACKAGE__, 'start', []],
145 restart => [ __PACKAGE__, 'restart', []],
146 stop => [ __PACKAGE__, 'stop', []],
147 status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
148 };
149
150 sub is_phone {
151 my ($ua) = @_;
152
153 return 0 if !$ua;
154
155 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
156
157 if ($ua =~ m/Mobile(\/|\s)/) {
158 return 1 if $ua =~ m/(BlackBerry|BB)/;
159 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
160 }
161
162 return 0;
163 }
164
165 # NOTE: Requests to those pages are not authenticated
166 # so we must be very careful here
167
168 sub get_index {
169 my ($nodename, $server, $r, $args) = @_;
170
171 my $lang = 'en';
172 my $username;
173 my $token = 'null';
174
175 if (my $cookie = $r->header('Cookie')) {
176 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
177 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
178 $lang = $newlang;
179 }
180 }
181 my $ticket = PVE::APIServer::Formatter::extract_auth_cookie($cookie, $server->{cookie_name});
182 if (($username = PVE::AccessControl::verify_ticket($ticket, 1))) {
183 $token = PVE::AccessControl::assemble_csrf_prevention_token($username);
184 }
185 }
186
187 $username = '' if !$username;
188
189 my $mobile = is_phone($r->header('User-Agent')) ? 1 : 0;
190
191 if (defined($args->{mobile})) {
192 $mobile = $args->{mobile} ? 1 : 0;
193 }
194
195 my $page;
196
197 if (defined($args->{console}) && $args->{novnc}) {
198 $page = PVE::NoVncIndex::get_index($lang, $username, $token, $args->{console}, $nodename);
199 } elsif ($mobile) {
200 $page = PVE::TouchIndex::get_index($lang, $username, $token, $args->{console}, $nodename);
201 } else {
202 $page = PVE::ExtJSIndex::get_index($lang, $username, $token, $args->{console}, $nodename,
203 $server->{debug});
204 }
205 my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8");
206 my $resp = HTTP::Response->new(200, "OK", $headers, $page);
207
208 return $resp;
209 }
210
211 1;