Side-project: I’ll just build my own dashcam.

While I am trying to repair the Koonlung K1S Dashcam, I am going to build my own dashcam. Sure, I can buy one and pay a ridiculous amount of money for a half-way decent one, but I paid € 230 for the K1S which I assumed did not turn out to be trash.

And so, I am going to build one myself and learn from it in the process. The project was first going to be a pwnagotchi, but I think building a dashcam is more useful. For now @_@

While I am waiting for the auxiliary battery, I will have to start with soldering the 40-pin header onto the board. And if you wonder why I didn;t buy one with a header soldered on it already, my answer is: “I had no idea that this was an option!!1 D: ”

A box of parts that will become a dashcam at some point. There is a raspberri Pi Zero board, a E-Inkt screen, a clock timer, a camera and some wiring
The box of stuff!

Loading

Trying to fix a Koonlung K1S, a probably long-term project. [plus downloads]

The Koonlung K1S is a very nice dashcam, with GPS, emergency button and HD recording and everything is made quite small. The HD video has a bit of a low bit rate, but it’s still not bad.

That said, it became a piece of rubbish. No matter what kind of SD card is inserted, the software locks up and the whole device becomes useless.

The front side of the Koonlung K1S Dashcam
The Koonlung K1S Dashcam

I have tried all the versions of the firmware [⇓1]. I have tried to decompile the firmware [⇓2][⇓3]. Nothing worked, whatever I tried.

So the time has come to take the thing apart. It cannot be any more broken than it is now, so maybe it is a blown cap or something? It is not, by the first look. There seems to be a corroded button cell soldered onto the motherboard.

The close-up of what looks like a small button-cell battery
Hmmm, is this a corroded button cell?
Probably a corroded button-cell
This seems to be at least a problem.

Alright. So what model is it? It’s not like you can just take it out and read what’s stamped onto it. Luckily I have a reference sheet in my archive that can be helpful. Hooray for being a data hoarder? >_>

The diameter is 4.8mm and the height is 2.0mm. The height is somewhat debatable as everything is small and kinda hard to reach. According to my datasheet, there are two candidates as a replacement battery:

  1. 4.8 x 2.15 – 1.55V – Maxell model: SR421SW – Seiko SB-A6 – Citizen 280-77 – GP 348
  2. 4.8 x 1.65 – 1.55V – Maxell model: SR416SW – Seiko SB-A5 – Citizen 280-75 – GP 337

As I want to get the exact battery, I grabbed a mini caliper and measured 1.65mm. There we go. It will be a SR416SW or anything compatible.

€5,46 has been paid for a set of 10 batteries because it was impossible to buy just one and now it is time to get the battery out.

More progress updates later!


[1] K1S-R158-50417-MPHK1S-R179-50825K1S-R180-KPHK1S-R180-MPHK1S-R215-KPH
[2] k1s.bin_hlil – Type: Mapped, Platform: thumb2, Architecture: thumb2
[3] k1s.bin_hlil-arm7 – Type: Mapped, Platform: armv7, Architecture: armv7

Loading

Function to change ownership of the www folder

Just another snippet of code that can be implemented somewhere:

set-perms.sh
#!/bin/bash

# Function to change ownership of the www folder
change_ownership() {
    sudo chown -R "$1":www-data /var/www
    echo "Ownership of the www folder has been set to $1:www-data."
}

# Loop until a valid username is provided
while true; do
    # Prompt the user to enter the desired username
    read -p "Enter the username for permissions: " username

    # Check if the username provided exists
    if id "$username" &>/dev/null; then
        change_ownership "$username"
        break  # Exit the loop if a valid username is provided
    else
        echo "Error: User $username does not exist."
    fi
done

echo "done!"

To make the script executable:

sudo chmod +x set-perms.sh

and run it with

sudo ./set-perms.sh

Loading

Setting up an automated mySQL / MariaDB backup schedule

Just digging deeper in automating thing, so I made two scripts:

backup_cron.sh
#!/bin/bash

# Define the path to the backup script
BACKUP_SCRIPT="/backup_mysql.sh"

# Define the cron job command to run the backup script once a week (every Sunday at midnight)
CRON_COMMAND="0 0 * * 0 $BACKUP_SCRIPT"

