]>
git.proxmox.com Git - pve-http-server.git/blob - simple-demo.pl
7 use HTTP
::Status
qw(:constants);
10 use PVE
::APIServer
::AnyEvent
;
11 use PVE
::Exception
qw(raise_param_exc);
13 use base
('PVE::APIServer::AnyEvent');
17 my $secret = Digest
::MD5
::md5_base64
($$ . time());
22 return "$username:$secret";
26 my ($self, $method, $rel_uri, $ticket, $token, $peer_host) = @_;
28 # explicitly allow some calls without authentication
29 if ($rel_uri eq '/access/ticket' &&
30 ($method eq 'POST' || $method eq 'GET')) {
31 return; # allow call to create ticket
34 die "no ticket" if !defined($ticket);
36 my ($userid, $rest) = split(/:/, $ticket, 2);
37 die "invalid unsername" if $userid ne 'demo';
38 die "invalid ticket" if $rest ne $secret;
47 my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
50 status
=> HTTP_NOT_IMPLEMENTED
,
51 message
=> "Method '$method $rel_uri' not implemented",
53 if ($rel_uri eq '/access/ticket') {
54 if ($method eq 'POST') {
55 if ($params->{username
} && $params->{username
} eq 'demo' &&
56 $params->{password
} && $params->{password
} eq 'demo') {
60 ticket
=> create_ticket
($params->{username
}),
65 } elsif ($method eq 'GET') {
66 # this is allowed to display the login form
67 return { status
=> HTTP_OK
, data
=> {} };
76 clientip
=> $clientip,
81 info
=> { description
=> "You called API method '$method $rel_uri'" },
94 use Socket
qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN);
99 use PVE
::Tools
qw(run_command);
101 use PVE
::APIServer
::Formatter
::Standard
;
102 use PVE
::APIServer
::Formatter
::HTML
;
104 my $nodename = PVE
::INotify
::nodename
();
107 if (! -f
"simple-demo.pem") {
108 print "generating demo server certificate\n";
109 my $cmd = ['make-ssl-cert', '/usr/share/ssl-cert/ssleay.cnf',
114 my $socket = IO
::Socket
::IP-
>new(
115 LocalAddr
=> $nodename,
119 GetAddrInfoFlags
=> 0,
121 die "unable to create socket - $@\n";
123 # we often observe delays when using Nagle algorithm,
124 # so we disable that to maximize performance
125 setsockopt($socket, IPPROTO_TCP
, TCP_NODELAY
, 1);
127 my $accept_lock_fn = "simple-demo.lck";
128 my $lockfh = IO
::File-
>new(">>${accept_lock_fn}") ||
129 die "unable to open lock file '${accept_lock_fn}' - $!\n";
131 my $server = DemoServer-
>new(
133 lockfile
=> $accept_lock_fn,
135 title
=> 'Simple Demo API',
137 tls_ctx
=> { verify
=> 0, cert_file
=> "simple-demo.pem" },
139 '/' => sub { get_index
($nodename, @_) },
143 # NOTE: Requests to non-API pages are not authenticated
144 # so you must be very careful here
146 my $root_page = <<__EOD__;
150 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
151 <meta http-equiv="X-UA-Compatible" content="IE=edge">
152 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
153 <title>Simple Demo Server</title>
156 <h1>Simple Demo Server ($nodename)</h1>
158 You can browse the API <a href='/api2/html' >here</a>. Please sign
159 in with usrename <b>demo</b> and passwort <b>demo</b>.
166 my ($nodename, $server, $r, $args) = @_;
168 my $headers = HTTP
::Headers-
>new(Content_Type
=> "text/html; charset=utf-8");
169 my $resp = HTTP
::Response-
>new(200, "OK", $headers, $root_page);
173 print "demo server listens at: https://$nodename:$port/\n";