Skip to content

Commit

Permalink
Merge branch 'oskarbraten-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Aug 15, 2023
2 parents cdd9ea7 + 7ddf66a commit 4b3cc25
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 94 deletions.
2 changes: 1 addition & 1 deletion html5/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@
<script type="text/javascript" src="js/lib/jszip.js"></script>
<script type="text/javascript" src="js/lib/detect-zoom.js"></script>

<script type="text/javascript" src="js/Utilities.js"></script>
<script type="text/javascript" src="js/Keycodes.js"></script>
<script type="text/javascript" src="js/Protocol.js"></script>
<script type="text/javascript" src="js/Window.js"></script>
<script type="text/javascript" src="js/Notifications.js"></script>
<script type="text/javascript" src="js/Constants.js"></script>
<script type="text/javascript" src="js/Utilities.js"></script>
<script type="text/javascript" src="js/MediaSourceUtil.js"></script>
<script type="text/javascript" src="js/RgbHelpers.js"></script>
<script type="text/javascript" src="js/VideoDecoder.js"></script>
Expand Down
48 changes: 22 additions & 26 deletions html5/js/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ class XpraClient {
let name = "HTML";
if (navigator.userAgentData) {
const brands = navigator.userAgentData.brands;
if (brands.length>0) {
if (brands.length > 0) {
name = brands[0].brand + " " + brands[0].version;
}
}
Expand Down Expand Up @@ -1593,7 +1593,7 @@ class XpraClient {
"encodings.packet": true,
//skipping some keys
//ie: "encoding.min-quality": 50,
//ie: "encoding.min-speed": 80,
"encoding.min-speed": 50,
//ie: "encoding.non-scroll": ["rgb32", "png", "jpeg"],
//video stuff:
"encoding.color-gamut": Utilities.getColorGamut(),
Expand Down Expand Up @@ -2019,34 +2019,28 @@ class XpraClient {
e.preventDefault();
return;
}
let paste_data;
if (navigator.clipboard && navigator.clipboard.readText) {
navigator.clipboard.readText().then(
(text) => {
this.cdebug("clipboard", "paste event, text=", text);
const paste_data = unescape(encodeURIComponent(text));
this.clipboard_buffer = paste_data;
this.send_clipboard_token(paste_data);
this.clipboard_buffer = text;
const data = Utilities.StringToUint8(text);
this.send_clipboard_token(data);
},
(error) => this.cdebug("clipboard", "paste event failed:", error)
);
} else {
let datatype = TEXT_PLAIN;
if (Utilities.isIE()) {
datatype = "Text";
}
paste_data = unescape(
encodeURIComponent(clipboardData.getData(datatype))
);
cdebug("clipboard", "paste event, data=", paste_data);
this.clipboard_buffer = paste_data;
this.send_clipboard_token(paste_data);
const text = clipboardData.getData(TEXT_PLAIN);
cdebug("clipboard", "paste event, text=", text);
this.clipboard_buffer = text;
const data = Utilities.StringToUint8(text);
this.send_clipboard_token(data);
}
});
window.addEventListener("copy", (e) => {
const clipboard_buffer = this.get_clipboard_buffer();
const pasteboard = $(PASTEBOARD_SELECTOR);
pasteboard.text(decodeURIComponent(escape(clipboard_buffer)));
pasteboard.text(clipboard_buffer);
pasteboard.select();
this.cdebug(
"clipboard",
Expand All @@ -2058,7 +2052,7 @@ class XpraClient {
window.addEventListener("cut", (e) => {
const clipboard_buffer = this.get_clipboard_buffer();
const pasteboard = $(PASTEBOARD_SELECTOR);
pasteboard.text(decodeURIComponent(escape(clipboard_buffer)));
pasteboard.text(clipboard_buffer);
pasteboard.select();
this.cdebug(
"clipboard",
Expand Down Expand Up @@ -2188,7 +2182,7 @@ class XpraClient {
navigator.clipboard.readText().then(
(text) => {
this.debug("clipboard", "paste event, text=", text);
const clipboard_buffer = unescape(encodeURIComponent(text));
const clipboard_buffer = encodeURIComponent(text);
if (clipboard_buffer != this.clipboard_buffer) {
this.debug("clipboard", "clipboard contents have changed");
this.clipboard_buffer = clipboard_buffer;
Expand Down Expand Up @@ -2775,7 +2769,10 @@ class XpraClient {
if (!hello["client-shutdown"]) {
$("#shutdown_menu_entry").hide();
}
if (!this.file_transfer || ((!hello["file-transfer"]) && (!hello["file"] || !hello["file"]["enabled"]))) {
if (
!this.file_transfer ||
(!hello["file-transfer"] && (!hello["file"] || !hello["file"]["enabled"]))
) {
$("#upload_menu_entry").hide();
}

Expand Down Expand Up @@ -2962,10 +2959,10 @@ class XpraClient {
return;
}
}
const digest = Utilities.s(packet[3]);
const server_salt = Utilities.s(packet[1]);
const salt_digest = Utilities.s(packet[4]) || "xor";
const prompt = (Utilities.s(packet[5]) || "password").replace(
const digest = packet[3];
const server_salt = Uint8ToString(packet[1]);
const salt_digest = packet[4] || "xor";
const prompt = (packet[5] || "password").replace(
/[^\d+,. /:a-z]/gi,
""
);
Expand Down Expand Up @@ -3311,8 +3308,7 @@ class XpraClient {
);
if (this.server_is_desktop || this.server_is_shadow) {
window.noWindowList();
}
else if (win && win.has_decorations) {
} else if (win && win.has_decorations) {
const trimmedTitle = Utilities.trimString(win.title, 30);
window.addWindowListItem(win, wid, trimmedTitle);
}
Expand Down
42 changes: 34 additions & 8 deletions html5/js/OffscreenDecodeWorkerHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,39 @@
*/

const XpraOffscreenWorker = {
// OffscreenCanvas supported since v.16.4
isSafariVersionSupported() {
var match = navigator.userAgent.match(/version\/(\d+\.\d+)/i);
if (match && match[1]) {
var version = parseFloat(match[1]);
// Safari is buggy, see #227
return version >= 999.9;
}
return false;
},

// OffscreenCanvas supported since v.105 (with fixed added for 107/108)
isFirefoxVersionSupported() {
var match = navigator.userAgent.match(/firefox\/(\d+)/i);
if (match && match[1]) {
var version = parseInt(match[1], 10);
return version >= 108;
}
return false;
},

isAvailable() {
const isSafari = navigator.userAgent.toLowerCase().includes("safari");
if (isSafari) {
return false;
}
// We do not support firefox as it makes canvases flicker
const isFirefox = navigator.userAgent.toLowerCase().includes("firefox");
if (typeof OffscreenCanvas !== "undefined" && !isFirefox) {
var isSafari = navigator.userAgent.toLowerCase().includes("safari");
if (isSafari && !this.isSafariVersionSupported()) {
return false;
}

var isFirefox = navigator.userAgent.toLowerCase().includes("firefox");
if (isFirefox && this.isFirefoxVersionSupported()) {
return false;
}

if (typeof OffscreenCanvas !== "undefined") {
//we also need the direct constructor:
try {
new OffscreenCanvas(256, 256);
Expand All @@ -29,7 +54,8 @@ const XpraOffscreenWorker = {
}
}
console.warn(
"Offscreen decoding is not available. Please consider using Google Chrome for better performance."
"Offscreen decoding is not available. Please consider using " +
"Google Chrome, Firefox >= 108 or Safari >= 16.4 for better performance."
);
return false;
},
Expand Down
17 changes: 9 additions & 8 deletions html5/js/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class XpraProtocol {

// decrypt if needed
if (proto_crypto) {
this.cipher_in.update(forge.util.createBuffer(uintToString(packet_data)));
this.cipher_in.update(forge.util.createBuffer(Utilities.Uint8ToString(packet_data)));
const decrypted = this.cipher_in.output.getBytes();
if (!decrypted || decrypted.length < packet_size - padding) {
this.error("error decrypting packet using", this.cipher_in);
Expand Down Expand Up @@ -397,10 +397,10 @@ class XpraProtocol {
//decode raw packet string into objects:
let packet = null;
try {
if (proto_flags == 0x1) {
packet = rdecodelegacy(packet_data);
} else if (proto_flags == 0x10) {
packet = rdecodeplus(packet_data);
if (proto_flags == 0x10) {
packet = rdecode(packet_data);
} else if (proto_flags == 0x1) {
throw `rencode legacy mode is not supported, protocol flag: ${proto_flags}`;
} else {
throw `invalid packet encoder flags ${proto_flags}`;
}
Expand Down Expand Up @@ -449,7 +449,7 @@ class XpraProtocol {
let proto_flags = 0x10;
let bdata = null;
try {
bdata = rencodeplus(packet);
bdata = rencode(packet);
} catch (error) {
this.error("Error: failed to encode packet:", packet);
this.error(" with packet encoder", this.packet_encoder);
Expand Down Expand Up @@ -490,7 +490,7 @@ class XpraProtocol {
} else {
//copy string one character at a time..
for (let index = 0; index < actual_size; index++) {
packet_data[8 + index] = ord(bdata[index]);
packet_data[8 + index] = Utilities.ord(bdata[index]);
}
}
// put into buffer before send
Expand Down Expand Up @@ -616,7 +616,8 @@ if (
"lib/lz4.js",
"lib/brotli_decode.js",
"lib/forge.js",
"lib/rencode.js"
"lib/rencode.js",
"Utilities.js"
);
// make protocol instance
const protocol = new XpraProtocol();
Expand Down
33 changes: 15 additions & 18 deletions html5/js/Utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ const Utilities = {
return s.join("");
},

ord(c) {
return c.charCodeAt(0);
},

getSecureRandomString(length_) {
const crypto = window.crypto || window.mscrypto;
if (!crypto) {
Expand Down Expand Up @@ -419,36 +423,29 @@ const Utilities = {
}
},

StringToUint8(string_) {
return Uint8Array.from([...string_].map((x) => x.charCodeAt(0)));
StringToUint8(value) {
return new TextEncoder().encode(value);
},

Uint8ToString(u8a) {
const CHUNK_SZ = 0x80_00;
const c = [];
for (let index = 0; index < u8a.length; index += CHUNK_SZ) {
c.push(
String.fromCharCode.apply(null, u8a.subarray(index, index + CHUNK_SZ))
);
}
return c.join("");
Uint8ToString(value) {
return new TextDecoder().decode(value);
},

s(v) {
if (v===undefined) {
return "";
}
if (v === undefined) {
return "";
}
const type = typeof v;
if (type === "object" && v.constructor === Uint8Array) {
return Utilities.Uint8ToString(v);
}
return v.toString();
},

u : function(v){
if (v===undefined) {
return new Uint8Array(0);
}
u(v) {
if (v === undefined) {
return new Uint8Array(0);
}
const type = typeof v;
if (type === 'object' && v.constructor===Uint8Array) {
return v;
Expand Down
34 changes: 1 addition & 33 deletions html5/js/lib/rencode.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,6 @@ function rencode_none() {
return new Uint8Array([RENCODE.CHR_NONE]);
}

//turn this flag off to use "rencodeplus" when encoding
//this will send Uint8Array as 'binary'
//(decoding is always supported since not having it is free)
let rencode_legacy_mode = false;
function rencodelegacy(obj) {
rencode_legacy_mode = true;
return rencode(obj);
}
function rencode(obj) {
if (obj === null || obj === undefined) {
return rencode_none();
Expand All @@ -271,15 +263,6 @@ function rencode(obj) {
return rencode_dict(obj);
}
if(obj.constructor===Uint8Array) {
if (rencode_legacy_mode) {
//legacy rencode cannot handle bytearrays
const CHUNK_SZ = 0x8000;
const c = [];
for (let i=0; i < u8a.length; i+=CHUNK_SZ) {
c.push(String.fromCharCode.apply(null, u8a.subarray(i, i+CHUNK_SZ)));
}
return rencode_string(c.join(""));
}
return rencode_uint8(obj);
}
return rencode_list(obj);
Expand All @@ -293,10 +276,6 @@ function rencode(obj) {
default: throw "invalid object type in source: "+type;
}
}
function rencodeplus(obj) {
rencode_legacy_mode = false;
return rencode(obj);
}

function rdecode_string(dec) {
let len = 0;
Expand All @@ -318,9 +297,7 @@ function rdecode_string(dec) {
if (str_len==0) {
return "";
}
if (rencode_legacy_mode) {
return Uint8ToString(bytes);
}

return utf8ByteArrayToString(bytes)
}
function Uint8ToString(u8a){
Expand Down Expand Up @@ -517,15 +494,6 @@ function _rdecode(dec) {
return decode(dec);
}

function rdecodelegacy(buf) {
rencode_legacy_mode = true;
return rdecode(buf);
}
function rdecodeplus(buf) {
rencode_legacy_mode = false;
return rdecode(buf);
}

function rdecode(buf) {
const type = typeof buf;
if (type === "string") {
Expand Down

0 comments on commit 4b3cc25

Please sign in to comment.