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