Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # $Id: tproxy.pl,v 1.81 2009/08/01 02:56:31 root Exp root $
- # A socket proxy deamon.
- use strict;
- use warnings;
- use feature "switch";
- use IO::Socket;
- use IO::Async::Loop;
- use IO::Async::Stream;
- use POE::Filter::Block;
- use constant;
- *STDOUT->autoflush(1);
- use constant LISTEN_PORT => 8081;
- use constant Client => 0;
- use constant Server => 1;
- use constant DIRTXT => ("Client", "Server", "Server raw", "Server rblock",
- "Server uncomp");
- my %connections; # $CONNECTC_PORT = [ clientpkthandler, serverpkthandler,
- # POE::Filter::Block client, POE::Filter::Block server,
- # POE::Filter::Block serveruncomp, [ [ skilltree ] ], CONNECTS_HOST ];
- # skilltree = [ total, base, ext ];
- my $callme = sub { return 0; };
- my $loop = IO::Async::Loop->new;
- ####################
- # Helper Functions #
- ####################
- sub hexprint {
- my $buff = shift @_;
- open(CC, "|/usr/bin/hd -C | uniq");
- print CC $buff;
- close CC;
- }
- sub defaultpkthandler {
- my $buff = shift @_;
- my $direction = shift @_;
- my $CONNECTC_PORT = shift @_;
- print "". (DIRTXT)[$direction] ." ${CONNECTC_PORT}:\n";
- hexprint $buff;
- }
- ########################
- # End Helper Functions #
- ########################
- ######################################################
- # Code jacked from mantralord, Cthulhon, and binrapt #
- ######################################################
- # http://heroinglands.googlecode.com/svn/trunk/heroin/compression.cpp
- use constant index_table => (
- 0x0247, 0x0236, 0x0225, 0x0214, 0x0203, 0x01F2, 0x01E1, 0x01D0,
- 0x01BF, 0x01AE, 0x019D, 0x018C, 0x017B, 0x016A, 0x0161, 0x0158,
- 0x014F, 0x0146, 0x013D, 0x0134, 0x012B, 0x0122, 0x0119, 0x0110,
- 0x0107, 0x00FE, 0x00F5, 0x00EC, 0x00E3, 0x00DA, 0x00D1, 0x00C8,
- 0x00BF, 0x00B6, 0x00AD, 0x00A8, 0x00A3, 0x009E, 0x0099, 0x0094,
- 0x008F, 0x008A, 0x0085, 0x0080, 0x007B, 0x0076, 0x0071, 0x006C,
- 0x0069, 0x0066, 0x0063, 0x0060, 0x005D, 0x005A, 0x0057, 0x0054,
- 0x0051, 0x004E, 0x004B, 0x0048, 0x0045, 0x0042, 0x003F, 0x003F,
- 0x003C, 0x003C, 0x0039, 0x0039, 0x0036, 0x0036, 0x0033, 0x0033,
- 0x0030, 0x0030, 0x002D, 0x002D, 0x002A, 0x002A, 0x0027, 0x0027,
- 0x0024, 0x0024, 0x0021, 0x0021, 0x001E, 0x001E, 0x001B, 0x001B,
- 0x0018, 0x0018, 0x0015, 0x0015, 0x0012, 0x0012, 0x0012, 0x0012,
- 0x000F, 0x000F, 0x000F, 0x000F, 0x000C, 0x000C, 0x000C, 0x000C,
- 0x0009, 0x0009, 0x0009, 0x0009, 0x0006, 0x0006, 0x0006, 0x0006,
- 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
- 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
- 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
- );
- use constant character_table => (
- 0x00, 0x00, 0x01, 0x00, 0x01, 0x04, 0x00, 0xFF, 0x06, 0x00, 0x14, 0x06,
- 0x00, 0x13, 0x06, 0x00, 0x05, 0x06, 0x00, 0x02, 0x06, 0x00, 0x80, 0x07,
- 0x00, 0x6D, 0x07, 0x00, 0x69, 0x07, 0x00, 0x68, 0x07, 0x00, 0x67, 0x07,
- 0x00, 0x1E, 0x07, 0x00, 0x15, 0x07, 0x00, 0x12, 0x07, 0x00, 0x0D, 0x07,
- 0x00, 0x0A, 0x07, 0x00, 0x08, 0x07, 0x00, 0x07, 0x07, 0x00, 0x06, 0x07,
- 0x00, 0x04, 0x07, 0x00, 0x03, 0x07, 0x00, 0x6C, 0x08, 0x00, 0x51, 0x08,
- 0x00, 0x20, 0x08, 0x00, 0x1F, 0x08, 0x00, 0x1D, 0x08, 0x00, 0x18, 0x08,
- 0x00, 0x17, 0x08, 0x00, 0x16, 0x08, 0x00, 0x11, 0x08, 0x00, 0x10, 0x08,
- 0x00, 0x0F, 0x08, 0x00, 0x0C, 0x08, 0x00, 0x0B, 0x08, 0x00, 0x09, 0x08,
- 0x01, 0x96, 0x09, 0x97, 0x09, 0x01, 0x90, 0x09, 0x95, 0x09, 0x01, 0x64,
- 0x09, 0x6B, 0x09, 0x01, 0x62, 0x09, 0x63, 0x09, 0x01, 0x56, 0x09, 0x58,
- 0x09, 0x01, 0x52, 0x09, 0x55, 0x09, 0x01, 0x4D, 0x09, 0x50, 0x09, 0x01,
- 0x45, 0x09, 0x4C, 0x09, 0x01, 0x40, 0x09, 0x43, 0x09, 0x01, 0x31, 0x09,
- 0x3B, 0x09, 0x01, 0x28, 0x09, 0x30, 0x09, 0x01, 0x1A, 0x09, 0x25, 0x09,
- 0x01, 0x0E, 0x09, 0x19, 0x09, 0x02, 0xE2, 0x0A, 0xE8, 0x0A, 0xF0, 0x0A,
- 0xF8, 0x0A, 0x02, 0xC0, 0x0A, 0xC2, 0x0A, 0xCE, 0x0A, 0xE0, 0x0A, 0x02,
- 0xA0, 0x0A, 0xA2, 0x0A, 0xB0, 0x0A, 0xB8, 0x0A, 0x02, 0x8A, 0x0A, 0x8F,
- 0x0A, 0x93, 0x0A, 0x98, 0x0A, 0x02, 0x81, 0x0A, 0x82, 0x0A, 0x83, 0x0A,
- 0x89, 0x0A, 0x02, 0x7C, 0x0A, 0x7D, 0x0A, 0x7E, 0x0A, 0x7F, 0x0A, 0x02,
- 0x77, 0x0A, 0x78, 0x0A, 0x79, 0x0A, 0x7A, 0x0A, 0x02, 0x73, 0x0A, 0x74,
- 0x0A, 0x75, 0x0A, 0x76, 0x0A, 0x02, 0x6E, 0x0A, 0x6F, 0x0A, 0x70, 0x0A,
- 0x72, 0x0A, 0x02, 0x61, 0x0A, 0x65, 0x0A, 0x66, 0x0A, 0x6A, 0x0A, 0x02,
- 0x5D, 0x0A, 0x5E, 0x0A, 0x5F, 0x0A, 0x60, 0x0A, 0x02, 0x57, 0x0A, 0x59,
- 0x0A, 0x5A, 0x0A, 0x5B, 0x0A, 0x02, 0x4A, 0x0A, 0x4B, 0x0A, 0x4E, 0x0A,
- 0x53, 0x0A, 0x02, 0x46, 0x0A, 0x47, 0x0A, 0x48, 0x0A, 0x49, 0x0A, 0x02,
- 0x3F, 0x0A, 0x41, 0x0A, 0x42, 0x0A, 0x44, 0x0A, 0x02, 0x3A, 0x0A, 0x3C,
- 0x0A, 0x3D, 0x0A, 0x3E, 0x0A, 0x02, 0x36, 0x0A, 0x37, 0x0A, 0x38, 0x0A,
- 0x39, 0x0A, 0x02, 0x32, 0x0A, 0x33, 0x0A, 0x34, 0x0A, 0x35, 0x0A, 0x02,
- 0x2B, 0x0A, 0x2C, 0x0A, 0x2D, 0x0A, 0x2E, 0x0A, 0x02, 0x26, 0x0A, 0x27,
- 0x0A, 0x29, 0x0A, 0x2A, 0x0A, 0x02, 0x21, 0x0A, 0x22, 0x0A, 0x23, 0x0A,
- 0x24, 0x0A, 0x03, 0xFB, 0x0B, 0xFC, 0x0B, 0xFD, 0x0B, 0xFE, 0x0B, 0x1B,
- 0x0A, 0x1B, 0x0A, 0x1C, 0x0A, 0x1C, 0x0A, 0x03, 0xF2, 0x0B, 0xF3, 0x0B,
- 0xF4, 0x0B, 0xF5, 0x0B, 0xF6, 0x0B, 0xF7, 0x0B, 0xF9, 0x0B, 0xFA, 0x0B,
- 0x03, 0xE9, 0x0B, 0xEA, 0x0B, 0xEB, 0x0B, 0xEC, 0x0B, 0xED, 0x0B, 0xEE,
- 0x0B, 0xEF, 0x0B, 0xF1, 0x0B, 0x03, 0xDE, 0x0B, 0xDF, 0x0B, 0xE1, 0x0B,
- 0xE3, 0x0B, 0xE4, 0x0B, 0xE5, 0x0B, 0xE6, 0x0B, 0xE7, 0x0B, 0x03, 0xD6,
- 0x0B, 0xD7, 0x0B, 0xD8, 0x0B, 0xD9, 0x0B, 0xDA, 0x0B, 0xDB, 0x0B, 0xDC,
- 0x0B, 0xDD, 0x0B, 0x03, 0xCD, 0x0B, 0xCF, 0x0B, 0xD0, 0x0B, 0xD1, 0x0B,
- 0xD2, 0x0B, 0xD3, 0x0B, 0xD4, 0x0B, 0xD5, 0x0B, 0x03, 0xC5, 0x0B, 0xC6,
- 0x0B, 0xC7, 0x0B, 0xC8, 0x0B, 0xC9, 0x0B, 0xCA, 0x0B, 0xCB, 0x0B, 0xCC,
- 0x0B, 0x03, 0xBB, 0x0B, 0xBC, 0x0B, 0xBD, 0x0B, 0xBE, 0x0B, 0xBF, 0x0B,
- 0xC1, 0x0B, 0xC3, 0x0B, 0xC4, 0x0B, 0x03, 0xB2, 0x0B, 0xB3, 0x0B, 0xB4,
- 0x0B, 0xB5, 0x0B, 0xB6, 0x0B, 0xB7, 0x0B, 0xB9, 0x0B, 0xBA, 0x0B, 0x03,
- 0xA9, 0x0B, 0xAA, 0x0B, 0xAB, 0x0B, 0xAC, 0x0B, 0xAD, 0x0B, 0xAE, 0x0B,
- 0xAF, 0x0B, 0xB1, 0x0B, 0x03, 0x9F, 0x0B, 0xA1, 0x0B, 0xA3, 0x0B, 0xA4,
- 0x0B, 0xA5, 0x0B, 0xA6, 0x0B, 0xA7, 0x0B, 0xA8, 0x0B, 0x03, 0x92, 0x0B,
- 0x94, 0x0B, 0x99, 0x0B, 0x9A, 0x0B, 0x9B, 0x0B, 0x9C, 0x0B, 0x9D, 0x0B,
- 0x9E, 0x0B, 0x03, 0x86, 0x0B, 0x87, 0x0B, 0x88, 0x0B, 0x8B, 0x0B, 0x8C,
- 0x0B, 0x8D, 0x0B, 0x8E, 0x0B, 0x91, 0x0B, 0x03, 0x2F, 0x0B, 0x4F, 0x0B,
- 0x54, 0x0B, 0x5C, 0x0B, 0x71, 0x0B, 0x7B, 0x0B, 0x84, 0x0B, 0x85, 0x0B
- );
- use constant bit_masks => (
- 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F,
- 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF
- );
- # Let's try and test this.
- if (0) {
- my $return;
- my $input = "\x17\x46\x34\x86\xc0\x38".
- "\xf9\x48\x9e\x2b\x0c\x8b\x3f\xff".
- "\x82\x18\xc6\x90\xd8\x07\x19\x2b".
- "\x17\x43\x33\x67\x43\x7c\x63\x48".
- "\x6c\x03\x80\x89\xb0\xc6\x90\xd8".
- "\x07\x6e\xbd\xde\xe1\x2e\xe1\x32".
- "\xe1\x36\xe1\x3a\xe7\xee\x32\x5c".
- "\xb1\x72\xcd\xc6\x5b\xaa\xb9\x6e".
- "\xe7\xb4\x4b\x2e\x35\x5a\x00\x94".
- "\xa3\x64\xc8\x6e\xb8\xe3\x71\xd2".
- "\xe1\xcc\x3c\x18\xd2\x1b\x00\xe0".
- "\x9d\x34\x3a\x87\xae\x17\x48\xe1".
- "\x8c\x24\x3b\x9f\x2a\x8e\x89\x48".
- "\xb1\x48\x0a\x8d\x24\x58\xa4\x05".
- "\x46\x91\x58\x43\x42\xbc\x43\x42".
- "\xb8\x21\x02\xa0\x14\x69\x81\x30".
- "\x2a\xba\x4e\x1d\x46\x34\x86\xc0".
- "\x39\xc6\xab\x6d\xb6\xd8\x75\x18".
- "\xd2\x1b\x00\xe8\x04\xb6\xdb\x6d".
- "\x97\xc6\x17\xfd\xef\xee\xef\xfd".
- "\xf7\x77\x77\xbd\xe4\xa4\x3f\x7b".
- "\x9a\x1c\x9a\x0b\x0b\x4e\x4b\x89".
- "\xcd\x08\xe6\x8f\x7a\x73\x41\xc8".
- "\xe1\x29\xa0\xf0\x6a\xc1\xa1\x24".
- "\xd1\xef\x73\x46\x0d\x11\xdc\xd0".
- "\xc0\xd1\xd3\x3d\xee\x68\xec\x92".
- "\xaf\xff\xf0\xe5\x6a\x3f\x27\x7f".
- "\xff\xe1\xf7\x5b\x5b\x5b\x20\x2b".
- "\x6b\x79\xdc\x80\xe4\x04\x80\xad".
- "\xad\xe7\x72\x02\xb6\xb6\xb6\x40".
- "\x48\x0f\xad\xad\xff\xe4\x07\xce".
- "\xfa\xde\xb7\xff\xfc\xc6\x31\xa4".
- "\x36\x01\xc3\x13\xf3\x98\xb8\x72".
- "\xe6\x67\x2b\x9e\x9c\x9f\x02\x87".
- "\x48\xa7\x8b\xd4\x18\x46\x33\xa3".
- "\x19\xab\xce\x4c\x84\x93\xa3\x33".
- "\xe9\x1e\x05\x50\x1d\x1a\x06\xa0".
- "\x26\x26\x88\xe6\x95\xb1\x96\x40".
- "\x87\xe5\xf0\xac\x85\x74\xa4\x98".
- "\x31\x01\x42\xe8\xd4\x01\x80\x38".
- "\x38\x1d\xc0";
- warn unless ($return = decompress_packet($input));
- defaultpkthandler($return, Server, 0);
- }
- ###########################################################
- # Stop Code jacked from mantralord, Cthulhon, and binrapt #
- ###########################################################
- ###############
- # LengthCodec #
- ###############
- # These two are 257 bytes long.
- # Undef values indecate to run function[256].
- use constant bngsclientoplength => (
- undef,5,9,5,9,5,9,9,5,9,9,1,5,9,9,5,
- 9,9,1,9,undef,undef,13,5,17,5,9,9,3,9,9,17,
- 13,9,5,9,5,9,13,9,9,9,9,undef,undef,undef,undef,9,
- 9,9,17,17,5,17,9,5,13,5,3,3,9,5,5,3,
- 1,1,undef,undef,17,9,13,13,1,9,undef,9,5,3,undef,7,
- 9,9,5,1,1,undef,undef,undef,3,17,undef,undef,undef,7,6,5,
- 1,3,5,5,9,undef,undef,undef,37,1,undef,1,undef,13,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,
- sub {
- my $stuff = shift @_;
- given ( $stuff ) {
- when ( /^\x14\x01\x00.*?\x00\x00\x00/s ) { return length($&); }
- when ( /^\x15\x01\x00.*?\x00\x00\x00/s ) { return length($&); }
- when ( /^\x14\x01\x00/s ) { return }
- when ( /^\x15\x01\x00/s ) { return }
- when ( /^\x2b/s ) { warn "Unused"; return }
- when ( /^\x2c/s ) { warn "Unused, hack"; return }
- when ( /^\x2d/s ) { warn "Unused, hack"; return }
- when ( /^\x2e/s ) { warn "Unknown"; return }
- when ( /^\x42/s ) { warn "Unknown/Unused"; return }
- when ( /^\x43/s ) { warn "Unknown/Unused"; return }
- when ( /^\x4a/s ) { warn "Unused"; return }
- when ( /^\x55/s ) { warn "Unused"; return }
- when ( /^\x56/s ) { warn "Unused"; return }
- when ( /^\x57/s ) { warn "Unused"; return }
- when ( /^\x5a/s ) { warn "Unused"; return 1; }
- when ( /^\x5b/s ) { warn "Unused"; return }
- when ( /^\x5c/s ) { warn "Unused"; return }
- when ( /^\x65/s ) { warn "Hack Detection"; return 1 }
- when ( /^\x66(.)/s ) { warn "Warden Response";
- return unpack("C", $1); }
- when ( /^\x67/s ) { warn "--"; return }
- when ( /^\x6a/s ) { warn "--"; return }
- when ( /^\x6c/s ) { warn "--"; return }
- when ( /^\x6e/s ) { warn "Unknown/Unused"; return }
- when ( /^\x6f/s ) { warn "Unknown/Unused"; return }
- default { return } };
- }
- );
- use constant bngsserveroplength => (
- 1,8,1,12,1,1,1,6,6,12,6,6,9,13,12,16,
- 16,8,26,14,18,11,undef,undef,15,2,2,3,5,3,4,6,
- 10,12,12,13,90,90,undef,40,103,97,15,undef,8,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,8,
- 13,undef,6,undef,undef,13,undef,11,11,undef,undef,undef,16,17,7,1,
- 15,14,42,10,10,3,undef,undef,14,26,40,36,5,5,38,5,
- 7,2,7,undef,undef,7,7,16,21,12,12,16,16,10,1,1,
- 1,1,1,32,10,13,6,2,21,6,13,8,6,18,5,10,
- undef,20,29,undef,undef,undef,undef,undef,undef,2,6,6,11,7,10,33,
- 13,26,6,8,undef,13,9,1,7,16,17,7,undef,undef,7,8,
- 10,7,8,24,3,8,undef,7,undef,7,undef,7,undef,9,undef,2,
- 1,53,undef,5,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,undef,undef,
- undef,undef,undef,undef,undef,undef,undef,
- sub {
- my $stuff = shift @_;
- given ( $stuff ) {
- when ( /^\x16/s ) { warn "Unknown"; return }
- when ( /^\x17/s ) { warn "Unused"; return }
- when ( /^\x26.{9}[^\x00]*\x00[^\x00]\x00/s ) {
- return length($&); }
- when ( /^\x2b/s ) { warn "Unused"; return }
- when ( /^[\x2d-\x3d]/s ) { warn "Unused"; return }
- when ( /^\x41/s ) { warn "Unused"; return }
- when ( /^\x43/s ) { warn "Unused"; return }
- when ( /^\x44/s ) { warn "Unused"; return }
- when ( /^\x46/s ) { warn "Unused"; return }
- when ( /^\x49/s ) { warn "Unused"; return }
- when ( /^\x4a/s ) { warn "Unused"; return }
- when ( /^\x4b/s ) { warn "Unused"; return }
- when ( /^\x56/s ) { warn "Unused"; return }
- when ( /^\x57/s ) { warn "Unused"; return }
- when ( /^\x63/s ) { warn "Waypoint Menu: ". length($stuff);
- return length($stuff); }
- when ( /^\x64/s ) { warn "Unused"; return }
- when ( /^\x80/s ) { warn "Unused"; return }
- when ( /^[\x83-\x88]/s ) { warn "Unused"; return }
- when ( /^\x94(.)/s ) { return (6 + unpack("C", $1) * 3); }
- when ( /^\x9c/s ) { warn "Item Action (World): ".
- length($stuff); return length($stuff); }
- when ( /^\x9d/s ) { warn "Item Action (Owned): ".
- length($stuff); return length($stuff); }
- when ( /^\xa6/s ) { warn "Unknown"; return }
- when ( /^\xa8.{5}(.)/s ) { warn "Set State";
- return unpack("C", $1); }
- when ( /^\xaa.{5}(.)/s ) { return unpack("C", $1); }
- when ( /^\xac.{11}(.)/s ) { warn "Assign NPC";
- return unpack("C", $1); }
- when ( /^\xae(..)/s ) { warn "Warden Request";
- return unpack("S<", $1); }
- when ( /^\xb2/s ) { warn "Unknown"; return }
- default { return } };
- }
- );
- if (0) {
- foreach ( 0..$#{[bngsclientoplength]} ) {
- printf("%3.3x %s\n", $_, (bngsclientoplength)[$_] || "");
- };
- };
- if (0) {
- foreach ( 0..$#{[bngsserveroplength]} ) {
- printf("%3.3x %s\n", $_, (bngsserveroplength)[$_] || "");
- };
- };
- ###################
- # End LengthCodec #
- ###################
- ###################
- # Packet handlers #
- ###################
- sub bncsclientpkthandler {
- my $oper = shift @_;
- my $CONNECTC_PORT = shift @_;
- unless ( defined(${$connections{$CONNECTC_PORT}}[2]) ||
- defined(${$connections{$CONNECTC_PORT}}[3]) ) {
- given ( $oper ) {
- when ( /^\x01/s ) {
- ${$connections{$CONNECTC_PORT}}[2] =
- POE::Filter::Block->new( LengthCodec => [ sub {
- die "never gonna get called" },
- sub { my $stuff = shift;
- unless ($$stuff =~ /^\xff.(..)/) {
- return;
- }
- return unpack("S<", $1);
- } ] );
- ${$connections{$CONNECTC_PORT}}[3] =
- POE::Filter::Block->new( LengthCodec => [ sub {
- die "never gonna get called" },
- sub { my $stuff = shift;
- unless ($$stuff =~
- /^\xff.(..)/) {
- return;
- }
- return unpack("S<", $1);
- } ] );
- warn unless ($oper =~ s/^\x01//s);
- }
- # when ( /^\x02/s ) {
- # }
- default {
- ${$connections{$CONNECTC_PORT}}[0] =
- sub { defaultpkthandler(shift @_, Client, shift @_); };
- ${$connections{$CONNECTC_PORT}}[1] =
- sub { defaultpkthandler(shift @_, Server, shift @_); };
- } };
- my_on_read(6112, $CONNECTC_PORT, Client, undef, \$oper, undef);
- return 0;
- };
- given ( $oper ) {
- when ( /^\xff\x50\x3a\x00\x00\x00\x00\x00(.{8})
- \x0c\x00\x00\x00\x53\x55\x6e\x65\xc0\xa8\xa7
- \x0a\x2c\x01\x00\x00\x09\x04\x00\x00\x09\x04
- \x00\x00(...)\x00(.{13})\x00$/xs ) {
- print "". (DIRTXT)[Client] ." $CONNECTC_PORT says hello: " .
- reverse($1). ", $3 $2.\n"
- }
- when ( /^\xff\x25\x08\x00(....)$/s ) {
- print "". (DIRTXT)[Client] ." $CONNECTC_PORT Pong 0x". unpack("H8", $1) .".\n";
- }
- when ( /^\xff\x33\x1e\x00\x04\x00\x00\x80\x00\x00\x00\x00
- \x62\x6e\x73\x65\x72\x76\x65\x72\x2d\x44\x32\x44
- \x56\x2e\x69\x6e\x69\x00$/xs ) { 1; }
- when ( /^\xff\x40\x04\x00$/xs ) { 1; }
- when ( /^\xff\x3a(..)(....)(....)(.{20})
- ([^\x00]*)\x00$/xs ) {
- print "". (DIRTXT)[Client] ." $CONNECTC_PORT log $5 the 0x".
- unpack("H8", $2) ." fuck into 0x".
- unpack("H8", $3) ." with\n\tthis 0x".
- unpack("H40", $4) ." and these ".
- unpack("S<", $1) ." bytes.\n";
- }
- when ( /^ \xff\x3e(..)\x01\x00\x00\x00(....)(.{16})
- ([^\x00]*)\x00$/xs ) {
- print "". (DIRTXT)[Client] ." $CONNECTC_PORT login to $4 with ".
- unpack("S<", $1) ." bytes, ClientID: 0x".
- unpack("H8", $2) ." and PassHash:\n\t0x".
- unpack("H32", $3) ."\n";
- }
- when ( /^\xff\x0b\x08\x00\x50\x58\x32\x44$/s ) { 1; }
- # when ( /^\xff\x51/s ) {
- # defaultpkthandler($oper, Client, $CONNECTC_PORT);
- # print "". (DIRTXT)[Client] ." $CONNECTC_PORT returning chalange and name.\n";
- # }
- default {
- defaultpkthandler($oper, Client, $CONNECTC_PORT);
- } };
- };
- sub bncsserverpkthandler {
- my $oper = shift @_;
- my $CONNECTC_PORT = shift @_;
- my $buffref = shift @_;
- given ( $oper ) {
- when ( /^\xff\x25\x08\x00(....)$/s ) {
- print "". (DIRTXT)[Server] ." $CONNECTC_PORT Ping 0x". unpack("H8", $1) .".\n";
- }
- when ( /^\xff\x33\x26\x00\x04\x00\x00\x80\x00\x00\x00\x00
- (.{8})
- \x62\x6e\x73\x65\x72\x76\x65\x72\x2d\x44\x32\x44
- \x56\x2e\x69\x6e\x69\x00$/xs ) {
- print "". (DIRTXT)[Server] ." $CONNECTC_PORT latest filetime quadword 0x".
- unpack("H16", $1) .".\n";
- }
- when ( /^\xff\x51\x09\x00\x00\x00\x00\x00\x00$/s ) {
- print "". (DIRTXT)[Server] ." $CONNECTC_PORT CD-Key and Game.exe passed.\n";
- }
- when ( /^\xff\x3a\x08\x00\x00\x00\x00\x00$/s ) {
- print "". (DIRTXT)[Server] ." $CONNECTC_PORT logged in successfully.\n";
- }
- when ( /^\xff\x3e\x0a(.{8})$/s ) {
- print "". (DIRTXT)[Server] ." $CONNECTC_PORT Realm Logon Failed 0x".
- unpack("H16", $1) .".\n";
- }
- when ( /^\xff\x3e(..)(....)(....)(.{8})(.)(.)(.)(.)(....)
- (.{48})([^\x00]*)\x00$/xs ) {
- print "". (DIRTXT)[Server] ." $CONNECTC_PORT Realm Logon successful: 0x".
- unpack("H8", $2) ." MCP Cookie 0x".
- unpack("H8", $3) ." MCP Status\n\t0x".
- unpack("H16", $4) ." MCP Chunk 1, IP=".
- sprintf("%3.3d.%3.3d.%3.3d.%3.3d:%d",
- unpack("C", $5),
- unpack("C", $6),
- unpack("C", $7),
- unpack("C", $8),
- unpack("L<", $9)). " MCP Chunk 2:\n\t".
- unpack("H96", $10) ."\n\t".
- unpack("S<", $1) ." bytes user=$11.\n";
- # We are going to have to do some SHIT here.
- my $CONNECT_HOST = inet_ntoa($5 . $6 . $7 . $8);
- my $CONNECT_PORT = unpack("L<", $9);
- my $CONNECTS_HOST = ${$connections{$CONNECTC_PORT}}[5];
- my ($NCONNECTS_PORT,$NCONNECTC_PORT) = ("","");
- my $callbk = $callme;
- $callme = sub {
- causepain(\$NCONNECTS_PORT,\$NCONNECTC_PORT,
- \$CONNECTS_HOST,\$CONNECT_HOST,\$CONNECT_PORT);
- };
- $loop->loop_stop();
- $callme = $callbk;
- my $buff = inet_aton($CONNECTS_HOST).
- pack("L<", $NCONNECTS_PORT);
- # Yes, right here I need to alter $$buffref.
- $$buffref =~ s/^(\xff\x3e.{18}).{8}
- (.{48}[^\x00]*\x00)$/$1$buff$2/xs;
- }
- when ( /^\xff\x40(..)(....)(....)/s ) {
- print "". (DIRTXT)[Server] ." $CONNECTC_PORT Relm List ". unpack("S<", $1)
- ."bytes code 0x". unpack("H8", $2) .", ". unpack("L<", $3)
- ." of them:\n";
- my $ctr = 12;
- for (0..unpack("L<", $3)) {
- my $dat = substr($oper, $ctr);
- if ($dat =~ /^(....)([^\x00]*)\x00([^\x00]*)\x00/s ) {
- $ctr += length($&);
- print "\tName=$2 Desc=$3 Id=0x".
- unpack("H8", $1) ."\n"
- }
- }
- }
- when ( /^\xff\x46/s ) {
- defaultpkthandler($oper, Server, $CONNECTC_PORT);
- print "". (DIRTXT)[Server] ." $CONNECTC_PORT returning MOTD.\n";
- print "$1.\n" if ( $oper =~ /(There are currently [\d]+ users playing [\d]+ games of .*?, and [\d]+ users playing [\d]+ games on Battle.net)/s );
- print "$1.\n" if ( $oper =~ /(Last logon: .*?)\x0a\00/s );
- }
- # Full channel list - who gives a flying FUCK.
- when ( /^\xff\x0b/s ) { 1; }
- default {
- defaultpkthandler($oper, Server, $CONNECTC_PORT);
- } };
- };
- sub bngsclientpkthandler {
- my $oper = shift @_;
- my $CONNECTC_PORT = shift @_;
- given ( $oper ) {
- when ( /\x68(....)(....)(.)(....)(....)(....)\x00(.{16})$/s ) {
- print "". (DIRTXT)[Client]
- ." $CONNECTC_PORT Invoke Player:\n\tServer Token 0x".
- unpack("H16", $1 . $2) ." name ".
- unpack("Z16", $7) ." the 0x".
- unpack("H2", $3) ."\n\tVersion 0x".
- unpack("H8", $4) ." x 0x".
- unpack("H16", $5 . $6) ."\n";
- }
- when ( /\x6d(....)(....)\x00{4}$/s ) {
- print "". (DIRTXT)[Client]
- ." $CONNECTC_PORT Ping: Tickcount 0x".
- unpack("H8", $1) ." Delay 0x".
- unpack("H8", $2) ."\n";
- }
- when ( /\x6b$/s ) {
- print "". (DIRTXT)[Client]
- ." $CONNECTC_PORT I'm Ready.\n";
- }
- when ( /^\x15\x01\x00(.*?)\x00\x00\x00/s ) {
- print "". (DIRTXT)[Client] ." $CONNECTC_PORT said: $1\n";
- given ( $1 ) {
- when ( /^Ident$/ ) {
- # $stream2->write( "\x04\x01\x00\x00\x00\x01\x00\x00\x00\x00" );
- } };
- }
- default {
- defaultpkthandler($oper, Client, $CONNECTC_PORT);
- } };
- };
- sub bngsserverpkthandler {
- my $tmp = decompress_packet(shift @_);
- my $CONNECTC_PORT = shift @_;
- # defaultpkthandler($tmp, 4, $CONNECTC_PORT);
- my $opers = [ ];
- $opers = ${$connections{$CONNECTC_PORT}}[4]->get( [ $tmp ] )
- unless ( length($tmp) == 0 );
- foreach my $oper (@$opers) {
- given ( $oper ) {
- when ( /^\x01(.)(....)(.)(.)$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Game Flags:\n\tDifficulty ".
- unpack("C", $1) ."\n\tBitField: ".
- unpack("H16", $2) ."\n\tExpansion: ".
- unpack("C", $3) ."\n\tLadder: ".
- unpack("C", $4) ."\n";
- }
- when ( /^\x00$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Game Loading\n";
- }
- when ( /^\x02$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Load Successful\n";
- }
- when ( /^\x8f(.{32})$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Pong:\n\t0x".
- unpack("H64", $1) ."\n";
- }
- when ( /^\x59(....)(.)(.{16})(..)(..)$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Spawn Player:\n\t0x".
- unpack("H16", $1) ." ".
- unpack("Z16", $3) ." the 0x".
- unpack("H2", $2) ."\n\tat 0x".
- unpack("H4", $2) ." x 0x".
- unpack("H4", $2) ."\n";
- }
- when ( /^\xaa(.)(....)(.)(.*)/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Spawn Unit:\n\t0x".
- unpack("H16", $2) ." the 0x".
- unpack("H2", $1) ." with ".
- unpack("C", $3) ." bytes\n\t0x".
- unpack("H*", $4) ."\n";
- }
- when ( /^\x76(.)(....)/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Player In Proximity:\n\t0x".
- unpack("H16", $2) ." the 0x".
- unpack("H2", $1) ."\n";
- }
- when ( /^\x94(.)(....)/s ) {
- my ($igunit, $sgunit, $ctr) = (
- unpack("L<", $2), unpack("H8", $2),
- unpack("C", $1)
- );
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Base Skill Levels: 0x$sgunit".
- " has $ctr skills:\n";
- for (0..$ctr) {
- my $dat = substr($oper, 6 + $_ * 3, 3);
- if ($dat =~ /^(..)(.)$/s ) {
- my ($iskl, $sskl, $pnt) = (
- unpack("S<", $1), unpack("H4", $1),
- unpack("C", $2)
- );
- print "\t\tSkill=0x$sskl".
- " Points=$pnt\n";
- my $skillptr =
- ${$connections{$CONNECTC_PORT}}
- [5]->[$igunit]->[$iskl];
- $$skillptr[2] = $pnt;
- unless (defined($$skillptr[1])) {
- $$skillptr[1] = $pnt
- } else {
- warn $$skillptr[1];
- };
- };
- }
- }
- when ( /^\x22(..)(....)(..)(.)(..)$/s ) {
- my ($igunit, $sgunit, $iskl, $sskl, $pnt) = (
- unpack("L<", $2), unpack("H8", $2),
- unpack("S<", $3), unpack("H4", $3),
- unpack("C", $4)
- );
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Update Item Skill:\n\t0x".
- unpack("H8", $2) ." the 0x".
- unpack("H4", $1) ." with ".
- unpack("C", $4) ." added in 0x".
- unpack("H4", $3) ." but 0x".
- unpack("H4", $4) ." Unknown\n";
- ${$connections{$CONNECTC_PORT}}
- [5]->[$igunit]->[$iskl] = []
- unless (defined(
- ${$connections{$CONNECTC_PORT}}
- [5]->[$igunit]->[$iskl]));
- my $skillptr =
- ${$connections{$CONNECTC_PORT}}
- [5]->[$igunit]->[$iskl];
- if (defined($$skillptr[1])) {
- $$skillptr[1] += $pnt
- } else {
- warn $skillptr;
- warn @$skillptr;
- };
- unless (defined($$skillptr[3])) {
- $$skillptr[3] = $pnt
- } else {
- warn $$skillptr[3];
- };
- print "Current skill lvl for 0x$sgunit with ".
- "0x$sskl is " . ${$skillptr}[1] .".\n"
- }
- when ( /^\x27(.)(....)\00(..)\xff\xff\xff\xff$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Set Right Skill: type 0x".
- unpack("H2", $1) ." unit 0x".
- unpack("H8", $2) ." as 0x".
- unpack("H4", $3) ."\n";
- }
- when ( /^\x27(.)(....)\01(..)\xff\xff\xff\xff$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Set Left Skill: type 0x".
- unpack("H2", $1) ." unit 0x".
- unpack("H8", $2) ." as 0x".
- unpack("H4", $3) ."\n";
- }
- when ( /^\x27(.)(....)\01(..)\xff\xff\xff\xff$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT Game Quest Info:\n\t0x".
- unpack("H2", $1) ." unit 0x".
- unpack("H8", $2) ." as 0x".
- unpack("H4", $3) ."\n";
- }
- when ( /^\x23(.)(....)(.{34})$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT NPC Info: type 0x".
- unpack("H2", $1) ." 0x".
- unpack("H8", $2) ." with these 34 bytes:\n";
- hexprint $3;
- }
- when ( /^\x26(.{9})([^\x00])*\x00([^\x00])\x00$/s ) {
- print "". (DIRTXT)[Server]
- ." $CONNECTC_PORT $2 and below said: $3\n";
- hexprint $1;
- }
- # when ( /^\x15\x01\x00(.*?)\x00\x00\x00/s ) {
- # print "". (DIRTXT)[Client] ." $CONNECTC_PORT said: $1\n";
- # }
- default {
- defaultpkthandler($oper, Server, $CONNECTC_PORT);
- } }; };
- };
- #######################
- # End Packet handlers #
- #######################
- #########################
- # Loop Helper Functions #
- #########################
- sub my_on_read {
- my ($CONNECT_PORT, $CONNECTC_PORT, $direction,
- $self, $buffref, $closed ) = @_;
- # defaultpkthandler($$buffref, 2, $CONNECTC_PORT) if ($direction == Server);
- my $opers = [ $$buffref ];
- $opers = ${$connections{$CONNECTC_PORT}}[2+$direction]->get([$$buffref])
- if ( defined(${$connections{$CONNECTC_PORT}}[2+$direction]) );
- foreach my $oper (@$opers) {
- # defaultpkthandler($oper, 3, $CONNECTC_PORT) if ($direction == Server);
- &{${$connections{$CONNECTC_PORT}}[0+$direction]}(
- $oper, $CONNECTC_PORT, $buffref);
- };
- return 0;
- };
- sub setupconnection {
- my ($CONNECT_PORT, $CONNECTC_PORT ) = @_;
- given( $CONNECT_PORT ) {
- when ( 6112 ) { $connections{$CONNECTC_PORT} = [
- sub { bncsclientpkthandler(shift @_, shift @_); },
- sub { bncsserverpkthandler(shift @_, shift @_); },
- undef,
- undef,
- undef
- ];
- }
- when ( 4000 ) { $connections{$CONNECTC_PORT} = [
- sub { bngsclientpkthandler(shift @_, shift @_); },
- sub { bngsserverpkthandler(shift @_, shift @_); },
- POE::Filter::Block->new( LengthCodec => [ sub {
- die "never gonna get called" },
- sub { my $stuff = shift @_;
- return if ( length($$stuff) == 0 );
- my $op = unpack("C",substr($$stuff,0,1));
- my $len = (bngsclientoplength)[$op];
- return $len if (defined ($len));
- return (bngsclientoplength)[256]($$stuff);
- } ] ),
- POE::Filter::Block->new( LengthCodec => [ sub {
- die "never gonna get called" },
- sub { my $stuff = shift @_;
- given ( $$stuff ) {
- when ( /^\xaf\x01/s ) {
- $$stuff =~ s/^\xaf\x01//s;
- return;
- }
- when ( /^([\xF0-\xFF].)/s ) {
- my $ret = unpack("S>", $1) - 61440 - 2;
- $$stuff =~ s/^[\xF0-\xFF].//s;
- return $ret;
- }
- when ( /^([^\xF0-\xFF])/s ) {
- my $ret = unpack("C", $1) - 1;
- $$stuff =~ s/^[^\xF0-\xFF]//s;
- return $ret;
- }
- default { return; }
- };
- warn; return;
- } ] ),
- POE::Filter::Block->new( LengthCodec => [ sub {
- die "never gonna get called" },
- sub { my $stuff = shift @_;
- return if ( length($$stuff) == 0 );
- my $op = unpack("C",substr($$stuff,0,1));
- my $len = (bngsserveroplength)[$op];
- return $len if (defined ($len));
- return (bngsserveroplength)[256]($$stuff);
- } ] )
- ];
- }
- default { warn "Unk port: $CONNECT_PORT"; $connections{$CONNECTC_PORT} = [
- sub { defaultpkthandler(shift @_, Client, shift @_); },
- sub { defaultpkthandler(shift @_, Server, shift @_); },
- undef,
- undef,
- undef
- ];
- }
- }
- return 0;
- };
- #############################
- # End Loop Helper Functions #
- #############################
- $loop->listen(
- service => LISTEN_PORT,
- socktype => SOCK_STREAM,
- on_accept => sub {
- my ( $socket1 ) = @_;
- my $CONNECTC_HOST = $socket1->peerhost;
- my $CONNECTC_PORT = $socket1->peerport;
- ${$connections{$CONNECTC_PORT}}[5] = $socket1->sockhost;
- my ($CONNECT_HOST, $CONNECT_PORT ) = getforwardaddr($socket1);
- setupconnection($CONNECT_PORT, $CONNECTC_PORT);
- $loop->connect(
- host => $CONNECT_HOST,
- service => $CONNECT_PORT,
- on_connected => sub {
- my ( $socket2 ) = @_;
- my $CONNECTC_HOST = $socket1->peerhost;
- my $CONNECTC_PORT = $socket1->peerport;
- # Now we need two Streams, cross-connected.
- my ( $stream1, $stream2 );
- $stream1 = IO::Async::Stream->new(
- handle => $socket1,
- on_read => sub {
- my ( undef, $buffref, undef) = @_;
- my_on_read($CONNECT_PORT, $CONNECTC_PORT, Client,
- shift @_, shift @_, shift @_);
- # Just copy all the data
- $stream2->write( $$buffref ); $$buffref = "";
- return 0;
- },
- on_closed => sub {
- $stream2->close_when_empty;
- print "Connection from $CONNECTC_HOST:$CONNECTC_PORT closed\n";
- },
- );
- $stream2 = IO::Async::Stream->new(
- handle => $socket2,
- on_read => sub {
- my ( undef, $buffref, undef) = @_;
- my_on_read($CONNECT_PORT, $CONNECTC_PORT, Server,
- shift @_, shift @_, shift @_);
- # Just copy all the data
- $stream1->write( $$buffref ); $$buffref = "";
- },
- on_closed => sub {
- $stream1->close_when_empty;
- print "Connection to $CONNECT_HOST:$CONNECT_PORT closed\n";
- },
- );
- $loop->add( $stream1 );
- $loop->add( $stream2 );
- },
- on_resolve_error => sub { print STDERR "Cannot resolve - $_[0]\n"; },
- on_connect_error => sub { print STDERR "Cannot connect\n"; },
- );
- },
- on_resolve_error => sub { die "Cannot resolve - $_[0]\n"; },
- on_listen_error => sub { die "Cannot listen\n"; },
- );
- $loop->loop_forever
- until (&$callme);
- # *****************************************************
- # * Beyond this point is code I'd rather not look at. *
- # *****************************************************
- ######################################################
- # Code jacked from mantralord, Cthulhon, and binrapt #
- ######################################################
- # http://heroinglands.googlecode.com/svn/trunk/heroin/compression.cpp
- sub decompress_packet {
- my ($input) = @_;
- my ($size, $output_buffer) =
- (length($input), "");
- my ($a, $b, $c, $d);
- my ($maximum_count, $index, $count, $input_pointer);
- $b = 0;
- $count = 0x20;
- while(1)
- {
- if($count >= 0x8)
- {
- while($size > 0 && $count >= 8)
- {
- $count -= 0x8;
- $size--;
- $a = unpack("C", $input)
- << $count;
- $input =~ s/^.//;
- $b |= $a;
- }
- }
- $index = (index_table)[( $b >> 0x18 )];
- $a = (character_table)[$index];
- $d = ($b >> (0x18 - $a)) & (bit_masks)[$a];
- $c = (character_table)[$index + 2 * $d + 2];
- $count += $c;
- if($count > 0x20)
- {
- return $output_buffer;
- }
- $output_buffer .= pack("C",
- (character_table)[$index + 2 * $d + 1]);
- $b <<= ($c & 0xFF);
- }
- };
- ##########################################################
- # End Code jacked from mantralord, Cthulhon, and binrapt #
- ##########################################################
- ############################################################
- # Linux keeps the original destination for us, pull it out #
- ############################################################
- sub getforwardaddr {
- my $sock = shift @_;
- my ($my_port, $my_ipnum) = ($sock->sockport, $sock->sockhost);
- my ($cl_port, $cl_ipnum) = ($sock->peerport, $sock->peerhost);
- my ($re_ipnum, $re_port);
- open(INFO, "</proc/net/ip_conntrack") or die "Can't open proc: $!";
- while ( my $pline = <INFO> ) {
- if ( $pline =~ /^tcp 6 ([0-9]+) ESTABLISHED src=([0-9.]+) dst=([0-9.]+) sport=([0-9]+) dport=([0-9]+) packets=[0-9]+ bytes=[0-9]+ src=${my_ipnum} dst=${cl_ipnum} sport=${my_port} dport=${cl_port} packets=[0-9]+ bytes=[0-9]+ \[ASSURED\] mark=[0-9]+ secmark=[0-9]+ use=[0-9]+$/ ) {
- ($re_ipnum, $re_port) = ($3, $5);
- last;
- }
- }
- close(INFO);
- return ($re_ipnum, $re_port);
- }
- ##################################
- # End Linux Original Destination #
- ##################################
- #########################
- # Fresh rehash of main. #
- #########################
- sub causepain {
- my ($CONNECTS_PORT,$CONNECTC_PORT,$CONNECTS_HOST,$CONNECT_HOST,
- $CONNECT_PORT);
- $CONNECTS_PORT = ${shift @_};
- $CONNECTC_PORT = ${shift @_};
- $CONNECTS_HOST = ${shift @_};
- $CONNECT_HOST = ${shift @_};
- $CONNECT_PORT = ${shift @_};
- my ($socket1,$socket2,$CONNECTC_HOST);
- my @pepsid = (undef, sub {
- my ($CONNECT_PORT, $CONNECTC_PORT, $CONNECTC_HOST,
- $CONNECT_HOST, $socket1, $socket2) = @_;
- setupconnection($CONNECT_PORT, $CONNECTC_PORT);
- # Now we need two Streams, cross-connected.
- my ( $stream1, $stream2 );
- $stream1 = IO::Async::Stream->new(
- handle => $socket1,
- on_read => sub {
- my ( undef, $buffref, undef) = @_;
- my_on_read($CONNECT_PORT, $CONNECTC_PORT, Client,
- shift @_, shift @_, shift @_);
- # Just copy all the data
- $stream2->write( $$buffref ); $$buffref = "";
- return 0;
- },
- on_closed => sub {
- $stream2->close_when_empty;
- print "Connection from $CONNECTC_HOST:$CONNECTC_PORT closed\n";
- },
- );
- $stream2 = IO::Async::Stream->new(
- handle => $socket2,
- on_read => sub {
- my ( undef, $buffref, undef) = @_;
- my_on_read($CONNECT_PORT, $CONNECTC_PORT, Server,
- shift @_, shift @_, shift @_);
- # Just copy all the data
- $stream1->write( $$buffref ); $$buffref = "";
- },
- on_closed => sub {
- $stream1->close_when_empty;
- print "Connection to $CONNECT_HOST:$CONNECT_PORT closed\n";
- },
- );
- $loop->add( $stream1 );
- $loop->add( $stream2 );
- } );
- $loop->connect(
- host => $CONNECT_HOST,
- service => $CONNECT_PORT,
- on_connected => sub {
- ( $socket2 ) = @_;
- my $tmp = shift @pepsid;
- &$tmp($CONNECT_PORT, $CONNECTC_PORT, $CONNECTC_HOST,
- $CONNECT_HOST, $socket1, $socket2)
- if defined($tmp);
- },
- on_resolve_error => sub { warn "Cannot resolve - $_[0]\n"; },
- on_connect_error => sub { warn "Cannot connect\n"; },
- );
- $loop->listen(
- service => 0,
- socktype => SOCK_STREAM,
- # Make sure we are on the SAME address client is connected.
- host => $CONNECTS_HOST,
- # D2 does not support IPV6, even if we do ;).
- family => AF_INET,
- on_listen => sub {
- my $sock = shift @_;
- $CONNECTS_PORT = $sock->sockport;
- $loop->loop_stop();
- },
- on_accept => sub {
- ( $socket1 ) = @_;
- $CONNECTC_HOST = $socket1->peerhost;
- $CONNECTC_PORT = $socket1->peerport;
- my $tmp = shift @pepsid;
- &$tmp($CONNECT_PORT, $CONNECTC_PORT, $CONNECTC_HOST,
- $CONNECT_HOST, $socket1, $socket2)
- if defined($tmp);
- },
- on_resolve_error => sub { warn "Cannot resolve - $_[0]\n"; },
- on_listen_error => sub { warn "Cannot listen\n"; },
- );
- return 0;
- };
- #############################
- # End Fresh rehash of main. #
- #############################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement