#!/usr/bin/perl use strict; use Getopt::Long; use IO::Select; use JSON; use POSIX qw(strftime setsid setuid setgid); use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; use Regexp::Common qw(net); use Socket qw(AF_INET AF_INET6 inet_ntop unpack_sockaddr_in unpack_sockaddr_in6); use URI; # 1. Parse options my ( $accessLog, $addr, $cert, $daemon, $_debug, $group, $help, $key, $maxRequestSize, $maxRequestTime, $maxRestarts, $maxWaitingConn, $quiet, $pidFile, $port, $restartWindow, $token, $user, ); # HTTP header separator use constant HS => qr/(?:\r?\n){2}/; my @CMDLINE = @ARGV; GetOptions( 'access-log=s' => \$accessLog, 'addr|a=s@' => \$addr, 'cert=s' => \$cert, 'daemon' => \$daemon, 'debug|d' => \$_debug, 'group|g=s' => \$group, 'help|h' => \$help, 'key=s' => \$key, 'max-request-size=s' => \$maxRequestSize, 'max-request-time=s' => \$maxRequestTime, 'max-restarts=s' => \$maxRestarts, 'max-waiting-conn=s' => \$maxWaitingConn, 'quiet|q' => \$quiet, 'pid-file=s' => \$pidFile, 'port|p=s' => \$port, 'restart-window' => \$restartWindow, 'token=s' => \$token, 'user|u=s' => \$user, ); $accessLog ||= $ENV{PUBSUB_ACCESS_LOG}; $addr ||= [ split( /\s+/, $ENV{PUBSUB_ADDR} ) ]; $cert ||= $ENV{PUBSUB_CERT}; $daemon ||= $ENV{PUBSUB_DAEMON}; $_debug ||= $ENV{PUBSUB_DEBUG}; $group ||= $ENV{PUBSUB_GROUP}; $key ||= $ENV{PUBSUB_KEY}; $maxRequestSize ||= $ENV{PUBSUB_MAX_REQUEST_SIZE} || 8192; $maxRequestTime ||= $ENV{PUBSUB_MAX_REQUEST_TIME} || 5; $maxRestarts ||= $ENV{PUBSUB_MAX_RESTARTS} || 5; $maxWaitingConn ||= $ENV{PUBSUB_MAX_WAITING_CONN} || 10; $pidFile ||= $ENV{PUBSUB_PID_FILE}; $quiet ||= $ENV{PUBSUB_QUIET}; $restartWindow ||= $ENV{PUBSUB_RESTART_WINDOW} || 60; $token ||= $ENV{PUBSUB_TOKEN}; $user ||= $ENV{PUBSUB_USER}; if ($help) { require Pod::Usage; Pod::Usage::pod2usage( -exitcode => 0, -verbose => 2 ); } &daemonize if $daemon; sub debug { print STDERR map { "$_\n" } @_ if $_debug; } sub warning { print STDERR map { "$_\n" } @_ unless $quiet; } sub error { print STDERR map { "$_\n" } @_; } sub printWebLog { return if $quiet and !$accessLog; my ( $msg, $ip ) = @_; my $date = strftime( "[%d/%b/%Y:%H:%M:%S %z]", localtime ); my $log = "$ip - - $date $msg -\n"; if ( $accessLog eq 'STDERR' ) { print STDERR $log; } else { print $log; } debug "Request: $msg"; } if ( $accessLog and $accessLog ne '-' and $accessLog ne 'STDERR' ) { debug "Access logs will be written into $accessLog\n"; open STDOUT, '>>', $accessLog or die "Unable to write logs: $!"; if ($daemon) { open STDERR, '>&', fileno(STDOUT) or die "Can't dup STDERR: $!"; STDERR->autoflush(1); } } if ( $cert xor $key ) { die '--cert and --key must be used together'; } unless ($token) { warning "No token given, this means that anybody can use this server" unless $quiet; } # 2. Create server my @servers; my $selector = IO::Select->new; $addr = [0] unless @$addr; foreach my $a (@$addr) { my $server; my $p; # IPv6 addresses with port if ( $a =~ s/^\s*\[($RE{net}{IPv6})\]:(\d+)\s*$/$1/ ) { $p = $2; } elsif ( $a =~ s/^\s*($RE{net}{IPv4}):(\d+)\s*$/$1/ ) { $p = $2; } elsif ( $a and $a !~ s/^\s*\[?($RE{net}{IPv4}|$RE{net}{IPv6})\]?\s*/$1/ ) { die qq'Malformed IP address "$a"'; } else { $p = $port || ( $ENV{PUBSUB_PORT} || ( $cert ? 8443 : 8080 ) ); } my %args = ( ( $a ? ( LocalAddr => $a ) : () ), LocalPort => $p, Listen => $maxWaitingConn, ReuseAddr => 1, ReusePort => 1, ); if ($cert) { require IO::Socket::SSL; $server = IO::Socket::SSL->new( %args, SSL_cert_file => $cert, SSL_key_file => $key, SSL_server => 1, ); } else { require IO::Socket::IP; $server = IO::Socket::IP->new( %args, Proto => 'tcp', ); } die "Cannot create socket (" . ( $a ? "address: $a, " : '' ) . "port: $p): $!" unless $server; debug 'Listening on ' . ( $a ? "$a:" : 'port ' ) . $p; $selector->add($server); push @servers, $server; } if ($group) { my $grp = getgrnam($group) or die "Can't change gid to $group"; setgid($grp) or die "setgid: $!"; } if ($user) { my $uid = getpwnam($user) or die "Can't change uid to $user"; setuid($uid) or die "setuid: $!"; } my $json = JSON->new->utf8; # 3. Manage requests my %clients; while (1) { for my $sock ( $selector->can_read(0.1) ) { my ($server) = grep { $sock eq $_ } @servers; if ($server) { my $client = $server->accept or next; $client->autoflush(1); $selector->add($client); } else { my $fileno = fileno($sock); my $buf = ''; my $n = sysread( $sock, $buf, 4096 ); if ( !defined $n || $n == 0 ) { cleanupClient($sock); next; } # Known clients are subscribers, we can discard if ( exists $clients{$fileno} ) { my $frame = $clients{$fileno}->{frame}; $frame->append($buf); while ( my $msg = $frame->next ) { # read-only client; we discard messages } } else { my $req = $buf; my $start = time; while ( $req !~ HS ) { last if length($req) > $maxRequestSize; last if ( time - $start ) > $maxRequestTime; my $tmp; my $r = sysread( $sock, $tmp, 4096 ); $req .= $tmp if $r; } # Get headers my ( $headers, $body ) = split( HS, $req, 2 ); my @lines = split /\r?\n/, $headers; my $request = shift @lines; my %hdr; for (@lines) { if (/^([^:]+):\s*(.+)$/) { $hdr{ lc $1 } = $2; } } # Auth check if ( $token and $hdr{authorization} ne "Bearer $token" ) { sendHttp( $sock, 401, "Unauthorized", "Bad token", $request ); next; } # WebSocket GET if ( $request =~ m{^GET\s+(/subscribe\S*)\s+HTTP/1.[01]$} ) { my %params = eval { URI->new($1)->query_form }; warning "URI parse error: $@" if $@; my @channels = split /,/, ( $params{channels} // '' ); unless (@channels) { warning "Missing channel(s)"; sendHttp( $sock, 400, "Bad Request", "Missing channel(s)", $request ); } my $hs = Protocol::WebSocket::Handshake::Server->new; $hs->parse($req); if ( $hs->is_done ) { my $ip = getIp($sock); my $res = $hs->to_string; my $len = length($res); print $sock $res; $clients{$fileno} = { socket => $sock, frame => Protocol::WebSocket::Frame->new, channels => { map { $_ => 1 } @channels }, }; $request =~ s/[\r\n]//g; printWebLog( qq("$request" 200 $len), $ip ); } else { warning "ERROR: " . $hs->error; sendHttp( $sock, 400, "Bad Request", "WebSocket handshake failed", $request ); } } # Publish API elsif ( $request =~ m{^POST\s+/publish\s+HTTP/1.[01]$} ) { my $len = $hdr{'content-length'} // length($body); my $remaining = $len - length($body); while ( $remaining > 0 ) { last if length($req) > $maxRequestSize; last if ( time - $start ) > $maxRequestTime; my $tmp; my $r = sysread( $sock, $tmp, $remaining ); last unless $r; $req .= $tmp; $body .= $tmp; $remaining -= $r; } if ($remaining) { sendHttp( $sock, 408, 'Request timeout', $request ); next; } my $data = eval { $json->decode($body) }; if ( $@ || !defined $data ) { sendHttp( $sock, 400, "Bad Request", "Missing or invalid JSON", $request ); next; } debug "Message to publish: $body"; my $channel = $data->{channel}; unless ($channel) { sendHttp( $sock, 400, "Bad Request", "Missing channel", $request ); next; } my $msg = $body; my $frame = Protocol::WebSocket::Frame->new; $frame->append($msg); my $bytes = $frame->to_bytes; for my $c ( values %clients ) { next unless $c->{channels}->{$channel}; my $ws_sock = $c->{socket}; print $ws_sock $bytes; } sendHttp( $sock, 200, "OK", "Message sent", $request ); } else { sendHttp( $sock, 404, "Not Found", "Unknown endpoint", $request ); } } } } } sub sendHttp { my ( $sock, $code, $status, $body, $request ) = @_; my $ip = getIp($sock); my $len = length($body); my $resp = "HTTP/1.1 $code $status\r\n" . "Content-Type: text/plain\r\n" . "Content-Length: $len\r\n" . "Connection: close\r\n\r\n" . $body; print $sock $resp; cleanupClient($sock); $request =~ s/[\r\n]//g; $len = length($resp); printWebLog( qq("$request" $code $len), $ip ); } sub cleanupClient { my ($sock) = @_; $selector->remove($sock); my $fileno = fileno($sock); delete $clients{$fileno}; close $sock; } sub getIp { my ($sock) = @_; my $peer = getpeername($sock); my ($family) = unpack( 'S', $peer ); if ( $family == AF_INET ) { my ( $port, $addr ) = unpack_sockaddr_in($peer); return inet_ntop( AF_INET, $addr ); } else { my ( $port, $addr ) = unpack_sockaddr_in6($peer); return inet_ntop( AF_INET6, $addr ); } } sub daemonize { open STDIN, '<', '/dev/null' or die "Can't read /dev/null: $!"; unless ( $accessLog eq '-' ) { open STDOUT, '>', '/dev/null' or die "Can't write to /dev/null: $!"; } unless ( $accessLog eq 'STDERR' ) { open STDERR, '>', '/dev/null' or die "Can't write to /dev/null: $!"; } defined( my $pid = fork ) or die "Can't fork: $!"; exit if $pid; setsid() or die "Can't start a new session: $!"; defined( $pid = fork ) or die "Can't fork again: $!"; exit if $pid; umask 0; if ($pidFile) { open my $fh, '>', $pidFile or die "Can't write $pidFile: $!"; print $fh "$$\n"; close $fh; debug "Master pid written ($$)"; } else { debug "Master pid is $$"; } $0 = join ' ', $0, @CMDLINE; my $cpid; # Master process code (relaunch webserver in case of failure) if ( $cpid = fork ) { $| = 1; my @restartTimes; my $term; $SIG{CHLD} = 'DEFAULT'; $SIG{TERM} = sub { kill 'TERM', $cpid; waitpid( $cpid, 0 ); exit 0; }; while (1) { my $endedPid = waitpid( $cpid, 0 ); my $exitStatus = $? >> 8; print STDERR "Server $endedPid ended with status $exitStatus\n"; @restartTimes = grep { time() - $_ < $restartWindow } @restartTimes; print STDERR "@restartTimes >= $maxRestarts\n"; if ( @restartTimes >= $maxRestarts ) { print STDERR "Too many failures in $restartWindow, aborting\n"; exit 1; } sleep 1; last unless $cpid = fork; push @restartTimes, time; } } die $! unless defined $cpid; # Don't propagate 'TERM' signal to master setpgrp( 0, 0 ); } __END__ =encoding UTF-8 =head1 NAME llng-pubsub-server - Pub/Sub server based on websockets =head1 SYNOPSIS llng-pubsub-server