1 | #! perl |
1 | #! perl # mandatory |
2 | |
2 | |
3 | # this listens for new tcp connections and hands them over to the server core |
3 | # this listens for new tcp connections and hands them over to the server core |
4 | # wether this being an extension introduces or reduces stability problems |
4 | # whether this being an extension introduces or reduces stability problems |
5 | # is unknown as of today. |
5 | # is unknown as of today. |
6 | |
6 | |
7 | use Socket; |
7 | use Socket; |
8 | use IO::Socket::INET; |
8 | use AnyEvent::Socket; |
9 | |
9 | |
10 | our $LISTEN = new IO::Socket::INET |
10 | CONF BIND_ADDRESSES = [[undef, 13327]]; |
11 | LocalPort => cf::settings->csport, |
|
|
12 | Listen => 1, |
|
|
13 | Blocking => 0, |
|
|
14 | ReuseAddr => 1; |
|
|
15 | |
11 | |
16 | if (!$LISTEN) { |
12 | our $MAX_DETECT = 32; # how many bytes to read to identify the protocol |
17 | # extension yes, completely stupid, not yet |
13 | |
18 | warn "unable to establish listen socket, exiting.\n"; |
14 | our @LISTENERS; |
19 | exit (2); |
15 | |
|
|
16 | sub flash_policy_server { |
|
|
17 | my ($fh) = @_; |
|
|
18 | |
|
|
19 | # socket policy file, just write answer and hope the kernel accepts it in one go |
|
|
20 | syswrite $fh, <<EOF . "\x00" |
|
|
21 | <?xml version="1.0"?> |
|
|
22 | <!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"> |
|
|
23 | <cross-domain-policy> |
|
|
24 | <allow-access-from domain="*" to-ports="*"/> |
|
|
25 | </cross-domain-policy> |
|
|
26 | EOF |
20 | } |
27 | } |
21 | |
28 | |
22 | our $LISTENER = EV::io $LISTEN, EV::READ, sub { |
29 | for (@$BIND_ADDRESSES) { |
23 | my ($fh, $peername) = $LISTEN->accept |
30 | my ($host, $port) = @$_; |
24 | or return; |
31 | cf::info "listening on ", (format_hostport $host, $port), "\n"; |
25 | |
32 | |
26 | my $fd = fileno $fh; |
33 | push @LISTENERS, tcp_server $host, $port, sub { |
27 | my $host = inet_ntoa +(sockaddr_in $peername)[1]; |
34 | my ($fh, $host, $port) = @_ |
|
|
35 | or return; |
28 | |
36 | |
29 | warn "new connection from $host\n"; |
37 | my $lhost = AnyEvent::Socket::format_address |
|
|
38 | +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; |
30 | |
39 | |
31 | # HACK to avoid blocking on common files on log-in. |
40 | my $id = format_hostport $host, $port; |
32 | # remove once async |
|
|
33 | cf::async {#d# |
|
|
34 | warn "HACK: $cf::CONFDIR/$_", Coro::AIO::aio_load "$cf::CONFDIR/$_", my $dummy for qw(rules news motd);#d# |
|
|
35 | warn "HACK: $cf::VARDIR/crossfiremail", Coro::AIO::aio_load "$cf::VARDIR/crossfiremail", my $dummy;#d# |
|
|
36 | cf::client::create $fd, $host; |
|
|
37 | undef $fh;#d# |
|
|
38 | };#d#d |
|
|
39 | }; |
|
|
40 | |
41 | |
|
|
42 | cf::info "$id: new connection\n" |
|
|
43 | if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs |
|
|
44 | |
|
|
45 | my $buf; |
|
|
46 | my $w; $w = AE::io $fh, 0, sub { |
|
|
47 | my $len = sysread $fh, $buf, 512, length $buf; |
|
|
48 | |
|
|
49 | if ($len) { |
|
|
50 | if ($buf =~ /^..version /s) { # deliantra protocol |
|
|
51 | undef $w; |
|
|
52 | |
|
|
53 | my $ns = cf::client::create fileno $fh, $host; |
|
|
54 | $ns->inbuf_append ($buf); |
|
|
55 | |
|
|
56 | } elsif ($buf =~ /^GET / && defined &ext::http::server) { # http or websocket |
|
|
57 | undef $w; |
|
|
58 | |
|
|
59 | &ext::http::server ($id, $fh, $buf); |
|
|
60 | |
|
|
61 | } elsif ($buf =~ /^<policy-file-request\/>\x00/) { |
|
|
62 | undef $w; |
|
|
63 | |
|
|
64 | flash_policy_server $fh; |
|
|
65 | |
|
|
66 | } elsif (length $buf >= $MAX_DETECT) { # unable to detect protocol |
|
|
67 | undef $w; |
|
|
68 | |
|
|
69 | cf::info "$id: protocol detection error\n"; |
|
|
70 | } |
|
|
71 | } else { |
|
|
72 | undef $w; |
|
|
73 | |
|
|
74 | cf::info "$id: read error during protocol detection\n"; |
|
|
75 | } |
|
|
76 | }; |
|
|
77 | }; |
|
|
78 | } |
|
|
79 | |