This commit is contained in:
Comma Device
2024-04-28 03:48:55 +00:00
parent 9858a5f746
commit 1f32f2279c
11 changed files with 261 additions and 3 deletions

View File

@@ -0,0 +1,17 @@
#!/bin/bash
# Check for the correct number of arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 source destination"
exit 1
fi
# Set variables for source and destination
src="$1"
dest="$2"
# Read DongleId for decryption key
dongle_id=/data/params/d/DongleId
# Decrypt the file
cat "$src" | ccrypt -d -k "$dongle_id" > "$dest"

View File

@@ -0,0 +1,17 @@
#!/bin/bash
# Check for the correct number of arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 source destination"
exit 1
fi
# Set variables for source and destination
src="$1"
dest="$2"
# Read DongleId for encryption key
dongle_id=/data/params/d/DongleId
# Encrypt the file
cat "$src" | ccrypt -e -k "$dongle_id" > "$dest"

View File

@@ -0,0 +1,81 @@
#!/usr/bin/env python
from sys import argv
import os
import signal
# I've had problems with python's File objects at this low a level, so
# we're going to use integers to specify all files in this script.
stdin = 0
stdout = 1
stderr = 2
# Include this if passing the command and arguments to fish to
# prevent fish from applying any expansions.
#import re
#def fish_escape(args):
# def escape_one(arg):
# return "'" + re.sub(r"('|\\)", r'\\\1', arg) + "'"
# escaped_args = map(escape_one, args)
# return ' '.join(escaped_args)
if len(argv) < 2:
os.write(stderr,
b"""A tragically beautiful piece of hackery, made to fool programs like ls,
grep, rg, and fd into thinking they're actually connected to a terminal.
Its usage:
pty command [arg1 arg2 ...]
Examples:
pty ls --color -R | less -r
git log -p | pty rg <search terms> | less -r
""")
exit(255)
# We do not use forkpty here because it would block ^Cs from reaching the
# child process. And we don't need that.
ptm, pts = os.openpty()
pid = os.fork()
if pid == 0:
# The child runs this.
# To get the behaviour we want, we only need to replace the process's
# stdout with pts. Everything else should remain in place, so that things
# like `ps -eF | pty rg python | less -r` will still work as intended.
os.dup2(pts, stdout)
# This is not like a subprocess.call(). It replaces the entire child
# process with argv[1:], meaning execvp will not return! Web search
# "fork exec" for more.
os.execvp(argv[1], argv[1:])
# Use this if calling fish.
#os.execvp('fish', ['fish', '-c', fish_escape(argv[1:])])
# The parent runs this.
# If the parent doesn't close the slave end, the script won't be able to
# exit. The next read on ptm after the child process terminates would hang
# forever because pts would technically still be open.
os.close(pts)
# The whole process group gets SIGINT, including the child, so we don't need
# to react to it. We'll know when to leave, judging by what the child does.
signal.signal(signal.SIGINT, signal.SIG_IGN)
while True:
try:
chunk = os.read(ptm, 4096)
except OSError:
break
try:
os.write(stdout, chunk)
except BrokenPipeError:
# This happens when the parent is piping output to another process in a
# pipeline, like in `pty ls --color -R | less -r`, and the receiving
# process is terminated before the child has exited. If the receiving
# process is less, this can happen very easily. It happens every time
# the user decides to quit less before it has displayed all output. So,
# we need to stop the child process now.
os.kill(pid, signal.SIGTERM)
break
wait_pid, status = os.waitpid(pid, 0)
exit(status >> 8)

View File

@@ -0,0 +1,41 @@
#!/bin/bash
# Provision script for BrianBot
# These actions only occur on BrianBot's comma device.
# 1. Check the string in /data/params/d/DongleId
dongle_id=$(cat /data/params/d/DongleId)
if [[ ! $dongle_id == 90bb71a* ]]; then
echo "Invalid dongle ID."
exit 1
fi
echo "BrianBot dongle ID detected."
# 2. Check if ccrypt is installed, install if not
if ! command -v ccrypt >/dev/null 2>&1; then
echo "Installing ccrypt..."
sudo apt-get update
sudo apt-get install -y ccrypt
fi
# 3. Decrypt SSH keys if they have not been decrypted yet
if [ ! -f /data/openpilot/system/clearpilot/dev/id_rsa.pub ]; then
echo "Decrypting SSH keys..."
ccrypt -d -k "$dongle_id" /data/openpilot/system/clearpilot/dev/id_rsa.pub.ccrypt
ccrypt -d -k "$dongle_id" /data/openpilot/system/clearpilot/dev/id_rsa.ccrypt
ccrypt -d -k "$dongle_id" /data/openpilot/system/clearpilot/dev/reverse_ssh.ccrypt
fi
# 4. Ensure .ssh directory and keys exist
ssh_dir="/home/comma/.ssh"
if [[ ! -f "$ssh_dir/id_rsa" || ! -f "$ssh_dir/id_rsa.pub" ]]; then
echo "Setting up SSH directory and keys..."
mkdir -p "$ssh_dir"
cp /data/openpilot/system/clearpilot/dev/id_rsa /data/openpilot/system/clearpilot/dev/id_rsa.pub "$ssh_dir"
chmod 700 "$ssh_dir"
chmod 600 "$ssh_dir/id_rsa" "$ssh_dir/id_rsa.pub"
fi
echo "Script execution complete."
exit 0

View File

@@ -0,0 +1,37 @@
#!/bin/bash
# Usage:
# scrun instancename command
# - If instancename doesnt exist, starts a screen with instancename and executes the given command.
# - Does not run if the instance is already running.
# - Runs in the same context as a shell (loads environment variables).
# - Logs output into /var/log/scrun/instance/DATE.log, with rotation
# bash -l -c "$@"
# Based on https://gist.github.com/camperdave/980040
echo "defshell -bash" > ~/.screenrc
echo "startup_message off" >> ~/.screenrc
echo "vbell off" >> ~/.screenrc
echo "deflogin on" >> ~/.screenrc
echo "defscrollback 10000" >> ~/.screenrc
echo "defutf8 on" >> ~/.screenrc
echo "defflow off" >> ~/.screenrc
echo "msgwait 20" >> ~/.screenrc
echo "term screen-256color-bce" >> ~/.screenrc
#SCREENNAME=scrun_$1_
SCREENNAME=$1
screen -wipe 2>/dev/null >/dev/null
if ! screen -list | grep -q $SCREENNAME; then
cesc="${@:2}" # Everything but first one
# cesc="${cesc@Q}" # Escape it
screen -dmS $SCREENNAME python3 /data/openpilot/system/clearpilot/tools/faketty.py bash -l -c "$cesc"
echo screen -dmS $SCREENNAME python3 /data/openpilot/system/clearpilot/tools/faketty.py bash -l -c "$cesc"
# screen -dmS $1 "$@"
else
echo $SCREENNAME is already running
fi