@chrischall/warapp-mcp 0.0.0 → 0.1.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/.claude-plugin/marketplace.json +4 -4
- package/.claude-plugin/plugin.json +2 -2
- package/README.md +199 -8
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/card-support.js +329 -0
- package/dist/analysis/card-threats.js +234 -0
- package/dist/analysis/cards-in-effect.js +130 -0
- package/dist/analysis/chains.js +59 -0
- package/dist/analysis/chokepoints.js +140 -0
- package/dist/analysis/combat.js +231 -0
- package/dist/analysis/commander-safety.js +149 -0
- package/dist/analysis/commitments.js +338 -0
- package/dist/analysis/exchange.js +155 -0
- package/dist/analysis/graph.js +82 -0
- package/dist/analysis/guard-off.js +54 -0
- package/dist/analysis/guess.js +351 -0
- package/dist/analysis/headroom.js +241 -0
- package/dist/analysis/hold-diff.js +64 -0
- package/dist/analysis/how-lost.js +130 -0
- package/dist/analysis/index.js +14 -0
- package/dist/analysis/local-deploy.js +159 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +551 -0
- package/dist/analysis/opponents.js +1245 -0
- package/dist/analysis/outlook.js +633 -0
- package/dist/analysis/overrides.js +60 -0
- package/dist/analysis/picks.js +1879 -0
- package/dist/analysis/plan/after-leftover.js +140 -0
- package/dist/analysis/plan/cap-spend.js +242 -0
- package/dist/analysis/plan/captures.js +1195 -0
- package/dist/analysis/plan/commander.js +236 -0
- package/dist/analysis/plan/completed.js +29 -0
- package/dist/analysis/plan/conceded-out.js +131 -0
- package/dist/analysis/plan/estimates.js +715 -0
- package/dist/analysis/plan/guard-off.js +232 -0
- package/dist/analysis/plan/holds.js +1631 -0
- package/dist/analysis/plan/leftover.js +760 -0
- package/dist/analysis/plan/move-order.js +319 -0
- package/dist/analysis/plan/orders.js +317 -0
- package/dist/analysis/plan/relations.js +702 -0
- package/dist/analysis/plan/report.js +672 -0
- package/dist/analysis/plan/shared.js +1218 -0
- package/dist/analysis/plan/stand-still.js +823 -0
- package/dist/analysis/plan/state.js +44 -0
- package/dist/analysis/plan/strike.js +1416 -0
- package/dist/analysis/plan/targets.js +651 -0
- package/dist/analysis/plan/team.js +299 -0
- package/dist/analysis/plan/threats.js +927 -0
- package/dist/analysis/plan/transfers.js +328 -0
- package/dist/analysis/plan/util.js +5 -0
- package/dist/analysis/plan/vacated.js +285 -0
- package/dist/analysis/planner.js +2922 -0
- package/dist/analysis/player-record.js +144 -0
- package/dist/analysis/player.js +268 -0
- package/dist/analysis/profile.js +347 -0
- package/dist/analysis/relations.js +1030 -0
- package/dist/analysis/schedule.js +88 -0
- package/dist/analysis/since-last.js +50 -0
- package/dist/analysis/static-seats.js +71 -0
- package/dist/analysis/stress.js +577 -0
- package/dist/analysis/team-brief.js +151 -0
- package/dist/analysis/tripwire.js +644 -0
- package/dist/analysis/turn-sim.js +259 -0
- package/dist/analysis/whereabouts.js +1109 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +117959 -36172
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +192 -0
- package/dist/client.js +361 -0
- package/dist/config.js +62 -0
- package/dist/direct/link.js +102 -0
- package/dist/direct/session.js +267 -0
- package/dist/direct/transport.js +424 -0
- package/dist/guess-salt.js +65 -0
- package/dist/hosted.js +24 -0
- package/dist/index.js +33 -0
- package/dist/join-color.js +140 -0
- package/dist/live.js +278 -0
- package/dist/map-stats.js +152 -0
- package/dist/map-store.js +204 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +180 -0
- package/dist/normalize.js +411 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +138 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +229 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +677 -0
- package/dist/protocol/bundle.js +375 -0
- package/dist/protocol/codec.js +556 -0
- package/dist/protocol/decoded.js +12 -0
- package/dist/protocol/interp.js +1904 -0
- package/dist/protocol/methods.js +531 -0
- package/dist/protocol/rpc.js +202 -0
- package/dist/push/listener.js +393 -0
- package/dist/push/packets.js +66 -0
- package/dist/push/socketio.js +333 -0
- package/dist/push/websocket.js +179 -0
- package/dist/redact.js +62 -0
- package/dist/relations-store.js +242 -0
- package/dist/replay.js +215 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +269 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2168 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/award.js +206 -0
- package/dist/tools/begin-turn.js +162 -0
- package/dist/tools/board.js +77 -0
- package/dist/tools/card-advice.js +256 -0
- package/dist/tools/card-effects.js +49 -0
- package/dist/tools/card-plays.js +434 -0
- package/dist/tools/card-support-advice.js +140 -0
- package/dist/tools/chat-inbox.js +146 -0
- package/dist/tools/chat.js +483 -0
- package/dist/tools/check-plan.js +688 -0
- package/dist/tools/commander-check.js +102 -0
- package/dist/tools/commitments.js +216 -0
- package/dist/tools/common.js +143 -0
- package/dist/tools/diplomacy.js +342 -0
- package/dist/tools/game.js +126 -0
- package/dist/tools/guess-view.js +56 -0
- package/dist/tools/healthcheck.js +90 -0
- package/dist/tools/hints.js +34 -0
- package/dist/tools/how-lost.js +154 -0
- package/dist/tools/index.js +142 -0
- package/dist/tools/join-game.js +327 -0
- package/dist/tools/leave-game.js +181 -0
- package/dist/tools/live-context.js +339 -0
- package/dist/tools/live.js +404 -0
- package/dist/tools/map-stats.js +117 -0
- package/dist/tools/map.js +133 -0
- package/dist/tools/my-games.js +123 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +151 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/picks.js +292 -0
- package/dist/tools/profile-hook.js +156 -0
- package/dist/tools/profile-opponents.js +121 -0
- package/dist/tools/profile-reads.js +99 -0
- package/dist/tools/push.js +385 -0
- package/dist/tools/relations.js +611 -0
- package/dist/tools/resolve.js +45 -0
- package/dist/tools/route.js +36 -0
- package/dist/tools/scout.js +87 -0
- package/dist/tools/session.js +163 -0
- package/dist/tools/strategy.js +200 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender.js +240 -0
- package/dist/tools/survival.js +189 -0
- package/dist/tools/team-brief.js +266 -0
- package/dist/tools/team-source.js +222 -0
- package/dist/tools/team.js +182 -0
- package/dist/tools/turn.js +2761 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +275 -0
- package/dist/tools/write-kit.js +158 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +452 -0
- package/dist/version.js +4 -0
- package/manifest.json +174 -4
- package/mint.yaml +101 -14
- package/package.json +11 -6
- package/server.json +40 -3
- package/skills/warapp-api/SKILL.md +95 -4
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +69 -8
- package/skills/warapp-mcp/SKILL.md +969 -21
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
// The chrome-less "direct" leg and the direct-first bridge.
|
|
2
|
+
//
|
|
3
|
+
// Live verification (2026-11-12) established that war.app's signed-in session
|
|
4
|
+
// is carried by a durable `AuthToken` cookie, and that this cookie ALONE
|
|
5
|
+
// authenticates both the server-rendered pages (InviteToken, Profile,
|
|
6
|
+
// CommonGames, MultiPlayer) and the binary `/Services` protocol. So every call
|
|
7
|
+
// the browser bridge relays can instead be a plain HTTPS request carrying that
|
|
8
|
+
// cookie — no persistent Chrome tab in normal operation.
|
|
9
|
+
//
|
|
10
|
+
// {@link DirectBridge} is that direct leg: it reads the AuthToken from the
|
|
11
|
+
// session store (src/direct/session.ts) per call and issues an HTTPS request
|
|
12
|
+
// through mcp-utils netguard `fetchPublic` (pinned to public addresses, so a
|
|
13
|
+
// name that resolves or rebinds inward is refused). Every hop must be the exact
|
|
14
|
+
// origin https://war.app; a non-GET follows no redirect at all, so its body (the
|
|
15
|
+
// account email, the orders) is never re-sent anywhere
|
|
16
|
+
// ({@link directFetchOptions}). It maps the reply into the same
|
|
17
|
+
// `FetchproxyFetchResult` shape the browser bridge returns, so the ONE
|
|
18
|
+
// injection point every consumer shares (WarappRpc, the page fetches, the
|
|
19
|
+
// healthcheck, the push socket) gains direct mode at once.
|
|
20
|
+
//
|
|
21
|
+
// {@link createDirectFirstBridge} wires the direct leg and the existing
|
|
22
|
+
// fetchproxy bridge (src/transport-bridge.ts `createWarappBridge`) through
|
|
23
|
+
// mcp-utils `createDirectFirstTransport`: `auto` (default) tries direct and
|
|
24
|
+
// falls back to the browser bridge when the session is absent or expired;
|
|
25
|
+
// `fetchproxy` is today's behaviour; `direct` never falls back. A non-GET that
|
|
26
|
+
// reached war.app on the direct leg is never replayed over the bridge (a write
|
|
27
|
+
// must not be sent twice), and a relink returns `auto` to the direct leg.
|
|
28
|
+
//
|
|
29
|
+
// SECURITY: the AuthToken is a full-account credential. It is never logged,
|
|
30
|
+
// never put in a tool result, never in an error or hint string — upstream
|
|
31
|
+
// errors are scrubbed BY VALUE ({@link scrubError}: message, hint, stack and
|
|
32
|
+
// cause chain). The direct leg refuses to attach the cookie to any host but
|
|
33
|
+
// war.app itself (no subdomain), so the token can never be sent elsewhere.
|
|
34
|
+
// Direct mode is LOCAL-ONLY, enforced here: on a hosted child (src/hosted.ts)
|
|
35
|
+
// {@link createDirectFirstBridge} pins the router to the browser bridge
|
|
36
|
+
// whatever WARAPP_TRANSPORT says, and its session store reads as empty.
|
|
37
|
+
import { detectEdgeBlock, EdgeBlockedError, McpToolError } from '@chrischall/mcp-utils';
|
|
38
|
+
import { createDirectFirstTransport, readTransportMode, } from '@chrischall/mcp-utils/fetchproxy';
|
|
39
|
+
import { fetchPublic } from '@chrischall/mcp-utils/netguard';
|
|
40
|
+
import { isHosted } from '../hosted.js';
|
|
41
|
+
import { SESSION_HOST, scrubError } from './session.js';
|
|
42
|
+
/** The env var pinning the transport: `direct` | `fetchproxy` | `auto` (default). */
|
|
43
|
+
export const TRANSPORT_ENV = 'WARAPP_TRANSPORT';
|
|
44
|
+
/** The byte cap on a direct reply (a board/standing reply is a few KB; pages tens of KB). */
|
|
45
|
+
const MAX_DIRECT_BODY_BYTES = 16 * 1024 * 1024;
|
|
46
|
+
/** The deadline over a direct request and its body read. */
|
|
47
|
+
const DIRECT_TIMEOUT_MS = 30_000;
|
|
48
|
+
/** No war.app session is linked — the router (in `auto`) falls back to the bridge. */
|
|
49
|
+
export class SessionNotLinkedError extends McpToolError {
|
|
50
|
+
name = 'SessionNotLinkedError';
|
|
51
|
+
constructor() {
|
|
52
|
+
super('No war.app session is linked for the direct transport. Run warapp_link_session to capture one.', {
|
|
53
|
+
kind: 'no_credential',
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** The linked session is no longer signed in — the router (in `auto`) falls back to the bridge. */
|
|
58
|
+
export class SessionExpiredError extends McpToolError {
|
|
59
|
+
name = 'SessionExpiredError';
|
|
60
|
+
/** When the expired session was captured (non-secret), so a relink can be told apart. */
|
|
61
|
+
capturedAt;
|
|
62
|
+
constructor(opts = {}) {
|
|
63
|
+
super('The linked war.app session is no longer signed in. ' +
|
|
64
|
+
(opts.notResent
|
|
65
|
+
? 'The request had already reached war.app, so it was not re-sent over the browser bridge (a write must ' +
|
|
66
|
+
'never be sent twice); later calls use the bridge. Retry, or run warapp_link_session to relink.'
|
|
67
|
+
: 'Run warapp_link_session to relink.'), { kind: 'session_expired' });
|
|
68
|
+
this.capturedAt = opts.capturedAt;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const decoder = new TextDecoder();
|
|
72
|
+
/** Whether a request may be re-sent (after it reached war.app, or by a redirect): only a read-only GET/HEAD. */
|
|
73
|
+
const isSafeToReplay = (method) => /^(?:GET|HEAD)$/i.test(method);
|
|
74
|
+
/** The one origin the direct leg talks to: scheme, host and default port. */
|
|
75
|
+
const SESSION_ORIGIN = `https://${SESSION_HOST}`;
|
|
76
|
+
/** Redirects a direct GET may follow (all within war.app). */
|
|
77
|
+
const MAX_DIRECT_GET_REDIRECTS = 5;
|
|
78
|
+
/**
|
|
79
|
+
* The status reported for a redirect a non-GET did not follow. netguard's
|
|
80
|
+
* `stopBefore` result carries the target but not the 3xx code, so this stands
|
|
81
|
+
* in for it: not 200, so WarappRpc reports an `http` failure, unless the target
|
|
82
|
+
* is the sign-in page ({@link looksSignedOut} reads the path).
|
|
83
|
+
*/
|
|
84
|
+
export const UNFOLLOWED_REDIRECT_STATUS = 302;
|
|
85
|
+
/**
|
|
86
|
+
* How the direct leg may follow redirects. Every hop must be the exact host
|
|
87
|
+
* war.app (netguard `allowHosts`, checked on the first URL and on each target
|
|
88
|
+
* before it is requested). A GET follows at most
|
|
89
|
+
* {@link MAX_DIRECT_GET_REDIRECTS} redirects within the exact origin
|
|
90
|
+
* https://war.app (another port or scheme is stopped, not requested). A non-GET follows
|
|
91
|
+
* none: netguard would re-send the method and body on a 307/308, and that body
|
|
92
|
+
* holds the account email and the orders, so the first redirect target is
|
|
93
|
+
* stopped before it is requested (`stopBefore` lets the request itself through
|
|
94
|
+
* and stops everything after it; `maxRedirects: 1` only lets netguard reach
|
|
95
|
+
* that check — with 0 it throws first and the sign-in target is lost). Each
|
|
96
|
+
* call returns fresh state.
|
|
97
|
+
*/
|
|
98
|
+
export function directFetchOptions(method) {
|
|
99
|
+
const base = { requireHttps: true, allowHosts: [SESSION_HOST] };
|
|
100
|
+
if (isSafeToReplay(method)) {
|
|
101
|
+
// allowHosts compares the host name only: war.app on another port or scheme is stopped here.
|
|
102
|
+
return { ...base, maxRedirects: MAX_DIRECT_GET_REDIRECTS, stopBefore: (url) => url.origin !== SESSION_ORIGIN };
|
|
103
|
+
}
|
|
104
|
+
let first = true;
|
|
105
|
+
return {
|
|
106
|
+
...base,
|
|
107
|
+
maxRedirects: 1,
|
|
108
|
+
stopBefore: () => {
|
|
109
|
+
if (first) {
|
|
110
|
+
first = false;
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Map a netguard `fetchPublic` result to {@link DirectFetchResult}. A stopped
|
|
119
|
+
* result (a non-GET's redirect, not followed) has no body and
|
|
120
|
+
* {@link UNFOLLOWED_REDIRECT_STATUS}, with the target as its URL.
|
|
121
|
+
*/
|
|
122
|
+
export function toDirectResult(res) {
|
|
123
|
+
if ('stopped' in res && res.stopped)
|
|
124
|
+
return { status: UNFOLLOWED_REDIRECT_STATUS, body: '', url: res.url };
|
|
125
|
+
const done = res;
|
|
126
|
+
const out = { status: done.status, body: decoder.decode(done.body), url: done.url };
|
|
127
|
+
if (done.headers) {
|
|
128
|
+
// The headers are kept: an edge challenge is read from them (#131).
|
|
129
|
+
const headers = {};
|
|
130
|
+
done.headers.forEach((v, k) => {
|
|
131
|
+
headers[k.toLowerCase()] = v;
|
|
132
|
+
});
|
|
133
|
+
out.headers = headers;
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
/** The default direct leg: a real HTTPS request through netguard `fetchPublic` (public-only, war.app-only). */
|
|
138
|
+
const fetchViaPublic = async (url, init) => {
|
|
139
|
+
const res = await fetchPublic(url, {
|
|
140
|
+
...directFetchOptions(init.method),
|
|
141
|
+
method: init.method,
|
|
142
|
+
headers: init.headers,
|
|
143
|
+
maxBytes: MAX_DIRECT_BODY_BYTES,
|
|
144
|
+
timeoutMs: DIRECT_TIMEOUT_MS,
|
|
145
|
+
...(init.body !== undefined ? { body: init.body } : {}),
|
|
146
|
+
...(init.signal ? { signal: init.signal } : {}),
|
|
147
|
+
});
|
|
148
|
+
return toDirectResult(res);
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Resolve a bridge path to an absolute https URL on the exact host war.app,
|
|
152
|
+
* refusing any other host (a subdomain or look-alike included), userinfo and a
|
|
153
|
+
* non-default port: the full-account cookie goes to war.app and nowhere else.
|
|
154
|
+
*/
|
|
155
|
+
export function warappUrl(path) {
|
|
156
|
+
const raw = /^https?:\/\//i.test(path) ? path : `https://${SESSION_HOST}${path.startsWith('/') ? '' : '/'}${path}`;
|
|
157
|
+
let u;
|
|
158
|
+
try {
|
|
159
|
+
u = new URL(raw);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
throw new McpToolError(`The direct transport could not parse the request path.`, { kind: 'unknown' });
|
|
163
|
+
}
|
|
164
|
+
if (u.hostname !== SESSION_HOST || u.port !== '' || u.username !== '' || u.password !== '') {
|
|
165
|
+
// Never attach the full-account cookie to anything but war.app itself.
|
|
166
|
+
throw new McpToolError(`The direct transport only talks to ${SESSION_HOST}.`, { kind: 'unknown' });
|
|
167
|
+
}
|
|
168
|
+
if (u.protocol !== 'https:') {
|
|
169
|
+
throw new McpToolError('The direct transport requires https.', { kind: 'unknown' });
|
|
170
|
+
}
|
|
171
|
+
return u.href;
|
|
172
|
+
}
|
|
173
|
+
/** A sign-in page's URL (a login redirect landed here). */
|
|
174
|
+
const SIGNIN_PATH = /\/(?:sign-?in|log-?in)\b/i;
|
|
175
|
+
/** An `<input` tag's start. */
|
|
176
|
+
const INPUT_TAG = /<input(?=[\s/>])/gi;
|
|
177
|
+
/** One attribute (name, then an optional double-quoted, single-quoted or bare value), or the tag's end. */
|
|
178
|
+
const TAG_ATTR = /\s*(?:(>)|([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'<>`=]+)))?|\/)/y;
|
|
179
|
+
/**
|
|
180
|
+
* Regions whose content is not markup: HTML comments and `<script>`, `<style>`,
|
|
181
|
+
* `<textarea>` and `<title>` blocks. An unclosed one — an opener with no `>`
|
|
182
|
+
* included — runs to the end of the body, so the opener always matches and a
|
|
183
|
+
* body of many unclosed openers is read once, not once per opener.
|
|
184
|
+
*/
|
|
185
|
+
const NOT_MARKUP = /<!--[\s\S]*?(?:-->|$)|<(script|style|textarea|title)\b[^>]*(?:>|$)[\s\S]*?(?:<\/\1\s*>|$)/gi;
|
|
186
|
+
/**
|
|
187
|
+
* Whether an HTML body holds a real password input: an `<input …>` element
|
|
188
|
+
* whose `type` or `name` ATTRIBUTE is `password`. Player-written text on a page
|
|
189
|
+
* (names, taglines) is HTML-escaped, which leaves `=` alone but never yields a
|
|
190
|
+
* tag, and a value such as `value="type=password"` is read as that value, not
|
|
191
|
+
* as an attribute — so nothing a player writes can match. Comments and script,
|
|
192
|
+
* style, textarea and title blocks are skipped first ({@link NOT_MARKUP}): text a
|
|
193
|
+
* page embeds raw inside one (a JSON string, say) is not an element. The cost: a
|
|
194
|
+
* sign-in form that only a script writes into the page is not seen here; the
|
|
195
|
+
* sign-in redirect and the `/Services` non-frame reply still report that session.
|
|
196
|
+
*/
|
|
197
|
+
export function hasPasswordInput(raw) {
|
|
198
|
+
const body = raw.replace(NOT_MARKUP, ' ');
|
|
199
|
+
INPUT_TAG.lastIndex = 0;
|
|
200
|
+
for (let m = INPUT_TAG.exec(body); m; m = INPUT_TAG.exec(body)) {
|
|
201
|
+
TAG_ATTR.lastIndex = INPUT_TAG.lastIndex;
|
|
202
|
+
for (let a = TAG_ATTR.exec(body); a && !a[1]; a = TAG_ATTR.exec(body)) {
|
|
203
|
+
const name = a[2]?.toLowerCase();
|
|
204
|
+
const value = (a[3] ?? a[4] ?? a[5] ?? '').trim().toLowerCase();
|
|
205
|
+
if ((name === 'type' || name === 'name') && value === 'password')
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
/** Whether a `/Services?js=1` reply is a valid (even-length hex) reply frame. */
|
|
212
|
+
export function looksLikeHexFrame(body) {
|
|
213
|
+
const t = body.trim();
|
|
214
|
+
return t.length > 0 && t.length % 2 === 0 && /^[0-9a-fA-F]+$/.test(t);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Whether a direct reply shows the session is signed out: a login redirect
|
|
218
|
+
* (whatever the final status), or a 200 reply that is a sign-in page (a real
|
|
219
|
+
* password `<input>`, {@link hasPasswordInput} — never a bare `type=password`
|
|
220
|
+
* in the text, which any player can put on a Profile page) or a `/Services`
|
|
221
|
+
* reply that is not a valid frame. Any other status — a 5xx error
|
|
222
|
+
* page above all — is NOT a sign-out: it passes through, so WarappRpc reports
|
|
223
|
+
* it as an `http` failure instead of the router replaying the call over the
|
|
224
|
+
* bridge and marking a good session stale.
|
|
225
|
+
*/
|
|
226
|
+
export function looksSignedOut(requestedUrl, res) {
|
|
227
|
+
let finalPath = '';
|
|
228
|
+
try {
|
|
229
|
+
finalPath = new URL(res.url).pathname;
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
finalPath = '';
|
|
233
|
+
}
|
|
234
|
+
if (SIGNIN_PATH.test(finalPath))
|
|
235
|
+
return true;
|
|
236
|
+
// The origin refused the credential (an edge challenge is told apart first, by the caller).
|
|
237
|
+
if (res.status === 401 || res.status === 403)
|
|
238
|
+
return true;
|
|
239
|
+
if (res.status !== 200)
|
|
240
|
+
return false;
|
|
241
|
+
if (hasPasswordInput(res.body))
|
|
242
|
+
return true;
|
|
243
|
+
let reqPath = '';
|
|
244
|
+
try {
|
|
245
|
+
reqPath = new URL(requestedUrl).pathname;
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
reqPath = '';
|
|
249
|
+
}
|
|
250
|
+
if (/\/services\b/i.test(reqPath) && !looksLikeHexFrame(res.body))
|
|
251
|
+
return true;
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The direct leg: a plain HTTPS request to war.app carrying the linked
|
|
256
|
+
* AuthToken cookie, mapped to the bridge's `.fetch(init)` surface.
|
|
257
|
+
*/
|
|
258
|
+
export class DirectBridge {
|
|
259
|
+
store;
|
|
260
|
+
fetchImpl;
|
|
261
|
+
constructor(store, fetchImpl = fetchViaPublic) {
|
|
262
|
+
this.store = store;
|
|
263
|
+
this.fetchImpl = fetchImpl;
|
|
264
|
+
}
|
|
265
|
+
async fetch(init, signal) {
|
|
266
|
+
const sess = await this.store.read();
|
|
267
|
+
if (!sess)
|
|
268
|
+
throw new SessionNotLinkedError();
|
|
269
|
+
const token = sess.authToken;
|
|
270
|
+
// warappUrl refuses a non-war.app host BEFORE the cookie is built.
|
|
271
|
+
const url = warappUrl(init.path);
|
|
272
|
+
const headers = { ...(init.headers ?? {}), cookie: `AuthToken=${token}` };
|
|
273
|
+
let res;
|
|
274
|
+
try {
|
|
275
|
+
res = await this.fetchImpl(url, {
|
|
276
|
+
method: init.method,
|
|
277
|
+
headers,
|
|
278
|
+
...(init.body !== undefined ? { body: init.body } : {}),
|
|
279
|
+
...(signal ? { signal } : {}),
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
catch (err) {
|
|
283
|
+
throw scrubError(err, [token]);
|
|
284
|
+
}
|
|
285
|
+
// A CDN/WAF challenge (a 403 with `cf-mitigated`, a challenge page) never reached war.app: the
|
|
286
|
+
// router falls back to the browser bridge, whose real browser passes it (#131). A 200 that is
|
|
287
|
+
// a valid reply frame is war.app's own answer whatever header the edge added beside it: read
|
|
288
|
+
// as a challenge, the router would send a write that already arrived a second time.
|
|
289
|
+
const answered = res.status === 200 && looksLikeHexFrame(res.body);
|
|
290
|
+
if (!answered && (res.status !== 200 || res.headers?.['cf-mitigated'])) {
|
|
291
|
+
const edge = detectEdgeBlock({ body: res.body, ...(res.headers ? { headers: res.headers } : {}), status: res.status });
|
|
292
|
+
if (edge) {
|
|
293
|
+
throw new EdgeBlockedError(res.status, edge.vendor, {
|
|
294
|
+
service: SESSION_HOST,
|
|
295
|
+
method: init.method,
|
|
296
|
+
path: new URL(url).pathname,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (looksSignedOut(url, res))
|
|
301
|
+
throw new SessionExpiredError({ capturedAt: sess.capturedAt });
|
|
302
|
+
return { status: res.status, body: res.body, url: res.url };
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Behind a session wall, the session file is re-read (to notice a relink by
|
|
307
|
+
* another process) at most once per this many ms, not on every bridged call.
|
|
308
|
+
*/
|
|
309
|
+
export const RELINK_CHECK_MS = 5_000;
|
|
310
|
+
/**
|
|
311
|
+
* Wire the direct leg and the fetchproxy bridge into a direct-first router,
|
|
312
|
+
* returning an object shaped like today's bridge (`ready()` → `{ fetch }`) plus
|
|
313
|
+
* the healthcheck slice. On a direct→bridge fallback the router emits a switch
|
|
314
|
+
* notice (never printing the token). A non-GET whose direct attempt reached
|
|
315
|
+
* war.app is never replayed over the bridge. In `auto`, a session-caused wall
|
|
316
|
+
* is lifted when a NEW session is linked, so the direct leg is retried.
|
|
317
|
+
*/
|
|
318
|
+
export function createDirectFirstBridge(opts) {
|
|
319
|
+
// LOCAL-ONLY: a hosted child never rides the direct leg nor reads a stored
|
|
320
|
+
// session, whatever WARAPP_TRANSPORT (or `mode`) says.
|
|
321
|
+
const hosted = isHosted(opts.env ?? process.env);
|
|
322
|
+
const mode = hosted
|
|
323
|
+
? 'fetchproxy'
|
|
324
|
+
: (opts.mode ?? readTransportMode(TRANSPORT_ENV, opts.env ? { env: opts.env } : {}));
|
|
325
|
+
const store = hosted ? { read: async () => null } : opts.store;
|
|
326
|
+
const direct = new DirectBridge(store, opts.directFetch);
|
|
327
|
+
const log = opts.log ?? ((m) => console.error(m));
|
|
328
|
+
const now = opts.now ?? Date.now;
|
|
329
|
+
let wall = null;
|
|
330
|
+
let stale = false;
|
|
331
|
+
let lastRelinkCheck = Number.NEGATIVE_INFINITY;
|
|
332
|
+
const onFallback = ({ error }) => {
|
|
333
|
+
const next = error instanceof SessionExpiredError
|
|
334
|
+
? { kind: 'session', capturedAt: error.capturedAt ?? null }
|
|
335
|
+
: error instanceof SessionNotLinkedError
|
|
336
|
+
? { kind: 'session', capturedAt: null }
|
|
337
|
+
: { kind: 'edge' };
|
|
338
|
+
wall = next;
|
|
339
|
+
const why = error instanceof SessionExpiredError
|
|
340
|
+
? 'the linked war.app session is signed out'
|
|
341
|
+
: error instanceof SessionNotLinkedError
|
|
342
|
+
? 'no war.app session is linked'
|
|
343
|
+
: 'the site edge refused the direct request';
|
|
344
|
+
log(`[warapp-mcp] Direct transport unavailable (${why}) — switching to the ContextMint Bridge browser bridge. ` +
|
|
345
|
+
`Keep a ${SESSION_HOST} tab open and approve the pairing prompt in the extension if one appears` +
|
|
346
|
+
(next.kind === 'session' && !hosted ? '; run warapp_link_session to return to the direct transport.' : '.'));
|
|
347
|
+
};
|
|
348
|
+
const build = () => createDirectFirstTransport({
|
|
349
|
+
direct,
|
|
350
|
+
bridge: opts.makeBridge,
|
|
351
|
+
mode,
|
|
352
|
+
serverName: 'warapp-mcp',
|
|
353
|
+
hostLabel: SESSION_HOST,
|
|
354
|
+
shouldFallBack: (err) => err instanceof SessionExpiredError || err instanceof SessionNotLinkedError || err instanceof EdgeBlockedError,
|
|
355
|
+
bridgeHealth: (b) => b.transport(),
|
|
356
|
+
onFallback,
|
|
357
|
+
});
|
|
358
|
+
let legs = build();
|
|
359
|
+
/** Lift a session-caused wall: a fresh router, back on the direct leg. An edge wall stays. */
|
|
360
|
+
const liftSessionWall = () => {
|
|
361
|
+
if (wall?.kind !== 'session')
|
|
362
|
+
return;
|
|
363
|
+
wall = null;
|
|
364
|
+
legs = build();
|
|
365
|
+
};
|
|
366
|
+
/** Behind a session wall: has a DIFFERENT session been linked since (e.g. by another process)? */
|
|
367
|
+
const relinkedSinceWall = async () => {
|
|
368
|
+
if (wall?.kind !== 'session')
|
|
369
|
+
return false;
|
|
370
|
+
const t = now();
|
|
371
|
+
if (t - lastRelinkCheck < RELINK_CHECK_MS)
|
|
372
|
+
return false;
|
|
373
|
+
lastRelinkCheck = t;
|
|
374
|
+
const sess = await store.read();
|
|
375
|
+
return sess !== null && sess.capturedAt !== wall.capturedAt;
|
|
376
|
+
};
|
|
377
|
+
const runOne = async (init) => {
|
|
378
|
+
if (mode === 'auto' && (await relinkedSinceWall())) {
|
|
379
|
+
liftSessionWall();
|
|
380
|
+
stale = false;
|
|
381
|
+
}
|
|
382
|
+
// A non-GET whose direct attempt reached war.app (it answered signed-out)
|
|
383
|
+
// is never re-sent over the bridge: a write must never be sent twice.
|
|
384
|
+
let reached;
|
|
385
|
+
return legs.run(async (leg, ctx) => {
|
|
386
|
+
if (leg === direct) {
|
|
387
|
+
try {
|
|
388
|
+
const res = await direct.fetch(init, ctx.signal);
|
|
389
|
+
// Only a 200 proves the session works; a 5xx says nothing about it.
|
|
390
|
+
if (res.status === 200)
|
|
391
|
+
stale = false;
|
|
392
|
+
return res;
|
|
393
|
+
}
|
|
394
|
+
catch (err) {
|
|
395
|
+
if (err instanceof SessionExpiredError) {
|
|
396
|
+
stale = true;
|
|
397
|
+
if (!isSafeToReplay(init.method))
|
|
398
|
+
reached = err;
|
|
399
|
+
}
|
|
400
|
+
throw err;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (reached)
|
|
404
|
+
throw new SessionExpiredError({ capturedAt: reached.capturedAt, notResent: true });
|
|
405
|
+
return leg.ready().then((t) => t.fetch(init));
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
return {
|
|
409
|
+
ready: async () => ({ fetch: (init) => runOne(init) }),
|
|
410
|
+
status: () => legs.status(),
|
|
411
|
+
bridgeTransport: () => legs.bridgeTransport(),
|
|
412
|
+
stats: () => legs.stats(),
|
|
413
|
+
sessionStale: () => stale,
|
|
414
|
+
sessionLinked: () => {
|
|
415
|
+
stale = false;
|
|
416
|
+
if (mode === 'auto')
|
|
417
|
+
liftSessionWall();
|
|
418
|
+
},
|
|
419
|
+
identityState: async () => {
|
|
420
|
+
const sess = await store.read();
|
|
421
|
+
return JSON.stringify([legs.status().transport, stale, sess?.capturedAt ?? null]);
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// The local secret mixed into the border guess's seed (src/analysis/guess.ts `guessSeed`). The code
|
|
2
|
+
// is public, so a seed from the game id and turn alone could be reproduced by an opponent who reads
|
|
3
|
+
// it; with this salt the draw stays the same when a turn is planned again here, and unreadable
|
|
4
|
+
// elsewhere. Kept at `<dataDir>/guess-salt` (0600, atomic); without a data directory, or when it
|
|
5
|
+
// cannot be written, a salt for this process only. Never logged, never in a tool result.
|
|
6
|
+
import { randomBytes } from 'node:crypto';
|
|
7
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
const SALT = /^[0-9a-f]{32}$/;
|
|
10
|
+
let processSalt;
|
|
11
|
+
const cached = new Map();
|
|
12
|
+
export const guessSaltPath = (dir) => join(dir, 'guess-salt');
|
|
13
|
+
/**
|
|
14
|
+
* The salt {@link guessSalt} would return, without creating one: `undefined` when none exists yet (no
|
|
15
|
+
* guess has been drawn with it). For a reader that must write nothing (#275 pattern 3). Never throws.
|
|
16
|
+
*/
|
|
17
|
+
export async function peekGuessSalt(dir) {
|
|
18
|
+
if (!dir)
|
|
19
|
+
return processSalt;
|
|
20
|
+
const hit = cached.get(dir);
|
|
21
|
+
if (hit)
|
|
22
|
+
return hit;
|
|
23
|
+
try {
|
|
24
|
+
const text = (await readFile(guessSaltPath(dir), 'utf8')).trim();
|
|
25
|
+
if (SALT.test(text))
|
|
26
|
+
return text;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// Not there.
|
|
30
|
+
}
|
|
31
|
+
// A salt that could not be kept falls back to this process's (guessSalt).
|
|
32
|
+
return processSalt;
|
|
33
|
+
}
|
|
34
|
+
/** The salt for `dir` (created on first use), else this process's. Never throws. */
|
|
35
|
+
export async function guessSalt(dir) {
|
|
36
|
+
const fallback = () => (processSalt ??= randomBytes(16).toString('hex'));
|
|
37
|
+
if (!dir)
|
|
38
|
+
return fallback();
|
|
39
|
+
const hit = cached.get(dir);
|
|
40
|
+
if (hit)
|
|
41
|
+
return hit;
|
|
42
|
+
const path = guessSaltPath(dir);
|
|
43
|
+
try {
|
|
44
|
+
const text = (await readFile(path, 'utf8')).trim();
|
|
45
|
+
if (SALT.test(text)) {
|
|
46
|
+
cached.set(dir, text);
|
|
47
|
+
return text;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// Not there yet.
|
|
52
|
+
}
|
|
53
|
+
const salt = randomBytes(16).toString('hex');
|
|
54
|
+
try {
|
|
55
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
56
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
57
|
+
await writeFile(tmp, salt, { mode: 0o600 });
|
|
58
|
+
await rename(tmp, path);
|
|
59
|
+
cached.set(dir, salt);
|
|
60
|
+
return salt;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return fallback();
|
|
64
|
+
}
|
|
65
|
+
}
|
package/dist/hosted.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Is this process a hosted child (mcp-host), not a local install?
|
|
2
|
+
//
|
|
3
|
+
// mcp-host gives this server no explicit "hosted" flag, so a hosted child is
|
|
4
|
+
// recognised by the variables only the host sets ({@link HOSTED_MARKERS}).
|
|
5
|
+
// Consulted by the map statistics (always off when hosted, src/stats.ts) and by
|
|
6
|
+
// the direct transport, which is LOCAL-ONLY: when hosted the session store
|
|
7
|
+
// reads as empty and refuses writes (src/direct/session.ts), the router is
|
|
8
|
+
// pinned to the browser bridge (src/direct/transport.ts), and
|
|
9
|
+
// warapp_link_session is not registered (src/tools/session.ts).
|
|
10
|
+
import { readEnvVar } from '@chrischall/mcp-utils';
|
|
11
|
+
/**
|
|
12
|
+
* Variables only a hosted child sees: mcp-host's own (its child-env contract —
|
|
13
|
+
* the byte-meter file, the confirm-token key, the blob store) and the
|
|
14
|
+
* platform's machine id. Any one of them set means hosted.
|
|
15
|
+
*/
|
|
16
|
+
export const HOSTED_MARKERS = [
|
|
17
|
+
'MCP_HOST_METER_FILE',
|
|
18
|
+
'MCP_HOST_CONFIRM_SECRET',
|
|
19
|
+
'MCP_BLOB_BASE_URL',
|
|
20
|
+
'FLY_MACHINE_ID',
|
|
21
|
+
];
|
|
22
|
+
export function isHosted(env = process.env) {
|
|
23
|
+
return HOSTED_MARKERS.some((k) => readEnvVar(k, { env }) !== undefined);
|
|
24
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// warapp-mcp entrypoint.
|
|
3
|
+
//
|
|
4
|
+
// war.app (Warzone) exposes a per-player GameFeed API authenticated by the
|
|
5
|
+
// player's email + API token. Credentials come from the host env, or from an
|
|
6
|
+
// optional dotenv file (WARAPP_ENV_FILE, default ~/.config/warapp-mcp/.env)
|
|
7
|
+
// whose values never override the host's.
|
|
8
|
+
//
|
|
9
|
+
// Deferred config: the server boots and answers tools/list with no
|
|
10
|
+
// credentials; a missing credential surfaces on the first call that needs it.
|
|
11
|
+
//
|
|
12
|
+
// The live-game tools also speak the web-client protocol, relayed through the
|
|
13
|
+
// user's signed-in war.app tab by the fetchproxy bridge (src/transport-bridge.ts).
|
|
14
|
+
// One bridge per process, started at boot; start() only loads the identity —
|
|
15
|
+
// the port binds on the first request.
|
|
16
|
+
import { loadDotenvSafely, runMcp } from '@chrischall/mcp-utils';
|
|
17
|
+
import { envFilePath } from './config.js';
|
|
18
|
+
import { registerAllTools } from './tools/index.js';
|
|
19
|
+
import { createWarappBridge } from './transport-bridge.js';
|
|
20
|
+
import { VERSION } from './version.js';
|
|
21
|
+
await loadDotenvSafely({ path: envFilePath() });
|
|
22
|
+
const bridge = createWarappBridge();
|
|
23
|
+
// A failed start (e.g. an unwritable identity dir) is not a boot failure: the
|
|
24
|
+
// lazy transport forgets it, and the first bridged call retries and reports it.
|
|
25
|
+
bridge.ready().catch(() => undefined);
|
|
26
|
+
await runMcp({
|
|
27
|
+
name: 'warapp-mcp',
|
|
28
|
+
version: VERSION,
|
|
29
|
+
tools: [(server) => registerAllTools(server, { bridge })],
|
|
30
|
+
shutdown: { onSignal: () => bridge.transport().close() },
|
|
31
|
+
banner: `[warapp-mcp] v${VERSION} — war.app (Warzone) advisor; joining, leaving or surrendering a game, Begin, picks, turn orders, withdrawing them, chat and linking a session are confirm-gated. Set WARAPP_EMAIL and WARAPP_API_TOKEN. ` +
|
|
32
|
+
'This project was developed and is maintained by AI (Claude). Use at your own discretion.',
|
|
33
|
+
});
|