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