Skip to content

Commit

Permalink
Spend a day making TypeScript happy
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Oct 25, 2024
1 parent 50d916c commit 37287b6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 73 deletions.
16 changes: 12 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"gateway-addon": "^1.2.0-alpha.1",
"glob-to-regexp": "^0.4.1",
"http-proxy": "^1.18.1",
"ip": "^2.0.1",
"ip-regex": "^4.3.0",
"jsonwebtoken": "^8.5.1",
"minipass": "^3.3.5",
Expand Down Expand Up @@ -100,6 +101,7 @@
"@types/find": "^0.2.1",
"@types/glob-to-regexp": "^0.4.0",
"@types/http-proxy": "^1.17.5",
"@types/ip": "^1.1.3",
"@types/jest": "^27.0.1",
"@types/jsdom": "^16.2.6",
"@types/jsonfile": "^6.0.0",
Expand Down
94 changes: 43 additions & 51 deletions src/platforms/linux-ubuntu-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import ip from 'ip';
import { Netmask } from 'netmask';
import BasePlatform from './base';
import NetworkManager from './utilities/network-manager';
import ipRegex from 'ip-regex';
import NetworkManager, { ConnectionSettings } from './utilities/network-manager';
import { LanMode, NetworkAddresses, WirelessNetwork } from './types';
import { Netmask } from 'netmask';

