]>
Commit | Line | Data |
---|---|---|
1360e6f0 DM |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use lib '.'; | |
6 | ||
7 | use Socket qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN); | |
8 | use IO::Socket::IP; | |
9 | use HTTP::Headers; | |
10 | use HTTP::Response; | |
11 | use Data::Dumper; | |
12 | ||
1360e6f0 DM |
13 | use PVE::INotify; |
14 | use PVE::APIServer::Formatter::Standard; | |
15 | use PVE::APIServer::Formatter::HTML; | |
16 | ||
17 | use PMG::HTTPServer; | |
9d7f54a3 | 18 | use PMG::Ticket; |
1360e6f0 DM |
19 | |
20 | my $nodename = PVE::INotify::nodename(); | |
21 | my $port = 9999; | |
9d7f54a3 | 22 | my $cert_file = PMG::Ticket::generate_api_cert($nodename); |
d883fac2 | 23 | PMG::Ticket::generate_csrf_key(); |
c151b711 | 24 | PMG::Ticket::generate_auth_key(); |
1360e6f0 DM |
25 | |
26 | my $socket = IO::Socket::IP->new( | |
27 | LocalAddr => $nodename, | |
28 | LocalPort => $port, | |
29 | Listen => SOMAXCONN, | |
30 | Proto => 'tcp', | |
31 | GetAddrInfoFlags => 0, | |
32 | ReuseAddr => 1) || | |
33 | die "unable to create socket - $@\n"; | |
34 | ||
35 | # we often observe delays when using Nagle algorithm, | |
36 | # so we disable that to maximize performance | |
37 | setsockopt($socket, IPPROTO_TCP, TCP_NODELAY, 1); | |
38 | ||
39 | my $accept_lock_fn = "simple-demo.lck"; | |
40 | my $lockfh = IO::File->new(">>${accept_lock_fn}") || | |
41 | die "unable to open lock file '${accept_lock_fn}' - $!\n"; | |
42 | ||
43 | my $server = PMG::HTTPServer->new( | |
44 | debug => 1, | |
45 | socket => $socket, | |
46 | lockfile => $accept_lock_fn, | |
47 | lockfh => $lockfh, | |
48 | title => 'Proxmox Mail Gateway API', | |
49 | cookie_name => 'PMG', | |
50 | logfh => \*STDOUT, | |
51 | tls_ctx => { verify => 0, cert_file => $cert_file }, | |
52 | pages => { | |
53 | '/' => sub { get_index($nodename, @_) }, | |
54 | }, | |
55 | ); | |
56 | ||
57 | # NOTE: Requests to non-API pages are not authenticated | |
58 | # so you must be very careful here | |
59 | ||
60 | my $root_page = <<__EOD__; | |
61 | <!DOCTYPE html> | |
62 | <html> | |
63 | <head> | |
64 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
65 | <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
66 | <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> | |
67 | <title>Simple Demo Server</title> | |
68 | </head> | |
69 | <body> | |
70 | <h1>Proxmox Mail Gateway API ($nodename)</h1> | |
71 | ||
72 | You can browse the API <a href='/api2/html' >here</a>. Please sign | |
73 | in with usrename <b>demo</b> and passwort <b>demo</b>. | |
74 | ||
75 | </body> | |
76 | </html> | |
77 | __EOD__ | |
78 | ||
79 | sub get_index { | |
80 | my ($nodename, $server, $r, $args) = @_; | |
81 | ||
82 | my $headers = HTTP::Headers->new(Content_Type => "text/html; charset=utf-8"); | |
83 | my $resp = HTTP::Response->new(200, "OK", $headers, $root_page); | |
84 | ||
85 | } | |
86 | ||
87 | print "demo server listens at: https://$nodename:$port/\n"; | |
88 | ||
89 | $server->run(); |