]> git.proxmox.com Git - pve-http-server.git/commitdiff
websocket: improve masking performance
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 6 Mar 2020 10:20:27 +0000 (11:20 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Sat, 7 Mar 2020 19:11:12 +0000 (20:11 +0100)
in order to make websocket proxying feasible as general tunnel, we need
to be able to transfer more than a few MB/s

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
PVE/APIServer/AnyEvent.pm

index 654f8f9b317184698e0bfb41ac166f9afe6ac25c..534027d5a0f87a2af07f95b8d7cd2adab0016517 100644 (file)
@@ -479,19 +479,19 @@ sub websocket_proxy {
 
                    my $data = substr($hdl->{rbuf}, 0, $offset + 4 + $payload_len, ''); # now consume data
 
-                   my @mask = (unpack('C', substr($data, $offset+0, 1)),
-                       unpack('C', substr($data, $offset+1, 1)),
-                       unpack('C', substr($data, $offset+2, 1)),
-                       unpack('C', substr($data, $offset+3, 1)));
-
+                   my $mask = substr($data, $offset, 4);
                    $offset += 4;
 
                    my $payload = substr($data, $offset, $payload_len);
 
-                   for (my $i = 0; $i < $payload_len; $i++) {
-                       my $d = unpack('C', substr($payload, $i, 1));
-                       my $n = $d ^ $mask[$i % 4];
-                       substr($payload, $i, 1, pack('C', $n));
+                   # NULL-mask might be used over TLS, skip to increase performance
+                   if ($mask ne pack('N', 0)) {
+                       # repeat 4 byte mask to payload length + up to 4 byte
+                       $mask = $mask x (int($payload_len / 4) + 1);
+                       # truncate mask to payload length
+                       substr($mask, $payload_len) = "";
+                       # (un-)apply mask
+                       $payload ^= $mask;
                    }
 
                    $payload = decode_base64($payload) if !$binary;