1 package PVE
::Service
::pveproxy
;
14 use PVE
::APIServer
::AnyEvent
;
15 use PVE
::APIServer
::Formatter
::HTML
;
16 use PVE
::APIServer
::Formatter
::Standard
;
17 use PVE
::APIServer
::Formatter
;
18 use PVE
::APIServer
::Utils
;
21 use PVE
::DataCenterConfig
;
27 use base
qw(PVE::Daemon);
29 my $cmdline = [$0, @ARGV];
31 my %daemon_options = (
33 restart_on_error
=> 5,
35 leave_children_open_on_reload
=> 1,
38 pidfile
=> '/var/run/pveproxy/pveproxy.pid',
41 my $daemon = __PACKAGE__-
>new('pveproxy', $cmdline, %daemon_options);
44 my ($result_hash, $alias, $subdir) = @_;
46 PVE
::APIServer
::AnyEvent
::add_dirs
($result_hash, $alias, $subdir);
50 docs
=> '/usr/share/pve-docs',
51 extjs
=> '/usr/share/javascript/extjs',
52 fontawesome
=> '/usr/share/fonts-font-awesome',
53 fontlogos
=> '/usr/share/fonts-font-logos',
54 i18n
=> '/usr/share/pve-i18n',
55 manager
=> '/usr/share/pve-manager',
56 novnc
=> '/usr/share/novnc-pve',
57 sencha_touch
=> '/usr/share/javascript/sencha-touch',
58 widgettoolkit
=> '/usr/share/javascript/proxmox-widget-toolkit',
59 xtermjs
=> '/usr/share/pve-xtermjs',
65 # we use same ALLOW/DENY/POLICY as pveproxy
66 my $proxyconf = PVE
::APIServer
::Utils
::read_proxy_config
($self->{name
});
68 my $accept_lock_fn = "/var/lock/pveproxy.lck";
70 my $lockfh = IO
::File-
>new(">>${accept_lock_fn}") ||
71 die "unable to open lock file '${accept_lock_fn}' - $!\n";
73 my $listen_ip = $proxyconf->{LISTEN_IP
};
74 my $socket = $self->create_reusable_socket(8006, $listen_ip);
78 add_dirs
($dirs, '/novnc/' => "$basedirs->{novnc}/");
79 add_dirs
($dirs, '/pve-docs/' => "$basedirs->{docs}/");
80 add_dirs
($dirs, '/pve-docs/api-viewer/extjs/' => "$basedirs->{extjs}/");
81 add_dirs
($dirs, '/pve2/css/' => "$basedirs->{manager}/css/");
82 add_dirs
($dirs, '/pve2/ext6/', "$basedirs->{extjs}/");
83 add_dirs
($dirs, '/pve2/fa/css/' => "$basedirs->{fontawesome}/css/");
84 add_dirs
($dirs, '/pve2/fa/fonts/' => "$basedirs->{fontawesome}/fonts/");
85 add_dirs
($dirs, '/pve2/font-logos/' => "$basedirs->{fontlogos}/");
86 add_dirs
($dirs, '/pve2/images/' => "$basedirs->{manager}/images/");
87 add_dirs
($dirs, '/pve2/js/' => "$basedirs->{manager}/js/");
88 add_dirs
($dirs, '/pve2/locale/', "$basedirs->{i18n}/");
89 add_dirs
($dirs, '/pve2/sencha-touch/', "$basedirs->{sencha_touch}/");
90 add_dirs
($dirs, '/pve2/touch/', "$basedirs->{manager}/touch/");
91 add_dirs
($dirs, '/pwt/css/' => "$basedirs->{widgettoolkit}/css/");
92 add_dirs
($dirs, '/pwt/images/' => "$basedirs->{widgettoolkit}/images/");
93 add_dirs
($dirs, '/pwt/themes/' => "$basedirs->{widgettoolkit}/themes/");
94 add_dirs
($dirs, '/xtermjs/' => "$basedirs->{xtermjs}/");
96 $self->{server_config
} = {
97 title
=> 'Proxmox VE API',
100 max_requests
=> 1000,
101 lockfile
=> $accept_lock_fn,
104 debug
=> $self->{debug
},
105 trusted_env
=> 0, # not trusted, anyone can connect
106 logfile
=> '/var/log/pveproxy/access.log',
107 allow_from
=> $proxyconf->{ALLOW_FROM
},
108 deny_from
=> $proxyconf->{DENY_FROM
},
109 policy
=> $proxyconf->{POLICY
},
111 cipher_list
=> $proxyconf->{CIPHERS
},
112 ciphersuites
=> $proxyconf->{CIPHERSUITES
},
113 key_file
=> '/etc/pve/local/pve-ssl.key',
114 cert_file
=> '/etc/pve/local/pve-ssl.pem',
115 honor_cipher_order
=> $proxyconf->{HONOR_CIPHER_ORDER
},
117 compression
=> $proxyconf->{COMPRESSION
},
118 # Note: there is no authentication for those pages and dirs!
120 '/' => sub { get_index
($self->{nodename
}, @_) },
121 # avoid authentication when accessing favicon
123 file
=> "$basedirs->{manager}/images/favicon.ico",
125 '/proxmoxlib.js' => {
126 file
=> "$basedirs->{widgettoolkit}/proxmoxlib.js",
128 '/qrcode.min.js' => {
129 file
=> '/usr/share/javascript/qrcodejs/qrcode.min.js',
135 if (defined($proxyconf->{DHPARAMS
})) {
136 $self->{server_config
}->{ssl
}->{dh_file
} = $proxyconf->{DHPARAMS
};
138 if (defined($proxyconf->{DISABLE_TLS_1_2
})) {
139 $self->{server_config
}->{ssl
}->{tlsv1_2
} = !$proxyconf->{DISABLE_TLS_1_2
};
141 if (defined($proxyconf->{DISABLE_TLS_1_3
})) {
142 $self->{server_config
}->{ssl
}->{tlsv1_3
} = !$proxyconf->{DISABLE_TLS_1_3
};
144 my $custom_key_path = '/etc/pve/local/pveproxy-ssl.key';
145 if (defined($proxyconf->{TLS_KEY_FILE
})) {
146 $custom_key_path = $proxyconf->{TLS_KEY_FILE
};
148 if (-f
'/etc/pve/local/pveproxy-ssl.pem' && -f
$custom_key_path) {
149 $self->{server_config
}->{ssl
}->{cert_file
} = '/etc/pve/local/pveproxy-ssl.pem';
150 $self->{server_config
}->{ssl
}->{key_file
} = $custom_key_path;
151 syslog
('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.');
158 my $server = PVE
::HTTPServer-
>new(%{$self->{server_config
}});
162 $daemon->register_start_command();
163 $daemon->register_restart_command(1);
164 $daemon->register_stop_command();
165 $daemon->register_status_command();
168 start
=> [ __PACKAGE__
, 'start', []],
169 restart
=> [ __PACKAGE__
, 'restart', []],
170 stop
=> [ __PACKAGE__
, 'stop', []],
171 status
=> [ __PACKAGE__
, 'status', [], undef, sub { print shift . "\n";} ],
179 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
181 if ($ua =~ m/Mobile(\/|\s
)/) {
182 return 1 if $ua =~ m/(BlackBerry|BB)/;
183 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
189 # NOTE: Requests to those pages are not authenticated
190 # so we must be very careful here
193 my ($nodename, $server, $r, $args) = @_;
200 if (my $cookie = $r->header('Cookie')) {
201 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
202 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
207 if (my $newtheme = ($cookie =~ /(?:^|\s)PVEThemeCookie=([^;]*)/)[0]) {
208 # theme names need to be kebab case, with each segment a maximum of 10 characters long
209 # and at most 6 segments
210 if ($newtheme =~ m/^[a-z]{1,10}(-[a-z]{1,10}){0,5}$/) {
215 my $ticket = PVE
::APIServer
::Formatter
::extract_auth_value
($cookie, $server->{cookie_name
});
216 if (($username = PVE
::AccessControl
::verify_ticket
($ticket, 1))) {
217 $token = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
222 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
223 $lang = $dc_conf->{language
} // 'en';
226 my $mobile = (is_phone
($r->header('User-Agent')) && (!defined($args->{mobile
}) || $args->{mobile
})) || $args->{mobile
};
228 my $novnc = defined($args->{console
}) && $args->{novnc
};
229 my $xtermjs = defined($args->{console
}) && $args->{xtermjs
};
231 my $langfile = -f
"$basedirs->{i18n}/pve-lang-$lang.js" ?
1 : 0;
233 my $version = PVE
::pvecfg
::version
();
235 my $wtversionraw = PVE
::Tools
::file_read_firstline
("$basedirs->{widgettoolkit}/proxmoxlib.js");
236 my $wtversion = $wtversionraw =~ m
|^// (.*)$| ?
$1 : '';
238 my $debug = $server->{debug
};
239 if (exists $args->{debug
}) {
240 $debug = !defined($args->{debug
}) || $args->{debug
};
245 langfile
=> $langfile,
246 username
=> $username || '',
248 console
=> $args->{console
},
249 nodename
=> $nodename,
251 version
=> "$version",
252 wtversion
=> $wtversion,
256 # by default, load the normal index
257 my $dir = $basedirs->{manager
};
260 $dir = $basedirs->{novnc
};
262 $dir = $basedirs->{xtermjs
};
264 $dir = "$basedirs->{manager}/touch";
268 my $template = Template-
>new({ABSOLUTE
=> 1});
270 $template->process("$dir/index.html.tpl", $vars, \
$page) || die $template->error(), "\n";
272 my $headers = HTTP
::Headers-
>new(Content_Type
=> "text/html; charset=utf-8");
273 my $resp = HTTP
::Response-
>new(200, "OK", $headers, $page);