Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package App::TCC::Module;
- use 5.012001;
- use strict;
- use warnings;
- use POE qw(Component::Client::TCP);
- #require Exporter;
- #use AutoLoader qw(AUTOLOAD);
- #our @ISA = qw(Exporter);
- use Digest::MD5;
- use Digest::SHA;
- use Digest::CRC;
- use Data::Dumper;
- our $VERSION = '0.01';
- 1;
- sub new {
- my $package = shift;
- my %opts = %{$_[0]} if ($_[0]);
- $opts{ lc $_ } = delete $opts{$_} for keys %opts; # convert opts to lower case
- my $self = bless \%opts, $package;
- $self->{start} = time;
- $self->{cycles} = 0;
- # die "username: $self->{user}, password: $self->{pass}";
- $self->{me} = POE::Session->create(
- inline_states => {
- _start => sub {
- $_[KERNEL]->alias_set('module');
- $_[KERNEL]->yield("next");
- $_[KERNEL]->post('core','_modready');
- },
- next => sub { $_[KERNEL]->delay(next => 1) },
- connect => sub {
- my ($kernel,$heap,$self,$profile) = @_[KERNEL,HEAP,OBJECT,ARG0];
- $kernel->post('core','_log',{ msg=> "Connecting to: $profile->{host} $profile->{port}" });
- $self->{con} = POE::Component::Client::TCP->new(
- Args => [$profile],
- RemoteAddress => $profile->{host},
- RemotePort => $profile->{port},
- Started => \&Started,
- Connected => \&Connected,
- ServerInput => \&ServerInput,
- InlineStates => {
- testhandler => \&testhandler,
- },
- );
- },
- send2service => sub {
- my ($kernel,$heap,$self,$target) = @_[KERNEL,HEAP,OBJECT,ARG0];
- warn "Send2Service (pre)";
- $kernel->post($self->{con},'testhandler','test');
- },
- },
- );
- }
- sub Started {
- my ($kernel,$self,$heap,@args) = @_[KERNEL,OBJECT,HEAP,ARG0..$#_];
- $heap->{profile} = $args[0];
- $self->{stage} = 0;
- $kernel->post('core','_log',{ msg=> "Module initilized." });
- }
- sub Connected {
- my ($kernel,$heap,$self,$a0) = @_[KERNEL,HEAP,OBJECT,ARG0];
- $heap->{stage} = 1;
- $kernel->post('core','_log',{ msg=> "Connected to: $heap->{profile}->{host} $heap->{profile}->{port}" });
- $kernel->post('core','connected');
- }
- sub testhandler {
- warn "TestHandler";
- }
- sub ServerInput {
- my ($kernel,$heap,$a0) = @_[KERNEL,HEAP,ARG0];
- if (($heap->{stage} == 1) && ($a0 =~ m#^001\s+#)) {
- $heap->{stage} = 2;
- } elsif ($heap->{stage} == 1) {
- $kernel->post('core','_log',{ type=>'socket', msg=> "Stage1 initilization with non standard server reply: $a0" });
- $heap->{stage} = 0;
- } elsif ($heap->{stage} == 2) {
- my @tokens = split(m#\s+#,$a0);
- given($tokens[0]) {
- when('002') {
- foreach my $authm (@tokens) { $heap->{authen}->{$authm} = 1 if ($authm =~ m#::#) }
- my $authen = 'CRYPT::NONE';
- if ($heap->{profile}->{key}) {
- my $prefer;
- if ($heap->{profile}->{prefer}) { $prefer = $heap->{profile}->{prefer} } else { $prefer = "" }
- if (($prefer) && ($heap->{authen}->{$prefer})) { $authen = $prefer }
- }
- $kernel->post('core','_log',{ type=>'rawsend', msg=> "100 $authen" } );
- $heap->{server}->put("100 $authen");
- }
- when(m#^100,(200|201)$#) {
- my $reply = $1;
- if ($reply == 200) {
- my $sha = Digest::SHA->new('sha256');
- my $uid = $heap->{profile}->{uid};
- my $crypt = "";
- $crypt = $heap->{profile}->{key} if ($heap->{profile}->{key});
- $crypt = 'SHA256::B64::47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU' if (!$crypt);
- $kernel->post('core','_log',{ type=>'debug', msg=> "Got initial crypted channel, sending CRYPT CRC" } );
- $kernel->post('core','_log',{ type=>'rawsend', msg=>'101 LOGIN '.$crypt } );
- $heap->{server}->put('101 LOGIN '.$crypt);
- } else {
- $kernel->post('core','_log',{ type=>'socket', msg=> "Failure establishing initial crypted channel (CRITICAL)" } );
- }
- }
- when(m#^101,(200|201)$#) {
- my $reply = $1;
- $kernel->post('core','_log',{ type=>'socket', msg=> "Authed and ready synchronising" } );
- if (!$heap->{profile}->{mode}) { $heap->{profile}->{mode} = 'MASTER::SLAVE' }
- $kernel->post('core','_log',{ type=>'rawsend', msg=>'105 '.$heap->{profile}->{mode} } );
- $heap->{server}->put('105 '.$heap->{profile}->{mode});
- }
- when(m#^105,(200|201|203)$#) {
- my $reply = $1;
- if ($reply == 203) {
- $kernel->post('core','_log',{ type=>'auth', msg=> "Mode changed to $tokens[1]" });
- $heap->{profile}->{mode} = $tokens[1];
- } elsif ($reply == 201) {
- warn "About to die do not know what to do!";
- die "HELP!";
- }
- if ($heap->{profile}->{mode} eq 'MASTER::GUEST') {
- if ($heap->{profile}->{onetime}) {
- $kernel->post('core','_log',{ type=>'auth', msg=> "We do not appear to be autherized on this node, knocking wih onetime key" });
- $kernel->post('core','_log',{ type=>'rawsend', msg=>"102 ONETIME::KNOCK $heap->{profile}->{onetime}" } );
- $heap->{server}->put("102 ONETIME::KNOCK $heap->{profile}->{onetime}");
- } else {
- $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" });
- }
- } else {
- foreach my $key (keys %{$heap->{profile}->{funcs}}) {
- my $args = $heap->{profile}->{funcs}->{$key};
- $kernel->post('core','_log',{ type=>'rawsend', msg=>"110 CAPAB +$key/$args" } );
- $heap->{server}->put("110 CAPAB +$key/$args");
- }
- $kernel->post('core','_ready',$heap->{authen});
- }
- }
- }
- $kernel->post('core','_log',{ type=>'sockread', msg=>join(" ",@tokens) });
- } else {
- $kernel->post('core','_log',{ type=>'socket', msg=> "Stage($heap->{stage}) initilization failed" });
- }
- }
- __END__
- # Below is stub documentation for your module. You'd better edit it!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement