1 package PVE
::Service
::pveproxy
;
14 use PVE
::DataCenterConfig
;
15 use PVE
::APIServer
::Utils
;
17 use PVE
::APIServer
::Formatter
;
18 use PVE
::APIServer
::Formatter
::Standard
;
19 use PVE
::APIServer
::Formatter
::HTML
;
20 use PVE
::APIServer
::AnyEvent
;
28 use base
qw(PVE::Daemon);
30 my $cmdline = [$0, @ARGV];
32 my %daemon_options = (
34 restart_on_error
=> 5,
36 leave_children_open_on_reload
=> 1,
39 pidfile
=> '/var/run/pveproxy/pveproxy.pid',
42 my $daemon = __PACKAGE__-
>new('pveproxy', $cmdline, %daemon_options);
45 my ($result_hash, $alias, $subdir) = @_;
47 PVE
::APIServer
::AnyEvent
::add_dirs
($result_hash, $alias, $subdir);
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',
64 # we use same ALLOW/DENY/POLICY as pveproxy
65 my $proxyconf = PVE
::APIServer
::Utils
::read_proxy_config
($self->{name
});
67 my $accept_lock_fn = "/var/lock/pveproxy.lck";
69 my $lockfh = IO
::File-
>new(">>${accept_lock_fn}") ||
70 die "unable to open lock file '${accept_lock_fn}' - $!\n";
72 my $listen_ip = $proxyconf->{LISTEN_IP
};
73 my $socket = $self->create_reusable_socket(8006, $listen_ip);
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/");
92 $self->{server_config
} = {
93 title
=> 'Proxmox VE API',
97 lockfile
=> $accept_lock_fn,
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
},
107 cipher_list
=> $proxyconf->{CIPHERS
},
108 ciphersuites
=> $proxyconf->{CIPHERSUITES
},
109 key_file
=> '/etc/pve/local/pve-ssl.key',
110 cert_file
=> '/etc/pve/local/pve-ssl.pem',
111 honor_cipher_order
=> $proxyconf->{HONOR_CIPHER_ORDER
},
113 compression
=> $proxyconf->{COMPRESSION
},
114 # Note: there is no authentication for those pages and dirs!
116 '/' => sub { get_index
($self->{nodename
}, @_) },
117 # avoid authentication when accessing favicon
119 file
=> "$basedirs->{manager}/images/favicon.ico",
121 '/proxmoxlib.js' => {
122 file
=> "$basedirs->{widgettoolkit}/proxmoxlib.js",
124 '/qrcode.min.js' => {
125 file
=> '/usr/share/javascript/qrcodejs/qrcode.min.js',
131 if (defined($proxyconf->{DHPARAMS
})) {
132 $self->{server_config
}->{ssl
}->{dh_file
} = $proxyconf->{DHPARAMS
};
134 if (defined($proxyconf->{DISABLE_TLS_1_2
})) {
135 $self->{server_config
}->{ssl
}->{tlsv1_2
} = !$proxyconf->{DISABLE_TLS_1_2
};
137 if (defined($proxyconf->{DISABLE_TLS_1_3
})) {
138 $self->{server_config
}->{ssl
}->{tlsv1_3
} = !$proxyconf->{DISABLE_TLS_1_3
};
140 my $custom_key_path = '/etc/pve/local/pveproxy-ssl.key';
141 if (defined($proxyconf->{TLS_KEY_FILE
})) {
142 $custom_key_path = $proxyconf->{TLS_KEY_FILE
};
144 if (-f
'/etc/pve/local/pveproxy-ssl.pem' && -f
$custom_key_path) {
145 $self->{server_config
}->{ssl
}->{cert_file
} = '/etc/pve/local/pveproxy-ssl.pem';
146 $self->{server_config
}->{ssl
}->{key_file
} = $custom_key_path;
147 syslog
('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.');
154 my $server = PVE
::HTTPServer-
>new(%{$self->{server_config
}});
158 $daemon->register_start_command();
159 $daemon->register_restart_command(1);
160 $daemon->register_stop_command();
161 $daemon->register_status_command();
164 start
=> [ __PACKAGE__
, 'start', []],
165 restart
=> [ __PACKAGE__
, 'restart', []],
166 stop
=> [ __PACKAGE__
, 'stop', []],
167 status
=> [ __PACKAGE__
, 'status', [], undef, sub { print shift . "\n";} ],
175 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
177 if ($ua =~ m/Mobile(\/|\s
)/) {
178 return 1 if $ua =~ m/(BlackBerry|BB)/;
179 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
185 # NOTE: Requests to those pages are not authenticated
186 # so we must be very careful here
189 my ($nodename, $server, $r, $args) = @_;
195 if (my $cookie = $r->header('Cookie')) {
196 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
197 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
201 my $ticket = PVE
::APIServer
::Formatter
::extract_auth_value
($cookie, $server->{cookie_name
});
202 if (($username = PVE
::AccessControl
::verify_ticket
($ticket, 1))) {
203 $token = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
208 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
209 $lang = $dc_conf->{language
} // 'en';
212 $username = '' if !$username;
214 my $mobile = is_phone
($r->header('User-Agent')) ?
1 : 0;
216 if (defined($args->{mobile
})) {
217 $mobile = $args->{mobile
} ?
1 : 0;
220 my $novnc = defined($args->{console
}) && $args->{novnc
};
221 my $xtermjs = defined($args->{console
}) && $args->{xtermjs
};
224 my $template = Template-
>new({ABSOLUTE
=> 1});
228 if (-f
"$basedirs->{i18n}/pve-lang-$lang.js") {
232 my $version = PVE
::pvecfg
::version
();
234 my $wtversionraw = PVE
::Tools
::file_read_firstline
("$basedirs->{widgettoolkit}/proxmoxlib.js");
236 if ($wtversionraw =~ m
|^// (.*)$|) {
240 my $debug = $server->{debug
};
241 if (exists $args->{debug
}) {
242 $debug = !defined($args->{debug
}) || $args->{debug
};
247 langfile
=> $langfile,
248 username
=> $username,
250 console
=> $args->{console
},
251 nodename
=> $nodename,
253 version
=> "$version",
254 wtversion
=> $wtversion,
257 # by default, load the normal index
258 my $dir = $basedirs->{manager
};
261 $dir = $basedirs->{novnc
};
263 $dir = $basedirs->{xtermjs
};
265 $dir = "$basedirs->{manager}/touch";
268 $template->process("$dir/index.html.tpl", $vars, \
$page)
269 || die $template->error(), "\n";
270 my $headers = HTTP
::Headers-
>new(Content_Type
=> "text/html; charset=utf-8");
271 my $resp = HTTP
::Response-
>new(200, "OK", $headers, $page);