Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. package App::TCC::WWWserver;
  2.  
  3. use 5.012001;
  4. use strict;
  5. use warnings;
  6.  
  7. use Data::Dumper;
  8.  
  9. use URI;
  10. use URI::QueryParam;
  11.  
  12. use CGI qw/:standard -no_xhtml/;
  13. use CGI::Cookie;
  14.  
  15. use POE qw(Component::Server::TCP Filter::HTTPD);
  16.  
  17. require Exporter;
  18. use AutoLoader qw(AUTOLOAD);
  19.  
  20. our @ISA = qw(Exporter);
  21.  
  22. our @EXPORT = qw();
  23.  
  24. our $VERSION = '0.01';
  25.  
  26. my $css=<<END;
  27.  
  28. html { width:900px; }
  29. body { width:900px; }
  30. .loginbox {
  31. margin-left: auto;
  32. margin-right: auto;
  33. background-color: #defbfa;
  34. width: 204px;
  35. }
  36. .loginbox input { margin: 0.1em 2em; }
  37. .smallheader { font-size: 12pt; color: black; }
  38. .submitbox {
  39. margin-left: auto;
  40. margin-right: auto;
  41. background-color: #defbfa;
  42. width: 180px;
  43. }
  44. .submitbtn {
  45. margin-top: 6px;
  46. width: 180px;
  47. height: 30px;
  48. }
  49. END
  50.  
  51. 1;
  52.  
  53. sub new {
  54. my $package = shift;
  55. my %opts = %{$_[0]} if ($_[0]);
  56. $opts{ lc $_ } = delete $opts{$_} for keys %opts; # convert opts to lower case
  57. my $self = bless \%opts, $package;
  58.  
  59. $self->{start} = time;
  60. $self->{cycles} = 0;
  61.  
  62. # die "username: $self->{user}, password: $self->{pass}";
  63.  
  64. $self->{me} = POE::Component::Server::TCP->new(
  65. Alias => "webserver",
  66. Port => 8088,
  67. ClientFilter => 'POE::Filter::HTTPD',
  68. ClientInput => sub {
  69. my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0];
  70.  
  71. if ($request->isa("HTTP::Response")) {
  72. $heap->{client}->put($request);
  73. $kernel->yield("shutdown");
  74. return;
  75. }
  76.  
  77. my $headers;
  78. my $uri = URI->new($request->uri());
  79. my $cgi = CGI->new();
  80. my $authed = 0;
  81.  
  82. my %c = CGI::Cookie->parse($request->header('Cookie')) if ($request->header('Cookie'));
  83.  
  84. # Record wether its a post, get etc..
  85.  
  86. $headers->{target}->{method} = $request->method();
  87.  
  88. $headers->{param}->{compat} = "";
  89.  
  90. $request->headers()->scan(
  91. sub {
  92. my ($header, $value) = @_;
  93. $headers->{request}->{$header} = $value
  94. }
  95. );
  96. if ($headers->{target}->{method} eq 'POST') {
  97. foreach my $pair (split(m#&#,$request->content())) {
  98. my ($key,$value) = $pair =~ m#(.*?)=(.*)$#;
  99. $headers->{param}->{$key} = $value;
  100. }
  101. }
  102.  
  103. # Find any values passed to the script
  104.  
  105. foreach my $key ($uri->query_param) {
  106. $headers->{param}->{compat} .= "$key=".$uri->query_param($key)."\n";
  107. $headers->{param}->{$key} = $uri->query_param($key);
  108. }
  109.  
  110. # Find The target refference
  111.  
  112. if ($request->uri() =~ m#\?#) {
  113. ($headers->{target}->{url}) = $request->uri() =~ m#^(.*?)\?#;
  114. } else {
  115. $headers->{target}->{url} = $request->uri()
  116. }
  117.  
  118. # Send a 200 (TODO)
  119.  
  120. print STDERR "Serving [$headers->{target}->{method}]: $headers->{target}->{url}\n";
  121.  
  122. # Generate a response object
  123.  
  124. my $response;
  125.  
  126. given($headers->{target}->{url}) {
  127. when(m#^/$#) {
  128. $response = HTTP::Response->new(200);
  129. $response->push_header('Content-type', 'text/html');
  130.  
  131. my $username = "";
  132. my $emsg = "";
  133.  
  134. if ($headers->{target}->{method} eq 'POST') {
  135. warn "Its a post!";
  136. if (($headers->{param}->{username}) && ($headers->{param}->{password})) {
  137. if (($headers->{param}->{username} eq $self->{user}) && ($headers->{param}->{password} eq $self->{pass})) { $authed = 1 }
  138. else { $emsg = "Incorrect login credentials" }
  139. }
  140. }
  141.  
  142. my $cookie;
  143.  
  144. if ($authed) {
  145. $cookie = CGI::Cookie->new(-name=>'Auth',-value=>join(':',$self->{user},$self->{pass}))
  146. } else {
  147. if ($c{Auth}->value eq join(':',$self->{user},$self->{pass})) { $cookie = CGI::Cookie->new(-name=>'Auth',-value=>join(':',$self->{user},$self->{pass})); $aut
  148. else { $cookie = CGI::Cookie->new(-name=>'Auth',-value=>"") }
  149. }
  150.  
  151. $response->push_header('Set-Cookie',$cookie);
  152.  
  153. $response->content(
  154. $cgi->start_html(
  155. -title=>'TCC Management Panel',
  156. -author=>'opensource@interflecive.com',
  157. -style=>{'code'=>$css},
  158. ).
  159. $cgi->h1('Please login to continue').
  160. $cgi->p('If this is the first run on this node then please refer to the console where you launched TCC for the username/password').
  161. $cgi->p({'style'=>'color:red'},$emsg).
  162. $cgi->start_form(-method=>'POST',-action=>'/',-enctype=>'application/x-www-form-urlencoded').
  163. $cgi->div({'class'=>'loginbox'},
  164. $cgi->p({'class'=>'smallheader'},'Username').
  165. $cgi->textfield(-name=>'username',-value=>$username,-size=>'20')
  166. ).
  167. $cgi->div({'class'=>'loginbox'},
  168. $cgi->p({'class'=>'smallheader'},'Password').
  169. $cgi->password_field(-name=>'password',-value=>'',-size=>'20')
  170. ).
  171. $cgi->div({'class'=>'submitbox'},
  172. $cgi->submit({'class'=>'submitbtn'},'Action','Submit')
  173. ).
  174. $cgi->end_form().
  175. $cgi->end_html
  176. );
  177. }
  178. default {
  179. $response = HTTP::Response->new(404);
  180. $response->push_header('Content-type', 'text/html');
  181.  
  182. $response->content('<html><head><title>TCC: 404 not found</title></head><body><h1>404 - not found</h1><p>The resource you have requested is not availible at present.</
  183.  
  184. }
  185. }
  186.  
  187. $heap->{client}->put($response);
  188. $kernel->yield("shutdown");
  189. }
  190. );
  191. }
  192.  
  193. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement