[pve-devel] [PATCH common] add websocket client implementation
Fabian Grünbichler
f.gruenbichler at proxmox.com
Fri Mar 6 11:20:26 CET 2020
based on existing one in pve-client's LXC console handling code by René
Jochum.
it probably would make sense to generalize this further to allow code
sharing with pve-http-server's server implementation.
Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
src/Makefile | 3 +-
src/PVE/WebSocket.pm | 356 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 358 insertions(+), 1 deletion(-)
create mode 100644 src/PVE/WebSocket.pm
diff --git a/src/Makefile b/src/Makefile
index 02f1f56..7eb575a 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -33,7 +33,8 @@ LIB_SOURCES = \
SysFSTools.pm \
Systemd.pm \
Ticket.pm \
- Tools.pm
+ Tools.pm \
+ WebSocket.pm
all:
diff --git a/src/PVE/WebSocket.pm b/src/PVE/WebSocket.pm
new file mode 100644
index 0000000..4cda43d
--- /dev/null
+++ b/src/PVE/WebSocket.pm
@@ -0,0 +1,356 @@
+package PVE::WebSocket;
+
+use strict;
+use warnings;
+
+use Errno qw(EINTR EAGAIN);
+use IO::Socket::SSL;
+use MIME::Base64;
+use Digest::SHA;
+use HTTP::Response;
+use URI::Escape;
+
+my $CRLF = "\x0D\x0A";
+
+sub new {
+ my ($class, $host, $port, $path, %options) = @_;
+
+ my $self = {
+ host => $host,
+ port => $port,
+ path => $path,
+ max_payload_size => $options{max_payload_size} // 128*1024,
+ buf => '',
+ };
+
+ my $ssl_opts = $options{ssl_opts};
+ if (!$ssl_opts) {
+ $ssl_opts->{SSL_verify_mode} = SSL_VERIFY_NONE;
+ }
+
+ my $socket = IO::Socket::SSL->new(
+ PeerHost => $host,
+ PeerPort => $port,
+ %$ssl_opts,
+ timeout => 30) ||
+ die "failed to connect: $!\n";
+
+ $self->{socket} = $socket;
+
+ return bless $self, $class;
+};
+
+my $wb_socket_read_available_bytes = sub {
+ my ($self) = @_;
+ my $nr = $self->{socket}->sysread($self->{buf}, $self->{max_payload_size}, length($self->{buf}));
+ if (!defined($nr) && !($! == EINTR || $! == EAGAIN)) {
+ die "web socket read error - $!\n";
+ }
+ return $nr;
+};
+
+sub connect {
+ my ($self, $header) = @_;
+
+ my $key = '';
+ $key .= chr(int(rand(256))) for 1 .. 16;
+ my $enckey = MIME::Base64::encode_base64($key, '');
+
+ my $request = "GET $self->{path} HTTP/1.1$CRLF"
+ . "Upgrade: WebSocket$CRLF"
+ . "Connection: Upgrade$CRLF"
+ . "Host: $self->{host}$CRLF"
+ . "Sec-WebSocket-Key: $enckey$CRLF"
+ . "Sec-WebSocket-Version: 13$CRLF"
+ . "Sec-WebSocket-Protocol: binary$CRLF"
+ . ($header ? "${header}$CRLF" : "")
+ . "$CRLF";
+
+ $self->{enckey} = $enckey;
+
+ $self->send($request, 1);
+
+ my $raw_response = '';
+
+ while(1) {
+ my $nr = $wb_socket_read_available_bytes->($self);
+ if ($self->{buf} =~ s/^(.*?)$CRLF$CRLF//s) {
+ $raw_response = $1;
+ last;
+ }
+ last if !$nr;
+ };
+
+ my $response = HTTP::Response->parse($raw_response);
+ my $wsaccept = Digest::SHA::sha1_base64("${enckey}258EAFA5-E914-47DA-95CA-C5AB0DC85B11") . "=";
+
+ die "got invalid websocket reponse: $raw_response\n"
+ if !(($response->code == 101) &&
+ (lc $response->header('connection') eq 'upgrade') &&
+ (lc $response->header('upgrade') eq 'websocket') &&
+ ($response->header('sec-websocket-protocol') eq 'binary') &&
+ ($response->header('sec-websocket-accept') eq $wsaccept));
+
+ return;
+};
+
+sub encode {
+ my ($self, $payload, $opcode) = @_;
+
+ my $string = $opcode ? "$opcode" : "\x82"; # binary frame
+ my $payload_len = length($payload);
+ if ($payload_len <= 125) {
+ $string .= pack 'C', $payload_len | 128;
+ } elsif ($payload_len <= 0xffff) {
+ $string .= pack 'C', 126 | 128;
+ $string .= pack 'n', $payload_len;
+ } else {
+ $string .= pack 'C', 127 | 128;
+ $string .= pack 'Q>', $payload_len;
+ }
+
+ $string .= pack 'N', 0; # we simply use 0 as mask
+ $string .= $payload;
+
+ return $string;
+};
+
+sub send {
+ my ($self, $data, $raw) = @_;
+
+ $data = $self->encode($data) if !$raw;
+
+ my $len = length($data);
+ my $todo = $len;
+ my $offset = 0;
+ while(1) {
+ my $nr = syswrite($self->{socket}, $data, $todo, $offset);
+ if (!defined($nr)) {
+ next if $! == EINTR || $! == EAGAIN;
+ die "websocket write error - $!\n"
+ }
+ $offset += $nr;
+ $todo -= $nr;
+ last if $todo <= 0;
+ }
+
+ if ($self->{pong}) {
+ # heartbeat / pong
+ $self->send($self->encode($self->{pong_data}, "\x8A"), 1);
+ $self->{pong} = 0;
+ $self->{pong_data} = undef;
+ }
+
+ return $len;
+};
+
+sub recv {
+ my ($self) = @_;
+
+ my $bytes = $wb_socket_read_available_bytes->($self);
+ if (!defined($bytes)) {
+ return undef;
+ } elsif ($bytes == 0) {
+ return (undef, 0); # EOF
+ } else {
+ my ($payload, $req_close) = $self->decode();
+ return ($payload, $bytes, $req_close);
+ }
+
+}
+
+sub decode {
+ my ($self) = @_;
+
+ my $payload;
+ my $req_close = 0;
+
+ my $buf = \$self->{buf};
+
+ while (my $len = length($$buf)) {
+ last if $len < 2;
+
+ my $hdr = unpack('C', substr($$buf, 0, 1));
+ my $opcode = $hdr & 0b00001111;
+ my $fin = $hdr & 0b10000000;
+
+ die "received fragmented websocket frame\n" if !$fin;
+
+ my $rsv = $hdr & 0b01110000;
+ die "received websocket frame with RSV flags\n" if $rsv;
+
+ my $payload_len = unpack 'C', substr($$buf, 1, 1);
+
+ my $masked = $payload_len & 0b10000000;
+ die "received masked websocket frame from server\n" if $masked;
+
+ my $offset = 2;
+ $payload_len = $payload_len & 0b01111111;
+ if ($payload_len == 126) {
+ last if $len < 4;
+ $payload_len = unpack('n', substr($$buf, $offset, 2));
+ $offset += 2;
+ } elsif ($payload_len == 127) {
+ last if $len < 10;
+ $payload_len = unpack('Q>', substr($$buf, $offset, 8));
+ $offset += 8;
+ }
+
+ die "received too large websocket frame (len = $payload_len)\n"
+ if ($payload_len > $self->{max_payload_size}) || ($payload_len < 0);
+
+ last if $len < ($offset + $payload_len);
+
+ my $data = substr($$buf, 0, $offset + $payload_len, ''); # now consume data
+
+ my $frame_data = substr($data, $offset, $payload_len);
+
+ if ($opcode == 1 || $opcode == 2) {
+ # text or binary data
+ $payload = '' if !defined($payload);
+ $payload .= $frame_data;
+ # continue
+ } elsif ($opcode == 8) {
+ # close received
+ my $statuscode = unpack ("n", $frame_data);
+ $req_close = 1;
+ } elsif ($opcode == 9) {
+ # ping received, schedule pong
+ $self->{pong} = 1;
+ $self->{pong_data} = $frame_data;
+ } elsif ($opcode == 0xA) {
+ # pong received, continue
+ } else {
+ die "received unhandled websocket opcode $opcode\n";
+ }
+ }
+
+ return ($payload, $req_close);
+};
+
+sub process {
+ my ($self) = @_;
+
+ my $read_select = IO::Select->new;
+ my $write_select = IO::Select->new;
+
+ my $output_buffer = ''; # write buffer for output/writer
+ my $websock_buffer = ''; # write buffer for websocket
+
+ eval {
+ $SIG{TERM} = $SIG{INT} = $SIG{KILL} = sub { die "received interrupt\n"; };
+
+ $read_select->add($self->{socket});
+ my $input_fh = $self->{reader} // \*STDIN;
+ $read_select->add($input_fh);
+
+ my $output_fh = $self->{writer} // \*STDOUT;
+
+ my $max_buffer_len = 256*1024;
+
+ my $drain_buffer = sub {
+ my ($fh, $buffer_ref) = @_;
+
+ if (length($$buffer_ref)) {
+ my $nr = syswrite($fh, $$buffer_ref);
+ if (!defined($nr)) {
+ return if $! == EINTR || $! == EAGAIN;
+ die "drain buffer - write error - $!\n";
+ }
+ return $nr if !$nr;
+ substr($$buffer_ref, 0, $nr, '');
+ }
+
+ $write_select->remove($fh) if !length($$buffer_ref);
+ };
+
+ my $close = 0;
+
+ while (!$close) {
+ $! = 0;
+ while(my ($readable, $writable) = IO::Select->select($read_select, $write_select, undef, 3)) {
+ foreach my $fh (@$writable) {
+ if ($fh == $output_fh) {
+ $drain_buffer->($output_fh, \$output_buffer);
+ $read_select->add($self->{socket})
+ if length($output_buffer) <= $max_buffer_len;
+ } elsif ($fh == $self->{socket}) {
+ $drain_buffer->($self->{socket}, \$websock_buffer);
+ }
+ }
+
+ foreach my $fh (@$readable) {
+ if ($fh == $self->{socket}) {
+ my $nr = $wb_socket_read_available_bytes->($self);
+ if (!defined($nr)) {
+ # wait
+ } elsif ($nr == 0) {
+ $read_select->remove($self->{socket});
+ $write_select->remove($self->{socket});
+ $close = 1;
+ next;
+ } else {
+ my ($payload, $req_close) = $self->decode();
+ if (defined($payload) && length($payload)) {
+ $output_buffer .= $payload;
+ $write_select->add($output_fh);
+ if (length($output_buffer) > $max_buffer_len) {
+ $read_select->remove($self->{socket});
+ }
+ }
+ if ($req_close) {
+ $close = 1;
+ next;
+ }
+ }
+
+ } elsif ($fh == $input_fh) {
+ my $nr = sysread($input_fh, my $buff, 63*1024);
+ if (!$nr) {
+ # EOF or error
+ $read_select->remove($input_fh);
+ # close connection
+ if (!$close) {
+ $websock_buffer .= $self->encode(pack('n', 0), "\x88"); # close with status code 0
+ $close = 1;
+ $write_select->add($self->{socket});
+ }
+ } else {
+ $websock_buffer .= $self->encode($buff);
+ $write_select->add($self->{socket});
+ }
+ }
+ }
+ }
+
+ if (!$close) {
+ if ($!) {
+ die "error processing websocket connection - $!\n";
+ }
+ # heartbeat / ping
+ $websock_buffer .= $self->encode('1', "\x89");
+ $write_select->add($self->{socket});
+ }
+ }
+ };
+ my $err = $@;
+
+ eval {
+ if ($self->{socket}->connected) {
+ # close connection
+ $websock_buffer .= $self->encode(pack('n', 0), "\x88"); # close with status code 0
+ $self->send($websock_buffer, 1);
+ $websock_buffer = '';
+ }
+ $self->{socket}->close();
+ $self->{reader}->close() if defined($self->{reader});
+ $self->{writer}->close() if defined($self->{writer});
+ };
+ warn $@ if $@; # show cleanup errors
+
+ print STDERR "\nERROR: $err" if $err;
+
+ return undef;
+}
+
+1;
--
2.20.1
More information about the pve-devel
mailing list