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 i18n
=> '/usr/share/pve-i18n',
54 manager
=> '/usr/share/pve-manager',
55 novnc
=> '/usr/share/novnc-pve',
56 sencha_touch
=> '/usr/share/javascript/sencha-touch',
57 widgettoolkit
=> '/usr/share/javascript/proxmox-widget-toolkit',
58 xtermjs
=> '/usr/share/pve-xtermjs',
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, '/novnc/' => "$basedirs->{novnc}/");
78 add_dirs
($dirs, '/pve-docs/' => "$basedirs->{docs}/");
79 add_dirs
($dirs, '/pve-docs/api-viewer/extjs/' => "$basedirs->{extjs}/");
80 add_dirs
($dirs, '/pve2/css/' => "$basedirs->{manager}/css/");
81 add_dirs
($dirs, '/pve2/ext6/', "$basedirs->{extjs}/");
82 add_dirs
($dirs, '/pve2/fa/css/' => "$basedirs->{fontawesome}/css/");
83 add_dirs
($dirs, '/pve2/fa/fonts/' => "$basedirs->{fontawesome}/fonts/");
84 add_dirs
($dirs, '/pve2/images/' => "$basedirs->{manager}/images/");
85 add_dirs
($dirs, '/pve2/js/' => "$basedirs->{manager}/js/");
86 add_dirs
($dirs, '/pve2/locale/', "$basedirs->{i18n}/");
87 add_dirs
($dirs, '/pve2/sencha-touch/', "$basedirs->{sencha_touch}/");
88 add_dirs
($dirs, '/pve2/touch/', "$basedirs->{manager}/touch/");
89 add_dirs
($dirs, '/pwt/css/' => "$basedirs->{widgettoolkit}/css/");
90 add_dirs
($dirs, '/pwt/images/' => "$basedirs->{widgettoolkit}/images/");
91 add_dirs
($dirs, '/pwt/themes/' => "$basedirs->{widgettoolkit}/themes/");
92 add_dirs
($dirs, '/xtermjs/' => "$basedirs->{xtermjs}/");
94 $self->{server_config
} = {
95 title
=> 'Proxmox VE API',
99 lockfile
=> $accept_lock_fn,
102 debug
=> $self->{debug
},
103 trusted_env
=> 0, # not trusted, anyone can connect
104 logfile
=> '/var/log/pveproxy/access.log',
105 allow_from
=> $proxyconf->{ALLOW_FROM
},
106 deny_from
=> $proxyconf->{DENY_FROM
},
107 policy
=> $proxyconf->{POLICY
},
109 cipher_list
=> $proxyconf->{CIPHERS
},
110 ciphersuites
=> $proxyconf->{CIPHERSUITES
},
111 key_file
=> '/etc/pve/local/pve-ssl.key',
112 cert_file
=> '/etc/pve/local/pve-ssl.pem',
113 honor_cipher_order
=> $proxyconf->{HONOR_CIPHER_ORDER
},
115 compression
=> $proxyconf->{COMPRESSION
},
116 # Note: there is no authentication for those pages and dirs!
118 '/' => sub { get_index
($self->{nodename
}, @_) },
119 # avoid authentication when accessing favicon
121 file
=> "$basedirs->{manager}/images/favicon.ico",
123 '/proxmoxlib.js' => {
124 file
=> "$basedirs->{widgettoolkit}/proxmoxlib.js",
126 '/qrcode.min.js' => {
127 file
=> '/usr/share/javascript/qrcodejs/qrcode.min.js',
133 if (defined($proxyconf->{DHPARAMS
})) {
134 $self->{server_config
}->{ssl
}->{dh_file
} = $proxyconf->{DHPARAMS
};
136 if (defined($proxyconf->{DISABLE_TLS_1_2
})) {
137 $self->{server_config
}->{ssl
}->{tlsv1_2
} = !$proxyconf->{DISABLE_TLS_1_2
};
139 if (defined($proxyconf->{DISABLE_TLS_1_3
})) {
140 $self->{server_config
}->{ssl
}->{tlsv1_3
} = !$proxyconf->{DISABLE_TLS_1_3
};
142 my $custom_key_path = '/etc/pve/local/pveproxy-ssl.key';
143 if (defined($proxyconf->{TLS_KEY_FILE
})) {
144 $custom_key_path = $proxyconf->{TLS_KEY_FILE
};
146 if (-f
'/etc/pve/local/pveproxy-ssl.pem' && -f
$custom_key_path) {
147 $self->{server_config
}->{ssl
}->{cert_file
} = '/etc/pve/local/pveproxy-ssl.pem';
148 $self->{server_config
}->{ssl
}->{key_file
} = $custom_key_path;
149 syslog
('info', 'Using \'/etc/pve/local/pveproxy-ssl.pem\' as certificate for the web interface.');
156 my $server = PVE
::HTTPServer-
>new(%{$self->{server_config
}});
160 $daemon->register_start_command();
161 $daemon->register_restart_command(1);
162 $daemon->register_stop_command();
163 $daemon->register_status_command();
166 start
=> [ __PACKAGE__
, 'start', []],
167 restart
=> [ __PACKAGE__
, 'restart', []],
168 stop
=> [ __PACKAGE__
, 'stop', []],
169 status
=> [ __PACKAGE__
, 'status', [], undef, sub { print shift . "\n";} ],
177 return 1 if $ua =~ m/(iPhone|iPod|Windows Phone)/;
179 if ($ua =~ m/Mobile(\/|\s
)/) {
180 return 1 if $ua =~ m/(BlackBerry|BB)/;
181 return 1 if ($ua =~ m/(Android)/) && ($ua !~ m/(Silk)/);
187 # NOTE: Requests to those pages are not authenticated
188 # so we must be very careful here
191 my ($nodename, $server, $r, $args) = @_;
198 if (my $cookie = $r->header('Cookie')) {
199 if (my $newlang = ($cookie =~ /(?:^|\s)PVELangCookie=([^;]*)/)[0]) {
200 if ($newlang =~ m/^[a-z]{2,3}(_[A-Z]{2,3})?$/) {
205 if (my $newtheme = ($cookie =~ /(?:^|\s)PVEThemeCookie=([^;]*)/)[0]) {
206 # theme names need to be kebab case, with each segment a maximum of 10 characters long
207 # and at most 6 segments
208 if ($newtheme =~ m/^[a-z]{1,10}(-[a-z]{1,10}){0,5}$/) {
213 my $ticket = PVE
::APIServer
::Formatter
::extract_auth_value
($cookie, $server->{cookie_name
});
214 if (($username = PVE
::AccessControl
::verify_ticket
($ticket, 1))) {
215 $token = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
220 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
221 $lang = $dc_conf->{language
} // 'en';
224 my $mobile = (is_phone
($r->header('User-Agent')) && (!defined($args->{mobile
}) || $args->{mobile
})) || $args->{mobile
};
226 my $novnc = defined($args->{console
}) && $args->{novnc
};
227 my $xtermjs = defined($args->{console
}) && $args->{xtermjs
};
229 my $langfile = -f
"$basedirs->{i18n}/pve-lang-$lang.js" ?
1 : 0;
231 my $version = PVE
::pvecfg
::version
();
233 my $wtversionraw = PVE
::Tools
::file_read_firstline
("$basedirs->{widgettoolkit}/proxmoxlib.js");
234 my $wtversion = $wtversionraw =~ m
|^// (.*)$| ?
$1 : '';
236 my $debug = $server->{debug
};
237 if (exists $args->{debug
}) {
238 $debug = !defined($args->{debug
}) || $args->{debug
};
243 langfile
=> $langfile,
244 username
=> $username || '',
246 console
=> $args->{console
},
247 nodename
=> $nodename,
249 version
=> "$version",
250 wtversion
=> $wtversion,
254 # by default, load the normal index
255 my $dir = $basedirs->{manager
};
258 $dir = $basedirs->{novnc
};
260 $dir = $basedirs->{xtermjs
};
262 $dir = "$basedirs->{manager}/touch";
266 my $template = Template-
>new({ABSOLUTE
=> 1});
268 $template->process("$dir/index.html.tpl", $vars, \
$page) || 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);