export class LinuxUbuntuCorePlatform extends BasePlatform {
/**
Expand Down Expand Up @@ -71,28 +71,27 @@ export class LinuxUbuntuCorePlatform extends BasePlatform {
.then((connection) => {
return NetworkManager.getConnectionSettings(connection);
})
.then((settings: any) => {
if (settings.ipv4.method == 'auto') {
.then((settings: ConnectionSettings) => {
if (settings && settings.ipv4 && settings.ipv4.method == 'auto') {
result.mode = 'dhcp';
} else if (settings.ipv4.method == 'manual') {
} else if (settings && settings.ipv4 && settings.ipv4.method == 'manual') {
result.mode = 'static';
}
if (
settings.ipv4['address-data'][0] &&
settings.ipv4['address-data'][0].hasOwnProperty('address')
) {
result.options.ipaddr = settings.ipv4['address-data'][0].address;
if (settings.ipv4 && settings.ipv4['address-data'] && settings.ipv4['address-data'][0]) {
if (settings.ipv4['address-data'][0].hasOwnProperty('address')) {
result.options.ipaddr = settings.ipv4['address-data'][0].address;
}
if (result.options.ipaddr && settings.ipv4['address-data'][0].hasOwnProperty('prefix')) {
// Convert cidr style prefix to dot-decimal netmask
const ip = result.options.ipaddr;
const cidr = settings.ipv4['address-data'][0].prefix;
const block = new Netmask(`${ip}/${cidr}`);
result.options.netmask = block.mask;
}
}
if (settings.ipv4.hasOwnProperty('gateway')) {
if (settings.ipv4 && settings.ipv4.hasOwnProperty('gateway')) {
result.options.gateway = settings.ipv4.gateway;
}
if (result.options.ipaddr && settings.ipv4['address-data'][0].hasOwnProperty('prefix')) {
// Convert cidr style prefix to dot-decimal netmask
const ip = result.options.ipaddr;
const cidr = settings.ipv4['address-data'][0].prefix;
const block = new Netmask(`${ip}/${cidr}`);
result.options.netmask = block.mask;
}
return result;
})
.catch((error) => {
Expand All @@ -108,7 +107,7 @@ export class LinuxUbuntuCorePlatform extends BasePlatform {
* @param {<Record<string, any>} options Mode-specific options.
* @returns {Promise<boolean>} Promise that resolves true if successful and false if not.
*/
async setLanModeAsync(mode: string, options: Record<string, any>): Promise<boolean> {
async setLanModeAsync(mode: string, options: Record<string, unknown>): Promise<boolean> {
let lanDevice: string;
let lanConnection: string;
return NetworkManager.getEthernetDevices()
Expand All @@ -122,28 +121,29 @@ export class LinuxUbuntuCorePlatform extends BasePlatform {
return NetworkManager.getConnectionSettings(lanConnection);
})
.then((oldSettings) => {
const settings: Record<string, any> = {};
// Carry over some values from the old settings
settings.connection = {
id: oldSettings.connection.id,
uuid: oldSettings.connection.uuid,
type: oldSettings.connection.type,
const settings: ConnectionSettings = {
connection: {
id: oldSettings.connection.id,
uuid: oldSettings.connection.uuid,
type: oldSettings.connection.type,
},
};

if (mode == 'dhcp') {
// Set dynamic IP
settings.ipv4 = {
method: 'auto',
};
} else if (mode == 'static') {
const regex = ipRegex({ exact: true });
if (
!(
options.hasOwnProperty('ipaddr') &&
regex.test(<string>options.ipaddr) &&
ip.isV4Format(<string>options.ipaddr) &&
options.hasOwnProperty('gateway') &&
regex.test(<string>options.gateway) &&
ip.isV4Format(<string>options.gateway) &&
options.hasOwnProperty('netmask') &&
regex.test(<string>options.netmask)
ip.isV4Format(<string>options.netmask)
)
) {
console.log(
Expand All @@ -153,32 +153,24 @@ export class LinuxUbuntuCorePlatform extends BasePlatform {
}
// Set static IP address
// Convert dot-decimal netmask to cidr style prefix for storage
const netmask = new Netmask(options.ipaddr, options.netmask);
const netmask = new Netmask(options.ipaddr as string, options.netmask as string);
const prefix = netmask.bitmask;
// Convert dot-decimal IP and gateway to little endian integers for storage
const ipaddrInt = options.ipaddr
.split('.')
.reverse()
.reduce(function (int: any, value: any) {
return int * 256 + +value;
});
const gatewayInt = options.gateway
.split('.')
.reverse()
.reduce(function (int: any, value: any) {
return int * 256 + +value;
});
const ipaddrReversed = (options.ipaddr as string).split('.').reverse().join('.');
const ipaddrInt = ip.toLong(ipaddrReversed);
const gatewayReversed = (options.gateway as string).split('.').reverse().join('.');
const gatewayInt = ip.toLong(gatewayReversed);
settings.ipv4 = {
method: 'manual',
addresses: [[ipaddrInt, prefix, gatewayInt]],
// The NetworkManager docs say that the addresses property is deprecated,
// but using address-data and gateway doesn't seem to work on Ubuntu yet.
/*
'address-data': [{
'address': options.ipaddr,
'prefix': prefix
}],
'gateway': options.gateway
'address-data': [{
'address': options.ipaddr,
'prefix': prefix
}],
'gateway': options.gateway
*/
};
} else {
Expand Down Expand Up @@ -240,7 +232,7 @@ export class LinuxUbuntuCorePlatform extends BasePlatform {
async setWirelessModeAsync(
enabled: boolean,
mode = 'ap',
options: Record<string, any> = {}
options: Record<string, unknown> = {}
): Promise<boolean> {
const valid = [
// 'ap', //TODO: Implement ap mode
Expand Down Expand Up @@ -272,7 +264,7 @@ export class LinuxUbuntuCorePlatform extends BasePlatform {
console.log('Could not connect to wireless network because no SSID provided');
return false;
}
const accessPoint = await NetworkManager.getAccessPointbySsid(options.ssid);
const accessPoint = await NetworkManager.getAccessPointbySsid(options.ssid as string);
if (accessPoint == null) {
console.log('No network with specified SSID found');
return false;
Expand All @@ -285,9 +277,9 @@ export class LinuxUbuntuCorePlatform extends BasePlatform {
NetworkManager.connectToWifiAccessPoint(
wifiDevices[0],
accessPoint,
options.ssid,
<string>options.ssid,
secure,
options.key
<string>options.key
);
} catch (error) {
console.error(`Error connecting to Wi-Fi access point: ${error}`);
Expand Down
Loading

0 comments on commit 37287b6

Please sign in to comment.