omp-mobile 0.0.0-stage → 0.1.1
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 +201 -0
- package/NOTICE +6 -0
- package/README.md +75 -2
- package/package.json +53 -4
- package/src/admission.mjs +207 -0
- package/src/attention.mjs +243 -0
- package/src/omp-extension.mjs +270 -0
- package/src/omp-sessions.mjs +203 -0
- package/src/qr.mjs +67 -0
- package/src/rpc-session.mjs +265 -0
- package/src/server.mjs +886 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="assets/omp-mobile-logo.svg" width="64" height="64" alt="OMP Mobile logo">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">OMP Mobile</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="README.md">English</a> ·
|
|
9
|
+
<a href="README.zh-CN.md">简体中文</a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p align="center">Your oh-my-pi sessions, on your phone.</p>
|
|
13
|
+
|
|
14
|
+
OMP Mobile is a native phone app for [oh-my-pi](https://github.com/can1357/oh-my-pi) (OMP), with a small daemon on each computer. Start, follow and steer OMP sessions from the phone: transcript, tool calls, approvals, and the sessions you started in a terminal.
|
|
15
|
+
|
|
16
|
+
- **Native app:** The app draws the transcript, tool cards, thinking, Markdown and composer itself. No WebView, no web page.
|
|
17
|
+
- **Approvals on the phone:** OMP's own tool approvals and questions arrive as a sheet, and the turn waits for your answer.
|
|
18
|
+
- **Terminal sessions:** Sessions you started in a terminal on the computer are listed too and continue on the phone. One still open in a terminal waits until that terminal exits.
|
|
19
|
+
- **Notifications:** Android notifications for approvals, questions, and finished turns while the app is in the background.
|
|
20
|
+
- **All your machines:** Pair each computer by QR code, switch between them from the session list. A reopened chat shows at once, also offline.
|
|
21
|
+
- **Tailscale only:** The daemon listens on the computer's Tailscale address and admits only your own devices. No relay or cloud service of its own.
|
|
22
|
+
- **An OMP plugin:** `/mobile` in OMP starts the daemon in the background and shows the pairing code. The daemon keeps running after the terminal closes.
|
|
23
|
+
|
|
24
|
+
## Getting started
|
|
25
|
+
|
|
26
|
+
### Prerequisites
|
|
27
|
+
|
|
28
|
+
- On every computer: OMP 18.x
|
|
29
|
+
- [Tailscale](https://tailscale.com) signed in to the same account on the computer and the phone
|
|
30
|
+
- An Android phone
|
|
31
|
+
|
|
32
|
+
### Computer
|
|
33
|
+
|
|
34
|
+
Install the plugin into OMP on every computer:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
omp plugin install omp-mobile
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then in OMP, run `/mobile`. It starts the daemon in the background, bound to the computer's Tailscale address, and shows a pairing code above the composer until the phone connects. The daemon runs on the Bun inside OMP and outlives the terminal and OMP; `/mobile status` shows it and `/mobile stop` ends it. Phone sessions start in the directory where you ran `/mobile` the first time.
|
|
41
|
+
|
|
42
|
+
Without the plugin, the same daemon runs from npm (Node.js 22.19 or newer) in the foreground: `npm install -g omp-mobile`, then `omp-mobile --host tailscale --cwd ~/project`. It prints the same code.
|
|
43
|
+
|
|
44
|
+
### Phone
|
|
45
|
+
|
|
46
|
+
Install the APK from the [releases page](https://github.com/Rinai-R/omp-mobile/releases). In the app, tap **Scan to pair** and scan the code. **Enter address** takes the address and token by hand, or a pasted pairing link.
|
|
47
|
+
|
|
48
|
+
For notifications, open **Settings → Notifications** in the app.
|
|
49
|
+
|
|
50
|
+
A device on your Tailscale account that has the token can run anything OMP can on that computer.
|
|
51
|
+
|
|
52
|
+
## Development
|
|
53
|
+
|
|
54
|
+
- `daemon`: the computer side (Node.js), which runs OMP in RPC mode and serves the app
|
|
55
|
+
- `app`: the phone app (Expo, React Native)
|
|
56
|
+
- `app/modules/omp-attention`: the Android notification service (Kotlin)
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install
|
|
60
|
+
npm run typecheck
|
|
61
|
+
npm test # daemon and app
|
|
62
|
+
|
|
63
|
+
npm start -w @omp-mobile/app # Expo dev server
|
|
64
|
+
|
|
65
|
+
# Android APK (JDK 17, Android SDK); app/android is generated, not committed
|
|
66
|
+
cd app && npx expo prebuild --platform android --clean --no-install
|
|
67
|
+
cd android && ./gradlew assembleRelease
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Against an emulator, run the daemon from the checkout with `npm run daemon -- --host 127.0.0.1` and forward the port with `adb reverse tcp:7390 tcp:7390`. How the daemon works, its options and HTTP API, and the source map are in [ARCHITECTURE.md](ARCHITECTURE.md).
|
|
71
|
+
|
|
72
|
+
A release is a `v<version>` tag on `main` whose version matches `package.json`, `daemon/package.json`, `app/package.json` and `app/app.json`, with a matching section in [CHANGELOG.md](CHANGELOG.md). CI then publishes `omp-mobile` to npm and a GitHub Release with the APK.
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
Apache-2.0. OMP Mobile is not affiliated with oh-my-pi or Tailscale.
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "omp-mobile",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "The computer side of OMP Mobile: runs oh-my-pi sessions and serves them to the phone app over Tailscale.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mobile",
|
|
7
|
+
"oh-my-pi",
|
|
8
|
+
"omp",
|
|
9
|
+
"omp-plugin",
|
|
10
|
+
"remote",
|
|
11
|
+
"tailscale"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/Rinai-R/omp-mobile#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/Rinai-R/omp-mobile/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "Rinai",
|
|
20
|
+
"email": "1130157066@qq.com"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/Rinai-R/omp-mobile.git",
|
|
25
|
+
"directory": "daemon"
|
|
26
|
+
},
|
|
27
|
+
"type": "module",
|
|
28
|
+
"bin": {
|
|
29
|
+
"omp-mobile": "src/server.mjs"
|
|
30
|
+
},
|
|
31
|
+
"omp": {
|
|
32
|
+
"extensions": [
|
|
33
|
+
"./src/omp-extension.mjs"
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"src",
|
|
38
|
+
"NOTICE"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public",
|
|
42
|
+
"provenance": true
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"start": "node src/server.mjs",
|
|
46
|
+
"test": "node --test"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"qrcode-generator": "^2.0.4",
|
|
50
|
+
"ws": "^8.18.3"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=22.19.0"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tailscale identity admission for the daemon listener.
|
|
3
|
+
*
|
|
4
|
+
* The bearer token proves the client was paired; admission proves the request
|
|
5
|
+
* reached this node on purpose (Host names this node) and comes from a device
|
|
6
|
+
* of the same tailnet user (`tailscale whois`). Both run before the token.
|
|
7
|
+
*/
|
|
8
|
+
import { execFile } from "node:child_process";
|
|
9
|
+
import { platform } from "node:os";
|
|
10
|
+
|
|
11
|
+
const MAC_APP_BINARY = "/Applications/Tailscale.app/Contents/MacOS/Tailscale";
|
|
12
|
+
const TIMEOUT_MS = 5_000;
|
|
13
|
+
const WHOIS_TTL_MS = 60_000;
|
|
14
|
+
const FAILURE_TTL_MS = 5_000;
|
|
15
|
+
|
|
16
|
+
/** @typedef {{ips: string[], dnsName: string, userId: number, loginName: string}} SelfStatus */
|
|
17
|
+
/** @typedef {{userId: number, loginName: string, tags: string[], node: string}} WhoisResult */
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Run a command and resolve its stdout.
|
|
21
|
+
* @param {string} binary
|
|
22
|
+
* @param {string[]} args
|
|
23
|
+
* @returns {Promise<string>}
|
|
24
|
+
*/
|
|
25
|
+
function run(binary, args) {
|
|
26
|
+
return new Promise((resolveRun, reject) => {
|
|
27
|
+
execFile(binary, args, { timeout: TIMEOUT_MS, maxBuffer: 16 * 1024 * 1024 }, (error, stdout) => {
|
|
28
|
+
if (error) reject(error);
|
|
29
|
+
else resolveRun(stdout);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Thin wrapper over the local `tailscale` CLI. */
|
|
35
|
+
class Tailscale {
|
|
36
|
+
/**
|
|
37
|
+
* @param {{binary?: string, run?: typeof run, platform?: string}} [options]
|
|
38
|
+
*/
|
|
39
|
+
constructor(options = {}) {
|
|
40
|
+
this.binary = options.binary ?? "tailscale";
|
|
41
|
+
this.run = options.run ?? run;
|
|
42
|
+
// A bare "tailscale" that is not on PATH falls back to the macOS app bundle.
|
|
43
|
+
this.fallbacks =
|
|
44
|
+
this.binary === "tailscale" && (options.platform ?? platform()) === "darwin" ? [MAC_APP_BINARY] : [];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @param {string[]} args */
|
|
48
|
+
async exec(args) {
|
|
49
|
+
try {
|
|
50
|
+
return await this.run(this.binary, args);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (error?.code !== "ENOENT" || this.fallbacks.length === 0) throw error;
|
|
53
|
+
const next = this.fallbacks.shift();
|
|
54
|
+
this.binary = next;
|
|
55
|
+
return this.exec(args);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @returns {Promise<SelfStatus>} */
|
|
60
|
+
async status() {
|
|
61
|
+
const raw = JSON.parse(await this.exec(["status", "--json"]));
|
|
62
|
+
const self = raw?.Self ?? {};
|
|
63
|
+
if (raw?.BackendState !== undefined && raw.BackendState !== "Running") {
|
|
64
|
+
throw new Error(`tailscale is not running (BackendState ${raw.BackendState})`);
|
|
65
|
+
}
|
|
66
|
+
const ips = Array.isArray(self.TailscaleIPs) ? self.TailscaleIPs.map(String) : [];
|
|
67
|
+
const userId = Number(self.UserID ?? 0);
|
|
68
|
+
const profile = raw?.User?.[String(userId)];
|
|
69
|
+
return {
|
|
70
|
+
ips,
|
|
71
|
+
dnsName: String(self.DNSName ?? "").replace(/\.$/, "").toLowerCase(),
|
|
72
|
+
userId: Number.isFinite(userId) ? userId : 0,
|
|
73
|
+
loginName: typeof profile?.LoginName === "string" ? profile.LoginName : "",
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Identify the node and user behind an address. Rejects when the CLI fails,
|
|
79
|
+
* which includes addresses outside the tailnet (whois exits non-zero).
|
|
80
|
+
* @param {string} ip
|
|
81
|
+
* @returns {Promise<WhoisResult>}
|
|
82
|
+
*/
|
|
83
|
+
async whois(ip) {
|
|
84
|
+
const raw = JSON.parse(await this.exec(["whois", "--json", ip]));
|
|
85
|
+
const userId = Number(raw?.Node?.User ?? raw?.UserProfile?.ID ?? 0);
|
|
86
|
+
return {
|
|
87
|
+
userId: Number.isFinite(userId) ? userId : 0,
|
|
88
|
+
loginName: typeof raw?.UserProfile?.LoginName === "string" ? raw.UserProfile.LoginName : "",
|
|
89
|
+
tags: Array.isArray(raw?.Node?.Tags) ? raw.Node.Tags.map(String) : [],
|
|
90
|
+
node: String(raw?.Node?.Name ?? "").replace(/\.$/, ""),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Host header without port, brackets, trailing dot; lower case.
|
|
97
|
+
* @param {string | undefined} host
|
|
98
|
+
*/
|
|
99
|
+
function hostnameOf(host) {
|
|
100
|
+
if (typeof host !== "string") return "";
|
|
101
|
+
const bracket = /^\[([^\]]+)\]/.exec(host);
|
|
102
|
+
const name = bracket === null ? host.replace(/:\d+$/, "") : bracket[1];
|
|
103
|
+
return name.toLowerCase().replace(/\.$/, "");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Socket remote address with IPv4-mapped IPv6 reduced to dotted IPv4.
|
|
108
|
+
* @param {string | undefined} address
|
|
109
|
+
*/
|
|
110
|
+
function remoteIp(address) {
|
|
111
|
+
if (typeof address !== "string") return "";
|
|
112
|
+
return address.toLowerCase().startsWith("::ffff:") && address.includes(".") ? address.slice(7) : address;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** @param {string} host */
|
|
116
|
+
function isLoopbackHost(host) {
|
|
117
|
+
return host === "127.0.0.1" || host === "::1" || host === "localhost" || host === "[::1]";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @typedef {{ok: true, ip: string, user: string} | {ok: false, status: number, error: string, ip: string}} Verdict
|
|
122
|
+
*/
|
|
123
|
+
|
|
124
|
+
/** Listener admission: Host names this node, source is a device of this node's user. */
|
|
125
|
+
class Admission {
|
|
126
|
+
/**
|
|
127
|
+
* @param {{
|
|
128
|
+
* self: SelfStatus,
|
|
129
|
+
* bindHost: string,
|
|
130
|
+
* tailscale?: {whois(ip: string): Promise<WhoisResult>},
|
|
131
|
+
* whois?: boolean,
|
|
132
|
+
* now?: () => number,
|
|
133
|
+
* }} options
|
|
134
|
+
*/
|
|
135
|
+
constructor(options) {
|
|
136
|
+
this.self = options.self;
|
|
137
|
+
this.tailscale = options.tailscale;
|
|
138
|
+
this.whoisEnabled = options.whois !== false;
|
|
139
|
+
this.now = options.now ?? Date.now;
|
|
140
|
+
this.hosts = new Set(
|
|
141
|
+
[hostnameOf(options.bindHost), ...options.self.ips.map((ip) => ip.toLowerCase()), options.self.dnsName]
|
|
142
|
+
.filter(Boolean),
|
|
143
|
+
);
|
|
144
|
+
this.ownIps = new Set(options.self.ips.map((ip) => ip.toLowerCase()));
|
|
145
|
+
/** Pending or settled lookups per address; concurrent requests share one. */
|
|
146
|
+
/** @type {Map<string, {expires: number, who: Promise<WhoisResult | undefined>}>} */
|
|
147
|
+
this.cache = new Map();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @param {{headers: Record<string, string | string[] | undefined>, socket: {remoteAddress?: string}}} req
|
|
152
|
+
* @returns {Promise<Verdict>}
|
|
153
|
+
*/
|
|
154
|
+
async check(req) {
|
|
155
|
+
const ip = remoteIp(req.socket?.remoteAddress);
|
|
156
|
+
const host = hostnameOf(/** @type {string | undefined} */ (req.headers.host));
|
|
157
|
+
if (!this.hosts.has(host)) {
|
|
158
|
+
return { ok: false, status: 421, ip, error: `misdirected request: host "${host}" does not name this node` };
|
|
159
|
+
}
|
|
160
|
+
if (!this.whoisEnabled) return { ok: true, ip, user: "" };
|
|
161
|
+
// This node's own addresses belong to its user; the token still applies.
|
|
162
|
+
if (this.ownIps.has(ip.toLowerCase())) return { ok: true, ip, user: this.self.loginName };
|
|
163
|
+
const who = await this.identify(ip);
|
|
164
|
+
if (who === undefined) {
|
|
165
|
+
return { ok: false, status: 403, ip, error: `source ${ip} is not a known tailnet device` };
|
|
166
|
+
}
|
|
167
|
+
if (who.tags.length > 0) {
|
|
168
|
+
return { ok: false, status: 403, ip, error: `source ${ip} is a tagged device (${who.tags.join(", ")}); tagged devices have no user` };
|
|
169
|
+
}
|
|
170
|
+
if (who.userId === 0 || who.userId !== this.self.userId) {
|
|
171
|
+
return {
|
|
172
|
+
ok: false,
|
|
173
|
+
status: 403,
|
|
174
|
+
ip,
|
|
175
|
+
error: `source ${ip} belongs to ${who.loginName || "another user"}, not ${this.self.loginName || "this node's user"}`,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
return { ok: true, ip, user: who.loginName };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Whois with a cache: identities for 60 s, failures for 5 s.
|
|
183
|
+
* @param {string} ip
|
|
184
|
+
*/
|
|
185
|
+
identify(ip) {
|
|
186
|
+
const now = this.now();
|
|
187
|
+
const hit = this.cache.get(ip);
|
|
188
|
+
if (hit !== undefined && now < hit.expires) return hit.who;
|
|
189
|
+
const slot = { expires: now + WHOIS_TTL_MS, who: Promise.resolve(undefined) };
|
|
190
|
+
slot.who = Promise.resolve()
|
|
191
|
+
.then(() => this.tailscale?.whois(ip))
|
|
192
|
+
.then(
|
|
193
|
+
(found) => {
|
|
194
|
+
if (found === undefined) slot.expires = this.now() + FAILURE_TTL_MS;
|
|
195
|
+
return found;
|
|
196
|
+
},
|
|
197
|
+
() => {
|
|
198
|
+
slot.expires = this.now() + FAILURE_TTL_MS;
|
|
199
|
+
return undefined;
|
|
200
|
+
},
|
|
201
|
+
);
|
|
202
|
+
this.cache.set(ip, slot);
|
|
203
|
+
return slot.who;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export { Admission, Tailscale, hostnameOf, remoteIp, isLoopbackHost, MAC_APP_BINARY };
|