@domicile-desktop/system-network 0.0.0-placeholder → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +128 -0
- package/dist/across-locks.d.ts +9 -0
- package/dist/across-locks.js +28 -0
- package/dist/follow.d.ts +13 -0
- package/dist/follow.js +66 -0
- package/dist/has-owner.d.ts +8 -0
- package/dist/has-owner.js +15 -0
- package/dist/interfaces.d.ts +59 -0
- package/dist/interfaces.js +165 -0
- package/dist/iwd-wifi.d.ts +24 -0
- package/dist/iwd-wifi.js +204 -0
- package/dist/iwd.d.ts +13 -0
- package/dist/iwd.js +144 -0
- package/dist/network-state.d.ts +47 -0
- package/dist/network-state.js +33 -0
- package/dist/network.d.ts +15 -0
- package/dist/network.js +64 -0
- package/dist/networkmanager-wifi.d.ts +27 -0
- package/dist/networkmanager-wifi.js +257 -0
- package/dist/networkmanager.d.ts +11 -0
- package/dist/networkmanager.js +126 -0
- package/dist/portal.d.ts +15 -0
- package/dist/portal.js +47 -0
- package/dist/react.d.ts +30 -0
- package/dist/react.js +29 -0
- package/dist/signal-strength.d.ts +5 -0
- package/dist/signal-strength.js +5 -0
- package/dist/variant.d.ts +7 -0
- package/dist/variant.js +3 -0
- package/dist/wifi-state.d.ts +58 -0
- package/dist/wifi-state.js +8 -0
- package/dist/wifi.d.ts +25 -0
- package/dist/wifi.js +116 -0
- package/dist/wpa-supplicant.d.ts +14 -0
- package/dist/wpa-supplicant.js +135 -0
- package/package.json +72 -4
package/dist/wifi.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// The Wi-Fi device from whichever service manages it, NetworkManager or iwd,
|
|
2
|
+
// and the requests that change it. It also forwards the Wi-Fi types, which
|
|
3
|
+
// live in `wifi-state` so the backends can import them without a cycle. See
|
|
4
|
+
// docs/SHELL-SYSTEM-ACCESS.md.
|
|
5
|
+
import { Err, None, Ok, Some } from "@cprussin/option-result";
|
|
6
|
+
import { hasOwner } from "./has-owner";
|
|
7
|
+
import { connectIwdWifi, disconnectIwdWifi, scanIwdWifi, setIwdWifiEnabled, watchIwdWifi, } from "./iwd-wifi";
|
|
8
|
+
import { connectNetworkManagerWifi, disconnectNetworkManagerWifi, scanNetworkManagerWifi, setNetworkManagerWifiEnabled, watchNetworkManagerWifi, } from "./networkmanager-wifi";
|
|
9
|
+
import { WifiBackend } from "./wifi-state";
|
|
10
|
+
export { WifiBackend } from "./wifi-state";
|
|
11
|
+
/** Each backend's bus name, in the order they are tried. */
|
|
12
|
+
const BACKENDS = [
|
|
13
|
+
["org.freedesktop.NetworkManager", WifiBackend.NetworkManager],
|
|
14
|
+
["net.connman.iwd", WifiBackend.Iwd],
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* Calls `onWifi` with the Wi-Fi device now and after each change, and returns
|
|
18
|
+
* a function that stops watching.
|
|
19
|
+
*
|
|
20
|
+
* Watches the first of NetworkManager and iwd on the system bus when called.
|
|
21
|
+
* `None` is no Wi-Fi device, or neither service: wpa_supplicant alone is not
|
|
22
|
+
* controlled. An `Err` is a D-Bus failure.
|
|
23
|
+
*/
|
|
24
|
+
export const watchWifi = (domicile, onWifi) => {
|
|
25
|
+
const watch = { stop: undefined, stopped: false };
|
|
26
|
+
detect(domicile, BACKENDS)
|
|
27
|
+
.then((found) => {
|
|
28
|
+
if (!watch.stopped) {
|
|
29
|
+
found.match({
|
|
30
|
+
Err: (error) => {
|
|
31
|
+
onWifi(Err(error));
|
|
32
|
+
},
|
|
33
|
+
Ok: (backend) => {
|
|
34
|
+
backend.match({
|
|
35
|
+
None: () => {
|
|
36
|
+
onWifi(Ok(None()));
|
|
37
|
+
},
|
|
38
|
+
Some: (chosen) => {
|
|
39
|
+
watch.stop = watchBackend(chosen)(domicile, onWifi);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
.catch((error) => {
|
|
47
|
+
// biome-ignore lint/suspicious/noConsole: surfacing a background failure
|
|
48
|
+
console.error("Failed to find a Wi-Fi service", error);
|
|
49
|
+
});
|
|
50
|
+
return () => {
|
|
51
|
+
watch.stopped = true;
|
|
52
|
+
watch.stop?.();
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
/** Turn the radio on or off. */
|
|
56
|
+
export const setWifiEnabled = (domicile, wifi, enabled) => {
|
|
57
|
+
switch (wifi.backend) {
|
|
58
|
+
case WifiBackend.NetworkManager:
|
|
59
|
+
return setNetworkManagerWifiEnabled(domicile, wifi, enabled);
|
|
60
|
+
case WifiBackend.Iwd:
|
|
61
|
+
return setIwdWifiEnabled(domicile, wifi, enabled);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
/** Ask for a scan. Networks it finds arrive through the watch. */
|
|
65
|
+
export const scanWifi = (domicile, wifi) => {
|
|
66
|
+
switch (wifi.backend) {
|
|
67
|
+
case WifiBackend.NetworkManager:
|
|
68
|
+
return scanNetworkManagerWifi(domicile, wifi);
|
|
69
|
+
case WifiBackend.Iwd:
|
|
70
|
+
return scanIwdWifi(domicile, wifi);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Join `network`. A secured network with no saved profile needs `passphrase`;
|
|
75
|
+
* the call throws without one.
|
|
76
|
+
*/
|
|
77
|
+
export const connectWifi = (domicile, wifi, network, passphrase) => {
|
|
78
|
+
switch (wifi.backend) {
|
|
79
|
+
case WifiBackend.NetworkManager:
|
|
80
|
+
return connectNetworkManagerWifi(domicile, wifi, network, passphrase);
|
|
81
|
+
case WifiBackend.Iwd:
|
|
82
|
+
return connectIwdWifi(domicile, wifi, network, passphrase);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
/** Leave the current network. */
|
|
86
|
+
export const disconnectWifi = (domicile, wifi) => {
|
|
87
|
+
switch (wifi.backend) {
|
|
88
|
+
case WifiBackend.NetworkManager:
|
|
89
|
+
return disconnectNetworkManagerWifi(domicile, wifi);
|
|
90
|
+
case WifiBackend.Iwd:
|
|
91
|
+
return disconnectIwdWifi(domicile, wifi);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const watchBackend = (backend) => {
|
|
95
|
+
switch (backend) {
|
|
96
|
+
case WifiBackend.NetworkManager:
|
|
97
|
+
return watchNetworkManagerWifi;
|
|
98
|
+
case WifiBackend.Iwd:
|
|
99
|
+
return watchIwdWifi;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
/** The first backend whose service has an owner on the bus, if any. */
|
|
103
|
+
const detect = async (domicile, backends) => {
|
|
104
|
+
const [first, ...rest] = backends;
|
|
105
|
+
if (first === undefined) {
|
|
106
|
+
return Ok(None());
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const [name, backend] = first;
|
|
110
|
+
const owned = await hasOwner(domicile, name);
|
|
111
|
+
return owned.match({
|
|
112
|
+
Err: (error) => Promise.resolve(Err(error)),
|
|
113
|
+
Ok: (has) => has ? Promise.resolve(Ok(Some(backend))) : detect(domicile, rest),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Result } from "@cprussin/option-result";
|
|
2
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
3
|
+
import type { Network, NetworkDesktop } from "./network-state";
|
|
4
|
+
/**
|
|
5
|
+
* Calls `onNetwork` with wpa_supplicant's network now and after each change,
|
|
6
|
+
* and returns a function that stops watching. wpa_supplicant is on the bus
|
|
7
|
+
* only when it runs with `-u`.
|
|
8
|
+
*
|
|
9
|
+
* wpa_supplicant knows only Wi-Fi: connectivity is always `Unknown`, and the
|
|
10
|
+
* link is the connected Wi-Fi network or `None`. An `Err` is a D-Bus failure,
|
|
11
|
+
* such as wpa_supplicant not on the bus. The watch keeps listening after one,
|
|
12
|
+
* so a later change reads again.
|
|
13
|
+
*/
|
|
14
|
+
export declare const watchWpaSupplicant: (domicile: NetworkDesktop, onNetwork: (network: Result<Network, SystemError>) => void) => (() => void);
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// wpa_supplicant over the system bus: the connected Wi-Fi network and its
|
|
2
|
+
// signal, kept current. See docs/SHELL-SYSTEM-ACCESS.md.
|
|
3
|
+
import { Err, Ok, Result } from "@cprussin/option-result";
|
|
4
|
+
import { Bus } from "@domicile-desktop/sdk/system";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { followBus } from "./follow";
|
|
7
|
+
import { Connectivity, Link } from "./network-state";
|
|
8
|
+
import { strengthOfDbm } from "./signal-strength";
|
|
9
|
+
import { variant } from "./variant";
|
|
10
|
+
const SERVICE = "fi.w1.wpa_supplicant1";
|
|
11
|
+
const ROOT = "/fi/w1/wpa_supplicant1";
|
|
12
|
+
/**
|
|
13
|
+
* Calls `onNetwork` with wpa_supplicant's network now and after each change,
|
|
14
|
+
* and returns a function that stops watching. wpa_supplicant is on the bus
|
|
15
|
+
* only when it runs with `-u`.
|
|
16
|
+
*
|
|
17
|
+
* wpa_supplicant knows only Wi-Fi: connectivity is always `Unknown`, and the
|
|
18
|
+
* link is the connected Wi-Fi network or `None`. An `Err` is a D-Bus failure,
|
|
19
|
+
* such as wpa_supplicant not on the bus. The watch keeps listening after one,
|
|
20
|
+
* so a later change reads again.
|
|
21
|
+
*/
|
|
22
|
+
export const watchWpaSupplicant = (domicile, onNetwork) => followBus(domicile, { bus: Bus.System, sender: SERVICE }, () => read(domicile), onNetwork);
|
|
23
|
+
/**
|
|
24
|
+
* Read the network. An interface coming or going matters, as does a property
|
|
25
|
+
* change on an object read for it.
|
|
26
|
+
*/
|
|
27
|
+
const read = async (domicile) => {
|
|
28
|
+
const found = await readPaths(domicile);
|
|
29
|
+
return {
|
|
30
|
+
matters: (signal) => matters(found.paths, signal),
|
|
31
|
+
value: found.network.map((link) => ({
|
|
32
|
+
connectivity: Connectivity.Unknown,
|
|
33
|
+
link,
|
|
34
|
+
})),
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Whether `signal` can change the network. wpa_supplicant also sends its own
|
|
39
|
+
* `PropertiesChanged` on each object's interface; only the standard one is
|
|
40
|
+
* read.
|
|
41
|
+
*/
|
|
42
|
+
const matters = (paths, signal) => {
|
|
43
|
+
switch (signal.interface) {
|
|
44
|
+
case SERVICE: {
|
|
45
|
+
return (signal.member === "InterfaceAdded" ||
|
|
46
|
+
signal.member === "InterfaceRemoved");
|
|
47
|
+
}
|
|
48
|
+
case "org.freedesktop.DBus.Properties": {
|
|
49
|
+
return signal.member === "PropertiesChanged" && paths.has(signal.path);
|
|
50
|
+
}
|
|
51
|
+
default: {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const readPaths = async (domicile) => {
|
|
57
|
+
const root = await properties(domicile, ROOT, SERVICE, rootSchema);
|
|
58
|
+
return root.match({
|
|
59
|
+
Err: (error) => Promise.resolve({ network: Err(error), paths: new Set() }),
|
|
60
|
+
Ok: async ({ Interfaces: paths }) => {
|
|
61
|
+
const interfaces = Result.collect(await Promise.all(paths.map(async (path) => (await properties(domicile, path, `${SERVICE}.Interface`, interfaceSchema)).map((held) => ({ ...held, path })))));
|
|
62
|
+
return interfaces.match({
|
|
63
|
+
Err: (error) => Promise.resolve({ network: Err(error), paths: new Set(paths) }),
|
|
64
|
+
Ok: async (held) => {
|
|
65
|
+
const connected = held.find(({ State: state }) => isOnline(state));
|
|
66
|
+
return connected === undefined
|
|
67
|
+
? { network: Ok(Link.None()), paths: new Set(paths) }
|
|
68
|
+
: {
|
|
69
|
+
network: await linkOf(domicile, connected.CurrentBSS),
|
|
70
|
+
paths: new Set([...paths, connected.CurrentBSS]),
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Whether an interface in `state` is on its network. A group rekey moves a
|
|
79
|
+
* connected interface from `completed` to `group_handshake` and back, so
|
|
80
|
+
* `group_handshake` counts too.
|
|
81
|
+
*/
|
|
82
|
+
const isOnline = (state) => {
|
|
83
|
+
switch (state) {
|
|
84
|
+
case "completed":
|
|
85
|
+
case "group_handshake": {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
case "disconnected":
|
|
89
|
+
case "inactive":
|
|
90
|
+
case "interface_disabled":
|
|
91
|
+
case "scanning":
|
|
92
|
+
case "authenticating":
|
|
93
|
+
case "associating":
|
|
94
|
+
case "associated":
|
|
95
|
+
case "4way_handshake":
|
|
96
|
+
case "unknown": {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
/** The network at the BSS `path`. */
|
|
102
|
+
const linkOf = async (domicile, path) => (await properties(domicile, path, `${SERVICE}.BSS`, bssSchema)).map(({ SSID: ssid, Signal: signal }) => Link.Wifi(new TextDecoder().decode(new Uint8Array(ssid)), strengthOfDbm(signal)));
|
|
103
|
+
/** `Properties.GetAll` on one of wpa_supplicant's objects, parsed. */
|
|
104
|
+
const properties = async (domicile, path, interfaceName, schema) => (await domicile.dbus.call({
|
|
105
|
+
body: [interfaceName],
|
|
106
|
+
bus: Bus.System,
|
|
107
|
+
destination: SERVICE,
|
|
108
|
+
interface: "org.freedesktop.DBus.Properties",
|
|
109
|
+
member: "GetAll",
|
|
110
|
+
path,
|
|
111
|
+
signature: "s",
|
|
112
|
+
})).map(({ body }) => z.tuple([schema]).parse(body)[0]);
|
|
113
|
+
const rootSchema = z.object({ Interfaces: variant(z.array(z.string())) });
|
|
114
|
+
/** `wpa_supplicant_state_txt`, lowercased. */
|
|
115
|
+
const interfaceStateSchema = z.enum([
|
|
116
|
+
"disconnected",
|
|
117
|
+
"inactive",
|
|
118
|
+
"interface_disabled",
|
|
119
|
+
"scanning",
|
|
120
|
+
"authenticating",
|
|
121
|
+
"associating",
|
|
122
|
+
"associated",
|
|
123
|
+
"4way_handshake",
|
|
124
|
+
"group_handshake",
|
|
125
|
+
"completed",
|
|
126
|
+
"unknown",
|
|
127
|
+
]);
|
|
128
|
+
const interfaceSchema = z.object({
|
|
129
|
+
CurrentBSS: variant(z.string()),
|
|
130
|
+
State: variant(interfaceStateSchema),
|
|
131
|
+
});
|
|
132
|
+
const bssSchema = z.object({
|
|
133
|
+
Signal: variant(z.number()),
|
|
134
|
+
SSID: variant(z.array(z.number())),
|
|
135
|
+
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,78 @@
|
|
|
1
1
|
{
|
|
2
|
+
"dependencies": {
|
|
3
|
+
"@cprussin/option-result": "^2.0.1",
|
|
4
|
+
"@domicile-desktop/react": "0.0.0",
|
|
5
|
+
"@domicile-desktop/sdk": "1.0.0",
|
|
6
|
+
"zod": "^4.5.4"
|
|
7
|
+
},
|
|
8
|
+
"description": "The network for a Domicile shell, from NetworkManager, iwd or wpa_supplicant: connectivity and the primary connection, with its Wi-Fi network and signal, kept current.",
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"@cprussin/tsconfig": "^5.0.0",
|
|
11
|
+
"@types/bun": "^1.4.2",
|
|
12
|
+
"@types/react": "^19.2.18",
|
|
13
|
+
"react": "^19.2.8",
|
|
14
|
+
"typescript": "^7.0.2"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"bun": ">=1.4.2",
|
|
18
|
+
"node": ">=24"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
"./interfaces": {
|
|
22
|
+
"types": "./dist/interfaces.d.ts",
|
|
23
|
+
"default": "./dist/interfaces.js"
|
|
24
|
+
},
|
|
25
|
+
"./iwd": {
|
|
26
|
+
"types": "./dist/iwd.d.ts",
|
|
27
|
+
"default": "./dist/iwd.js"
|
|
28
|
+
},
|
|
29
|
+
"./network": {
|
|
30
|
+
"types": "./dist/network.d.ts",
|
|
31
|
+
"default": "./dist/network.js"
|
|
32
|
+
},
|
|
33
|
+
"./networkmanager": {
|
|
34
|
+
"types": "./dist/networkmanager.d.ts",
|
|
35
|
+
"default": "./dist/networkmanager.js"
|
|
36
|
+
},
|
|
37
|
+
"./portal": {
|
|
38
|
+
"types": "./dist/portal.d.ts",
|
|
39
|
+
"default": "./dist/portal.js"
|
|
40
|
+
},
|
|
41
|
+
"./wifi": {
|
|
42
|
+
"types": "./dist/wifi.d.ts",
|
|
43
|
+
"default": "./dist/wifi.js"
|
|
44
|
+
},
|
|
45
|
+
"./wpa-supplicant": {
|
|
46
|
+
"types": "./dist/wpa-supplicant.d.ts",
|
|
47
|
+
"default": "./dist/wpa-supplicant.js"
|
|
48
|
+
},
|
|
49
|
+
"./react": {
|
|
50
|
+
"types": "./dist/react.d.ts",
|
|
51
|
+
"default": "./dist/react.js"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"files": [
|
|
55
|
+
"dist/**"
|
|
56
|
+
],
|
|
57
|
+
"license": "MIT",
|
|
2
58
|
"name": "@domicile-desktop/system-network",
|
|
3
|
-
"
|
|
4
|
-
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"react": "^19.2.8"
|
|
61
|
+
},
|
|
62
|
+
"private": false,
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
},
|
|
5
66
|
"repository": {
|
|
67
|
+
"directory": "packages/system-network",
|
|
6
68
|
"type": "git",
|
|
7
69
|
"url": "git+https://github.com/cprussin/domicile.git"
|
|
8
70
|
},
|
|
9
|
-
"
|
|
10
|
-
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "tsc -p tsconfig.build.json",
|
|
73
|
+
"test:types": "tsc --noEmit",
|
|
74
|
+
"test:unit": "bun test"
|
|
75
|
+
},
|
|
76
|
+
"type": "module",
|
|
77
|
+
"version": "1.0.0"
|
|
78
|
+
}
|