This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
nm.ts
205 lines (177 loc) · 6.2 KB
/
nm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { NetworkManagerTypes } from './types';
import { BodyEntry } from 'dbus-native';
import {
dbusInvoker,
getProperty
} from './dbus';
export interface NetworkDevice {
iface: string; // IP interface name
path: string; // DBus object path
type: string;
driver: string;
connected: boolean;
}
export interface WirelessDevice extends NetworkDevice {
apCapable: boolean;
}
export interface WiredDevice extends NetworkDevice {
}
export interface WirelessNetwork {
iface: string;
ssid: string;
password?: string;
}
const nm: string = 'org.freedesktop.NetworkManager';
// Wireless
export const createAccessPoint = async (device: WirelessNetwork): Promise<any> => {
try {
// Error out if the interface does not exist
const wifiDevices: NetworkDevice[] = await getWiFiDevices()
if (!wifiDevices.some(d => d.iface === device.iface)) {
console.log(`Selected interface ${device.iface} does not exist. Hotspot creation aborted...`);
return
}
const connectionParams: BodyEntry[] = [
['connection', [
['id', ['s', device.ssid]],
['type', ['s', '802-11-wireless']],
]],
['802-11-wireless', [
['ssid', ['ay', stringToArrayOfBytes(device.ssid)]],
['mode', ['s', 'ap']],
]],
['802-11-wireless-security', [
['key-mgmt', ['s', 'wpa-psk']],
['psk', ['s', device.password]],
]],
['ipv4', [
['method', ['s', 'shared']],
]],
['ipv6', [
['method', ['s', 'ignore']],
]],
];
const dbusPath = await getPathByIface(device.iface);
const connection = await addConnection(connectionParams);
const result = await activateConnection(connection, dbusPath);
return result
} catch (error) {
console.log(`Error creating Hotspot: ${error}`);
}
};
export const connectToWifi = async (network: WirelessNetwork): Promise<any> => {
try {
const connectionParams = [
['connection', [
['id', ['s', network.ssid]],
['type', ['s', '802-11-wireless']],
]],
['802-11-wireless', [
['ssid', ['ay', stringToArrayOfBytes(network.ssid)]],
['mode', ['s', 'infrastructure']],
]],
['802-11-wireless-security', [
['key-mgmt', ['s', 'wpa-psk']],
['psk', ['s', network.password]],
]],
['ipv4', [
['method', ['s', 'auto']],
]],
['ipv6', [
['method', ['s', 'auto']],
]],
];
let device = await getPathByIface(network.iface);
let connection = await addConnection(connectionParams);
let result = await activateConnection(connection, device);
return result
} catch (error) {
console.log(`Error connecting to WiFi: ${error}`);
}
};
// NetworkManager
export const getWiFiDevices = async (): Promise<WirelessDevice[]> => {
const devices: NetworkDevice[] = await getDevicesByType(NetworkManagerTypes.DEVICE_TYPE.WIFI)
const wifiDevices: WirelessDevice[] = []
for await (const device of devices) {
const apCapable: boolean = !!(await getProperty(nm, device.path, 'org.freedesktop.NetworkManager.Device.Wireless', 'WirelessCapabilities') & NetworkManagerTypes.WIFI_DEVICE_CAP.AP);
wifiDevices.push({ ...device, apCapable });
}
return wifiDevices;
};
export const getWiredDevices = async (): Promise<WiredDevice[]> => {
return await getDevicesByType(NetworkManagerTypes.DEVICE_TYPE.ETHERNET);
};
export const getDevicesByType = async (type: number): Promise<NetworkDevice[]> => {
const paths: string[] = await getDevicesPath();
const devices: NetworkDevice[] = [];
for await (const path of paths) {
const deviceType: number = await getProperty(nm, path, 'org.freedesktop.NetworkManager.Device', 'DeviceType');
if (deviceType === type) {
const iface: string = await getProperty(nm, path, 'org.freedesktop.NetworkManager.Device', 'Interface');
const connected: boolean = await getProperty(nm, path, 'org.freedesktop.NetworkManager.Device', 'Ip4Connectivity') === NetworkManagerTypes.CONNECTIVITY.FULL;
const driver: string = await getProperty(nm, path, 'org.freedesktop.NetworkManager.Device', 'Driver');
const typeName: string = Object.keys(NetworkManagerTypes.DEVICE_TYPE).find(key => NetworkManagerTypes.DEVICE_TYPE[key] === type) || "UNKNOWN";
devices.push({ path, iface, connected, driver, type: typeName });
}
}
return devices
};
export const getDevicesPath = async (): Promise<string[]> => {
return await dbusInvoker({
destination: nm,
path: '/org/freedesktop/NetworkManager',
interface: 'org.freedesktop.NetworkManager',
member: 'GetDevices'
});
};
export const getPathByIface = async (iface: string): Promise<string> => {
return await dbusInvoker({
destination: nm,
path: '/org/freedesktop/NetworkManager',
interface: 'org.freedesktop.NetworkManager',
member: 'GetDeviceByIpIface',
signature: 's',
body: [iface]
});
};
export const checkDeviceConnectivity = async (iface: string): Promise<any> => {
const path: string = await getPathByIface(iface)
return await getProperty(nm, path, 'org.freedesktop.NetworkManager.Device', 'Ip4Connectivity')
};
export const checkNMConnectivity = async (): Promise<any> => {
let nmConnectivityState = await dbusInvoker({
destination: nm,
path: '/org/freedesktop/NetworkManager',
interface: 'org.freedesktop.NetworkManager',
member: 'CheckConnectivity'
});
return nmConnectivityState === NetworkManagerTypes.CONNECTIVITY.FULL
};
export const addConnection = async (params: BodyEntry[]): Promise<any> => {
return await dbusInvoker({
destination: nm,
path: '/org/freedesktop/NetworkManager/Settings',
interface: 'org.freedesktop.NetworkManager.Settings',
member: 'AddConnection',
signature: 'a{sa{sv}}',
body: [params]
});
};
export const activateConnection = async (connection: string, path: string) => {
return await dbusInvoker({
destination: nm,
path: '/org/freedesktop/NetworkManager',
interface: 'org.freedesktop.NetworkManager',
member: 'ActivateConnection',
signature: 'ooo',
body: [connection, path, '/']
});
};
function stringToArrayOfBytes(str) {
const bytes: number[] = [];
for (let i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
}
return bytes;
}