-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vehicle-discovery: Fix implementation to allow finding IPs generated …
…by a DHCP server in the vehicle The vehicle usually has some IPs generated by it's own servers (e.g.: 192.168.3.1 for USB C connections), which offers a dynamic IP for the topside computer. With this change, those IPs are also findable. This commit should be in the original PR, but got lost on the rebase process.
- Loading branch information
1 parent
a982448
commit c6c4ace
Showing
6 changed files
with
96 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { contextBridge, ipcRenderer } from 'electron' | ||
|
||
contextBridge.exposeInMainWorld('electronAPI', { | ||
getNetworkInfo: () => ipcRenderer.invoke('get-network-info'), | ||
getInfoOnSubnets: () => ipcRenderer.invoke('get-info-on-subnets'), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
import { ipcMain } from 'electron' | ||
import { networkInterfaces } from 'os' | ||
|
||
/** | ||
* Information about the network | ||
*/ | ||
interface NetworkInfo { | ||
/** | ||
* The subnet of the local machine | ||
*/ | ||
subnet: string | ||
} | ||
|
||
import { NetworkInfo } from '../../src/types/network' | ||
/** | ||
* Get the network information | ||
* @returns {NetworkInfo} The network information | ||
*/ | ||
const getNetworkInfo = (): NetworkInfo => { | ||
const nets = networkInterfaces() | ||
const getInfoOnSubnets = (): NetworkInfo[] => { | ||
const allSubnets = networkInterfaces() | ||
|
||
for (const name of Object.keys(nets)) { | ||
for (const net of nets[name] ?? []) { | ||
// Skip over non-IPv4 and internal addresses | ||
if (net.family === 'IPv4' && !net.internal) { | ||
// Return the subnet (e.g., if IP is 192.168.1.5, return 192.168.1) | ||
return { subnet: net.address.split('.').slice(0, 3).join('.') } | ||
} | ||
} | ||
const ipv4Subnets = Object.entries(allSubnets) | ||
.flatMap(([_, nets]) => { | ||
return nets.map((net) => ({ ...net, interfaceName: _ })) | ||
}) | ||
.filter((net) => net.family === 'IPv4') | ||
.filter((net) => !net.internal) | ||
|
||
if (ipv4Subnets.length === 0) { | ||
throw new Error('No network interfaces found.') | ||
} | ||
|
||
throw new Error('No network interface found.') | ||
return ipv4Subnets.map((subnet) => { | ||
// TODO: Use the mask to calculate the available addresses. The current implementation is not correct for anything else than /24. | ||
const subnetPrefix = subnet.address.split('.').slice(0, 3).join('.') | ||
const availableAddresses: string[] = [] | ||
for (let i = 1; i <= 254; i++) { | ||
availableAddresses.push(`${subnetPrefix}.${i}`) | ||
} | ||
|
||
return { | ||
topSideAddress: subnet.address, | ||
macAddress: subnet.mac, | ||
interfaceName: subnet.interfaceName, | ||
availableAddresses, | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Setup the network service | ||
*/ | ||
export const setupNetworkService = (): void => { | ||
ipcMain.handle('get-network-info', getNetworkInfo) | ||
ipcMain.handle('get-info-on-subnets', getInfoOnSubnets) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Information about the network | ||
*/ | ||
export interface NetworkInfo { | ||
/** | ||
* The top side address of the local machine | ||
*/ | ||
topSideAddress: string | ||
/** | ||
* The MAC address of the local machine | ||
*/ | ||
macAddress: string | ||
/** | ||
* The name of the network interface | ||
*/ | ||
interfaceName: string | ||
/** | ||
* The CIDR of the local machine | ||
*/ | ||
availableAddresses: string[] | ||
} |