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 novnc
=> '/usr/share/novnc-pve',
51 extjs
=> '/usr/share/javascript/extjs',
52 manager
=> '/usr/share/pve-manager',
53 i18n
=> '/usr/share/pve-i18n',
54 docs
=> '/usr/share/pve-docs',
55 fontawesome
=> '/usr/share/fonts-font-awesome',
56 xtermjs
=> '/usr/share/pve-xtermjs',
57 widgettoolkit
=> '/usr/share/javascript/proxmox-widget-toolkit',
63 # we use same ALLOW/DENY/POLICY as pveproxy
64 my $proxyconf = PVE
::APIServer
::Utils
::read_proxy_config
($self->{name
});
66 my $accept_lock_fn = "/var/lock/pveproxy.lck";
68 my $lockfh = IO
::File-
>new(">>${accept_lock_fn}") ||
69 die "unable to open lock file '${accept_lock_fn}' - $!\n";
71 my $listen_ip = $proxyconf->{LISTEN_IP
};
72 my $socket = $self->create_reusable_socket(8006, $listen_ip);
76 add_dirs
($dirs, '/novnc/' => "$basedirs->{novnc}/");
77 add_dirs
($dirs, '/pve-docs/' => "$basedirs->{docs}/");
78 add_dirs
($dirs, '/pve-docs/api-viewer/extjs/' => "$basedirs->{extjs}/");
79 add_dirs
($dirs, '/pve2/css/' => "$basedirs->{manager}/css/");
80 add_dirs
($dirs, '/pve2/ext6/', "$basedirs->{extjs}/");
81 add_dirs
($dirs, '/pve2/fa/css/' => "$basedirs->{fontawesome}/css/");
82 add_dirs
($dirs, '/pve2/fa/fonts/' => "$basedirs->{fontawesome}/fonts/");
83 add_dirs
($dirs, '/pve2/images/' => "$basedirs->{manager}/images/");
84 add_dirs
($dirs, '/pve2/js/' => "$basedirs->{manager}/js/");
85 add_dirs
($dirs, '/pve2/locale/', "$basedirs->{i18n}/");
86 add_dirs
($dirs, '/pve2/touch/', "$basedirs->{manager}/touch/");
87 add_dirs
($dirs, '/pwt/css/' => "$basedirs->{widgettoolkit}/css/");
88 add_dirs
($dirs, '/pwt/images/' => "$basedirs->{widgettoolkit}/images/");
89 add_dirs
($dirs, '/pwt/themes/' => "$basedirs->{widgettoolkit}/themes/");
90 add_dirs
($dirs, '/xtermjs/' => "$basedirs->{xtermjs}/");
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) = @_;
196 if (my $cookie = $r->header('Cookie')) {
197 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
198 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
203 if (my $newtheme = ($cookie =~ /(?:^|\s)PVEThemeCookie=([^;]*)/)[0]) {
204 # theme names need to be kebab case, with each segment a maximum of 10 characters long
205 # and at most 6 segments
206 if ($newtheme =~ m/^[a-z]{1,10}(-[a-z]{1,10}){0,5}$/) {
211 my $ticket = PVE
::APIServer
::Formatter
::extract_auth_value
($cookie, $server->{cookie_name
});
212 if (($username = PVE
::AccessControl
::verify_ticket
($ticket, 1))) {
213 $token = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
218 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
219 $lang = $dc_conf->{language
} // 'en';
222 my $mobile = is_phone
($r->header('User-Agent')) && (!defined($args->{mobile
}) || $args->{mobile
});
224 my $novnc = defined($args->{console
}) && $args->{novnc
};
225 my $xtermjs = defined($args->{console
}) && $args->{xtermjs
};
227 my $langfile = -f
"$basedirs->{i18n}/pve-lang-$lang.js" ?
1 : 0;
229 my $version = PVE
::pvecfg
::version
();
231 my $wtversionraw = PVE
::Tools
::file_read_firstline
("$basedirs->{widgettoolkit}/proxmoxlib.js");
232 my $wtversion = $wtversionraw =~ m
|^// (.*)$| ?
$1 : '';
234 my $debug = $server->{debug
};
235 if (exists $args->{debug
}) {
236 $debug = !defined($args->{debug
}) || $args->{debug
};
241 langfile
=> $langfile,
242 username
=> $username || '',
244 console
=> $args->{console
},
245 nodename
=> $nodename,
247 version
=> "$version",
248 wtversion
=> $wtversion,
252 # by default, load the normal index
253 my $dir = $basedirs->{manager
};
256 $dir = $basedirs->{novnc
};
258 $dir = $basedirs->{xtermjs
};
260 $dir = "$basedirs->{manager}/touch";
264 my $template = Template-
>new({ABSOLUTE
=> 1});
266 $template->process("$dir/index.html.tpl", $vars, \
$page) || die $template->error(), "\n";
268 my $headers = HTTP
::Headers-
>new(Content_Type
=> "text/html; charset=utf-8");
269 my $resp = HTTP
::Response-
>new(200, "OK", $headers, $page);