Skip to content

Commit

Permalink
Get IP addresses for Ethernet and Wi-Fi
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Oct 10, 2024
1 parent ead0c4d commit fcbcb7b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 28 deletions.
8 changes: 6 additions & 2 deletions src/controllers/settings_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import TunnelService from '../tunnel-service';
import * as CertificateManager from '../certificate-manager';
import pkg from '../package.json';
import { HttpErrorWithCode } from '../errors';
import { LanMode } from '../platforms/types';
import { LanMode, NetworkAddresses } from '../platforms/types';

function build(): express.Router {
const auth = jwtMiddleware.middleware();
Expand Down Expand Up @@ -468,7 +468,11 @@ function build(): express.Router {
});

controller.get('/network/addresses', auth, (_request, response) => {
if (Platform.implemented('getNetworkAddresses')) {
if (Platform.implemented('getNetworkAddressesAsync')) {
Platform.getNetworkAddressesAsync().then((networkAddresses: NetworkAddresses) => {
response.json(networkAddresses);
});
} else if (Platform.implemented('getNetworkAddresses')) {
response.json(Platform.getNetworkAddresses());
} else {
response.status(500).send('Network addresses not implemented');
Expand Down
1 change: 1 addition & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export const getMacAddress = wrapPlatform<string | null>(platform, 'getMacAddres
export const getMdnsServerStatus = wrapPlatform<boolean>(platform, 'getMdnsServerStatus');
export const setMdnsServerStatus = wrapPlatform<boolean>(platform, 'setMdnsServerStatus');
export const getNetworkAddresses = wrapPlatform<NetworkAddresses>(platform, 'getNetworkAddresses');
export const getNetworkAddressesAsync = wrapPlatform<Promise<NetworkAddresses>>(platform, 'getNetworkAddressesAsync');
export const getSshServerStatus = wrapPlatform<boolean>(platform, 'getSshServerStatus');
export const setSshServerStatus = wrapPlatform<boolean>(platform, 'setSshServerStatus');
export const getWirelessMode = wrapPlatform<WirelessMode>(platform, 'getWirelessMode');
Expand Down
126 changes: 100 additions & 26 deletions src/platforms/linux-ubuntu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import BasePlatform from './base';
import DBus from 'dbus';
import {LanMode} from './types';
import {LanMode, NetworkAddresses} from './types';

class LinuxUbuntuPlatform extends BasePlatform {

Expand Down Expand Up @@ -69,7 +69,7 @@ class LinuxUbuntuPlatform extends BasePlatform {
}

/**
* Get a list of network Ethernet adapters from the system network manager.
* Get a list of Ethernet network adapters from the system network manager.
*
* @returns {Promise<Array<string>>} A promise which resolves with an array
* of DBus object paths.
Expand All @@ -88,6 +88,26 @@ class LinuxUbuntuPlatform extends BasePlatform {
return ethernetDevices;
}

/**
* Get a list of Wi-Fi network adapters from the system network manager.
*
* @returns {Promise<Array<string>>} A promise which resolves with an array
* of DBus object paths.
*/
private async getWifiDevices(): Promise<string[]> {
// Get a list of all network adapter devices
let devices = await this.getDevices();
let wifiDevices: string[] = [];
// Filter by type
for (const device of devices) {
const type = await this.getDeviceType(device);
if (type == 2) {
wifiDevices.push(device);
}
}
return wifiDevices;
}

/**
* Get the active connection associated with a device.
*
Expand Down Expand Up @@ -159,6 +179,49 @@ class LinuxUbuntuPlatform extends BasePlatform {
});
}

/**
* Get an IPv4 configuration for a given device path.
*
* @param {String} path Object path for a device.
* @returns {Promise<IP4Config>} Promise resolves with IP4Config object.
*/
private getDeviceIp4Config(path: string): Promise<any> {
const systemBus = this.systemBus;
return new Promise((resolve, reject) => {
systemBus.getInterface('org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.Device',
function(error, iface) {
if (error) {
console.error(error);
reject();
}
iface.getProperty('Ip4Config', function(error, ip4ConfigPath) {
if (error) {
console.error(error);
reject();
}
systemBus.getInterface('org.freedesktop.NetworkManager',
ip4ConfigPath,
'org.freedesktop.NetworkManager.IP4Config',
function(error, iface) {
if (error) {
console.error(error);
reject();
}
iface.getProperty('AddressData', function(error, value) {
if (error) {
console.error(error);
reject();
}
resolve(value);
});
});
});
});
});
}

/**
* Get the LAN mode and options.
*
Expand Down Expand Up @@ -188,34 +251,45 @@ class LinuxUbuntuPlatform extends BasePlatform {
});
}

// Currently unused code...

/**
* Get the path to the IPv4 configuration for a given network adapter.
* Get the current addresses for Wi-Fi and LAN.
*
* @param {String} path Object path for a device.
* @returns {Promise<string>} Promise resolves with path to configuration object.
* @returns {Promise<NetworkAddresses>} Promise that resolves with
* {
* lan: '...',
* wlan: {
* ip: '...',
* ssid: '...',
* }
* }
*/
/*getDeviceIp4ConfigPath(path: string) {
return new Promise((resolve, reject) => {
this.systemBus.getInterface('org.freedesktop.NetworkManager',
path,
'org.freedesktop.NetworkManager.Device',
function(error, iface) {
if (error) {
console.error(error);
reject();
}
iface.getProperty('Ip4Config', function(error, value) {
if (error) {
console.error(error);
reject();
}
resolve(value);
});
});
async getNetworkAddressesAsync(): Promise<NetworkAddresses> {
// TODO: Handle the case where there is no Ethernet adapter or no Wi-Fi
// adapter or one of them isn't assigned an IP
let result: NetworkAddresses = {
lan: '',
wlan: {
ip: '',
ssid: ''
}
};
return this.getEthernetDevices().then((ethernetDevices) => {
return this.getDeviceIp4Config(ethernetDevices[0]);
}).then((ethernetIp4Config) => {
result.lan = ethernetIp4Config[0].address;
return this.getWifiDevices();
}).then((wifiDevices) => {
return this.getDeviceIp4Config(wifiDevices[0]);
}).then((wifiIp4Config) => {
result.wlan.ip = wifiIp4Config[0].address;
return result;
}).catch((error) => {
console.error('Error getting IP addresses from NetworkManager: ' + error);
return result;
});
}*/
}

// Currently unused code...

/**
* Get the DHCP configuration for a given network adapter.
Expand Down

0 comments on commit fcbcb7b

Please sign in to comment.