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