Browse Source

Decode messages from chrome native messaging from bytes, not from string

Johann Schmitz 7 months ago
parent
commit
6f64f46433
Signed by: Johann Schmitz <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
1 changed files with 12 additions and 9 deletions
  1. 12
    9
      esgp/__main__.py

+ 12
- 9
esgp/__main__.py View File

@@ -17,10 +17,10 @@
17 17
 import json
18 18
 import logging
19 19
 import struct
20
-from argparse import ArgumentParser
21
-import daiquiri
22 20
 import sys
21
+from argparse import ArgumentParser
23 22
 
23
+import daiquiri
24 24
 from PyQt5.QtWidgets import QApplication
25 25
 
26 26
 from esgp.config import Configuration
@@ -28,10 +28,9 @@ from esgp.ui import MainWindow
28 28
 
29 29
 
30 30
 def _read_chrome_native_message():
31
-    logger = logging.getLogger(__file__)
32
-    text_length_bytes = sys.stdin.read(4)
33
-    text_length = struct.unpack("i", bytes(text_length_bytes, 'utf-8'))[0]
34
-    text_decoded = sys.stdin.read(text_length)
31
+    text_length_bytes = sys.stdin.buffer.read(4)
32
+    text_length = struct.unpack("i", text_length_bytes)[0]
33
+    text_decoded = sys.stdin.buffer.read(text_length)
35 34
     return json.loads(text_decoded)
36 35
 
37 36
 
@@ -45,12 +44,16 @@ if __name__ == "__main__":
45 44
     args = parser.parse_args()
46 45
 
47 46
     daiquiri.setup(level=logging.DEBUG if args.verbose else logging.WARNING)
48
-    
47
+    logger = logging.getLogger(__file__)
48
+
49 49
     url = None
50 50
     
51 51
     if (args.arg or "").startswith('chrome-extension://'):
52
-        # started from chrome extension
53
-        url = _read_chrome_native_message()['url']
52
+        try:
53
+            # started from chrome extension
54
+            url = _read_chrome_native_message()['url']
55
+        except:
56
+            logger.exception("Failed to communicate with chrome native messaging")
54 57
 
55 58
     config = Configuration()
56 59
     config.read()