@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Connor Prussin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# @domicile-desktop/system-network
|
|
2
|
+
|
|
3
|
+
The network for a Domicile shell, from NetworkManager, iwd or wpa_supplicant:
|
|
4
|
+
connectivity and the primary connection, with the Wi-Fi network's name and
|
|
5
|
+
signal. The Wi-Fi device, from NetworkManager or iwd: its networks and
|
|
6
|
+
connection, and the requests that change them. Built on
|
|
7
|
+
the `dbus`, `files` and `processes` namespaces of
|
|
8
|
+
the [`Domicile`](../chrome-sdk/README.md) a shell is handed.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { watchNetwork } from "@domicile-desktop/system-network/network";
|
|
14
|
+
|
|
15
|
+
const stop = watchNetwork(domicile, (network) => {
|
|
16
|
+
network.match({
|
|
17
|
+
Err: () => hide(),
|
|
18
|
+
Ok: ({ connectivity, link }) => show(connectivity, link),
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `watchNetwork` watches the first service on the system bus, by
|
|
24
|
+
`NameHasOwner`: NetworkManager, then iwd, then wpa_supplicant. With none, it
|
|
25
|
+
watches NetworkManager. It does not switch if another starts later.
|
|
26
|
+
- To force one, call its watch, which takes the same arguments:
|
|
27
|
+
|
|
28
|
+
| Export | Service | Reads |
|
|
29
|
+
|---|---|---|
|
|
30
|
+
| `networkmanager` `watchNetworkManager` | `org.freedesktop.NetworkManager` | connectivity; `link` is `None`, `Wired`, `Wifi` or `Other` (VPNs and the rest) |
|
|
31
|
+
| `iwd` `watchIwd` | `net.connman.iwd` | the connected station's network; signal from `GetOrderedNetworks` |
|
|
32
|
+
| `wpa-supplicant` `watchWpaSupplicant` | `fi.w1.wpa_supplicant1` (only with `-u`) | the current BSS of an interface that is `completed` or rekeying (`group_handshake`) |
|
|
33
|
+
|
|
34
|
+
- iwd and wpa_supplicant know only Wi-Fi: `connectivity` is always `Unknown`,
|
|
35
|
+
and `link` is `Wifi` or `None`, even with a cable plugged in.
|
|
36
|
+
- `strength` is 0 through 1. Both grade dBm as NetworkManager does: -100 dBm
|
|
37
|
+
is 0, -40 dBm is 1. iwd's signal changes after each scan.
|
|
38
|
+
- Reports once, then after each change the service signals.
|
|
39
|
+
- An `Err` is a D-Bus failure, such as the service not running.
|
|
40
|
+
- A bug or an unexpected reply shape throws, logged to the console.
|
|
41
|
+
|
|
42
|
+
## Wi-Fi
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { connectWifi, watchWifi } from "@domicile-desktop/system-network/wifi";
|
|
46
|
+
|
|
47
|
+
const stop = watchWifi(domicile, (wifi) => {
|
|
48
|
+
wifi.match({
|
|
49
|
+
Err: () => hide(),
|
|
50
|
+
Ok: (found) => found.match({ None: () => hide(), Some: (device) => show(device) }),
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
await connectWifi(domicile, device, network, "passphrase");
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
- `watchWifi` watches the first Wi-Fi device of NetworkManager, else iwd, by
|
|
57
|
+
`NameHasOwner`. `None` is no device, or neither service. wpa_supplicant is
|
|
58
|
+
not controlled.
|
|
59
|
+
- Requests: `setWifiEnabled`, `scanWifi`, `connectWifi`, `disconnectWifi`.
|
|
60
|
+
Each resolves `Ok` once the service accepts it; the watch reports the
|
|
61
|
+
outcome.
|
|
62
|
+
- `connectWifi` uses a network's saved profile when it has one. A new secured
|
|
63
|
+
network needs a passphrase:
|
|
64
|
+
- NetworkManager: a new WPA-PSK profile, saved.
|
|
65
|
+
- iwd: `iwctl --passphrase … station <interface> connect <ssid>`, since iwd
|
|
66
|
+
asks an agent for it and a page cannot serve one. `iwctl` must be on the
|
|
67
|
+
compositor's `PATH`.
|
|
68
|
+
- NetworkManager reads re-run on each property change of an object read, an
|
|
69
|
+
access point coming or going, or a device coming or going. It does not say
|
|
70
|
+
when a scan runs, so `scanning` is always `false`.
|
|
71
|
+
- iwd does not report addresses, so `connection.ip` is `undefined`.
|
|
72
|
+
|
|
73
|
+
## Interfaces
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { watchAddresses, watchInterfaces } from "@domicile-desktop/system-network/interfaces";
|
|
77
|
+
|
|
78
|
+
const stop = watchInterfaces(domicile, (interfaces) => interfaces.map(show));
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
- `readInterfaces` lists `/sys/class/net`'s interfaces with a `device`:
|
|
82
|
+
wired or wireless, hardware address, whether the link is up, and a wired
|
|
83
|
+
link's speed. Loopback, bridges and VPNs are left out. Works while locked.
|
|
84
|
+
- `readAddresses` runs `ip -j address show` and `ip -j route show default`:
|
|
85
|
+
each interface's lasting global IPv4 and IPv6 addresses (temporary IPv6
|
|
86
|
+
addresses left out) and default gateway. `ip` must be on the compositor's
|
|
87
|
+
`PATH`.
|
|
88
|
+
- The watches read every 5 seconds, since a cable's carrier sends no uevent,
|
|
89
|
+
and report only a change.
|
|
90
|
+
|
|
91
|
+
## Captive portals
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { checkPortal } from "@domicile-desktop/system-network/portal";
|
|
95
|
+
|
|
96
|
+
(await checkPortal(domicile)).map((portal) =>
|
|
97
|
+
portal.map((url) => domicile.browser.open(url)),
|
|
98
|
+
);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
- `checkPortal` asks `curl` for `http://www.gstatic.com/generate_204`
|
|
102
|
+
without following redirects, as Chrome does. `204` is `None`. Any other
|
|
103
|
+
answer is `Some` of the sign-in page: the redirect's target, else the probe.
|
|
104
|
+
- It retries a probe with no answer three times, since an address can arrive
|
|
105
|
+
after the link. An `Err` is a probe that never got an answer.
|
|
106
|
+
- `curl` must be on the compositor's `PATH`.
|
|
107
|
+
|
|
108
|
+
See [SHELL-SYSTEM-ACCESS.md](/docs/SHELL-SYSTEM-ACCESS.md).
|
|
109
|
+
|
|
110
|
+
## React
|
|
111
|
+
|
|
112
|
+
`@domicile-desktop/system-network/react`, for a shell on
|
|
113
|
+
[`@domicile-desktop/react`](../react/README.md):
|
|
114
|
+
|
|
115
|
+
- `useNetwork()`, `useNetworkInterfaces()`, `useAddresses()`, `useWifi()`:
|
|
116
|
+
each `undefined` until first read.
|
|
117
|
+
- `deskNetwork(domicile)`: the shared watches behind them, one per desktop.
|
|
118
|
+
The network service refuses calls while the desktop is locked, so those
|
|
119
|
+
watches start again on unlock (`watchAcrossLocks`).
|
|
120
|
+
|
|
121
|
+
## Test
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
bun run turbo test --filter @domicile-desktop/system-network
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Tests replay D-Bus replies, in the compositor's JSON, through an injected
|
|
128
|
+
`dbus` namespace.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Domicile } from "@domicile-desktop/sdk/desktop/desktop";
|
|
2
|
+
/**
|
|
3
|
+
* `start`, begun again each time the desktop unlocks.
|
|
4
|
+
*
|
|
5
|
+
* The compositor refuses the network service's calls while the desktop is
|
|
6
|
+
* locked (docs/LOCK.md), and a refused read is not retried until the service
|
|
7
|
+
* signals a change, which may be long after the unlock.
|
|
8
|
+
*/
|
|
9
|
+
export declare const watchAcrossLocks: <T>(domicile: Pick<Domicile, "session">, start: (onValue: (value: T) => void) => () => void) => (onValue: (value: T) => void) => (() => void);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `start`, begun again each time the desktop unlocks.
|
|
3
|
+
*
|
|
4
|
+
* The compositor refuses the network service's calls while the desktop is
|
|
5
|
+
* locked (docs/LOCK.md), and a refused read is not retried until the service
|
|
6
|
+
* signals a change, which may be long after the unlock.
|
|
7
|
+
*/
|
|
8
|
+
export const watchAcrossLocks = (domicile, start) => (onValue) => {
|
|
9
|
+
const watch = { locked: false, stop: start(onValue) };
|
|
10
|
+
const { session } = domicile;
|
|
11
|
+
const changed = () => {
|
|
12
|
+
// `undefined` until the compositor says.
|
|
13
|
+
const { locked } = session;
|
|
14
|
+
if (locked !== undefined) {
|
|
15
|
+
if (watch.locked && !locked) {
|
|
16
|
+
watch.stop();
|
|
17
|
+
watch.stop = start(onValue);
|
|
18
|
+
}
|
|
19
|
+
watch.locked = locked;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
changed();
|
|
23
|
+
const stopHearing = session.on("lockchange", changed);
|
|
24
|
+
return () => {
|
|
25
|
+
stopHearing();
|
|
26
|
+
watch.stop();
|
|
27
|
+
};
|
|
28
|
+
};
|
package/dist/follow.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import type { DbusMatch, DbusSignal, SystemError } from "@domicile-desktop/sdk/system";
|
|
3
|
+
import type { NetworkDesktop } from "./network-state";
|
|
4
|
+
/** What one read found, and which signals can change it. */
|
|
5
|
+
export type Reading<T extends NonNullable<unknown>> = {
|
|
6
|
+
value: Result<T, SystemError>;
|
|
7
|
+
matters: (signal: DbusSignal) => boolean;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Calls `onValue` with what `read` finds now and after each signal that
|
|
11
|
+
* matters, and returns a function that stops watching.
|
|
12
|
+
*/
|
|
13
|
+
export declare const followBus: <T extends NonNullable<unknown>>(domicile: NetworkDesktop, match: DbusMatch, read: () => Promise<Reading<T>>, onValue: (value: Result<T, SystemError>) => void) => (() => void);
|
package/dist/follow.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// The loop each watch runs: listen, read, then read again on each signal that
|
|
2
|
+
// can change what was read, and after a compositor restart.
|
|
3
|
+
import { Err } from "@cprussin/option-result";
|
|
4
|
+
import { ListenedKind } from "@domicile-desktop/sdk/system";
|
|
5
|
+
/**
|
|
6
|
+
* Calls `onValue` with what `read` finds now and after each signal that
|
|
7
|
+
* matters, and returns a function that stops watching.
|
|
8
|
+
*/
|
|
9
|
+
export const followBus = (domicile, match, read, onValue) => {
|
|
10
|
+
const watch = { listening: undefined, stopped: false };
|
|
11
|
+
const report = (value) => {
|
|
12
|
+
if (!watch.stopped) {
|
|
13
|
+
onValue(value);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
follow(domicile, match, read, watch, report).catch((error) => {
|
|
17
|
+
// biome-ignore lint/suspicious/noConsole: surfacing a background failure
|
|
18
|
+
console.error("Failed to watch the network", error);
|
|
19
|
+
});
|
|
20
|
+
return () => {
|
|
21
|
+
watch.stopped = true;
|
|
22
|
+
watch.listening?.stop();
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const follow = async (domicile, match, read, watch, report) => {
|
|
26
|
+
const matched = await domicile.dbus.match(match);
|
|
27
|
+
await matched.match({
|
|
28
|
+
Err: (error) => {
|
|
29
|
+
report(Err(error));
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
},
|
|
32
|
+
Ok: async (listening) => {
|
|
33
|
+
watch.listening = listening;
|
|
34
|
+
if (watch.stopped) {
|
|
35
|
+
listening.stop();
|
|
36
|
+
}
|
|
37
|
+
await changes(listening, read, report);
|
|
38
|
+
const ended = await listening.ended;
|
|
39
|
+
ended.match({
|
|
40
|
+
Err: (error) => {
|
|
41
|
+
report(Err(error));
|
|
42
|
+
},
|
|
43
|
+
Ok: () => {
|
|
44
|
+
/* stopped by the caller */
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Read now, and again on each signal that matters to the last read and after
|
|
52
|
+
* a compositor restart.
|
|
53
|
+
*/
|
|
54
|
+
const changes = async (listening, read, report) => {
|
|
55
|
+
const reader = listening.items.getReader();
|
|
56
|
+
const first = await read();
|
|
57
|
+
report(first.value);
|
|
58
|
+
let matters = first.matters;
|
|
59
|
+
for (let next = await reader.read(); !next.done; next = await reader.read()) {
|
|
60
|
+
if (next.value.kind === ListenedKind.Missed || matters(next.value.item)) {
|
|
61
|
+
const again = await read();
|
|
62
|
+
report(again.value);
|
|
63
|
+
matters = again.matters;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
3
|
+
import type { NetworkDesktop } from "./network-state";
|
|
4
|
+
/**
|
|
5
|
+
* Whether `name` has an owner on the system bus. `NameHasOwner` does not start
|
|
6
|
+
* a service that D-Bus can activate, as a call to the service would.
|
|
7
|
+
*/
|
|
8
|
+
export declare const hasOwner: (domicile: NetworkDesktop, name: string) => Promise<Result<boolean, SystemError>>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Bus } from "@domicile-desktop/sdk/system";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
/**
|
|
4
|
+
* Whether `name` has an owner on the system bus. `NameHasOwner` does not start
|
|
5
|
+
* a service that D-Bus can activate, as a call to the service would.
|
|
6
|
+
*/
|
|
7
|
+
export const hasOwner = async (domicile, name) => (await domicile.dbus.call({
|
|
8
|
+
body: [name],
|
|
9
|
+
bus: Bus.System,
|
|
10
|
+
destination: "org.freedesktop.DBus",
|
|
11
|
+
interface: "org.freedesktop.DBus",
|
|
12
|
+
member: "NameHasOwner",
|
|
13
|
+
path: "/org/freedesktop/DBus",
|
|
14
|
+
signature: "s",
|
|
15
|
+
})).map(({ body }) => z.tuple([z.boolean()]).parse(body)[0]);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Result } from "@cprussin/option-result";
|
|
2
|
+
import type { Files } from "@domicile-desktop/sdk/desktop/files";
|
|
3
|
+
import type { Processes } from "@domicile-desktop/sdk/desktop/processes";
|
|
4
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
5
|
+
export declare enum InterfaceKind {
|
|
6
|
+
Wired = 0,
|
|
7
|
+
Wireless = 1
|
|
8
|
+
}
|
|
9
|
+
/** A network interface with hardware behind it. */
|
|
10
|
+
export type NetworkInterface = {
|
|
11
|
+
name: string;
|
|
12
|
+
kind: InterfaceKind;
|
|
13
|
+
hardwareAddress: string;
|
|
14
|
+
/** Whether it has a link: a cable in, or a Wi-Fi network joined. */
|
|
15
|
+
up: boolean;
|
|
16
|
+
/** A wired link's speed in Mbit/s, when the driver knows it. */
|
|
17
|
+
speed: number | undefined;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* An interface's lasting global addresses with their prefix length, e.g.
|
|
21
|
+
* `192.168.1.40/24`, from `ip`. Temporary IPv6 addresses are left out: they
|
|
22
|
+
* rotate daily and nothing reaches the machine at them.
|
|
23
|
+
*/
|
|
24
|
+
export type Addressing = {
|
|
25
|
+
ipv4: string[];
|
|
26
|
+
ipv6: string[];
|
|
27
|
+
/** The default route's gateway through it. */
|
|
28
|
+
gateway: string | undefined;
|
|
29
|
+
};
|
|
30
|
+
/** The calls {@link readInterfaces} makes. Each works while the desktop is locked. */
|
|
31
|
+
export type InterfaceDesktop = {
|
|
32
|
+
readonly files: Pick<Files, "readDir" | "readText" | "stat">;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The interfaces with hardware behind them, by name. Virtual ones, such as
|
|
36
|
+
* the loopback, a bridge or a VPN, have no `device` link and are left out.
|
|
37
|
+
*/
|
|
38
|
+
export declare const readInterfaces: (domicile: InterfaceDesktop) => Promise<Result<NetworkInterface[], SystemError>>;
|
|
39
|
+
/**
|
|
40
|
+
* Calls `onInterfaces` with {@link readInterfaces} now and whenever it
|
|
41
|
+
* changes, and returns a function that stops watching. Reads every 5 seconds.
|
|
42
|
+
*/
|
|
43
|
+
export declare const watchInterfaces: (domicile: InterfaceDesktop, onInterfaces: (interfaces: Result<NetworkInterface[], SystemError>) => void, every?: typeof everyInterval) => (() => void);
|
|
44
|
+
/**
|
|
45
|
+
* Calls `onAddresses` with {@link readAddresses} now and whenever it changes,
|
|
46
|
+
* and returns a function that stops watching. Reads every 5 seconds.
|
|
47
|
+
*/
|
|
48
|
+
export declare const watchAddresses: (domicile: {
|
|
49
|
+
readonly processes: Pick<Processes, "run">;
|
|
50
|
+
}, onAddresses: (addresses: Result<Map<string, Addressing>, SystemError>) => void, every?: typeof everyInterval) => (() => void);
|
|
51
|
+
/**
|
|
52
|
+
* Each interface's lasting global addresses and default gateway, by name, from
|
|
53
|
+
* `ip -j`. `ip` must be on the compositor's `PATH`.
|
|
54
|
+
*/
|
|
55
|
+
export declare const readAddresses: (domicile: {
|
|
56
|
+
readonly processes: Pick<Processes, "run">;
|
|
57
|
+
}) => Promise<Result<Map<string, Addressing>, SystemError>>;
|
|
58
|
+
declare const everyInterval: (ms: number, tick: () => void) => (() => void);
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// The machine's network hardware, read from `/sys/class/net`, and each
|
|
2
|
+
// interface's addresses, from `ip`. Every backend's machine has both, so the
|
|
3
|
+
// wired port shows the same with NetworkManager, iwd or wpa_supplicant.
|
|
4
|
+
import { Err, None, Ok, Some } from "@cprussin/option-result";
|
|
5
|
+
import { SystemErrorKind } from "@domicile-desktop/sdk/system";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
const NET = "/sys/class/net";
|
|
8
|
+
/** How often the watches read again: a cable's carrier sends no uevent. */
|
|
9
|
+
const EVERY_MS = 5000;
|
|
10
|
+
export var InterfaceKind;
|
|
11
|
+
(function (InterfaceKind) {
|
|
12
|
+
InterfaceKind[InterfaceKind["Wired"] = 0] = "Wired";
|
|
13
|
+
InterfaceKind[InterfaceKind["Wireless"] = 1] = "Wireless";
|
|
14
|
+
})(InterfaceKind || (InterfaceKind = {}));
|
|
15
|
+
/**
|
|
16
|
+
* The interfaces with hardware behind them, by name. Virtual ones, such as
|
|
17
|
+
* the loopback, a bridge or a VPN, have no `device` link and are left out.
|
|
18
|
+
*/
|
|
19
|
+
export const readInterfaces = async (domicile) => (await domicile.files.readDir(NET)).match({
|
|
20
|
+
Err: (error) => Promise.resolve(Err(error)),
|
|
21
|
+
Ok: async (entries) => {
|
|
22
|
+
const read = await Promise.all(entries
|
|
23
|
+
.map(({ name }) => name)
|
|
24
|
+
.toSorted()
|
|
25
|
+
.map((name) => interfaceOf(domicile, name)));
|
|
26
|
+
return read.reduce((all, each) => all.andThen((found) => each.map((one) => one.match({ None: () => found, Some: (it) => [...found, it] }))), Ok([]));
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* Calls `onInterfaces` with {@link readInterfaces} now and whenever it
|
|
31
|
+
* changes, and returns a function that stops watching. Reads every 5 seconds.
|
|
32
|
+
*/
|
|
33
|
+
export const watchInterfaces = (domicile, onInterfaces, every = everyInterval) => poll(() => readInterfaces(domicile), onInterfaces, every);
|
|
34
|
+
/**
|
|
35
|
+
* Calls `onAddresses` with {@link readAddresses} now and whenever it changes,
|
|
36
|
+
* and returns a function that stops watching. Reads every 5 seconds.
|
|
37
|
+
*/
|
|
38
|
+
export const watchAddresses = (domicile, onAddresses, every = everyInterval) => poll(() => readAddresses(domicile), onAddresses, every);
|
|
39
|
+
/**
|
|
40
|
+
* Each interface's lasting global addresses and default gateway, by name, from
|
|
41
|
+
* `ip -j`. `ip` must be on the compositor's `PATH`.
|
|
42
|
+
*/
|
|
43
|
+
export const readAddresses = async (domicile) => {
|
|
44
|
+
const addresses = await ip(domicile, ["address", "show"], addressesSchema);
|
|
45
|
+
const routes = await ip(domicile, ["route", "show", "default"], routesSchema);
|
|
46
|
+
return addresses.andThen((listed) => routes.map((defaults) => new Map(listed.map(({ addr_info: info, ifname }) => [
|
|
47
|
+
ifname,
|
|
48
|
+
{
|
|
49
|
+
gateway: defaults.find(({ dev }) => dev === ifname)?.gateway,
|
|
50
|
+
ipv4: lastingOf(info, "inet"),
|
|
51
|
+
ipv6: lastingOf(info, "inet6"),
|
|
52
|
+
},
|
|
53
|
+
]))));
|
|
54
|
+
};
|
|
55
|
+
/** Reports `read` now and every {@link EVERY_MS}, when it differs from the last report. */
|
|
56
|
+
const poll = (read, onValue, every) => {
|
|
57
|
+
const watch = {
|
|
58
|
+
reading: Promise.resolve(),
|
|
59
|
+
shown: undefined,
|
|
60
|
+
stopped: false,
|
|
61
|
+
};
|
|
62
|
+
const reread = () => {
|
|
63
|
+
watch.reading = watch.reading
|
|
64
|
+
.then(async () => {
|
|
65
|
+
const value = await read();
|
|
66
|
+
const shown = JSON.stringify(value.match({
|
|
67
|
+
Err: (error) => error,
|
|
68
|
+
// A `Map` is `{}` to JSON.
|
|
69
|
+
Ok: (found) => (found instanceof Map ? [...found] : found),
|
|
70
|
+
}));
|
|
71
|
+
if (!watch.stopped && shown !== watch.shown) {
|
|
72
|
+
watch.shown = shown;
|
|
73
|
+
onValue(value);
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
.catch((error) => {
|
|
77
|
+
// biome-ignore lint/suspicious/noConsole: surfacing a background failure; the next tick reads again
|
|
78
|
+
console.error("Failed to read the network interfaces", error);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
reread();
|
|
82
|
+
const stopClock = every(EVERY_MS, reread);
|
|
83
|
+
return () => {
|
|
84
|
+
watch.stopped = true;
|
|
85
|
+
stopClock();
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
const everyInterval = (ms, tick) => {
|
|
89
|
+
const id = setInterval(tick, ms);
|
|
90
|
+
return () => {
|
|
91
|
+
clearInterval(id);
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
/** The interface `name`, or `None` for a virtual one. */
|
|
95
|
+
const interfaceOf = async (domicile, name) => {
|
|
96
|
+
const at = `${NET}/${name}`;
|
|
97
|
+
return (await exists(domicile, `${at}/device`)).match({
|
|
98
|
+
Err: (error) => Promise.resolve(Err(error)),
|
|
99
|
+
Ok: async (physical) => physical
|
|
100
|
+
? (await hardwareOf(domicile, name)).map(Some)
|
|
101
|
+
: Ok(None()),
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
/** The interface `name`, which has a `device`. */
|
|
105
|
+
const hardwareOf = async (domicile, name) => {
|
|
106
|
+
const at = `${NET}/${name}`;
|
|
107
|
+
const wireless = await exists(domicile, `${at}/wireless`);
|
|
108
|
+
const address = await line(domicile, `${at}/address`);
|
|
109
|
+
const state = await line(domicile, `${at}/operstate`);
|
|
110
|
+
const up = state.map((operstate) => operstate === "up");
|
|
111
|
+
const speed = up.unwrapOr(false) && !wireless.unwrapOr(true)
|
|
112
|
+
? await speedOf(domicile, at)
|
|
113
|
+
: undefined;
|
|
114
|
+
return wireless.andThen((isWireless) => address.andThen((hardwareAddress) => up.map((linked) => ({
|
|
115
|
+
hardwareAddress,
|
|
116
|
+
kind: isWireless ? InterfaceKind.Wireless : InterfaceKind.Wired,
|
|
117
|
+
name,
|
|
118
|
+
speed,
|
|
119
|
+
up: linked,
|
|
120
|
+
}))));
|
|
121
|
+
};
|
|
122
|
+
/** Whether `path` exists. */
|
|
123
|
+
const exists = async (domicile, path) => (await domicile.files.stat(path)).match({
|
|
124
|
+
Err: (error) => error.kind === SystemErrorKind.NotFound ? Ok(false) : Err(error),
|
|
125
|
+
Ok: () => Ok(true),
|
|
126
|
+
});
|
|
127
|
+
/** A one-line file under `/sys`, without its newline. */
|
|
128
|
+
const line = async (domicile, path) => (await domicile.files.readText(path)).map((text) => text.trim());
|
|
129
|
+
/**
|
|
130
|
+
* A wired link's speed. The kernel fails the read, or answers `-1`, when the
|
|
131
|
+
* driver does not know it, which is no speed to show.
|
|
132
|
+
*/
|
|
133
|
+
const speedOf = async (domicile, at) => (await line(domicile, `${at}/speed`)).match({
|
|
134
|
+
Err: () => undefined,
|
|
135
|
+
Ok: (text) => {
|
|
136
|
+
const mbits = Number.parseInt(text, 10);
|
|
137
|
+
return mbits > 0 ? mbits : undefined;
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
/** `ip -j` with `argv`, parsed with `schema`. */
|
|
141
|
+
const ip = async (domicile, argv, schema) => (await domicile.processes.run(["ip", "-j", ...argv])).andThen(({ code, stderr, stdout }) => code === 0
|
|
142
|
+
? Ok(schema.parse(JSON.parse(stdout)))
|
|
143
|
+
: Err({
|
|
144
|
+
kind: SystemErrorKind.Other,
|
|
145
|
+
message: `ip ${argv.join(" ")} exited ${String(code)}: ${stderr.trim()}`,
|
|
146
|
+
}));
|
|
147
|
+
const addressInfoSchema = z.object({
|
|
148
|
+
// An IPv6 address past its preferred lifetime, kept only for open connections.
|
|
149
|
+
deprecated: z.boolean().optional(),
|
|
150
|
+
family: z.string(),
|
|
151
|
+
local: z.string(),
|
|
152
|
+
prefixlen: z.number(),
|
|
153
|
+
scope: z.string(),
|
|
154
|
+
// An IPv6 privacy address, which outgoing connections use and rotate daily.
|
|
155
|
+
temporary: z.boolean().optional(),
|
|
156
|
+
});
|
|
157
|
+
const addressesSchema = z.array(z.object({ addr_info: z.array(addressInfoSchema), ifname: z.string() }));
|
|
158
|
+
/** `info`'s global addresses in `family` that are neither deprecated nor temporary. */
|
|
159
|
+
const lastingOf = (info, family) => info
|
|
160
|
+
.filter((address) => address.family === family &&
|
|
161
|
+
address.scope === "global" &&
|
|
162
|
+
address.deprecated !== true &&
|
|
163
|
+
address.temporary !== true)
|
|
164
|
+
.map(({ local, prefixlen }) => `${local}/${prefixlen}`);
|
|
165
|
+
const routesSchema = z.array(z.object({ dev: z.string(), gateway: z.string().optional() }));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Option, Result } from "@cprussin/option-result";
|
|
2
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
3
|
+
import type { NetworkDesktop } from "./network-state";
|
|
4
|
+
import type { Wifi, WifiDesktop, WifiDevice, WifiNetwork } from "./wifi-state";
|
|
5
|
+
/**
|
|
6
|
+
* Calls `onWifi` with the first station device now and after each change, and
|
|
7
|
+
* returns a function that stops watching. `None` is a machine without one.
|
|
8
|
+
*
|
|
9
|
+
* An `Err` is a D-Bus failure, such as iwd not running. The watch keeps
|
|
10
|
+
* listening after one, so a later change reads again.
|
|
11
|
+
*/
|
|
12
|
+
export declare const watchIwdWifi: (domicile: NetworkDesktop, onWifi: (wifi: Result<Option<Wifi>, SystemError>) => void) => (() => void);
|
|
13
|
+
/** Turn the device on or off. */
|
|
14
|
+
export declare const setIwdWifiEnabled: (domicile: NetworkDesktop, wifi: WifiDevice, enabled: boolean) => Promise<Result<"done", SystemError>>;
|
|
15
|
+
/** Ask for a scan. Networks it finds arrive as changes. */
|
|
16
|
+
export declare const scanIwdWifi: (domicile: NetworkDesktop, wifi: WifiDevice) => Promise<Result<"done", SystemError>>;
|
|
17
|
+
/**
|
|
18
|
+
* Join `network`. A known or open network joins over D-Bus. A new secured one
|
|
19
|
+
* joins through `iwctl`, which answers iwd's request for the passphrase: iwd
|
|
20
|
+
* asks an agent for it, and a page cannot serve one.
|
|
21
|
+
*/
|
|
22
|
+
export declare const connectIwdWifi: (domicile: WifiDesktop, wifi: WifiDevice, network: WifiNetwork, passphrase: string | undefined) => Promise<Result<"done", SystemError>>;
|
|
23
|
+
/** Leave the current network, until asked to join one. */
|
|
24
|
+
export declare const disconnectIwdWifi: (domicile: NetworkDesktop, wifi: WifiDevice) => Promise<Result<"done", SystemError>>;
|