-
Notifications
You must be signed in to change notification settings - Fork 30
/
bserver.pl
executable file
·115 lines (105 loc) · 3.38 KB
/
bserver.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env perl
# this is PoC for the Mikrotik bandwith server. On mikorotik use
# /tool bandwidth-test <ip> duration=10s protocol=tcp tcp-connection-count=1
# to connect to it
use IO::Socket::INET;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Data::Dumper;
# auto-flush on socket
$| = 1;
my $require_auth=0;
my $udp_port_offset=256;
# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '0.0.0.0',
LocalPort => '2000',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 2000\n";
while(1)
{
# waiting for a new client connection
my $client_socket = $socket->accept();
# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address:$client_port\n";
print "sending hello\n";
# write response data to the connected client
$data = pack 'H*', '01000000';
$client_socket->send($data);
print "reading reply\n";
$client_socket->recv($data, 1024);
print "data is ".unpack('H*', "$data")."\n";
my $action=unpackCmd($data);
print(Dumper($action));
# send auth requested command
if ($require_auth) {
$client_socket->send(pack 'H*', '02000000');
$digest=pack 'H*', '00000000000000000000000000000000';
# print "digest md5=".md5_hex($digest)." expected reply is: ".md5_hex(md5($digest))."\n";
$client_socket->send($digest);
$client_socket->recv($data, 1024);
print "client auth data is ".unpack('H*', "$data")."\n";
# $client_socket->send(pack 'H*', '00000000'); # auth failed
$client_socket->send(pack 'H*', '01000000'); # auth accepted
}
if ($action->{proto} eq 'TCP') {
# send data
while (1) {
$client_socket->send(pack 'H*', '00' x $action->{tx_size});
print ".";
}
# notify client that response has been sent
shutdown($client_socket, 1);
} else {
print "sending hello\n";
# write response data to the connected client
$data = pack 'H*', '01000000';
$client_socket->send($data);
my $locudpport=2001;
$data= pack('n', $locudpport);
print "sending local port: " . unpack('H*', $data) . "\n";
$client_socket->send($data);
#while(defined($client_socket->recv($data, 32768))) {
# print "client recv data is ".unpack('H*', "$data")."\n";
#}
#exit(0);
my $tx_socket = new IO::Socket::INET (
PeerHost => "$client_address",
PeerPort => $locudpport+$udp_port_offset,
LocalHost => '0.0.0.0',
LocalPort => $locudpport,
Proto => 'udp',
);
die "cannot create socket $!\n" unless $tx_socket;
#sleep(10);
my $seq=1;
while (1) {
$tx_socket->send(pack 'NNH*', 0, $seq, '00' x ($action->{tx_size}-8-28));
$seq++;
print ".";
}
$tx_socket->close();
}
}
$socket->close();
sub unpackCmd {
my $data=shift;
my @cmdlist=unpack('CCCCvvNN', $data);
my $cmd={
proto => $cmdlist[0] ? 'TCP' : 'UDP',
direction => ($cmdlist[1]==1) ? 'RX' : (($cmdlist[1]==2) ? 'TX' : 'TXRX'),
random => ($cmdlist[2]==0),
tcp_conn_count => ($cmdlist[3]==0) ? 1 : $cmdlist[3],
tx_size => $cmdlist[4],
unknown => $cmdlist[5],
remote_tx_speed => $cmdlist[6],
local_tx_speed => $cmdlist[7],
};
#print "data is ".unpack('H*', "$data")."\n";
return($cmd);
}