Skip to content

Commit

Permalink
Refactor formHandler and main.ts to improve PSK type handling
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kastl <daniel@georepublic.de>
  • Loading branch information
dkastl committed Oct 3, 2024
1 parent d810261 commit 234404b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/formHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// formHandler.ts

/**
* Get the values from the form using FormData API.
*/
Expand All @@ -12,7 +14,6 @@ export function getFormValues() {

// Extract form values and ensure the correct types
const channelName = formData.get('channelName') as string;
const pskType = formData.get('pskType') as string;
const psk = formData.get('psk') as string;

// FormData returns strings, so we need to parse the necessary fields
Expand All @@ -25,13 +26,11 @@ export function getFormValues() {
const downlinkEnabled = formData.get('downlinkEnabled') === 'on';
const positionPrecision = Number(formData.get('positionPrecision'));
const isClientMuted = formData.get('isClientMuted') === 'on';

const configOkToMqtt = formData.get('configOkToMqtt') === 'on';
const ignoreMqtt = formData.get('ignoreMqtt') === 'on';

return {
channelName,
pskType,
psk,
region,
modemPreset,
Expand Down Expand Up @@ -64,4 +63,15 @@ export function populateForm(formValues: any) {
(form.elements.namedItem('hopLimit') as HTMLInputElement).value = String(formValues.hopLimit || 3);
(form.elements.namedItem('ignoreMqtt') as HTMLInputElement).checked = formValues.ignoreMqtt || false;
(form.elements.namedItem('configOkToMqtt') as HTMLInputElement).checked = formValues.configOkToMqtt || false;

// Set the pskType based on the byte length of the psk (psk.length / 2)
let pskType = 'none'; // Default to 'none'
if (formValues.psk && formValues.psk.length === 64) {
pskType = 'aes256'; // AES-256
} else if (formValues.psk && formValues.psk.length === 32) {
pskType = 'aes128'; // AES-128
}

// Update the pskType dropdown
(form.elements.namedItem('pskType') as HTMLSelectElement).value = pskType;
}
13 changes: 13 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// main.ts

import { getFormValues, populateForm } from './formHandler';
import { generatePSK } from './pskGenerator';
import { buildProtobuf } from './protobufBuilder';
Expand Down Expand Up @@ -45,8 +47,19 @@ function loadConfigurationFromHash(hash: string): void {

// Extract the channel settings from the Protobuf message
const channelSettings = channelSet.settings[0];

// Determine PSK type based on the length of the PSK
const pskLength = channelSettings.psk.length;
let pskType = 'none';
if (pskLength === 16) {
pskType = 'aes128';
} else if (pskLength === 32) {
pskType = 'aes256';
}

const formValues = {
channelName: channelSettings.name,
pskType: pskType, // Derived from PSK length
psk: new TextDecoder().decode(channelSettings.psk),
uplinkEnabled: channelSettings.uplinkEnabled,
downlinkEnabled: channelSettings.downlinkEnabled,
Expand Down
7 changes: 3 additions & 4 deletions src/protobufBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// protobufBuilder.ts

import { Protobuf } from "@meshtastic/js";

/**
* Build a Protobuf message for a channel.
* @param channelName - The name of the channel.
* @param psk - The pre-shared key for the channel.
* @param pskType - The type of pre-shared key.
* @param region - The region for the channel.
* @param modemPreset - The modem preset for the channel.
* @param hopLimit - The hop limit for the channel.
Expand All @@ -19,7 +20,6 @@ import { Protobuf } from "@meshtastic/js";
export function buildProtobuf({
channelName,
psk,
pskType,
region,
modemPreset,
hopLimit,
Expand All @@ -32,7 +32,6 @@ export function buildProtobuf({
}: {
channelName: string;
psk: string;
pskType: string;
region: number;
modemPreset: number;
hopLimit: number;
Expand All @@ -58,7 +57,7 @@ export function buildProtobuf({
});

const channelSettings = new Protobuf.Channel.ChannelSettings({
psk: pskType === 'none' ? new Uint8Array() : new TextEncoder().encode(psk),
psk: psk ? new TextEncoder().encode(psk) : new Uint8Array(), // Encode PSK
name: channelName,
uplinkEnabled: uplinkEnabled || false,
downlinkEnabled: downlinkEnabled || false,
Expand Down
2 changes: 2 additions & 0 deletions src/pskGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// pskGenerator.ts

/**
* Generate a PSK (Pre-Shared Key) for the given PSK type.
* @param pskType
Expand Down
2 changes: 2 additions & 0 deletions src/qrCodeGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// qrCodeGenerator.ts

import QRCode from 'qrcode';

/**
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// utils.ts

import { fromByteArray, toByteArray } from "base64-js";

/**
Expand Down

0 comments on commit 234404b

Please sign in to comment.