… | |
… | |
7 | use Socket; |
7 | use Socket; |
8 | use AnyEvent::Socket; |
8 | use AnyEvent::Socket; |
9 | |
9 | |
10 | CONF BIND_ADDRESSES = [[undef, 13327]]; |
10 | CONF BIND_ADDRESSES = [[undef, 13327]]; |
11 | |
11 | |
12 | our $MAX_DETECT = 16; # how many bytes to raed to identify the protocol |
12 | our $MAX_DETECT; # how many bytes to read to identify the protocol |
13 | |
13 | |
14 | our @LISTENERS; |
14 | our @LISTENERS; |
|
|
15 | our @DETECTORS; |
|
|
16 | our %DETECTORS; |
|
|
17 | |
|
|
18 | sub _update_detectors { |
|
|
19 | $MAX_DETECT = List::Util::max map $_->[1], values %DETECTORS; |
|
|
20 | |
|
|
21 | use Data::Dump; |
|
|
22 | ddx [$MAX_DETECT, \%DETECTORS]; |
|
|
23 | } |
|
|
24 | |
|
|
25 | sub register($$$$) { |
|
|
26 | my ($name, $max_detect, $detect, $serve) = @_; |
|
|
27 | |
|
|
28 | $DETECTORS{$name} = [$max_detect, $detect, $serve]; |
|
|
29 | _update_detectors; |
|
|
30 | |
|
|
31 | Guard::guard { |
|
|
32 | delete $DETECTORS{$name}; |
|
|
33 | _update_detectors; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | |
|
|
37 | our $deliantra_detector = ext::tcp::register deliantra => 10, sub { |
|
|
38 | /^..version /s |
|
|
39 | }, sub { |
|
|
40 | my $ns = cf::client::create fileno $_[1], $_[0]; |
|
|
41 | $ns->inbuf_append ($_[2]); |
|
|
42 | }; |
15 | |
43 | |
16 | for (@$BIND_ADDRESSES) { |
44 | for (@$BIND_ADDRESSES) { |
17 | my ($host, $port) = @$_; |
45 | my ($host, $port) = @$_; |
18 | cf::info "listening on ", (format_hostport $host, $port), "\n"; |
46 | cf::info "listening on ", (format_hostport $host, $port), "\n"; |
19 | |
47 | |
… | |
… | |
24 | my $lhost = AnyEvent::Socket::format_address |
52 | my $lhost = AnyEvent::Socket::format_address |
25 | +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; |
53 | +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; |
26 | |
54 | |
27 | my $id = format_hostport $host, $port; |
55 | my $id = format_hostport $host, $port; |
28 | |
56 | |
29 | cf::info "$id: new connection\n" |
57 | cf::info "$id: accepted connection.\n" |
30 | if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs |
58 | if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs |
31 | |
59 | |
32 | my $buf; |
60 | my $buf; |
33 | my $w; $w = AE::io $fh, 0, sub { |
61 | my $w; $w = AE::io $fh, 0, sub { |
34 | my $len = sysread $fh, $buf, 512, length $buf; |
62 | my $len = sysread $fh, $buf, 512, length $buf; |
35 | |
63 | |
36 | if ($len) { |
64 | if ($len) { |
37 | if ($buf =~ /^..version /s) { # deliantra protocol |
65 | for ($buf) { |
|
|
66 | while (my ($name, $v) = each %DETECTORS) { |
|
|
67 | if (my $cb = $v->[1]()) { |
38 | undef $w; |
68 | undef $w; |
|
|
69 | cf::debug "$id: detected protocol $name.\n"; |
|
|
70 | $v->[2]($id, $fh, $buf); |
|
|
71 | return; |
|
|
72 | } |
|
|
73 | } |
39 | |
74 | |
40 | my $ns = cf::client::create fileno $fh, $host; |
75 | if (length >= $MAX_DETECT) { # unable to detect protocol |
41 | $ns->inbuf_append ($buf); |
|
|
42 | |
|
|
43 | } elsif ($buf =~ /^GET / && defined &ext::http::server) { # http or websocket |
|
|
44 | undef $w; |
76 | undef $w; |
45 | |
77 | cf::debug "$id: data received, but cannot detect protocol, closing.\n"; |
46 | &ext::http::server ($fh, $buf); |
|
|
47 | |
|
|
48 | } elsif (length $buf >= $MAX_DETECT) { # unable to detect protocol |
|
|
49 | undef $w; |
78 | } |
50 | |
|
|
51 | cf::info "$id: protocol detection error\n"; |
|
|
52 | } |
79 | } |
53 | } else { |
80 | } else { |
54 | undef $w; |
81 | undef $w; |
55 | |
|
|
56 | cf::info "$id: read error during protocol detection\n"; |
82 | cf::info "$id: read error during protocol detection ($!)\n"; |
57 | } |
83 | } |
58 | }; |
84 | }; |
59 | }; |
85 | }; |
60 | } |
86 | } |
61 | |
87 | |