# Add the cron job to the crontab
(crontab -l 2>/dev/null; echo "$CRON_COMMAND") | crontab -

echo "Backup cron job set up successfully."

And the script that executes the backup every Sunday:

backup_mysql.sh
#!/bin/bash

# Define the directory where backup files will be stored
BACKUP_DIR="/var/backups/mysql"

# Ensure the backup directory exists
mkdir -p "$BACKUP_DIR"

# Define the filename for the backup file (include date in the filename)
BACKUP_FILE="$BACKUP_DIR/mysql_backup_$(date +%Y-%m-%d_%H-%M-%S).sql"

# Define the compressed filename
COMPRESSED_FILE="$BACKUP_FILE.gz"

# Define MySQL username and password (replace with your MySQL credentials)
DB_USER="USERNAME"
DB_PASS="PASSWORD"

# Dump all databases into a single SQL file
mysqldump -u "$DB_USER" -p"$DB_PASS" --all-databases > "$BACKUP_FILE"

# Add permissions to the backup file
chmod 600 "$BACKUP_FILE"

# Compress the backup file
gzip "$BACKUP_FILE"

echo "Database backup completed. Backup stored in: $COMPRESSED_FILE"

To make the script executable:

sudo chmod +x backup_cron.sh
sudo chmod +x backup_mysql.sh

and run it with

sudo ./backup_cron.sh

Loading

Revised script for LEMP install with PHP7.4 and PHP8.0

Currently successfully needs more testing on a Debian OS 11.5.0 installation. (I now remember why I didn’t want to touch this stuff any more. But oh well, I may as well completely overhaul this script again. The current one does work, but not well enough for me)

Step-by-step is functioning, will now wipe the server to start from scratch again. Also, the previous article will now be deleted.

Note:
If you are using this on a fresh Debian install, SUDO might not have been installed, nor has the user (so not the root) been add to SUDO. This is how you do that if you are logged in as user:

su -
apt update
apt install sudo
usermod -aG sudo <username>
exit
exit

You need to exit twice so you log out as root and as user. After that, you need to log back in as user so you can start using sudo from now on.


Let’s begin! You can name the script something like

setup.sh
#!/bin/bash

# Update system
sudo apt update
sudo apt upgrade -y

# Install necessary packages
sudo apt install gnupg nginx mariadb-server php7.4-fpm php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip ssh ufw nano fail2ban curl wget sudo openssl net-tools unzip -y

# Add repository key for PHP 8.0
sudo apt install software-properties-common -y
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

# Add repository for PHP 8.0
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt update

# Install PHP 8.0 and extensions
sudo apt install php8.0-fpm php8.0-mysql php8.0-curl php8.0-gd php8.0-mbstring php8.0-xml php8.0-zip -y

# Configure UFW to allow web traffic and SSH
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw allow 3306/tcp

# Enable UFW
sudo ufw enable

# Configure automatic security updates
echo 'APT::Periodic::Update-Package-Lists "1";' | sudo tee -a /etc/apt/apt.conf.d/10periodic
echo 'APT::Periodic::Download-Upgradeable-Packages "1";' | sudo tee -a /etc/apt/apt.conf.d/10periodic
echo 'APT::Periodic::AutocleanInterval "7";' | sudo tee -a /etc/apt/apt.conf.d/10periodic
echo 'APT::Periodic::Unattended-Upgrade "1";' | sudo tee -a /etc/apt/apt.conf.d/20auto-upgrades

# Disable root login via SSH
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd

# Generate SSH key pairs
ssh-keygen -t rsa -b 4096

# Configure Nginx
sudo systemctl start nginx
sudo systemctl enable nginx

# Configure PHP 7.4
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm

# Configure PHP 8.0
sudo systemctl start php8.0-fpm
sudo systemctl enable php8.0-fpm

# Configure MariaDB
sudo mysql_secure_installation

# Allow Nginx to use PHP
sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/7.4/fpm/php.ini
sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/8.0/fpm/php.ini

# Restart Nginx and PHP services
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
sudo systemctl restart php8.0-fpm

# Create a temporary file to store server and PHP info
tmpfile=$(mktemp)

# Get server info
uname -a > "$tmpfile"

# Get PHP version
php -v >> "$tmpfile"

# Append server and PHP info to index.html
cat "$tmpfile" | sudo tee /var/www/html/index.html >/dev/null

# Remove temporary file
rm "$tmpfile"

# Install adminer 
sudo apt install adminer -y
sudo ln -s /usr/share/adminer/adminer.php /var/www/html/adminer.php
sudo chown -R www-data:www-data /usr/share/adminer /var/www/html/adminer.php

# Set the permissions correct for www
sudo chmod g+w /var/www
sudo chown -R :www-data www
sudo chmod g+s www

change_ownership() {
    sudo chown -R "$1":www-data /var/www
    echo "Ownership of the www folder has been set to $1:www-data."
}

# Loop until a valid username is provided
while true; do
    # Prompt the user to enter the desired username
    read -p "Enter the username for permissions: " username

    # Check if the username provided exists
    if id "$username" &>/dev/null; then
        change_ownership "$username"
        break  # Exit the loop if a valid username is provided
    else
        echo "Error: User $username does not exist."
    fi
done
echo "LEMP setup completed successfully."
echo "Check if Adminer is working by opening a browser, and entering http://IP_address/adminer.php in the address bar"

To make the script executable:

sudo chmod +x setup.sh

and run it with

sudo ./setup.sh

When things like PHP fail to work (becuase y’know… PHP)
These are the files that have to look this way to make PHP happen.

sudo nano /etc/nginx/sites-available/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or WordPress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    
    #	# With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
#non-standard addition
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
    #	# With php-cgi (or other tcp sockets):
    #	fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #	deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}
sudo nano /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    ##
    # PHP Config
    ##

    # PHP-FPM upstream
    upstream php {
        server unix:/var/run/php/php8.0-fpm.sock; # Adjust the PHP version if necessary
    }


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
#
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}
}

And when you enter the following commands, you restart NGINX and PHP FPM and there should be no error message.

sudo systemctl restart nginx

# Adjust the PHP version if necessary. In my case, I want PHP 8.0

sudo systemctl restart php8.0-fpm

Good luck 🙂

Loading

Static IP address in Linux (Debian)

You can give your Debian installation a static IP address by following these steps:

1. Determine your network interface name: Use the following command to determine the name of your network interface:

ip addr

This will display information about all of your network interfaces. Look for the interface that you want to configure with a fixed IP address. The interface name will be listed on the left-hand side of the output (e.g. eth0, enp0s3, etc.)

2. Edit the network configuration file: Use a text editor to edit the /etc/network/interfaces file. For example, you can use the nano editor by running the following command:

sudo nano /etc/network/interfaces

3. Configure the network interface: Add the following lines to the file, replacing the interface name and IP address with your own values:

auto <interface-name>
iface <interface-name> inet static
address <ip-address>
netmask <subnet-mask>
gateway <default-gateway>
dns-nameservers <dns-server-ip-address>

For example, if your network interface name is “eth0” and you want to set the IP address to “192.168.0.10”, the subnet mask to “255.255.255.0”, the default gateway to “192.168.0.1”, and the DNS server to “8.8.8.8”, the configuration would look like this:

auto eth0
iface eth0 inet static
address 192.168.0.10
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 8.8.8.8

4. Save and close the file: Press Ctrl+O to save the file, and then press Ctrl+X to close the editor.

5. Restart the networking service: Use the following command to restart the networking service and apply the changes:

sudo systemctl restart networking

After you’ve completed these steps, your Debian installation should have a fixed IP address. You can verify the configuration by using the “ip addr” command again and looking for the interface that you configured.

More info here: https://wiki.debian.org/NetworkConfiguration
Thanks to https://fosstodon.org/@HankB for the tips and better choice of words!

Loading

Permanently switching off the GUI in Debian Linux

* Disable the Display Manager: The Display Manager is the graphical login screen that appears when you start up Debian. You can disable it by stopping the service and preventing it from starting at boot time. Use the following command to stop the service:

sudo systemctl stop display-manager.service

And then disable the service from starting at boot time:

sudo systemctl disable display-manager.service

* Remove the GUI packages: You can remove the GUI packages from your Debian installation by using the following command:

sudo apt-get remove task-gnome-desktop

This will remove the GNOME desktop environment and all its associated packages. If you’re using a different desktop environment, replace “gnome” with the name of your desktop environment.

* Reboot your system: Once you’ve disabled the Display Manager and removed the GUI packages, you’ll need to reboot your system for the changes to take effect. Use the following command to reboot your system:

sudo reboot

After the reboot, your Debian installation should boot into a command-line interface without any GUI.

Keep in mind that disabling the GUI permanently may make some tasks more difficult or time-consuming to perform. It’s recommended to proceed with caution and ensure that you have a backup plan in case you need to re-enable the GUI later.


The following has been suggested by https://fosstodon.org/@HankB :

Or

systemctl set-default multi-user.target

sudo systemctl set-default graphical.target

Loading

Script Dump: Live Epoch Time QR Code

This is a script that I needed to investigate a certain security method. Not needed any more, so here ya go!

<!DOCTYPE html>
<html>
<head>
    <title>Live Epoch Time QR Code</title>
    <style>
        .qr-code {
            display: inline-block;
            width: 128px;
            height: 128px;
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <h1>Live Epoch Time QR Code</h1>
    <div id="epochTime"></div>
    <div id="qrcode" class="qr-code"></div>

    <script>
        // Function to update the epoch time and QR code
        function updateTimeAndQRCode() {
            var epochTime = Math.floor(Date.now() / 1000); // Current epoch time in seconds
            document.getElementById('epochTime').innerHTML = 'Epoch Time: ' + epochTime;

            // Generate QR code using CSS
            var qrCodeElement = document.getElementById("qrcode");
            qrCodeElement.style.backgroundImage = "url('https://api.qrserver.com/v1/create-qr-code/?data=" + epochTime + "&size=128x128')";
        }

        // Update the time and QR code every second
        setInterval(updateTimeAndQRCode, 1000);

        // Initial call to display time and QR code
        updateTimeAndQRCode();
    </script>
</body>
</html>

 

Loading

Script Dump: Bogosort Musical Notes

I’m done playing with this so grab some notes in MP3 format and run the script. Do adapt it to your own likings and have fun!

<html>
<head>
    <title>Music Bogosort</title>
    <script>
        function bogosortAndPlay() {
            var notes = ["C", "D", "E", "F", "G", "A", "B"];
            var sortedNotes = [];
            while (sortedNotes.length < 6) {
                var randomIndex = Math.floor(Math.random() * notes.length);
                var randomNote = notes[randomIndex];
                if (!sortedNotes.includes(randomNote)) {
                    sortedNotes.push(randomNote);
                }
            }
            document.getElementById("unsorted_notes").innerHTML = "Unsorted notes: " + sortedNotes.join(", ");

            // Bogosort (this is extremely inefficient)
            while (!isSorted(sortedNotes)) {
                shuffleArray(sortedNotes);
            }

            document.getElementById("sorted_notes").innerHTML = "Sorted notes: " + sortedNotes.join(", ");
            playNotes(sortedNotes);
        }

        function isSorted(array) {
            for (var i = 0; i < array.length - 1; i++) {
                if (array[i] > array[i + 1]) {
                    return false;
                }
            }
            return true;
        }

        function shuffleArray(array) {
            for (var i = array.length - 1; i > 0; i--) {
                var j = Math.floor(Math.random() * (i + 1));
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }

        function playNotes(notes) {
            var index = 0;
            function playNextNote() {
                if (index < notes.length) {
                    var audio = new Audio("notes/" + notes[index] + ".mp3");
                    audio.play();
                    index++;
                    // Delay between notes (adjust as needed)
                    setTimeout(playNextNote, 1000); // Delay of 1 second between notes
                }
            }
            playNextNote();
        }
    </script>
</head>
<body>
    <h1>Music Bogosort</h1>
    <button onclick="bogosortAndPlay()">Sort and Play</button>
    <p id="unsorted_notes"></p>
    <p id="sorted_notes"></p>
</body>
</html>

 

Loading