Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. package App::TCC::Module;
  2.  
  3. use 5.012001;
  4. use strict;
  5. use warnings;
  6. use POE qw(Component::Client::TCP);
  7.  
  8. #require Exporter;
  9. #use AutoLoader qw(AUTOLOAD);
  10. #our @ISA = qw(Exporter);
  11.  
  12. use Digest::MD5;
  13. use Digest::SHA;
  14. use Digest::CRC;
  15.  
  16. use Data::Dumper;
  17.  
  18. our $VERSION = '0.01';
  19.  
  20. 1;
  21.  
  22. sub new {
  23. my $package = shift;
  24. my %opts = %{$_[0]} if ($_[0]);
  25. $opts{ lc $_ } = delete $opts{$_} for keys %opts; # convert opts to lower case
  26. my $self = bless \%opts, $package;
  27.  
  28. $self->{start} = time;
  29. $self->{cycles} = 0;
  30.  
  31. # die "username: $self->{user}, password: $self->{pass}";
  32.  
  33. $self->{me} = POE::Session->create(
  34. inline_states => {
  35. _start => sub {
  36. $_[KERNEL]->alias_set('module');
  37. $_[KERNEL]->yield("next");
  38. $_[KERNEL]->post('core','_modready');
  39. },
  40. next => sub { $_[KERNEL]->delay(next => 1) },
  41. connect => sub {
  42. my ($kernel,$heap,$self,$profile) = @_[KERNEL,HEAP,OBJECT,ARG0];
  43.  
  44. $kernel->post('core','_log',{ msg=> "Connecting to: $profile->{host} $profile->{port}" });
  45.  
  46. $self->{con} = POE::Component::Client::TCP->new(
  47. Args => [$profile],
  48. RemoteAddress => $profile->{host},
  49. RemotePort => $profile->{port},
  50. Started => \&Started,
  51. Connected => \&Connected,
  52. ServerInput => \&ServerInput,
  53. InlineStates => {
  54. testhandler => \&testhandler,
  55. },
  56. );
  57. },
  58. send2service => sub {
  59. my ($kernel,$heap,$self,$target) = @_[KERNEL,HEAP,OBJECT,ARG0];
  60.  
  61. warn "Send2Service (pre)";
  62. $kernel->post($self->{con},'testhandler','test');
  63. },
  64. },
  65. );
  66. }
  67.  
  68. sub Started {
  69. my ($kernel,$self,$heap,@args) = @_[KERNEL,OBJECT,HEAP,ARG0..$#_];
  70.  
  71. $heap->{profile} = $args[0];
  72.  
  73. $self->{stage} = 0;
  74.  
  75. $kernel->post('core','_log',{ msg=> "Module initilized." });
  76. }
  77.  
  78. sub Connected {
  79. my ($kernel,$heap,$self,$a0) = @_[KERNEL,HEAP,OBJECT,ARG0];
  80.  
  81. $heap->{stage} = 1;
  82.  
  83. $kernel->post('core','_log',{ msg=> "Connected to: $heap->{profile}->{host} $heap->{profile}->{port}" });
  84. $kernel->post('core','connected');
  85.  
  86. }
  87. sub testhandler {
  88. warn "TestHandler";
  89. }
  90. sub ServerInput {
  91. my ($kernel,$heap,$a0) = @_[KERNEL,HEAP,ARG0];
  92.  
  93. if (($heap->{stage} == 1) && ($a0 =~ m#^001\s+#)) {
  94. $heap->{stage} = 2;
  95. } elsif ($heap->{stage} == 1) {
  96. $kernel->post('core','_log',{ type=>'socket', msg=> "Stage1 initilization with non standard server reply: $a0" });
  97. $heap->{stage} = 0;
  98. } elsif ($heap->{stage} == 2) {
  99.  
  100. my @tokens = split(m#\s+#,$a0);
  101.  
  102. given($tokens[0]) {
  103. when('002') {
  104. foreach my $authm (@tokens) { $heap->{authen}->{$authm} = 1 if ($authm =~ m#::#) }
  105. my $authen = 'CRYPT::NONE';
  106.  
  107. if ($heap->{profile}->{key}) {
  108. my $prefer;
  109. if ($heap->{profile}->{prefer}) { $prefer = $heap->{profile}->{prefer} } else { $prefer = "" }
  110. if (($prefer) && ($heap->{authen}->{$prefer})) { $authen = $prefer }
  111. }
  112.  
  113. $kernel->post('core','_log',{ type=>'rawsend', msg=> "100 $authen" } );
  114.  
  115. $heap->{server}->put("100 $authen");
  116. }
  117. when(m#^100,(200|201)$#) {
  118. my $reply = $1;
  119. if ($reply == 200) {
  120. my $sha = Digest::SHA->new('sha256');
  121. my $uid = $heap->{profile}->{uid};
  122. my $crypt = "";
  123.  
  124. $crypt = $heap->{profile}->{key} if ($heap->{profile}->{key});
  125. $crypt = 'SHA256::B64::47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU' if (!$crypt);
  126.  
  127. $kernel->post('core','_log',{ type=>'debug', msg=> "Got initial crypted channel, sending CRYPT CRC" } );
  128.  
  129. $kernel->post('core','_log',{ type=>'rawsend', msg=>'101 LOGIN '.$crypt } );
  130.  
  131. $heap->{server}->put('101 LOGIN '.$crypt);
  132. } else {
  133. $kernel->post('core','_log',{ type=>'socket', msg=> "Failure establishing initial crypted channel (CRITICAL)" } );
  134. }
  135. }
  136. when(m#^101,(200|201)$#) {
  137. my $reply = $1;
  138. $kernel->post('core','_log',{ type=>'socket', msg=> "Authed and ready synchronising" } );
  139. if (!$heap->{profile}->{mode}) { $heap->{profile}->{mode} = 'MASTER::SLAVE' }
  140. $kernel->post('core','_log',{ type=>'rawsend', msg=>'105 '.$heap->{profile}->{mode} } );
  141. $heap->{server}->put('105 '.$heap->{profile}->{mode});
  142. }
  143. when(m#^105,(200|201|203)$#) {
  144. my $reply = $1;
  145.  
  146. if ($reply == 203) {
  147. $kernel->post('core','_log',{ type=>'auth', msg=> "Mode changed to $tokens[1]" });
  148. $heap->{profile}->{mode} = $tokens[1];
  149. } elsif ($reply == 201) {
  150. warn "About to die do not know what to do!";
  151. die "HELP!";
  152. }
  153.  
  154.  
  155. if ($heap->{profile}->{mode} eq 'MASTER::GUEST') {
  156. if ($heap->{profile}->{onetime}) {
  157. $kernel->post('core','_log',{ type=>'auth', msg=> "We do not appear to be autherized on this node, knocking wih onetime key" });
  158. $kernel->post('core','_log',{ type=>'rawsend', msg=>"102 ONETIME::KNOCK $heap->{profile}->{onetime}" } );
  159. $heap->{server}->put("102 ONETIME::KNOCK $heap->{profile}->{onetime}");
  160. } else {
  161. $kernel->post('core','_log',{ type=>'auth', msg=> "We do not appear to be autherized on this node, please contact the administrator to create an entry key" });
  162. }
  163. } else {
  164. foreach my $key (keys %{$heap->{profile}->{funcs}}) {
  165. my $args = $heap->{profile}->{funcs}->{$key};
  166. $kernel->post('core','_log',{ type=>'rawsend', msg=>"110 CAPAB +$key/$args" } );
  167. $heap->{server}->put("110 CAPAB +$key/$args");
  168. }
  169. $kernel->post('core','_ready',$heap->{authen});
  170. }
  171. }
  172. }
  173.  
  174. $kernel->post('core','_log',{ type=>'sockread', msg=>join(" ",@tokens) });
  175. } else {
  176. $kernel->post('core','_log',{ type=>'socket', msg=> "Stage($heap->{stage}) initilization failed" });
  177. }
  178. }
  179.  
  180. __END__
  181. # Below is stub documentation for your module. You'd better edit it!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement