Giter VIP home page Giter VIP logo

pyscrlink's Introduction

pyscrlink

Pyscrlink is a Scratch-link for Linux. Scratch-link is a software module which connects Scratch to Bluetooth devices such as micro:bit. However, as of October 2020, it only works on Windows and MacOS, and Linux operating systems can not connect Scratch and micro:bit.

Pyscrlink allows you to connect Scratch and bluetooth devices with the Linux OSes. It uses the Linux Bluetooth protocol stack Bluez and bluepy to handle Bluetooth Low Energy (BLE) connections. It has been reported that pyscrlink connects Scratch 3.0 with micro:bit, LEGO WeDo, LEGO Boost and toio.

Until version v0.2.5, pyscrlink supported Bluetooth Classic protocol using pybluez. Unfortunately, pybluez is not well maintained and caused technical troubles. Then Bluetooth Classic protocol support is dropped from pyscrlink. This means that LEGO Mindstorm EV3 can not be connected with pyscrlink. Bluetooth Classic support is the improvement opportunity of pyscrlink.

To use websockets, pyscrlink requires python version 3.6 or later. If your system has python older than version 3.6, install newer version. If your system has python 3 explicit command names python3 and pip3, use them in the steps below.

Pyscrlink was launched in 2019 as "bluepy-scratch-link". This was a small task dedicated to micro:bit and bluepy for BLE connection. After many contributions, it expanded coverage to pybluez with other devices for Bluetooth connectivity. It was misleading that the name "bluepy-scratch-link" indicates that it depends only on bluepy. As of October 2020, name of the project has been changed from "bluepy-scratch-link" to "pyscrlink" to avoid confusion.

Confirmed Environments

The instructions below was confirmed with following devices and distros. Trial with other distros and feed-backs will be appreciated.

Pyscrlink was confirmed with following devices, Linux distros and browsers.

Devices:

  • micro:bit

Linux distros:

  • Arch Linux

Browsers:

  • Firefox
  • Chromium

It was reported that pyscrlink (former bluepy-scratch-link) working with following devices and Linux distros.

Devices:

  • LEGO WeDo by @zhaowe, @KingBBQ
  • LEGO Boost and compatible devices by @laurentchar, @miguev, @jacquesdt, @n3storm
  • toio by @shimodash

Linux distros:

  • Raspbian by @chrisglencross
  • Ubuntu 16.04 @jacquesdt
  • Ubuntu Studio 20.04 @miguev
  • Debian 11 @n3storm

Installation

  1. Prepare Bluetooth/BLE controller.

    Confirm that your Linux PC has a Bluetooth controller with BLE support. Bluetooth 4.0 controller supports BLE. If your PC does not have it, need to plug USB Bluetooth 4.0 adapter.

    Note: BLED112 USB dongle with Bluegiga BGAPI is not supported.

  2. Install required packages.

    Ubuntu
    $ sudo apt install bluez libbluetooth-dev libnss3-tools libcap2-bin libglib2.0-dev
    Arch
    $ sudo pacman -S bluez bluez-utils nss libcap
  3. Install python modules.

    $ pip install pyscrlink
    Or if your system has python3 command,
    $ pip3 install pyscrlink
  4. Set bluepy-helper capability.

    $ bluepy_helper_cap
    Set capacbility 'cap_net_raw,cap_net_admin' to /usr/lib/python3.8/site-packages/bluepy-1.3.0-py3.8.egg/bluepy/bluepy-helper
    

    The command above requires super user privilege. It may request to input super user password.

  5. For micro:bit, install Scratch-link hex on your device.

    • Download and unzip the micro:bit Scratch Hex file.
    • Flash the micro:bit over USB with the Scratch Hex File, you will see the five character name of the micro:bit scroll across the screen such as 'zo9ev'.

Usage

  1. Start scratch-link python script.

    $ scratch_link

    If your device is toio, add "-s 1" option to the scratch_link command. It allows the toio Do Visual Programming to connect to toio automatically.

  2. Connect scratch to the target device such as micro:bit:

    • Open FireFox or Chrome. (Make sure to run as the same user for scratch-link python script.)
    • Access Scratch 3.0 and create your project.
    • Select the "Add Extension" button.
    • Select the extension for your device (e.g., micro:bit) and follow the prompts to connect.
    • Build your project with the extension blocks.

In Case You Fail to Connect

  1. If Scratch says "Make sure you have Scratch Link installed" but you are sure that scratch-link python script is running, check that Firefox or Chrome allows local server certificate.

    • Open Firefox or Chrome and access https://device-manager.scratch.mit.edu:20110/. You will see a security risk warning.
    • In Firefox: Click "Advanced" and click "Accept Risk and Continue".
    • In Chrome: type the special bypass keyword thisisunsafe.
    • Immediately, you will see "Failed to open a WebSocket connection". This is expected.
  2. If device scan fails, check systemd bluetooth service status.

    systemctl status bluetooth.service
    
    • If the service is not working, refer guide of your distro to set it up.
    • If the service is working, also check that /etc/bluetooth/main.conf sets AutoEnable=true.
  3. If device scan still fails, use -r option to retry device scan. The command line below does device scan twice. Each scan takes 10 seconds.

    $ scratch_link -r 2
    

    It would be good to use -s option together to reduce each scan duration. The command line below does 3 seconds device scan twice.

    $ scratch_link -r 2 -s 3
    
  4. If scratch_link.py says "failed to connect to BT device: [Errno 13] Permission denied", make sure to pair the bluetooth device to your PC before connecting to Scratch.

  5. To connect to multiple devices at the same time, make all the target devices ready for scan at the first device scan. This is important for toio. The toio allows a single project to connect to two toio devices.

    • When the second device was prepared after the first device was connected, device scan can not find the second device.
    • To scan and find the second device, disconnect connections for the first device beforehand.

Issus Reporting

Please file issues to GitHub issue tracker.

Releases

Release 0.2.8

  • Supported Microbit More v2

Release 0.2.7

  • Supported Snap Firefox and Chromium
  • Added -r option to retry BLE scan

Release 0.2.6

  • Removed Bluetooth Classic and LEGO Mindstorm EV3 support

Release 0.2.5

  • Fixed handling of multiple UUIDs for LEGO Boost

Release 0.2.4

  • Added -s option to specify BLE scan duration
  • Improved README.md

Release 0.2.3

  • Fixed eternal loop caused by hostname resolve failure

Release 0.2.2

  • Supported multiple device connections for toio
  • Improved session closure handling

Release 0.2.1

  • Added libglib to required package list in README.md
  • Improved setcap and getcap tool finding

Release 0.2.0

  • Latency issue fix for BLE devices' write characteristics

Release 0.1.0

  • Initial release

pyscrlink's People

Contributors

chrisglencross avatar errorjan avatar kawasaki avatar mdevel1 avatar n3storm avatar ngammarano avatar rcy avatar sza2 avatar typekpb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyscrlink's Issues

BUG Method always returns None and awaits a miracle? ;)

Hi,

Just heaving trying to have some fun with an ev3 and I realized that something is never gonna happen until it's changed...

jsonres = self.handle_request(jsonreq['method'], jsonreq['params'])

I cannot connect to EV3 as jsonres is always None due to handle_request always returning None.

def handle_request(self, method, params):

Log:
2023-01-20 11:04:04,499 start recv_request 2023-01-20 11:04:04,499 request: {"jsonrpc":"2.0","method":"discover","params":{"majorDeviceClass":8,"minorDeviceClass":1},"id":0} 2023-01-20 11:04:04,499 default handle_request: discover, {'majorDeviceClass': 8, 'minorDeviceClass': 1} 2023-01-20 11:04:04,499 Failure in session for web socket path: /scratch/bt

What should jsonres contain?
Thank you!

Try to connect to Lego Boost

Dear Kawasaki,

I found your bluepy-scratch-link project and got it up and running folowing your README. My goal is to control the Lego Boost robot from scratch 3.
I found one very small issue at line 198 (del(self))
close(self) did not work for me, I chaged it to self.close() and it works.

Then, connecting from Chrom+scratch works fine.

Next step was that in function matches(self,dev,filters) the conversion from string s to type UUID dit not work when trying to cast to int() first. so I changed "given_uuid = UUID(int(s))" to just be "given_uuid = UUID(s)". works.

(btw: I can create a pull request later, if required).
Can you try to confirm these bugs and check if you micro:bit works with my fixes as well?

Now the next big thing:
Scratch requests a service with UUID 00001623-1212-efde-1623-785feabcd123 nad if I sniff to the boost device using gatttool, I can see that it advertises exactly this ID. But so far I have no clue how to make use of this.

Last action I can observe is, that the line "service_class_uuid = dev.getValueText(0x3)" results in NONE and the next sanity check bailes out. So either dev is not filled properly, or the access by getValueText() is not robust to find the right string.

Can you help me adapting your script to Lego boost? I can supply you with scans and details, work on the code and test it.

best,
friso

Scratux support

Hi! I'm trying to make it work with Scratux but it throws the following error when I press the "Try again" button:

Start session for web socket path: /scratch/ble
Failure in session for web socket path: /scratch/ble
code = 1005 (no status code [internal]), no reason

It looks like Scratux is able to connect but then something goes wrong. Any idea about what might be wrong?

demo

connect to lego wedo2

device: LPF2 Smart Hub 2 I/O
scratch-link output:

BLE service not found: [{'services': ['00001523-1212-efde-1523-785feabcd123']}]. Check BLE device.

While I run it with -d, the output is

adtype 128b: None
adtype 16b: None

I add code in scratch_link.py as below:

    def matches(self, dev, filters):
        """
        Check if the found BLE device mathces the filters Scracth specifies.
        """
        logger.debug(f"in matches {dev} {filters}")
        dev_data = dev.getScanData()
        logger.debug(f"dev data: {dev_data}")

and run it with -d. I see the service value is in the Incomplete List of 128-bit Service Class UUIDs. So, I add codes as below

                    service_class_uuid = dev.getValueText(self.ADTYPE_COMP_128B)
                    logger.debug(f"adtype 128b: {service_class_uuid}")
                    if not service_class_uuid:
                        service_class_uuid = dev.getValueText(self.ADTYPE_INCOMP_128B)
                        logger.debug(f"adtype incomp 128b: {service_class_uuid}")
                        if not service_class_uuid:
                            service_class_uuid = dev.getValueText(self.ADTYPE_COMP_16B)
                            logger.debug(f"adtype 16b: {service_class_uuid}")
                            if not service_class_uuid:
                                continue

It works! The set-color command sended from scratch run in the wedo2 controller perfectly.

LEGO boost

I am trying to connect to LEGO boost from Ubuntu/Firefox/Scratch via a BLE dongle using pyscrlink. Installation (following the instructions at https://github.com/kawasaki/pyscrlink) works fine. But trying to connect from Scratch, as described in the instructions, fails, the LEGO boost extension saying "no devices found". Doing so, scratch_link -d outputs the following:

Print debug messages
2023-02-25 15:52:14,387 set scan_seconds: 10.0
2023-02-25 15:52:14,412 Certificate is ready in FireFox NSS DB: /home/ratschan/.mozilla/firefox/wx5xyc5d.default-release
2023-02-25 15:52:15,271 Certificate is ready in FireFox NSS DB: /home/ratschan/.mozilla/firefox/dj0c5tt0.default
2023-02-25 15:52:15,301 Certificate is ready for Chrome
2023-02-25 15:52:15,356 Started scratch-link
2023-02-25 15:54:57,652 Start session for web socket path: /scratch/ble
2023-02-25 15:54:57,652 start session handler
2023-02-25 15:54:57,652 start recv_request
2023-02-25 15:54:57,754 start recv_request
2023-02-25 15:54:57,754 request: {"jsonrpc":"2.0","method":"discover","params":{"filters":[{"services":["00001623-1212-efde-1623-785feabcd123"],"manufacturerData":{"919":{"dataPrefix":[0,64],"mask":[0,255]}}}],"optionalServices":[]},"id":0}
2023-02-25 15:54:57,754 handle request to BLE device
2023-02-25 15:54:57,754 discover
2023-02-25 15:54:57,754 {'filters': [{'services': ['00001623-1212-efde-1623-785feabcd123'], 'manufacturerData': {'919': {'dataPrefix': [0, 64], 'mask': [0, 255]}}}], 'optionalServices': []}
2023-02-25 15:54:57,763 start BLE scan: 10.0 seconds
2023-02-25 15:55:07,788 Failure in session for web socket path: /scratch/ble
2023-02-25 15:55:07,788 BTLEDisconnectError: Device disconnected

I also tried to follow the steps "at In Case You Fail to Connect" but could not really make sense of the instructions. Opening "https://device-manager.scratch.mit.edu:20110/" in the browser, I get "Failed to open a WebSocket connection: invalid Connection header: keep-alive. You cannot access a WebSocket server directly with a browser. You need a WebSocket client.". But the instructions say "This is expected". Now I do not understand what to conclude from this ...

Lego WeDo Support

Hello and big thanks also from my side to try to help to support Scratch Link on Linux!

I am working with kids a lot with Lego WeDo 2.0 - the "education" version of boost. Up till now I have been forced to use Windows for working with the devices (as Lego only support Windows and Mac, same for Link) so I was delighted to find your project.

I was setting everything up according to the docs (I hope I didn't miss anything) and thought I would give it a try.

When I do, I get this error:

$ ~/bluepy-scratch-link$ sudo btmgmt power on
hci0 Set Powered complete, settings: powered connectable discoverable bondable ssp br/edr le secure-conn 
$ ~/bluepy-scratch-link$ sudo btmgmt le on
hci0 Set Low Energy complete, settings: powered connectable discoverable bondable ssp br/edr le secure-conn 
$ ~/bluepy-scratch-link$ sudo python3 ./scratch_link.py
Started scratch-link
Start session for web socket path: /scratch/ble
Failed to execute management command 'scanend' (code: 13, error: Invalid Parameters)
Can not scan BLE devices. Check BLE controller.

I tried to connect directly via Gnome with the BT-LE device, which works fine. So I guess the hardware should support BLE.
I am using Ubuntu 20.04

Any help is appreciated!

thanks
Gregor

Unicode decode error

Hi kawasaki,
I'm having trouble trying to link my EV3 to Scratch in Firefox on my Ubuntu desktop. Everything works fine but running scan the terminal indicates the following error

antoine@aldebaran:~$ scratch_link
2022-07-09 15:52:49,216 Certificate is ready in FireFox NSS DB: /home/antoine/.mozilla/firefox/weuf2nuw.default
2022-07-09 15:52:49,222 Certificate is ready in FireFox NSS DB: /home/antoine/.mozilla/firefox/weuf2nuw.default
2022-07-09 15:52:49,323 Certificate is ready for Chrome
2022-07-09 15:52:49,345 Started scratch-link
2022-07-09 15:52:57,703 Start session for web socket path: /scratch/bt
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
self.run()
File "/home/antoine/.local/lib/python3.10/site-packages/pyscrlink/scratch_link.py", line 212, in run
self.discover()
File "/home/antoine/.local/lib/python3.10/site-packages/pyscrlink/scratch_link.py", line 189, in discover
discoverer.find_devices(lookup_names=True)
File "/home/antoine/.local/lib/python3.10/site-packages/bluetooth/bluez.py", line 434, in find_devices
_bt.hci_filter_all_events (flt)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 4: invalid start byte

Any idea ?
Best regards,
Antoine

pyscrlink for Debian 12 (Bookworm)

Is there a chance to get pyscrlink for Debian 12 (Bookworm).

A lot of school installations were updated to Debian 12 during the last year but now the Scratch 3 hardware link does not work anymore.

Bluetooh connection Raspberry Pi 4 with Lego WeDo 2.0

I have successfully connect a Lego WeDo 2.0 hub on my laptop (Ubuntu 20.04) using pyscrlink and tested it with Scratch 3.
I also tried to connect Raspberry Pi 4 with the same hub. The WeDo hub connected and after a few seconds disconnected.
I'm getting the following error message:

pi@raspberrypi:~ $ /home/pi/.local/bin/scratch_link
2021-01-21 19:37:08,798 FireFox NSS DB not found. Do not add certificate.
2021-01-21 19:37:08,851 Certificate is ready for Chrome
2021-01-21 19:37:09,186 Started scratch-link
2021-01-21 19:37:26,914 Start session for web socket path: /scratch/ble
2021-01-21 19:37:50,445 connected to the BLE peripheral: LPF2 Smart Hub 2 I/O
2021-01-21 19:37:54,885 Failure in session for web socket path: /scratch/ble
2021-01-21 19:37:54,885 Device disconnected

Any idea what's wrong?

Issue during device scanning

First of all, thanks for this project to allow using Scratch with Lego hardware under Linux. Unfortunately i have an issue connecting to a Boost device.


Ubuntu 21.10
pyscrlink pip3 package version 0.2.4
Firefox 94.0
Lego Boost

Steps I did:

  1. Install apt packages bluez libbluetooth-dev libnss3-tools libcap2-bin libglib2.0-dev
  2. Install pyscrlink with pip3 install pyscrlink
  3. Run bluepy_helper_cap
  4. Add device-manager.scratch.mit.edu as an alias for 127.0.0.1 in /etc/hosts
  5. Run scratch_link
  6. Visit https://device-manager.scratch.mit.edu:20110 with Firefox and add security exception
  7. Stop scratch_link
  8. Turn on Boost hardware by pressing green button (LED starts flashing white)
  9. Use bluetoothctl to pair with Boost hardware (Pairing successful reported and hardware LED turns from flashing white to permanent blue)
  10. Run scratch_link -d
  11. Go to scratch.mit.edu
  12. Start a new project
  13. Add Lego Boost block type
  14. Click on "Start searching"

Expected result

  • Connection to Boost working

Actual result

  • Scratch fails to connect
  • Firefox web developer tools report WSS network traffic to wss://device-manager.scratch.mit.edu:20110/scratch/ble with JSON payload response
    {"jsonrpc": "2.0", "error": {"message": "Can not scan BLE devices. Check BLE controller."}, "result": null, "id": 0}
    
  • scratch_link -d console output:
    2021-11-07 16:54:19,006 set scan_seconds: 10.0
    2021-11-07 16:54:19,014 Certificate is ready in FireFox NSS DB: /home/athlon/.mozilla/firefox/[...]
    2021-11-07 16:54:19,019 Certificate is ready in FireFox NSS DB: /home/athlon/.mozilla/firefox/[...]
    2021-11-07 16:54:19,062 Certificate is ready for Chrome
    2021-11-07 16:54:19,073 Started scratch-link
    2021-11-07 16:54:49,592 Start session for web socket path: /scratch/ble
    2021-11-07 16:54:49,592 start session handler
    2021-11-07 16:54:49,592 start recv_request
    2021-11-07 16:54:49,694 start recv_request
    2021-11-07 16:54:49,694 request: {"jsonrpc":"2.0","method":"discover","params":{"filters":[{"services":["00001623-1212-efde-1623-785feabcd123"],"manufacturerData":{"919":{"dataPrefix":[0,64],"mask":[0,255]}}}],"optionalServices":[]},"id":0}
    2021-11-07 16:54:49,694 handle request to BLE device
    2021-11-07 16:54:49,694 discover
    2021-11-07 16:54:49,694 {'filters': [{'services': ['00001623-1212-efde-1623-785feabcd123'], 'manufacturerData': {'919': {'dataPrefix': [0, 64], 'mask': [0, 255]}}}], 'optionalServices': []}
    2021-11-07 16:54:49,696 start BLE scan: 10.0 seconds
    2021-11-07 16:54:49,698 BLE iface #0: Failed to execute management command 'scanend' (code: 13, error: Invalid Parameters)
    2021-11-07 16:54:49,699 start BLE scan: 10.0 seconds
    2021-11-07 16:54:49,701 BLE iface #1: Failed to execute management command 'le on' (code: 17, error: Invalid Index)
    2021-11-07 16:54:49,702 start BLE scan: 10.0 seconds
    2021-11-07 16:54:49,704 BLE iface #2: Failed to execute management command 'le on' (code: 17, error: Invalid Index)
    2021-11-07 16:54:49,704 Can not scan BLE devices. Check BLE controller.
    2021-11-07 16:54:49,704 loop in BLE thread
    2021-11-07 16:54:49,704 {'jsonrpc': '2.0', 'error': {'message': 'Can not scan BLE devices. Check BLE controller.'}, 'result': None}
    2021-11-07 16:54:49,704 send out found devices
    2021-11-07 16:54:49,704 response: {"jsonrpc": "2.0", "error": {"message": "Can not scan BLE devices. Check BLE controller."}, "result": null, "id": 0}
    2021-11-07 16:54:49,704 end_request of BLESession
    2021-11-07 16:54:49,705 start to notify
    2021-11-07 16:54:49,705 in handle loop
    2021-11-07 16:54:49,705 start recv_request
    2021-11-07 16:54:49,705 start to notify
    2021-11-07 16:54:49,705 in handle loop
    [...]
    

Multiple users support

In issue #4, @miguev pointed out that some chore work is required to use bluepy-scratch-link by multiples users. Here I quote his comment.

It is also necessary for each user to have their own copy of bluepy-scratch-link and run ./gencert.sh in it, and keep ownership fo the bluepy-scratch-link directory.

I open this issue to think about it.

SyntaxError: File "./scratch_link.py", line 42

I've followed the steps in Readme, but I'm getting :

$ sudo python3 ./scratch_link.py
  File "./scratch_link.py", line 42
    logger.debug(f"request: {req}")
                                 ^
SyntaxError: invalid syntax

Any idea what is wrong? How can I help troubleshoot?

scratch_link procudes endless loop

When starting scratch_link on openSUSE, the program crashes and is immediately restarted with the message Restarting scratch-link..., what causes an endless loop.
The core of this behavior is that the address device-manager.scratch.mit.edu can't be resolved.
A workaround for this is to add following line to /etc/hosts
127.0.0.1 device-manager.scratch.mit.edu

Avoid sudo for scratcy_link.py

In issue #4, @miguev pointed out that sudo command is not handy for young children. I open this issue to think about this challenge.

For young children to use this without depending on adults, it is more convenient to make it work without using sudo with this one-time setup:

$ sudo apt-get install libcap2-bin
$ cd /usr/local/lib/python3.8/dist-packages/bluepy/
$ sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper
$ /opt/bluepy-scratch-link/scratch_link.py

I think this would be good to add to the README.md ─ without it, scratch_link.py fails to enable the low energy mode:

Failed to execute management command 'le on' (code: 20, error: Permission Denied)

Try to connect to LEGO HUB

Hi @kawasaki,
I have got a LEGO HUB from the LEGO Mindstorms robot inventor set. I am not sure if it should connect aswell using the scratch_link script. Is it compatible with LEGO HUB?
I managed to pair the LEGO HUB using bluetoothctl:

pi@raspberrypi:~ $ bluetoothctl
Agent registered
[bluetooth]# devices
Device 38:0B:3C:AD:54:5F LEGO Hub 38:0B:3C:AD:54:5F

Output from scratch_link:

pi@raspberrypi:~ $ /home/pi/.local/bin/scratch_link 
2021-01-03 15:34:12,556 FireFox NSS DB not found. Do not add certificate.
2021-01-03 15:34:12,616 Certificate is ready for Chrome
2021-01-03 15:34:12,659 Started scratch-link
2021-01-03 15:34:17,185 Start session for web socket path: /scratch/bt

But Scratch is not finding the device. ("No devices found").
Is the LEGO HUB equal / compatible to LEGO MINDSTORMS EV3?
Thanks for your support :-)
Best regards
Andy

bluetooth connection to lego boost

Hello,
I have a problem to establish a stable connection to lego boost module.
My set-up:
Lego Boost
+
NUC PC D34010WYK with dualboot ubuntu 20.4 and win 10.

Under Win10 I have installed scratch and also scratch link. There I could get connected stable to lego boost and from scratch drive all motors and also optical interface on lego boost.

Under Ubuntu I have installed scratux and also scratch link (as advised on https://pypi.org/project/pyscrlink/).
I can not say if scratux over scratch link is working at all, as my problem starts at very beginning.
When pairing in bluetooth, I get it on, but in a few seconds later is once again off and the lego boost auto-switches off.
Below the output from terminal while trying to connect. And also the output from hci.
Please help me, I can not find the solution by myself.

home@ubuntu:~$ bluetoothctl
Agent registered
[bluetooth]# pair 00:16:53:A5:14:EC
Attempting to pair with 00:16:53:A5:14:EC
[CHG] Device 00:16:53:A5:14:EC Connected: yes
[CHG] Device 00:16:53:A5:14:EC ServicesResolved: yes
[CHG] Device 00:16:53:A5:14:EC Paired: yes
Pairing successful
[CHG] Device 00:16:53:A5:14:EC ServicesResolved: no
[CHG] Device 00:16:53:A5:14:EC Paired: no
[CHG] Device 00:16:53:A5:14:EC Connected: no
[CHG] Controller D8:FC:93:70:00:B6 Discoverable: no
[bluetooth]#

home@ubuntu: hciconfig -a
hci0: Type: Primary Bus: USB
BD Address: D8:FC:93:70:00:B6 ACL MTU: 1021:5 SCO MTU: 96:6
UP RUNNING PSCAN
RX bytes:2592 acl:30 sco:0 events:163 errors:0
TX bytes:6262 acl:26 sco:0 commands:124 errors:0
Features: 0xff 0xfe 0x0f 0xfe 0xdb 0xff 0x7b 0x87
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH HOLD SNIFF
Link mode: SLAVE ACCEPT
Name: 'pepuntu'
Class: 0x1c0104
Service Classes: Rendering, Capturing, Object Transfer
Device Class: Computer, Desktop workstation
HCI Version: 4.0 (0x6) Revision: 0xe00
LMP Version: 4.0 (0x6) Subversion: 0xe00
Manufacturer: Intel Corp. (2)

Lego WeDo: Unable to reestablish connection

Hello,

I have been trying to use Lego WeDo 2.0 with ZorinOS.

After installation, then running scratch_link, I am able to make connection once using either Firefox and Opera (uses Chrominum engine on Linux?)

Connection is then attempted with Scratch via Browser

I have then tested the connection by running the motor, which works.

After a minute or two of inactivity the device stops responding, despite appearing to still being connected.

$ scratch_link
2023-03-31 21:47:25,754 Certificate is ready in FireFox NSS DB: /home/myuser/.mozilla/firefox/e26s94rt.default-release
2023-03-31 21:47:25,766 Certificate is ready in Chrome NSS DB: /home/myuser/.pki/nssdb
2023-03-31 21:47:25,795 Started scratch-link
2023-03-31 21:47:32,026 Start session for web socket path: /scratch/ble
2023-03-31 21:47:42,427 connected to the BLE peripheral: Epicman

The device still thinks it is connected, but no response, not recoverable without logout / login or restart. I don't but know how to safely stop scratch_link, so have resorted to CTRL + C, but then this causes another issue when attempting to restart scratch_link

BLE session disconnected with toio on playmat

Hi kawasaki-san
I found the issue with playing toio.
I put io on playmat to use absolutely position.
BLE session disconnected was occurred.

I attached log file scratch_link with -d option.
log.txt
disconnect may be occurred around line 83705.

Second device disconnected

@shimodash shared a pyscrlink issue through off-line communication today. I record the issue here.

The BLE device toio has its own Scratch enhancement for visual programming tool, and Scratch-link is required to connect the toio devices and the programming tool. The unique point of the Scratch for toio is that a single Scratch project can connect to two toio devices to allow users to be more creative. The Scratch-link on MacOS and Windows have no problem to support connections to the two toios. However, pyscrlink cannot support it. It is ok to connect the first single toio. When the user started to search the second toio for connection, the connection to the first toio gets lost, and pyscrlink shows the message "Disconnected".

Delay between sending command from Scratch and forwarding to the device

Hi,

I developing a firmware and a Scratch 3 extension to make control of Silabs' Thunderboard Sense 2 (BRD4166A) available from Scratch.

I mostly mimic the Micro:bit extension and implemented the firmware based on what I've found in the extension's source code (since I did not find the Micro:bit's firmware code anywhere, it seems only the binary is available).

Te issue is the following: when I click on a block in Scratch which sends a command to the device, there is a big delay between the click and the actual status change on the device.

As I checked, scratch_link.py receives the command quickly (in a millisecond range). The TB Sense 2 device's response also fast - I've tried this directly from Python.

So, it seems, scratch_link.py is the layer which adds the delay. it is in the ~second range (around 0.5s).

The other direction is OK, so reading buttons and tilt values does not suffer from this delay.

Could you please take a look on scratch_link.py and check what causes this delay and whether there is any way to shorten this delay?

Thx!

Edit:

Meanwhile I've figured out that the issue is caused by calling _get_characteristic() every time Scratch wants to write anything to the device:

        elif self.status == self.CONNECTED and method == 'write':
            logger.debug("handle write request")
            service_id = params['serviceId']
            chara_id = params['characteristicId']
            c = self._get_characteristic(chara_id)
            if not c or c.uuid != UUID(chara_id):
                logger.error(f"Failed to get characteristic {chara_id}")
                self.status = self.DONE
            else:

Wouldn't it be possible to cache the c value or directly write the characteristic instead of getting the handle from the device?

/sza2

CPU ressources

Hi,

I installed your 'pyscrlink' on Linux Mint. It works fine.

However when I checked in System Monitor, I found out it can use non-negligeable CPU ressource, it can go to 10% and more. And it can reach up to 50% (and stay there) when I stop a Scratch program using my Micro:bit.

I wonder if there is any way to lower the cpu usage? Changing the priority does not help.

PR_END_OF_FILE_ERROR - Secure Connection Failed

Hello,

the device search stops immediately so I can't find my wedo 2.0 device. The scratch-link is resetting session immediately after TLS Hello.

I am running firefox 111.0 on
$ cat /etc/issue
Ubuntu 22.04.2 LTS \n \l
with python 3.10

this might be same as #35
but not affected by #34

Installed as root:
1)
apt install bluez libbluetooth-dev libnss3-tools libcap2-bin python3-pip libglib2.0-dev python3-bluez python3-websockets

  1. pyscrlink-0.2.6 with bluepy, and older version later too (below):
    Requirement already satisfied: websockets in /usr/lib/python3/dist-packages (from pyscrlink==0.2.5) (9.1)
    Requirement already satisfied: pyOpenSSL in /usr/lib/python3/dist-packages (from pyscrlink==0.2.5) (21.0.0)
    Requirement already satisfied: pybluez in /usr/lib/python3/dist-packages (from pyscrlink==0.2.5) (0.23)
    Requirement already satisfied: bluepy in /usr/local/lib/python3.10/dist-packages (from pyscrlink==0.2.5) (1.3.0)
    Installing collected packages: pyscrlink
    Successfully installed pyscrlink-0.2.5

  2. set rights
    $ bluepy_helper_cap
    Set capacbility 'cap_net_raw,cap_net_admin' to /usr/local/lib/python3.10/dist-packages/bluepy/bluepy-helper
    and verified
    sudo getcap /usr/local/lib/python3.10/dist-packages/bluepy/bluepy-helper
    /usr/local/lib/python3.10/dist-packages/bluepy/bluepy-helper cap_net_admin,cap_net_raw=eip

  3. step 2 has been tested also as user, so both pyscrlink and bluepy were stored in home dir, ...same issue
    $ python3 ./.local/lib/python3.10/site-packages/pyscrlink/bluepy_helper_cap.py
    Set capacbility 'cap_net_raw,cap_net_admin' to /home/user/.local/lib/python3.10/site-packages/bluepy/bluepy-helper


The symptoms:

  • Scratch search dialog is immediately done with message "Make sure you have Scratch Link installed" and "Check Bluetooth Availability" (translation)
  • Firefox connection to https://device-manager.scratch.mit.edu:20110/ is closed with error:
    Secure Connection Failed
    An error occurred during a connection to device-manager.scratch.mit.edu:20110. PR_END_OF_FILE_ERROR

  • scratch-link is running without any new message as same user as firefox
    $ /usr/local/bin/scratch_link -d
    Print debug messages
    2023-03-12 20:34:08,165 set scan_seconds: 10.0
    2023-03-12 20:34:08,174 Certificate is ready in FireFox NSS DB: /home/user/.mozilla/firefox/xxxxxxx.default
    2023-03-12 20:34:08,181 Certificate is ready in FireFox NSS DB: /home/user/.mozilla/firefox/xxxxxx.default-release
    2023-03-12 20:34:08,188 Certificate is ready for Chrome
    2023-03-12 20:34:08,556 Started scratch-link
  • certs were regenerated
    2023-03-12 20:04:21,899 Generated certificate: /home/user/.local/share/pyscrlink/scratch-device-manager.cer
    2023-03-12 20:04:21,899 Generated key: /home/user/.local/share/pyscrlink/scratch-device-manager.key
    2023-03-12 20:04:21,908 Old certificate is in /home/user/.mozilla/firefox/xxxxxxx.default.
    2023-03-12 20:04:21,908 Add the new certificate to /home/user/.mozilla/firefox/xxxxxxx.default
    ...
  • I am not using firefox from snapstore, so there is no issue with paths (#34)
  • Connection is closed same way from edge (where should be no certificate), I believe it is closed before a security risk warning
  • it is translated to local host
    ;; ANSWER SECTION:
    device-manager.scratch.mit.edu. 900 IN A 127.0.0.1
  • scratch-link is listening
    $ netstat -tna | grep 20110
    tcp 0 0 127.0.0.1:20110 0.0.0.0:* LISTEN
  • wireshark of scratch is same like wireshark of direct connection to device-manager from firefox:
    when "start searching" is pressed, few packets to establish connection, TLSv1 Client Hello, answered by RST
  • behavior is same regardless "LPF2 Smart Hub 2" device is paired or not

Thanks for your ideas

Support for Intelino Smart Train

Hello,

I have a hard time connecting to Intelino Smart Train.

martin@bono:~$ scratch_link 
2021-12-24 21:06:48,682 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/qk78wey4.Default User
2021-12-24 21:06:48,689 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/qk78wey4.Default User
2021-12-24 21:06:48,703 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/qk78wey4.Default User/chrome_debugger_profile
2021-12-24 21:06:48,712 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/qb752b1e.ff75
2021-12-24 21:06:48,719 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/u16lwrru.default
2021-12-24 21:06:48,727 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/fq9mqhp8.Martin
2021-12-24 21:06:48,974 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/no8dwo1j.Default User
2021-12-24 21:06:48,983 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/jmegri7g.p1
2021-12-24 21:06:48,991 Certificate is ready in FireFox NSS DB: /home/martin/.mozilla/firefox/ya14x5g6.temp
2021-12-24 21:06:48,999 Certificate is ready for Chrome
2021-12-24 21:06:49,016 Started scratch-link
2021-12-24 21:06:56,467 Start session for web socket path: /scratch/ble
2021-12-24 21:07:06,878 Can not scan BLE devices. Check BLE controller.
martin@bono:~$ bluetoothctl 
Agent registered
[CHG] Controller 40:74:E0:9F:B6:B2 Pairable: yes
[bluetooth]# devices
Device 00:A0:50:B6:45:0E intelino J-1
Device 50:81:D8:01:8A:13 Sony PLAYSTATION(R)3 Controller
Device 98:D3:31:FB:14:F7 HC-06
Device 30:21:03:00:04:85 Creative T100
Device 20:1B:88:14:AD:70 Mi True Wireless EBs Basic 2
Device 34:88:5D:3F:5D:38 Bluetooth Mouse M557
Device 00:1B:FB:C3:8C:64 Sony PLAYSTATION(R)3 Controller
Device 20:01:08:00:A9:32 Escape 220
Device 74:51:BA:ED:0C:A5 Mi Phone
[bluetooth]# connect 00:A0:50:B6:45:0E
Attempting to connect to 00:A0:50:B6:45:0E
[CHG] Device 00:A0:50:B6:45:0E Connected: yes
Connection successful
[NEW] Primary Service (Handle 0x8746)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service000a
	00001801-0000-1000-8000-00805f9b34fb
	Generic Attribute Profile
[NEW] Characteristic (Handle 0x75eb)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service000a/char000b
	00002a05-0000-1000-8000-00805f9b34fb
	Service Changed
[NEW] Descriptor (Handle 0xf360)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service000a/char000b/desc000d
	00002902-0000-1000-8000-00805f9b34fb
	Client Characteristic Configuration
[NEW] Primary Service (Handle 0x80f0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service000e
	0000180a-0000-1000-8000-00805f9b34fb
	Device Information
[NEW] Characteristic (Handle 0x75eb)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service000e/char000f
	00002a24-0000-1000-8000-00805f9b34fb
	Model Number String
[NEW] Characteristic (Handle 0x75eb)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service000e/char0011
	00002a25-0000-1000-8000-00805f9b34fb
	Serial Number String
[NEW] Characteristic (Handle 0x75eb)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service000e/char0013
	00002a26-0000-1000-8000-00805f9b34fb
	Firmware Revision String
[NEW] Primary Service (Handle 0x80f0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service0015
	0000180f-0000-1000-8000-00805f9b34fb
	Battery Service
[NEW] Characteristic (Handle 0x75eb)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service0015/char0016
	00002a19-0000-1000-8000-00805f9b34fb
	Battery Level
[NEW] Descriptor (Handle 0x7360)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service0015/char0016/desc0018
	00002904-0000-1000-8000-00805f9b34fb
	Characteristic Format
[NEW] Descriptor (Handle 0x7360)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service0015/char0016/desc0019
	00002902-0000-1000-8000-00805f9b34fb
	Client Characteristic Configuration
[NEW] Primary Service (Handle 0x80f0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service001a
	00060000-f8ce-11e4-abf4-0002a5d5c51b
	Vendor specific
[NEW] Characteristic (Handle 0x75eb)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service001a/char001b
	00060001-f8ce-11e4-abf4-0002a5d5c51b
	Vendor specific
[NEW] Descriptor (Handle 0x76a0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service001a/char001b/desc001d
	00002902-0000-1000-8000-00805f9b34fb
	Client Characteristic Configuration
[NEW] Descriptor (Handle 0x76a0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service001a/char001b/desc001e
	00002901-0000-1000-8000-00805f9b34fb
	Characteristic User Description
[NEW] Primary Service (Handle 0x80f0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service001f
	43dfd9e9-17e5-4860-803d-9df8999b0d7a
	Vendor specific
[NEW] Characteristic (Handle 0x75eb)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service001f/char0020
	40c540d0-344c-4d0d-a1da-9cc260b82d43
	Vendor specific
[NEW] Descriptor (Handle 0x77a0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service001f/char0020/desc0022
	00002901-0000-1000-8000-00805f9b34fb
	Characteristic User Description
[NEW] Primary Service (Handle 0x80f0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service0023
	4dad4922-5c86-4ba7-a2e1-0f240537bd08
	Vendor specific
[NEW] Characteristic (Handle 0x75eb)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service0023/char0024
	a4b80869-a84c-4160-a3e0-72fa58ff480e
	Vendor specific
[NEW] Descriptor (Handle 0x7aa0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service0023/char0024/desc0026
	00002902-0000-1000-8000-00805f9b34fb
	Client Characteristic Configuration
[NEW] Descriptor (Handle 0x7aa0)
	/org/bluez/hci0/dev_00_A0_50_B6_45_0E/service0023/char0024/desc0027
	00002901-0000-1000-8000-00805f9b34fb
	Characteristic User Description
[CHG] Device 00:A0:50:B6:45:0E ServicesResolved: yes

Pairing doesn't work. Probably the train is not pairable.

[bluetooth]# pair 00:A0:50:B6:45:0E
Attempting to pair with 00:A0:50:B6:45:0E
[CHG] Device 00:A0:50:B6:45:0E Connected: yes
Failed to pair: org.bluez.Error.AuthenticationFailed
[CHG] Device 00:A0:50:B6:45:0E Connected: no

Using https://scratch.intelino.com/

I can connect to the device

failed to connect with latest master

2021-11-13 16:40:55,134 Start session for web socket path: /scratch/ble
2021-11-13 16:40:55,135 start session handler
2021-11-13 16:40:55,135 start recv_request
2021-11-13 16:40:55,237 start recv_request
2021-11-13 16:40:55,239 request: {"jsonrpc":"2.0","method":"discover","params":{"filters":[{"services":["00001623-1212-efde-1623-785feabcd123"]}],"optionalServices":[]},"id":0}
2021-11-13 16:40:55,239 handle request to BLE device
2021-11-13 16:40:55,239 discover
2021-11-13 16:40:55,239 {'filters': [{'services': ['00001623-1212-efde-1623-785feabcd123']}], 'optionalServices': []}
2021-11-13 16:40:55,246 start BLE scan: 5.0 seconds
2021-11-13 16:41:00,510 in matches 79:09:e5:82:11:7f [{'services': ['00001623-1212-efde-1623-785feabcd123']}]
2021-11-13 16:41:00,511 service to check: 00001623-1212-efde-1623-785feabcd123
2021-11-13 16:41:00,511 given: 00001623-1212-efde-1623-785feabcd123
2021-11-13 16:41:00,511 in matches 62:68:79:c3:db:3e [{'services': ['00001623-1212-efde-1623-785feabcd123']}]
2021-11-13 16:41:00,511 service to check: 00001623-1212-efde-1623-785feabcd123
2021-11-13 16:41:00,511 given: 00001623-1212-efde-1623-785feabcd123
2021-11-13 16:41:00,511 in matches b8:7c:6f:43:2d:af [{'services': ['00001623-1212-efde-1623-785feabcd123']}]
2021-11-13 16:41:00,512 service to check: 00001623-1212-efde-1623-785feabcd123
2021-11-13 16:41:00,512 given: 00001623-1212-efde-1623-785feabcd123
2021-11-13 16:41:00,512 adtype complete 16b
2021-11-13 16:41:00,513 Failure in session for web socket path: /scratch/ble
2021-11-13 16:41:00,513 Error: Non-hexadecimal digit found

Lego WeDo2.0 not connecting anymore

Hello,
thanks for you project. My kid loves playing with WeDo2.0 and your project keeps him being able to use it on linux. He used it on an older Linux Mint (originally installed around 2018-2019 i guess) until 1-2 weeks ago. I had the "bright" ideea to upgrade his system to latest Mint version, and now scratch_link doesn't seem to o anything.

When accessing https://scratch.mit.edu/ and trying to connect to WeDo the connection popup doesn't seem to spend any time searching for the actual device.
In the Firefox console I'm seeing this message:

Firefox can’t establish a connection to the server at ws://127.0.0.1:20111/scratch/ble. 
Firefox can’t establish a connection to the server at wss://device-manager.scratch.mit.edu:20110/scratch/ble.

Manually connecting to https://device-manager.scratch.mit.edu:20110/scratch/ble returns

Secure Connection Failed

An error occurred during a connection to device-manager.scratch.mit.edu:20110. PR_END_OF_FILE_ERROR

Error code: PR_END_OF_FILE_ERROR

As far a I remember PR_END_OF_FILE_ERROR is returned when the https server closes the connection without communicating anything, and scratch_link doesn't seem to log anything regarding to the webserver activity, so i guess that something is wrong on the ws side.

Screenshot from 2024-02-16 07-52-19

Screenshot from 2024-02-16 08-10-50

Can not scan BLE devices. Check BLE controller - Ubuntu 22.04

I'm trying connecting wedo 2.0 but have this error:

2024-04-14 14:22:35,923 Started scratch-link
2024-04-14 14:23:20,919 Start session for web socket path: /scratch/ble
2024-04-14 14:23:21,039 Can not scan BLE devices. Check BLE controller.

I started bluepy_helper_cap, I tried "hciconfig hci0 down" and "hciconfig hci0 up", I tried connecting the device with the OS BT pairing first (it does connect), I tried the dev version, but nothing

Can anybody suggest a solution ?

I got this when running with -d
Failed to execute management command 'le on' (code: 20, error: Permission Denied)

but can't run scratch_link with sudo. How do I give correct permissions ?

ERROR:ssl_client_socket_impl.cc handshake failed on Ubuntu 22.04

Hello,

I'm getting the following error when i press the "Start Searching" button in Scratch 3 with pyscrlink running on Ubuntu 22.04:
[10348:0306/122147.012576:ERROR:ssl_client_socket_impl.cc(981)] handshake failed; returned -1, SSL error code 1, net_error -101

In the terminal window running pyscrlink i get no error messages:
$ /usr/local/bin/scratch_link -d
Print debug messages
2023-03-06 12:21:07,006 set scan_seconds: 10.0
2023-03-06 12:21:07,025 Certificate is ready in FireFox NSS DB: /home/administrator/.mozilla/firefox/qj7kn4vq.default-release
2023-03-06 12:21:07,036 Certificate is ready for Chrome
2023-03-06 12:21:07,078 Started scratch-link

I have the following packages installed on my system:
sudo apt install bluez libbluetooth-dev libnss3-tools libcap2-bin python3-pip libglib2.0-dev python3-bluez python3-websockets
sudo pip3 install pyscrlink

and i have run the command:
/usr/local/bin/bluepy_helper_cap

bluez-tools required for btmgmt

In order to use btmgmt (on Arch at least) the bluez-tools package is required. It would be helpful to add this to the readme.

The certificate instructions could still be better

Hello! First of all, thanks for this project. 😄

I had a little trouble getting it to work at first and I couldn't figure out why the browser was part of the process. It eventually dawned on me that most people on Linux will be using Scratch 3 through the browser. I had downloaded the Electron Linux binary zip and extracted the Scratch 3 resources directory from the Windows installer. Try it if you haven't, it's dead easy and it works really well.

The quick and dirty way to make the desktop app work is to pass the --ignore-certificate-errors option to the electron binary. That's quite nasty though and I thought I could do better.

If we're shooting for a truly trusted certificate here then we need a longer expiry time. Let's say 10 years, so that's -days 3650.

Rather than use names like localhost and scratch-device-manager, we need the name to match what's actually being requested so use device-manager.scratch.mit.edu everywhere.

The -subj option can be dropped if you set prompt = no in the config and then you only need to specify the CN once.

Was the keyUsage=digitalSignature\nextendedKeyUsage=serverAuth part added for any particular reason? I found that OpenSSL fails to trust the certificate with this in place. I didn't test whether it impacts NSS (used by Firefox, Chrome, Electron) in the end but it works fine without.

The printf bit looks a bit clunky. We can use a here string instead like this:

-config /dev/stdin <<< $'[dn]\nCN=device-manager.scratch.mit.edu\n[req]\nprompt = no\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:device-manager.scratch.mit.edu'

I have no idea what the openssl pkcs12 part is supposed to be for. scratch-device-manager.pfx doesn't appear to be used for anything and I got this working without it.

Similarly, the grep | tr part is bizarre. cat will do the same job as grep -h ^ and OpenSSL doesn't write Windows-style line endings so the tr isn't needed. This could simply be:

cat scratch-device-manager.cer scratch-device-manager.key > scratch-device-manager.pem

Alternatively, why not just skip that step entirely and have Python read the certificate and key from separate files like this?

cert = pathlib.Path(__file__).with_name("scratch-device-manager.key")
key = pathlib.Path(__file__).with_name("scratch-device-manager.cer")
ssl_context.load_cert_chain(cert, key)

So to sum up, the one and only command you need for generating the key and certificate is:

openssl req -x509 -out scratch-device-manager.cer -keyout scratch-device-manager.key -newkey rsa:2048 -nodes -sha256 -days 3650 -extensions EXT -config /dev/stdin <<< $'[dn]\nCN=device-manager.scratch.mit.edu\n[req]\nprompt = no\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:device-manager.scratch.mit.edu'

Getting regular browsers to trust scratch-device-manager.cer is easy enough. Firefox, Chrome, and friends allow you to import trusted certificates through the UI but what about the desktop app? By default, Electron uses the same user-wide trust store as Chrome in ~/.pki/nssdb so if you import it into Chrome then Electron will see it too. What if you don't have Chrome? You can use the certutil tool from NSS to do it from the command line.

certutil -A -d sql:~/.pki/nssdb -n "Scratch Device Manager" -t CT,, -i scratch-device-manager.crt

So hopefully we can stop telling users to do thisisunsafe and the like now. There is a way to get Firefox, Chrome, and Electron all to use the same system-wide trust store. This could be useful for school environments but it's a little outside the scope of these instructions. Ask me if you're interested.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.