@bitkyc08/opencodex 2.99.0 → 2.100.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/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-CitvEefy.js +59 -0
- package/gui/dist/assets/Tray-CeKVWQyB.js +1 -0
- package/gui/dist/assets/{brand-mark-wzPe3R-J.js → brand-mark-M92UnxX2.js} +1 -1
- package/gui/dist/assets/index-BP47do6-.js +94 -0
- package/gui/dist/assets/index-DmDooSAG.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-CcxZBnxv.js} +1 -1
- package/gui/dist/classic/assets/App-aYgIQMxI.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-D8jfUXpQ.js} +1 -1
- package/gui/dist/classic/assets/index-CfGqHooN.js +94 -0
- package/gui/dist/classic/assets/quota-summary-R-ih7OlS.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +405 -153
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +49 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +39 -7
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +244 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
|
@@ -0,0 +1,1288 @@
|
|
|
1
|
+
use std::io::{BufRead, BufReader, Read, Write};
|
|
2
|
+
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream};
|
|
3
|
+
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
4
|
+
use std::sync::{Arc, Condvar, Mutex};
|
|
5
|
+
use std::thread;
|
|
6
|
+
use std::time::{Duration, Instant};
|
|
7
|
+
|
|
8
|
+
use crate::OCX_PROTOCOL_COMPAT_VERSION;
|
|
9
|
+
use crate::http_wire::{
|
|
10
|
+
BodyFraming, FramingError, HUB_RELAY_RESPONSE_BODY_MAX_BYTES, HttpRequestHead,
|
|
11
|
+
HttpResponseHead, LINK_RELAY_BODY_MAX_BYTES, is_sse_headers, read_bounded_body,
|
|
12
|
+
read_request_head, read_response_head, response_framing, sanitize_response_headers,
|
|
13
|
+
stream_upstream_to_chunked_client, validate_and_sanitize_request_headers, write_json_response,
|
|
14
|
+
};
|
|
15
|
+
use crate::journal::validate_existing_journal_paths;
|
|
16
|
+
use crate::parity::LinkChildParityReport;
|
|
17
|
+
use crate::platform::{
|
|
18
|
+
FileLock, SecretBytes, constant_time_eq, format_iso8601_utc, now_epoch_ms, random_hex,
|
|
19
|
+
};
|
|
20
|
+
use crate::relay_auth::{
|
|
21
|
+
LINK_RELAY_AUTH_PATH, LINK_RELAY_AUTH_VERSION, LINK_RELAY_CONNECTION_HEADER,
|
|
22
|
+
LINK_RELAY_CONNECTION_PROOF_HEADER, LINK_RELAY_PROBE_HEADER, LINK_RELAY_PROOF_HEADER,
|
|
23
|
+
LINK_RELAY_SESSION_HEADER, compute_child_shutdown_proof, compute_connection_proof,
|
|
24
|
+
create_relay_challenge, is_valid_connection_generation, relay_proof_matches,
|
|
25
|
+
};
|
|
26
|
+
use crate::routes::{RelayAuthKind, RouteDecision, classify_child_ingress};
|
|
27
|
+
use crate::state::{ChildPaths, StateReadOutcome, read_child_state, read_verified_link_key};
|
|
28
|
+
|
|
29
|
+
const AUTH_TIMEOUT: Duration = Duration::from_secs(5);
|
|
30
|
+
/// Whole-request bound for one-shot Home calls made by lifecycle commands.
|
|
31
|
+
const FETCH_DEADLINE: Duration = Duration::from_secs(60);
|
|
32
|
+
/// Whole-request bound for a parity report; per-read timeouts alone never end a trickle.
|
|
33
|
+
const PARITY_DEADLINE: Duration = Duration::from_secs(10);
|
|
34
|
+
const FETCH_READ_TIMEOUT: Duration = Duration::from_secs(15);
|
|
35
|
+
/// Per-read and per-write timeout for a control connection handled while every worker is busy.
|
|
36
|
+
const CONTROL_TIMEOUT: Duration = Duration::from_secs(1);
|
|
37
|
+
/// Deadline for every request head a control connection reads, so a sender that trickles
|
|
38
|
+
/// bytes inside `CONTROL_TIMEOUT` still releases its control slot.
|
|
39
|
+
const CONTROL_DEADLINE: Duration = Duration::from_secs(2);
|
|
40
|
+
/// Connect bound for a request that shutdown can cancel: a socket can be registered for
|
|
41
|
+
/// cancellation only once connected, so this bounds how long shutdown can wait on a connect.
|
|
42
|
+
const CANCELLABLE_CONNECT_TIMEOUT: Duration = Duration::from_secs(1);
|
|
43
|
+
const HEADER_AND_SSE_TIMEOUT: Duration = Duration::from_secs(300);
|
|
44
|
+
const MAX_CONCURRENT_WORKERS: usize = 32;
|
|
45
|
+
const MAX_CONTROL_CONNECTIONS: usize = 256;
|
|
46
|
+
const PEER_FEATURES: &str = "relay-auth/2,link-management-relay,link-peer-report/1";
|
|
47
|
+
|
|
48
|
+
#[derive(Debug)]
|
|
49
|
+
pub enum UpstreamError {
|
|
50
|
+
HomeUnreachable(String),
|
|
51
|
+
RelayAuthFailed(String),
|
|
52
|
+
ResponseHeadersTooLarge,
|
|
53
|
+
ResponseBodyTooLarge,
|
|
54
|
+
BadUpstreamResponse(String),
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
pub struct RelayCredentials {
|
|
58
|
+
pub api_key_id: String,
|
|
59
|
+
pub link_id: String,
|
|
60
|
+
pub token_fingerprint: String,
|
|
61
|
+
pub raw_token: SecretBytes,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// Upstream socket reader that enforces an optional whole-request deadline: each read is
|
|
65
|
+
/// bounded by the time left, so a peer trickling bytes cannot extend the request.
|
|
66
|
+
pub struct DeadlineStream {
|
|
67
|
+
stream: TcpStream,
|
|
68
|
+
deadline: Option<Instant>,
|
|
69
|
+
per_read: Duration,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
impl DeadlineStream {
|
|
73
|
+
fn new(stream: TcpStream, deadline: Option<Instant>, per_read: Duration) -> Self {
|
|
74
|
+
Self {
|
|
75
|
+
stream,
|
|
76
|
+
deadline,
|
|
77
|
+
per_read,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
pub fn get_ref(&self) -> &TcpStream {
|
|
82
|
+
&self.stream
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
impl Read for DeadlineStream {
|
|
87
|
+
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
|
88
|
+
if let Some(deadline) = self.deadline {
|
|
89
|
+
let now = Instant::now();
|
|
90
|
+
if now >= deadline {
|
|
91
|
+
return Err(std::io::Error::new(
|
|
92
|
+
std::io::ErrorKind::TimedOut,
|
|
93
|
+
"request deadline exceeded",
|
|
94
|
+
));
|
|
95
|
+
}
|
|
96
|
+
let wait = (deadline - now)
|
|
97
|
+
.min(self.per_read)
|
|
98
|
+
.max(Duration::from_millis(1));
|
|
99
|
+
self.stream.set_read_timeout(Some(wait))?;
|
|
100
|
+
}
|
|
101
|
+
self.stream.read(buf)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// Holds the socket of an in-flight request so another thread can abort it. Registration
|
|
106
|
+
/// and cancellation share one mutex, so a request either sees the cancellation before it
|
|
107
|
+
/// registers or is registered when cancellation shuts its socket down.
|
|
108
|
+
#[derive(Default)]
|
|
109
|
+
pub struct CancelSlot {
|
|
110
|
+
inner: Mutex<(bool, Option<TcpStream>)>,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
impl CancelSlot {
|
|
114
|
+
fn register(&self, stream: &TcpStream) -> Result<(), UpstreamError> {
|
|
115
|
+
let mut guard = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
|
116
|
+
if guard.0 {
|
|
117
|
+
return Err(UpstreamError::HomeUnreachable(
|
|
118
|
+
"request cancelled by shutdown".to_owned(),
|
|
119
|
+
));
|
|
120
|
+
}
|
|
121
|
+
guard.1 =
|
|
122
|
+
Some(stream.try_clone().map_err(|e| {
|
|
123
|
+
UpstreamError::HomeUnreachable(format!("socket clone failed: {e}"))
|
|
124
|
+
})?);
|
|
125
|
+
Ok(())
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
fn is_cancelled(&self) -> bool {
|
|
129
|
+
self.inner.lock().unwrap_or_else(|e| e.into_inner()).0
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
fn clear(&self) {
|
|
133
|
+
self.inner.lock().unwrap_or_else(|e| e.into_inner()).1 = None;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// Abort the registered request, if any, and refuse every later registration.
|
|
137
|
+
pub fn cancel(&self) {
|
|
138
|
+
let mut guard = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
|
139
|
+
guard.0 = true;
|
|
140
|
+
if let Some(stream) = guard.1.take() {
|
|
141
|
+
let _ = stream.shutdown(Shutdown::Both);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/// Bounds for one upstream request.
|
|
147
|
+
#[derive(Clone, Copy)]
|
|
148
|
+
pub struct RequestLimits<'a> {
|
|
149
|
+
pub deadline: Option<Instant>,
|
|
150
|
+
pub cancel: Option<&'a CancelSlot>,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
impl RequestLimits<'_> {
|
|
154
|
+
fn remaining(&self, cap: Duration) -> Result<Duration, UpstreamError> {
|
|
155
|
+
if self.cancel.is_some_and(CancelSlot::is_cancelled) {
|
|
156
|
+
return Err(UpstreamError::HomeUnreachable(
|
|
157
|
+
"request cancelled by shutdown".to_owned(),
|
|
158
|
+
));
|
|
159
|
+
}
|
|
160
|
+
match self.deadline {
|
|
161
|
+
None => Ok(cap),
|
|
162
|
+
Some(deadline) => {
|
|
163
|
+
let left = deadline.saturating_duration_since(Instant::now());
|
|
164
|
+
if left.is_zero() {
|
|
165
|
+
Err(UpstreamError::HomeUnreachable(
|
|
166
|
+
"request deadline exceeded".to_owned(),
|
|
167
|
+
))
|
|
168
|
+
} else {
|
|
169
|
+
Ok(left.min(cap))
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
pub struct AuthenticatedUpstream {
|
|
177
|
+
pub reader: BufReader<DeadlineStream>,
|
|
178
|
+
pub writer: TcpStream,
|
|
179
|
+
pub nonce: String,
|
|
180
|
+
pub connection_generation: Option<String>,
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
fn url_encode_component(s: &str) -> String {
|
|
184
|
+
let mut out = String::with_capacity(s.len());
|
|
185
|
+
for b in s.bytes() {
|
|
186
|
+
if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
|
|
187
|
+
out.push(b as char);
|
|
188
|
+
} else {
|
|
189
|
+
out.push_str(&format!("%{b:02X}"));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
out
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/// Opens ONE `TcpStream` to `127.0.0.1:<tunnel_port>`, runs the v2 HMAC challenge/proof,
|
|
196
|
+
/// and returns that exact same `TcpStream` ready to transmit one bound data request.
|
|
197
|
+
pub fn open_authenticated_upstream(
|
|
198
|
+
tunnel_port: u16,
|
|
199
|
+
creds: &RelayCredentials,
|
|
200
|
+
probe_only: bool,
|
|
201
|
+
) -> Result<AuthenticatedUpstream, UpstreamError> {
|
|
202
|
+
open_authenticated_upstream_with(
|
|
203
|
+
tunnel_port,
|
|
204
|
+
creds,
|
|
205
|
+
probe_only,
|
|
206
|
+
RequestLimits {
|
|
207
|
+
deadline: None,
|
|
208
|
+
cancel: None,
|
|
209
|
+
},
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
fn open_authenticated_upstream_with(
|
|
214
|
+
tunnel_port: u16,
|
|
215
|
+
creds: &RelayCredentials,
|
|
216
|
+
probe_only: bool,
|
|
217
|
+
limits: RequestLimits<'_>,
|
|
218
|
+
) -> Result<AuthenticatedUpstream, UpstreamError> {
|
|
219
|
+
let addr = SocketAddr::from(([127, 0, 0, 1], tunnel_port));
|
|
220
|
+
let connect_cap = if limits.cancel.is_some() {
|
|
221
|
+
CANCELLABLE_CONNECT_TIMEOUT
|
|
222
|
+
} else {
|
|
223
|
+
AUTH_TIMEOUT
|
|
224
|
+
};
|
|
225
|
+
let stream = TcpStream::connect_timeout(&addr, limits.remaining(connect_cap)?)
|
|
226
|
+
.map_err(|e| UpstreamError::HomeUnreachable(format!("tunnel connect failed: {e}")))?;
|
|
227
|
+
if let Some(cancel) = limits.cancel {
|
|
228
|
+
cancel.register(&stream)?;
|
|
229
|
+
}
|
|
230
|
+
let _ = stream.set_nodelay(true);
|
|
231
|
+
let _ = stream.set_read_timeout(Some(AUTH_TIMEOUT));
|
|
232
|
+
let _ = stream.set_write_timeout(Some(limits.remaining(AUTH_TIMEOUT)?));
|
|
233
|
+
let mut writer = stream
|
|
234
|
+
.try_clone()
|
|
235
|
+
.map_err(|e| UpstreamError::HomeUnreachable(format!("socket clone failed: {e}")))?;
|
|
236
|
+
let mut reader = BufReader::new(DeadlineStream::new(stream, limits.deadline, AUTH_TIMEOUT));
|
|
237
|
+
|
|
238
|
+
let challenge =
|
|
239
|
+
create_relay_challenge(&creds.token_fingerprint, &creds.api_key_id, &creds.link_id)
|
|
240
|
+
.map_err(UpstreamError::RelayAuthFailed)?;
|
|
241
|
+
|
|
242
|
+
let target = format!(
|
|
243
|
+
"{LINK_RELAY_AUTH_PATH}?version={LINK_RELAY_AUTH_VERSION}&key={}&link={}&nonce={}&proof={}",
|
|
244
|
+
url_encode_component(&creds.api_key_id),
|
|
245
|
+
url_encode_component(&creds.link_id),
|
|
246
|
+
challenge.nonce,
|
|
247
|
+
challenge.caller_proof,
|
|
248
|
+
);
|
|
249
|
+
let conn_hdr = if probe_only { "close" } else { "keep-alive" };
|
|
250
|
+
let mut preflight = format!(
|
|
251
|
+
"GET {target} HTTP/1.1\r\nHost: 127.0.0.1:{tunnel_port}\r\nAccept-Encoding: identity\r\nConnection: {conn_hdr}\r\nx-opencodex-link-peer-version: {OCX_PROTOCOL_COMPAT_VERSION}\r\nx-opencodex-link-peer-features: {PEER_FEATURES}\r\n"
|
|
252
|
+
);
|
|
253
|
+
if probe_only {
|
|
254
|
+
preflight.push_str(&format!("{LINK_RELAY_PROBE_HEADER}: 1\r\n"));
|
|
255
|
+
}
|
|
256
|
+
preflight.push_str("\r\n");
|
|
257
|
+
writer
|
|
258
|
+
.write_all(preflight.as_bytes())
|
|
259
|
+
.and_then(|()| writer.flush())
|
|
260
|
+
.map_err(|e| UpstreamError::HomeUnreachable(format!("preflight write failed: {e}")))?;
|
|
261
|
+
|
|
262
|
+
let resp = read_response_head(&mut reader).map_err(|e| match e {
|
|
263
|
+
FramingError::Eof => {
|
|
264
|
+
UpstreamError::HomeUnreachable("tunnel closed during preflight".to_owned())
|
|
265
|
+
}
|
|
266
|
+
FramingError::HeadersTooLarge => UpstreamError::ResponseHeadersTooLarge,
|
|
267
|
+
_ => UpstreamError::RelayAuthFailed("malformed preflight response".to_owned()),
|
|
268
|
+
})?;
|
|
269
|
+
if resp.status != 204 {
|
|
270
|
+
return Err(UpstreamError::RelayAuthFailed(format!(
|
|
271
|
+
"preflight returned status {}",
|
|
272
|
+
resp.status
|
|
273
|
+
)));
|
|
274
|
+
}
|
|
275
|
+
if let Some(cl) = resp.header("content-length")
|
|
276
|
+
&& cl.trim() != "0"
|
|
277
|
+
{
|
|
278
|
+
return Err(UpstreamError::RelayAuthFailed(
|
|
279
|
+
"preflight 204 carried non-zero content-length".to_owned(),
|
|
280
|
+
));
|
|
281
|
+
}
|
|
282
|
+
if resp.header("transfer-encoding").is_some() {
|
|
283
|
+
return Err(UpstreamError::RelayAuthFailed(
|
|
284
|
+
"preflight 204 carried transfer-encoding".to_owned(),
|
|
285
|
+
));
|
|
286
|
+
}
|
|
287
|
+
if !relay_proof_matches(
|
|
288
|
+
resp.header(LINK_RELAY_PROOF_HEADER),
|
|
289
|
+
Some(&challenge.expected_listener_proof),
|
|
290
|
+
) {
|
|
291
|
+
return Err(UpstreamError::RelayAuthFailed(
|
|
292
|
+
"listener HMAC proof mismatch".to_owned(),
|
|
293
|
+
));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let mut verified_gen = None;
|
|
297
|
+
if let (Some(gen_val), Some(gen_proof)) = (
|
|
298
|
+
resp.header(LINK_RELAY_CONNECTION_HEADER),
|
|
299
|
+
resp.header(LINK_RELAY_CONNECTION_PROOF_HEADER),
|
|
300
|
+
) && is_valid_connection_generation(gen_val)
|
|
301
|
+
{
|
|
302
|
+
let expected_conn = compute_connection_proof(
|
|
303
|
+
&creds.token_fingerprint,
|
|
304
|
+
&creds.api_key_id,
|
|
305
|
+
&creds.link_id,
|
|
306
|
+
&challenge.nonce,
|
|
307
|
+
gen_val,
|
|
308
|
+
);
|
|
309
|
+
if relay_proof_matches(Some(gen_proof), expected_conn.as_deref()) {
|
|
310
|
+
verified_gen = Some(gen_val.to_owned());
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
Ok(AuthenticatedUpstream {
|
|
315
|
+
reader,
|
|
316
|
+
writer,
|
|
317
|
+
nonce: challenge.nonce,
|
|
318
|
+
connection_generation: verified_gen,
|
|
319
|
+
})
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/// Request head carrying the link token, built in one allocation of its final size so no
|
|
323
|
+
/// partial copy is left behind, and wiped on drop.
|
|
324
|
+
fn token_request_head(
|
|
325
|
+
before: &str,
|
|
326
|
+
auth_kind: RelayAuthKind,
|
|
327
|
+
token: &[u8],
|
|
328
|
+
after: &str,
|
|
329
|
+
) -> SecretBytes {
|
|
330
|
+
let name: &[u8] = match auth_kind {
|
|
331
|
+
RelayAuthKind::ApiKeyHeader => b"x-opencodex-api-key: ",
|
|
332
|
+
RelayAuthKind::Bearer => b"Authorization: Bearer ",
|
|
333
|
+
};
|
|
334
|
+
let mut head = Vec::with_capacity(before.len() + name.len() + token.len() + 2 + after.len());
|
|
335
|
+
head.extend_from_slice(before.as_bytes());
|
|
336
|
+
head.extend_from_slice(name);
|
|
337
|
+
head.extend_from_slice(token);
|
|
338
|
+
head.extend_from_slice(b"\r\n");
|
|
339
|
+
head.extend_from_slice(after.as_bytes());
|
|
340
|
+
SecretBytes::new(head)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
pub fn fetch_home_authenticated(
|
|
344
|
+
tunnel_port: u16,
|
|
345
|
+
creds: &RelayCredentials,
|
|
346
|
+
method: &str,
|
|
347
|
+
target: &str,
|
|
348
|
+
auth_kind: RelayAuthKind,
|
|
349
|
+
body: Option<(&str, &[u8])>,
|
|
350
|
+
max_bytes: u64,
|
|
351
|
+
) -> Result<(HttpResponseHead, Vec<u8>, Option<String>), UpstreamError> {
|
|
352
|
+
let limits = RequestLimits {
|
|
353
|
+
deadline: Some(Instant::now() + FETCH_DEADLINE),
|
|
354
|
+
cancel: None,
|
|
355
|
+
};
|
|
356
|
+
fetch_home_with_limits(
|
|
357
|
+
tunnel_port,
|
|
358
|
+
creds,
|
|
359
|
+
method,
|
|
360
|
+
target,
|
|
361
|
+
auth_kind,
|
|
362
|
+
body,
|
|
363
|
+
max_bytes,
|
|
364
|
+
limits,
|
|
365
|
+
)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#[allow(clippy::too_many_arguments)]
|
|
369
|
+
fn fetch_home_with_limits(
|
|
370
|
+
tunnel_port: u16,
|
|
371
|
+
creds: &RelayCredentials,
|
|
372
|
+
method: &str,
|
|
373
|
+
target: &str,
|
|
374
|
+
auth_kind: RelayAuthKind,
|
|
375
|
+
body: Option<(&str, &[u8])>,
|
|
376
|
+
max_bytes: u64,
|
|
377
|
+
limits: RequestLimits<'_>,
|
|
378
|
+
) -> Result<(HttpResponseHead, Vec<u8>, Option<String>), UpstreamError> {
|
|
379
|
+
let result = fetch_home_inner(
|
|
380
|
+
tunnel_port,
|
|
381
|
+
creds,
|
|
382
|
+
method,
|
|
383
|
+
target,
|
|
384
|
+
auth_kind,
|
|
385
|
+
body,
|
|
386
|
+
max_bytes,
|
|
387
|
+
limits,
|
|
388
|
+
);
|
|
389
|
+
if let Some(cancel) = limits.cancel {
|
|
390
|
+
cancel.clear();
|
|
391
|
+
}
|
|
392
|
+
result
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
#[allow(clippy::too_many_arguments)]
|
|
396
|
+
fn fetch_home_inner(
|
|
397
|
+
tunnel_port: u16,
|
|
398
|
+
creds: &RelayCredentials,
|
|
399
|
+
method: &str,
|
|
400
|
+
target: &str,
|
|
401
|
+
auth_kind: RelayAuthKind,
|
|
402
|
+
body: Option<(&str, &[u8])>,
|
|
403
|
+
max_bytes: u64,
|
|
404
|
+
limits: RequestLimits<'_>,
|
|
405
|
+
) -> Result<(HttpResponseHead, Vec<u8>, Option<String>), UpstreamError> {
|
|
406
|
+
let mut upstream = open_authenticated_upstream_with(tunnel_port, creds, false, limits)?;
|
|
407
|
+
upstream.reader.get_mut().per_read = FETCH_READ_TIMEOUT;
|
|
408
|
+
let _ = upstream
|
|
409
|
+
.writer
|
|
410
|
+
.set_write_timeout(Some(limits.remaining(FETCH_READ_TIMEOUT)?));
|
|
411
|
+
std::str::from_utf8(creds.raw_token.as_slice())
|
|
412
|
+
.map_err(|_| UpstreamError::RelayAuthFailed("invalid utf8 token".to_owned()))?;
|
|
413
|
+
let before = format!(
|
|
414
|
+
"{method} {target} HTTP/1.1\r\nHost: 127.0.0.1:{tunnel_port}\r\nAccept-Encoding: identity\r\nConnection: close\r\n{LINK_RELAY_SESSION_HEADER}: {}\r\n",
|
|
415
|
+
upstream.nonce
|
|
416
|
+
);
|
|
417
|
+
let mut after = String::new();
|
|
418
|
+
if let Some((ct, payload)) = body {
|
|
419
|
+
after.push_str(&format!(
|
|
420
|
+
"Content-Type: {ct}\r\nContent-Length: {}\r\n",
|
|
421
|
+
payload.len()
|
|
422
|
+
));
|
|
423
|
+
}
|
|
424
|
+
after.push_str("\r\n");
|
|
425
|
+
let head = token_request_head(&before, auth_kind, creds.raw_token.as_slice(), &after);
|
|
426
|
+
upstream
|
|
427
|
+
.writer
|
|
428
|
+
.write_all(head.as_slice())
|
|
429
|
+
.map_err(|e| UpstreamError::HomeUnreachable(format!("request write failed: {e}")))?;
|
|
430
|
+
if let Some((_, payload)) = body {
|
|
431
|
+
upstream
|
|
432
|
+
.writer
|
|
433
|
+
.write_all(payload)
|
|
434
|
+
.map_err(|e| UpstreamError::HomeUnreachable(format!("body write failed: {e}")))?;
|
|
435
|
+
}
|
|
436
|
+
upstream
|
|
437
|
+
.writer
|
|
438
|
+
.flush()
|
|
439
|
+
.map_err(|e| UpstreamError::HomeUnreachable(format!("flush failed: {e}")))?;
|
|
440
|
+
|
|
441
|
+
let resp_head = read_response_head(&mut upstream.reader).map_err(|e| match e {
|
|
442
|
+
FramingError::HeadersTooLarge => UpstreamError::ResponseHeadersTooLarge,
|
|
443
|
+
_ => UpstreamError::HomeUnreachable("failed reading Home response head".to_owned()),
|
|
444
|
+
})?;
|
|
445
|
+
let framing = response_framing(resp_head.status, method, &resp_head.headers)
|
|
446
|
+
.map_err(|_| UpstreamError::BadUpstreamResponse("invalid response framing".to_owned()))?;
|
|
447
|
+
let resp_body = read_bounded_body(&mut upstream.reader, framing, max_bytes).map_err(|e| {
|
|
448
|
+
if e.to_string().contains("body_too_large") {
|
|
449
|
+
UpstreamError::ResponseBodyTooLarge
|
|
450
|
+
} else {
|
|
451
|
+
UpstreamError::BadUpstreamResponse(format!("body read failed: {e}"))
|
|
452
|
+
}
|
|
453
|
+
})?;
|
|
454
|
+
Ok((resp_head, resp_body, upstream.connection_generation))
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
pub fn post_child_parity_report(
|
|
458
|
+
tunnel_port: u16,
|
|
459
|
+
creds: &RelayCredentials,
|
|
460
|
+
report: &LinkChildParityReport,
|
|
461
|
+
) -> Result<u16, String> {
|
|
462
|
+
post_parity_with(tunnel_port, creds, report, None)
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
fn post_parity_with(
|
|
466
|
+
tunnel_port: u16,
|
|
467
|
+
creds: &RelayCredentials,
|
|
468
|
+
report: &LinkChildParityReport,
|
|
469
|
+
cancel: Option<&CancelSlot>,
|
|
470
|
+
) -> Result<u16, String> {
|
|
471
|
+
let json_bytes = report.to_bounded_json()?;
|
|
472
|
+
let limits = RequestLimits {
|
|
473
|
+
deadline: Some(Instant::now() + PARITY_DEADLINE),
|
|
474
|
+
cancel,
|
|
475
|
+
};
|
|
476
|
+
let (head, _, _) = fetch_home_with_limits(
|
|
477
|
+
tunnel_port,
|
|
478
|
+
creds,
|
|
479
|
+
"POST",
|
|
480
|
+
"/v1/link-management/child-parity",
|
|
481
|
+
RelayAuthKind::Bearer,
|
|
482
|
+
Some(("application/json", &json_bytes)),
|
|
483
|
+
1024,
|
|
484
|
+
limits,
|
|
485
|
+
)
|
|
486
|
+
.map_err(|e| format!("{e:?}"))?;
|
|
487
|
+
Ok(head.status)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
struct SharedServerState {
|
|
491
|
+
paths: ChildPaths,
|
|
492
|
+
listen_port: u16,
|
|
493
|
+
started_instant: Instant,
|
|
494
|
+
started_at_ms: u64,
|
|
495
|
+
started_at_iso: String,
|
|
496
|
+
stop_flag: AtomicBool,
|
|
497
|
+
stop_cv: (Mutex<bool>, Condvar),
|
|
498
|
+
last_generation: Mutex<Option<String>>,
|
|
499
|
+
/// False until the lifecycle parent commits this generation; only `/healthz` is
|
|
500
|
+
/// answered before that.
|
|
501
|
+
committed: Arc<AtomicBool>,
|
|
502
|
+
/// In-flight parity request, aborted by shutdown so the parity thread ends promptly.
|
|
503
|
+
parity_cancel: CancelSlot,
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
impl SharedServerState {
|
|
507
|
+
fn load_live_creds(&self) -> Result<RelayCredentials, &'static str> {
|
|
508
|
+
let state = match read_child_state(&self.paths.state_file) {
|
|
509
|
+
StateReadOutcome::Valid(s) => s,
|
|
510
|
+
_ => return Err("link_credential_unavailable"),
|
|
511
|
+
};
|
|
512
|
+
let raw_token = read_verified_link_key(&self.paths.key_file, &state.token_fingerprint)
|
|
513
|
+
.map_err(|_| "link_credential_unavailable")?;
|
|
514
|
+
Ok(RelayCredentials {
|
|
515
|
+
api_key_id: state.api_key_id,
|
|
516
|
+
link_id: state.link_id,
|
|
517
|
+
token_fingerprint: state.token_fingerprint,
|
|
518
|
+
raw_token,
|
|
519
|
+
})
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
fn signal_shutdown(&self) {
|
|
523
|
+
self.stop_flag.store(true, Ordering::SeqCst);
|
|
524
|
+
self.parity_cancel.cancel();
|
|
525
|
+
if let Ok(mut guard) = self.stop_cv.0.lock() {
|
|
526
|
+
*guard = true;
|
|
527
|
+
self.stop_cv.1.notify_all();
|
|
528
|
+
}
|
|
529
|
+
// Wake blocking TcpListener::accept()
|
|
530
|
+
let _ = TcpStream::connect_timeout(
|
|
531
|
+
&SocketAddr::from(([127, 0, 0, 1], self.listen_port)),
|
|
532
|
+
Duration::from_millis(100),
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/// Watch stdin for the parent's commit line. Before the commit, EOF, a read error, or any
|
|
538
|
+
/// other line means the parent died or abandoned this generation: exit without serving.
|
|
539
|
+
pub fn spawn_commit_watcher(generation: String) -> Arc<AtomicBool> {
|
|
540
|
+
let committed = Arc::new(AtomicBool::new(false));
|
|
541
|
+
let flag = Arc::clone(&committed);
|
|
542
|
+
thread::spawn(move || {
|
|
543
|
+
let expected = format!("commit {generation}\n");
|
|
544
|
+
let mut line = Vec::with_capacity(expected.len());
|
|
545
|
+
let read = std::io::stdin()
|
|
546
|
+
.lock()
|
|
547
|
+
.take(expected.len() as u64 + 1)
|
|
548
|
+
.read_until(b'\n', &mut line);
|
|
549
|
+
if read.is_ok() && line == expected.as_bytes() {
|
|
550
|
+
flag.store(true, Ordering::SeqCst);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
std::process::exit(3);
|
|
554
|
+
});
|
|
555
|
+
committed
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/// Serve a direct foreground relay started by the operator (no lifecycle parent).
|
|
559
|
+
pub fn serve_foreground(paths: ChildPaths, listen_port: u16) -> Result<(), String> {
|
|
560
|
+
serve_foreground_inner(paths, listen_port, None)
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/// Serve a relay started by a lifecycle parent: refuse unless state still records
|
|
564
|
+
/// `generation`, and answer only `/healthz` until `committed` is set.
|
|
565
|
+
pub fn serve_foreground_lifecycle(
|
|
566
|
+
paths: ChildPaths,
|
|
567
|
+
listen_port: u16,
|
|
568
|
+
generation: &str,
|
|
569
|
+
committed: Arc<AtomicBool>,
|
|
570
|
+
) -> Result<(), String> {
|
|
571
|
+
serve_foreground_inner(paths, listen_port, Some((generation, committed)))
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
fn serve_foreground_inner(
|
|
575
|
+
paths: ChildPaths,
|
|
576
|
+
listen_port: u16,
|
|
577
|
+
lifecycle: Option<(&str, Arc<AtomicBool>)>,
|
|
578
|
+
) -> Result<(), String> {
|
|
579
|
+
let startup_gate = FileLock::acquire_blocking(&paths.child_dir.join("daemon-start.lock"))
|
|
580
|
+
.map_err(|_| "serve refused: daemon startup gate unavailable".to_owned())?;
|
|
581
|
+
let listener = TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], listen_port)))
|
|
582
|
+
.map_err(|e| format!("failed to bind 127.0.0.1:{listen_port}: {e}"))?;
|
|
583
|
+
let state = match read_child_state(&paths.state_file) {
|
|
584
|
+
StateReadOutcome::Valid(state)
|
|
585
|
+
if state.pending_operation.is_none() && state.listen_port == listen_port =>
|
|
586
|
+
{
|
|
587
|
+
state
|
|
588
|
+
}
|
|
589
|
+
_ => return Err("serve refused: child state changed during startup".to_owned()),
|
|
590
|
+
};
|
|
591
|
+
if let Some((generation, _)) = &lifecycle
|
|
592
|
+
&& state.daemon_generation.as_deref() != Some(*generation)
|
|
593
|
+
{
|
|
594
|
+
return Err("serve refused: this daemon generation is not the recorded one".to_owned());
|
|
595
|
+
}
|
|
596
|
+
let committed = match lifecycle {
|
|
597
|
+
Some((_, committed)) => committed,
|
|
598
|
+
None => Arc::new(AtomicBool::new(true)),
|
|
599
|
+
};
|
|
600
|
+
if !matches!(validate_existing_journal_paths(&paths), Ok(true))
|
|
601
|
+
|| read_verified_link_key(&paths.key_file, &state.token_fingerprint).is_err()
|
|
602
|
+
{
|
|
603
|
+
return Err("serve refused: child recovery state changed during startup".to_owned());
|
|
604
|
+
}
|
|
605
|
+
drop(startup_gate);
|
|
606
|
+
let actual_port = listener
|
|
607
|
+
.local_addr()
|
|
608
|
+
.map_err(|e| format!("local_addr failed: {e}"))?
|
|
609
|
+
.port();
|
|
610
|
+
let started_at_ms = now_epoch_ms();
|
|
611
|
+
let shared = Arc::new(SharedServerState {
|
|
612
|
+
paths,
|
|
613
|
+
listen_port: actual_port,
|
|
614
|
+
started_instant: Instant::now(),
|
|
615
|
+
started_at_ms,
|
|
616
|
+
started_at_iso: format_iso8601_utc(started_at_ms),
|
|
617
|
+
stop_flag: AtomicBool::new(false),
|
|
618
|
+
stop_cv: (Mutex::new(false), Condvar::new()),
|
|
619
|
+
last_generation: Mutex::new(None),
|
|
620
|
+
committed,
|
|
621
|
+
parity_cancel: CancelSlot::default(),
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
let parity_shared = Arc::clone(&shared);
|
|
625
|
+
let parity_handle = thread::spawn(move || run_parity_loop(parity_shared));
|
|
626
|
+
let sem = Arc::new((Mutex::new(0usize), Condvar::new()));
|
|
627
|
+
let control_active = Arc::new(AtomicUsize::new(0));
|
|
628
|
+
|
|
629
|
+
for incoming in listener.incoming() {
|
|
630
|
+
if shared.stop_flag.load(Ordering::SeqCst) {
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
633
|
+
let Ok(client_stream) = incoming else {
|
|
634
|
+
continue;
|
|
635
|
+
};
|
|
636
|
+
if shared.stop_flag.load(Ordering::SeqCst) {
|
|
637
|
+
break;
|
|
638
|
+
}
|
|
639
|
+
let sem_clone = Arc::clone(&sem);
|
|
640
|
+
let admitted = {
|
|
641
|
+
let (lock, _) = &*sem_clone;
|
|
642
|
+
let mut active = lock.lock().unwrap_or_else(|e| e.into_inner());
|
|
643
|
+
if *active < MAX_CONCURRENT_WORKERS {
|
|
644
|
+
*active += 1;
|
|
645
|
+
true
|
|
646
|
+
} else {
|
|
647
|
+
false
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
if !admitted {
|
|
651
|
+
// Every worker is busy. Health checks and the authenticated shutdown still get a
|
|
652
|
+
// short-lived control thread, so a full worker pool cannot keep the shutdown
|
|
653
|
+
// connection from being handled. A control connection holds its slot for at most
|
|
654
|
+
// `CONTROL_DEADLINE` per request head; past `MAX_CONTROL_CONNECTIONS` concurrent
|
|
655
|
+
// ones, a new connection is closed unanswered.
|
|
656
|
+
if control_active.fetch_add(1, Ordering::SeqCst) >= MAX_CONTROL_CONNECTIONS {
|
|
657
|
+
control_active.fetch_sub(1, Ordering::SeqCst);
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
let control_shared = Arc::clone(&shared);
|
|
661
|
+
let control_count = Arc::clone(&control_active);
|
|
662
|
+
thread::spawn(move || {
|
|
663
|
+
let _ = handle_client_connection(
|
|
664
|
+
client_stream,
|
|
665
|
+
&control_shared,
|
|
666
|
+
ConnectionMode::ControlOnly,
|
|
667
|
+
);
|
|
668
|
+
control_count.fetch_sub(1, Ordering::SeqCst);
|
|
669
|
+
});
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
let worker_shared = Arc::clone(&shared);
|
|
673
|
+
thread::spawn(move || {
|
|
674
|
+
let _ = handle_client_connection(client_stream, &worker_shared, ConnectionMode::Full);
|
|
675
|
+
let (lock, cv) = &*sem_clone;
|
|
676
|
+
let mut active = lock.lock().unwrap_or_else(|e| e.into_inner());
|
|
677
|
+
*active = active.saturating_sub(1);
|
|
678
|
+
cv.notify_one();
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
shared.signal_shutdown();
|
|
683
|
+
let _ = parity_handle.join();
|
|
684
|
+
Ok(())
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
fn run_parity_loop(shared: Arc<SharedServerState>) {
|
|
688
|
+
let backoffs_ms: [u64; 5] = [2_000, 5_000, 15_000, 30_000, 60_000];
|
|
689
|
+
let mut failures = 0usize;
|
|
690
|
+
let mut local_seq: u64 = 0;
|
|
691
|
+
// Immediate initial attempt after short 250ms settle so Home sees parity quickly
|
|
692
|
+
let mut wait_ms = 250u64;
|
|
693
|
+
loop {
|
|
694
|
+
{
|
|
695
|
+
let (lock, cv) = &shared.stop_cv;
|
|
696
|
+
let guard = lock.lock().unwrap_or_else(|e| e.into_inner());
|
|
697
|
+
let (guard, _) = cv
|
|
698
|
+
.wait_timeout(guard, Duration::from_millis(wait_ms))
|
|
699
|
+
.unwrap_or_else(|e| e.into_inner());
|
|
700
|
+
if *guard || shared.stop_flag.load(Ordering::SeqCst) {
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
if !shared.committed.load(Ordering::SeqCst) {
|
|
705
|
+
wait_ms = 50;
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
let StateReadOutcome::Valid(st) = read_child_state(&shared.paths.state_file) else {
|
|
709
|
+
wait_ms = 5_000;
|
|
710
|
+
continue;
|
|
711
|
+
};
|
|
712
|
+
let Ok(creds) = shared.load_live_creds() else {
|
|
713
|
+
wait_ms = 5_000;
|
|
714
|
+
continue;
|
|
715
|
+
};
|
|
716
|
+
local_seq = local_seq.max(st.parity_sequence).saturating_add(1);
|
|
717
|
+
let (last_apply_at, digest) = match &st.applied_settings {
|
|
718
|
+
Some(rec) => (Some(rec.applied_at_ms), Some(rec.digest.clone())),
|
|
719
|
+
None => (None, None),
|
|
720
|
+
};
|
|
721
|
+
let report = LinkChildParityReport {
|
|
722
|
+
link_id: st.link_id,
|
|
723
|
+
last_apply_at,
|
|
724
|
+
runtime_started_at: shared.started_at_ms,
|
|
725
|
+
applied_codex_settings_digest: digest,
|
|
726
|
+
sequence: local_seq,
|
|
727
|
+
};
|
|
728
|
+
match post_parity_with(st.tunnel_port, &creds, &report, Some(&shared.parity_cancel)) {
|
|
729
|
+
Ok(204) => {
|
|
730
|
+
failures = 0;
|
|
731
|
+
wait_ms = 60_000;
|
|
732
|
+
}
|
|
733
|
+
Ok(404) => return, // Home does not support child-parity
|
|
734
|
+
_ => {
|
|
735
|
+
let idx = failures.min(backoffs_ms.len() - 1);
|
|
736
|
+
wait_ms = backoffs_ms[idx];
|
|
737
|
+
failures += 1;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
744
|
+
enum ConnectionMode {
|
|
745
|
+
Full,
|
|
746
|
+
/// Accepted while every worker is busy, with short timeouts and a request-head deadline:
|
|
747
|
+
/// `/healthz` and the authenticated shutdown only.
|
|
748
|
+
ControlOnly,
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
fn handle_client_connection(
|
|
752
|
+
stream: TcpStream,
|
|
753
|
+
shared: &SharedServerState,
|
|
754
|
+
mode: ConnectionMode,
|
|
755
|
+
) -> std::io::Result<()> {
|
|
756
|
+
let _ = stream.set_nodelay(true);
|
|
757
|
+
let head_deadline = if mode == ConnectionMode::ControlOnly {
|
|
758
|
+
let _ = stream.set_read_timeout(Some(CONTROL_TIMEOUT));
|
|
759
|
+
let _ = stream.set_write_timeout(Some(CONTROL_TIMEOUT));
|
|
760
|
+
// One deadline covers the whole control exchange, including the shutdown proof frame.
|
|
761
|
+
Some(Instant::now() + CONTROL_DEADLINE)
|
|
762
|
+
} else {
|
|
763
|
+
let _ = stream.set_read_timeout(Some(Duration::from_secs(30)));
|
|
764
|
+
let _ = stream.set_write_timeout(Some(HEADER_AND_SSE_TIMEOUT));
|
|
765
|
+
None
|
|
766
|
+
};
|
|
767
|
+
let mut writer = stream.try_clone()?;
|
|
768
|
+
let mut reader = BufReader::new(DeadlineStream::new(stream, head_deadline, CONTROL_TIMEOUT));
|
|
769
|
+
|
|
770
|
+
let req = match read_request_head(&mut reader) {
|
|
771
|
+
Ok(r) => r,
|
|
772
|
+
Err(FramingError::Eof) => return Ok(()),
|
|
773
|
+
Err(FramingError::HeadersTooLarge) => {
|
|
774
|
+
return write_json_response(
|
|
775
|
+
&mut writer,
|
|
776
|
+
431,
|
|
777
|
+
"Request Header Fields Too Large",
|
|
778
|
+
&[],
|
|
779
|
+
br#"{"error":"link relay request headers too large"}"#,
|
|
780
|
+
);
|
|
781
|
+
}
|
|
782
|
+
Err(_) => {
|
|
783
|
+
return write_json_response(
|
|
784
|
+
&mut writer,
|
|
785
|
+
400,
|
|
786
|
+
"Bad Request",
|
|
787
|
+
&[],
|
|
788
|
+
br#"{"error":"link relay request headers refused"}"#,
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
let decision = classify_child_ingress(
|
|
794
|
+
&req.method,
|
|
795
|
+
&req.path,
|
|
796
|
+
&req.query,
|
|
797
|
+
req.header("host"),
|
|
798
|
+
req.header("origin"),
|
|
799
|
+
req.header("upgrade"),
|
|
800
|
+
);
|
|
801
|
+
let committed = shared.committed.load(Ordering::SeqCst);
|
|
802
|
+
if mode == ConnectionMode::ControlOnly
|
|
803
|
+
&& !matches!(
|
|
804
|
+
decision,
|
|
805
|
+
RouteDecision::LocalHealthz | RouteDecision::LocalShutdown
|
|
806
|
+
)
|
|
807
|
+
{
|
|
808
|
+
return write_json_response(
|
|
809
|
+
&mut writer,
|
|
810
|
+
503,
|
|
811
|
+
"Service Unavailable",
|
|
812
|
+
&[("Retry-After", "1"), ("Cache-Control", "no-store")],
|
|
813
|
+
br#"{"error":"child_busy"}"#,
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
if !committed && decision != RouteDecision::LocalHealthz {
|
|
817
|
+
return write_json_response(
|
|
818
|
+
&mut writer,
|
|
819
|
+
503,
|
|
820
|
+
"Service Unavailable",
|
|
821
|
+
&[("Retry-After", "1"), ("Cache-Control", "no-store")],
|
|
822
|
+
br#"{"error":"child_starting"}"#,
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
match decision {
|
|
827
|
+
RouteDecision::LocalShutdown => {
|
|
828
|
+
let valid_frame = |frame: &HttpRequestHead| {
|
|
829
|
+
frame.method == "POST"
|
|
830
|
+
&& frame.path == "/__ocx_child/shutdown"
|
|
831
|
+
&& frame.query.is_empty()
|
|
832
|
+
&& frame.header("origin").is_none()
|
|
833
|
+
&& frame.header("upgrade").is_none()
|
|
834
|
+
&& frame.headers.iter().filter(|(k, _)| k.eq_ignore_ascii_case("host")).count() == 1
|
|
835
|
+
&& frame.header("host") == Some(format!("127.0.0.1:{}", shared.listen_port).as_str())
|
|
836
|
+
&& validate_and_sanitize_request_headers(&frame.headers)
|
|
837
|
+
.is_ok_and(|h| h.framing == BodyFraming::ContentLength(0))
|
|
838
|
+
};
|
|
839
|
+
let challenge_count = req.headers.iter()
|
|
840
|
+
.filter(|(k, _)| k.eq_ignore_ascii_case("x-ocx-child-shutdown-challenge"))
|
|
841
|
+
.count();
|
|
842
|
+
let Some(nonce) = req.header("x-ocx-child-shutdown-challenge") else {
|
|
843
|
+
return write_json_response(&mut writer, 404, "Not Found", &[], br#"{"error":"not_found"}"#);
|
|
844
|
+
};
|
|
845
|
+
let StateReadOutcome::Valid(state) = read_child_state(&shared.paths.state_file) else {
|
|
846
|
+
return write_json_response(&mut writer, 404, "Not Found", &[], br#"{"error":"not_found"}"#);
|
|
847
|
+
};
|
|
848
|
+
if !valid_frame(&req) || challenge_count != 1 || !crate::relay_auth::is_valid_hex64(nonce) {
|
|
849
|
+
return write_json_response(&mut writer, 404, "Not Found", &[], br#"{"error":"not_found"}"#);
|
|
850
|
+
}
|
|
851
|
+
let Ok(server_nonce) = random_hex(32) else {
|
|
852
|
+
return write_json_response(&mut writer, 503, "Service Unavailable", &[], br#"{"error":"unavailable"}"#);
|
|
853
|
+
};
|
|
854
|
+
let binding = format!("{nonce}{server_nonce}");
|
|
855
|
+
let Some(listener_proof) = compute_child_shutdown_proof(&state.shutdown_secret, "listener", &binding) else {
|
|
856
|
+
return write_json_response(&mut writer, 404, "Not Found", &[], br#"{"error":"not_found"}"#);
|
|
857
|
+
};
|
|
858
|
+
let Some(caller_proof) = compute_child_shutdown_proof(&state.shutdown_secret, "caller", &binding) else {
|
|
859
|
+
return Ok(());
|
|
860
|
+
};
|
|
861
|
+
write!(
|
|
862
|
+
writer,
|
|
863
|
+
"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nx-ocx-child-shutdown-nonce: {server_nonce}\r\nx-ocx-child-shutdown-proof: {listener_proof}\r\nConnection: keep-alive\r\n\r\n"
|
|
864
|
+
)?;
|
|
865
|
+
writer.flush()?;
|
|
866
|
+
let Ok(next) = read_request_head(&mut reader) else { return Ok(()) };
|
|
867
|
+
let proof_count = next.headers.iter()
|
|
868
|
+
.filter(|(k, _)| k.eq_ignore_ascii_case("x-ocx-child-shutdown-proof"))
|
|
869
|
+
.count();
|
|
870
|
+
let still_current = match read_child_state(&shared.paths.state_file) {
|
|
871
|
+
StateReadOutcome::Valid(current) => constant_time_eq(
|
|
872
|
+
current.shutdown_secret.as_bytes(),
|
|
873
|
+
state.shutdown_secret.as_bytes(),
|
|
874
|
+
),
|
|
875
|
+
_ => false,
|
|
876
|
+
};
|
|
877
|
+
if valid_frame(&next)
|
|
878
|
+
&& proof_count == 1
|
|
879
|
+
&& still_current
|
|
880
|
+
&& next.header("x-ocx-child-shutdown-proof")
|
|
881
|
+
.is_some_and(|proof| constant_time_eq(proof.as_bytes(), caller_proof.as_bytes()))
|
|
882
|
+
{
|
|
883
|
+
let _ = write_json_response(&mut writer, 200, "OK", &[], br#"{"ok":true}"#);
|
|
884
|
+
shared.signal_shutdown();
|
|
885
|
+
return Ok(());
|
|
886
|
+
}
|
|
887
|
+
write_json_response(&mut writer, 404, "Not Found", &[], br#"{"error":"not_found"}"#)
|
|
888
|
+
}
|
|
889
|
+
RouteDecision::LocalHealthz => {
|
|
890
|
+
let body = serde_json::to_vec(&serde_json::json!({
|
|
891
|
+
"service": "opencodex",
|
|
892
|
+
"version": OCX_PROTOCOL_COMPAT_VERSION,
|
|
893
|
+
"role": "client",
|
|
894
|
+
"ephemeralChild": true,
|
|
895
|
+
"uptime": shared.started_instant.elapsed().as_secs_f64(),
|
|
896
|
+
"pid": std::process::id(),
|
|
897
|
+
"port": shared.listen_port,
|
|
898
|
+
"startedAt": shared.started_at_iso,
|
|
899
|
+
"generationCommitted": committed,
|
|
900
|
+
}))
|
|
901
|
+
.unwrap_or_default();
|
|
902
|
+
write_json_response(&mut writer, 200, "OK", &[], &body)
|
|
903
|
+
}
|
|
904
|
+
RouteDecision::LocalReadyz => handle_local_readyz(&mut writer, shared),
|
|
905
|
+
RouteDecision::Reject403Origin => write_json_response(
|
|
906
|
+
&mut writer,
|
|
907
|
+
403,
|
|
908
|
+
"Forbidden",
|
|
909
|
+
&[],
|
|
910
|
+
br#"{"error":{"message":"cross-origin data-plane request blocked","type":"invalid_request_error","code":"origin_rejected"}}"#,
|
|
911
|
+
),
|
|
912
|
+
RouteDecision::Reject426Websocket => write_json_response(
|
|
913
|
+
&mut writer,
|
|
914
|
+
426,
|
|
915
|
+
"Upgrade Required",
|
|
916
|
+
&[],
|
|
917
|
+
br#"{"error":{"message":"Responses WebSocket transport is disabled; use HTTP","type":"invalid_request_error","code":"upgrade_required"}}"#,
|
|
918
|
+
),
|
|
919
|
+
RouteDecision::Reject404 => {
|
|
920
|
+
write_json_response(&mut writer, 404, "Not Found", &[], br#"{"error":"not_found"}"#)
|
|
921
|
+
}
|
|
922
|
+
RouteDecision::RelayData { auth } => {
|
|
923
|
+
relay_request(&mut reader, &mut writer, &req, shared, auth, LINK_RELAY_BODY_MAX_BYTES)
|
|
924
|
+
}
|
|
925
|
+
RouteDecision::RelayManagement => relay_request(
|
|
926
|
+
&mut reader,
|
|
927
|
+
&mut writer,
|
|
928
|
+
&req,
|
|
929
|
+
shared,
|
|
930
|
+
RelayAuthKind::Bearer,
|
|
931
|
+
HUB_RELAY_RESPONSE_BODY_MAX_BYTES,
|
|
932
|
+
),
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
fn handle_local_readyz<W: Write>(
|
|
937
|
+
writer: &mut W,
|
|
938
|
+
shared: &SharedServerState,
|
|
939
|
+
) -> std::io::Result<()> {
|
|
940
|
+
let uptime = shared.started_instant.elapsed().as_secs_f64();
|
|
941
|
+
let pid = std::process::id();
|
|
942
|
+
let port = shared.listen_port;
|
|
943
|
+
let StateReadOutcome::Valid(st) = read_child_state(&shared.paths.state_file) else {
|
|
944
|
+
let body = serde_json::to_vec(&serde_json::json!({
|
|
945
|
+
"service": "opencodex",
|
|
946
|
+
"version": OCX_PROTOCOL_COMPAT_VERSION,
|
|
947
|
+
"role": "client",
|
|
948
|
+
"status": "pending",
|
|
949
|
+
"uptime": uptime,
|
|
950
|
+
"pid": pid,
|
|
951
|
+
"port": port,
|
|
952
|
+
"protocolVersion": 1,
|
|
953
|
+
"link": { "state": "unavailable", "reason": "link_credential_unavailable" }
|
|
954
|
+
}))
|
|
955
|
+
.unwrap_or_default();
|
|
956
|
+
return write_json_response(
|
|
957
|
+
writer,
|
|
958
|
+
503,
|
|
959
|
+
"Service Unavailable",
|
|
960
|
+
&[("Retry-After", "1"), ("Cache-Control", "no-store")],
|
|
961
|
+
&body,
|
|
962
|
+
);
|
|
963
|
+
};
|
|
964
|
+
let Ok(creds) = shared.load_live_creds() else {
|
|
965
|
+
let body = serde_json::to_vec(&serde_json::json!({
|
|
966
|
+
"service": "opencodex",
|
|
967
|
+
"version": OCX_PROTOCOL_COMPAT_VERSION,
|
|
968
|
+
"role": "client",
|
|
969
|
+
"status": "pending",
|
|
970
|
+
"uptime": uptime,
|
|
971
|
+
"pid": pid,
|
|
972
|
+
"port": port,
|
|
973
|
+
"protocolVersion": 1,
|
|
974
|
+
"link": { "state": "unavailable", "reason": "link_credential_unavailable" }
|
|
975
|
+
}))
|
|
976
|
+
.unwrap_or_default();
|
|
977
|
+
return write_json_response(
|
|
978
|
+
writer,
|
|
979
|
+
503,
|
|
980
|
+
"Service Unavailable",
|
|
981
|
+
&[("Retry-After", "1"), ("Cache-Control", "no-store")],
|
|
982
|
+
&body,
|
|
983
|
+
);
|
|
984
|
+
};
|
|
985
|
+
match open_authenticated_upstream(st.tunnel_port, &creds, true) {
|
|
986
|
+
Ok(up) => {
|
|
987
|
+
if let Some(gen_val) = &up.connection_generation
|
|
988
|
+
&& let Ok(mut g) = shared.last_generation.lock()
|
|
989
|
+
{
|
|
990
|
+
*g = Some(gen_val.clone());
|
|
991
|
+
}
|
|
992
|
+
let link_obj = match up.connection_generation {
|
|
993
|
+
Some(g) => serde_json::json!({ "state": "available", "connectionGeneration": g }),
|
|
994
|
+
None => serde_json::json!({ "state": "available" }),
|
|
995
|
+
};
|
|
996
|
+
let body = serde_json::to_vec(&serde_json::json!({
|
|
997
|
+
"service": "opencodex",
|
|
998
|
+
"version": OCX_PROTOCOL_COMPAT_VERSION,
|
|
999
|
+
"role": "client",
|
|
1000
|
+
"status": "ready",
|
|
1001
|
+
"uptime": uptime,
|
|
1002
|
+
"pid": pid,
|
|
1003
|
+
"port": port,
|
|
1004
|
+
"protocolVersion": 1,
|
|
1005
|
+
"link": link_obj,
|
|
1006
|
+
}))
|
|
1007
|
+
.unwrap_or_default();
|
|
1008
|
+
write_json_response(writer, 200, "OK", &[], &body)
|
|
1009
|
+
}
|
|
1010
|
+
Err(err) => {
|
|
1011
|
+
let reason = match err {
|
|
1012
|
+
UpstreamError::RelayAuthFailed(_) => "link_auth_failed",
|
|
1013
|
+
_ => "home_unreachable",
|
|
1014
|
+
};
|
|
1015
|
+
let body = serde_json::to_vec(&serde_json::json!({
|
|
1016
|
+
"service": "opencodex",
|
|
1017
|
+
"version": OCX_PROTOCOL_COMPAT_VERSION,
|
|
1018
|
+
"role": "client",
|
|
1019
|
+
"status": "pending",
|
|
1020
|
+
"uptime": uptime,
|
|
1021
|
+
"pid": pid,
|
|
1022
|
+
"port": port,
|
|
1023
|
+
"protocolVersion": 1,
|
|
1024
|
+
"link": { "state": "unavailable", "reason": reason }
|
|
1025
|
+
}))
|
|
1026
|
+
.unwrap_or_default();
|
|
1027
|
+
write_json_response(
|
|
1028
|
+
writer,
|
|
1029
|
+
503,
|
|
1030
|
+
"Service Unavailable",
|
|
1031
|
+
&[("Retry-After", "1"), ("Cache-Control", "no-store")],
|
|
1032
|
+
&body,
|
|
1033
|
+
)
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
fn relay_request(
|
|
1039
|
+
client_reader: &mut BufReader<DeadlineStream>,
|
|
1040
|
+
client_writer: &mut TcpStream,
|
|
1041
|
+
req: &HttpRequestHead,
|
|
1042
|
+
shared: &SharedServerState,
|
|
1043
|
+
auth_kind: RelayAuthKind,
|
|
1044
|
+
response_cap_bytes: u64,
|
|
1045
|
+
) -> std::io::Result<()> {
|
|
1046
|
+
let validated = match validate_and_sanitize_request_headers(&req.headers) {
|
|
1047
|
+
Ok(v) => v,
|
|
1048
|
+
Err(FramingError::HeadersTooLarge) => {
|
|
1049
|
+
return write_json_response(
|
|
1050
|
+
client_writer,
|
|
1051
|
+
431,
|
|
1052
|
+
"Request Header Fields Too Large",
|
|
1053
|
+
&[],
|
|
1054
|
+
br#"{"error":"link relay request headers too large"}"#,
|
|
1055
|
+
);
|
|
1056
|
+
}
|
|
1057
|
+
Err(_) => {
|
|
1058
|
+
return write_json_response(
|
|
1059
|
+
client_writer,
|
|
1060
|
+
400,
|
|
1061
|
+
"Bad Request",
|
|
1062
|
+
&[],
|
|
1063
|
+
br#"{"error":"link relay request headers refused"}"#,
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
|
|
1068
|
+
let req_body = if req.method == "GET" || req.method == "HEAD" {
|
|
1069
|
+
Vec::new()
|
|
1070
|
+
} else {
|
|
1071
|
+
let _ = client_reader
|
|
1072
|
+
.get_ref()
|
|
1073
|
+
.get_ref()
|
|
1074
|
+
.set_read_timeout(Some(HEADER_AND_SSE_TIMEOUT));
|
|
1075
|
+
match read_bounded_body(client_reader, validated.framing, LINK_RELAY_BODY_MAX_BYTES) {
|
|
1076
|
+
Ok(b) => b,
|
|
1077
|
+
Err(e) if e.to_string().contains("body_too_large") => {
|
|
1078
|
+
return write_json_response(
|
|
1079
|
+
client_writer,
|
|
1080
|
+
413,
|
|
1081
|
+
"Payload Too Large",
|
|
1082
|
+
&[],
|
|
1083
|
+
br#"{"error":"link relay request body too large"}"#,
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
Err(_) => {
|
|
1087
|
+
return write_json_response(
|
|
1088
|
+
client_writer,
|
|
1089
|
+
400,
|
|
1090
|
+
"Bad Request",
|
|
1091
|
+
&[],
|
|
1092
|
+
br#"{"error":"invalid request body"}"#,
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
};
|
|
1097
|
+
|
|
1098
|
+
let StateReadOutcome::Valid(st) = read_child_state(&shared.paths.state_file) else {
|
|
1099
|
+
return write_json_response(
|
|
1100
|
+
client_writer,
|
|
1101
|
+
503,
|
|
1102
|
+
"Service Unavailable",
|
|
1103
|
+
&[],
|
|
1104
|
+
br#"{"error":"link_credential_unavailable"}"#,
|
|
1105
|
+
);
|
|
1106
|
+
};
|
|
1107
|
+
let Ok(creds) = shared.load_live_creds() else {
|
|
1108
|
+
return write_json_response(
|
|
1109
|
+
client_writer,
|
|
1110
|
+
503,
|
|
1111
|
+
"Service Unavailable",
|
|
1112
|
+
&[],
|
|
1113
|
+
br#"{"error":"link_credential_unavailable"}"#,
|
|
1114
|
+
);
|
|
1115
|
+
};
|
|
1116
|
+
|
|
1117
|
+
let req_id = random_hex(16).unwrap_or_else(|_| "00000000000000000000000000000000".to_owned());
|
|
1118
|
+
let mut upstream = match open_authenticated_upstream(st.tunnel_port, &creds, false) {
|
|
1119
|
+
Ok(u) => u,
|
|
1120
|
+
Err(UpstreamError::RelayAuthFailed(_)) => {
|
|
1121
|
+
return write_json_response(
|
|
1122
|
+
client_writer,
|
|
1123
|
+
503,
|
|
1124
|
+
"Service Unavailable",
|
|
1125
|
+
&[
|
|
1126
|
+
("Retry-After", "1"),
|
|
1127
|
+
("x-opencodex-link-request-id", &req_id),
|
|
1128
|
+
],
|
|
1129
|
+
br#"{"error":"link relay authentication failed","code":"link_relay_auth_failed"}"#,
|
|
1130
|
+
);
|
|
1131
|
+
}
|
|
1132
|
+
Err(_) => {
|
|
1133
|
+
return write_json_response(
|
|
1134
|
+
client_writer,
|
|
1135
|
+
503,
|
|
1136
|
+
"Service Unavailable",
|
|
1137
|
+
&[
|
|
1138
|
+
("Retry-After", "1"),
|
|
1139
|
+
("x-opencodex-link-request-id", &req_id),
|
|
1140
|
+
],
|
|
1141
|
+
br#"{"error":"Home is unreachable over Remote Link","code":"home_unreachable","message":"Home is unreachable over Remote Link"}"#,
|
|
1142
|
+
);
|
|
1143
|
+
}
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
if let Some(gen_val) = &upstream.connection_generation
|
|
1147
|
+
&& let Ok(mut g) = shared.last_generation.lock()
|
|
1148
|
+
{
|
|
1149
|
+
*g = Some(gen_val.clone());
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
let _ = upstream
|
|
1153
|
+
.writer
|
|
1154
|
+
.set_read_timeout(Some(HEADER_AND_SSE_TIMEOUT));
|
|
1155
|
+
let _ = upstream
|
|
1156
|
+
.writer
|
|
1157
|
+
.set_write_timeout(Some(HEADER_AND_SSE_TIMEOUT));
|
|
1158
|
+
|
|
1159
|
+
if std::str::from_utf8(creds.raw_token.as_slice()).is_err() {
|
|
1160
|
+
return write_json_response(
|
|
1161
|
+
client_writer,
|
|
1162
|
+
503,
|
|
1163
|
+
"Service Unavailable",
|
|
1164
|
+
&[],
|
|
1165
|
+
br#"{"error":"link_credential_unavailable"}"#,
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
let before = format!(
|
|
1170
|
+
"{} {} HTTP/1.1\r\nHost: 127.0.0.1:{}\r\nAccept-Encoding: identity\r\nConnection: close\r\n{LINK_RELAY_SESSION_HEADER}: {}\r\n",
|
|
1171
|
+
req.method, req.target, st.tunnel_port, upstream.nonce
|
|
1172
|
+
);
|
|
1173
|
+
let mut after = String::new();
|
|
1174
|
+
for (k, v) in &validated.sanitized {
|
|
1175
|
+
if k.eq_ignore_ascii_case("accept-encoding") {
|
|
1176
|
+
continue;
|
|
1177
|
+
}
|
|
1178
|
+
after.push_str(&format!("{k}: {v}\r\n"));
|
|
1179
|
+
}
|
|
1180
|
+
if req.method != "GET" && req.method != "HEAD" {
|
|
1181
|
+
after.push_str(&format!("Content-Length: {}\r\n", req_body.len()));
|
|
1182
|
+
}
|
|
1183
|
+
after.push_str("\r\n");
|
|
1184
|
+
let out_head = token_request_head(&before, auth_kind, creds.raw_token.as_slice(), &after);
|
|
1185
|
+
|
|
1186
|
+
if upstream.writer.write_all(out_head.as_slice()).is_err()
|
|
1187
|
+
|| (!req_body.is_empty() && upstream.writer.write_all(&req_body).is_err())
|
|
1188
|
+
|| upstream.writer.flush().is_err()
|
|
1189
|
+
{
|
|
1190
|
+
return write_json_response(
|
|
1191
|
+
client_writer,
|
|
1192
|
+
503,
|
|
1193
|
+
"Service Unavailable",
|
|
1194
|
+
&[
|
|
1195
|
+
("Retry-After", "1"),
|
|
1196
|
+
("x-opencodex-link-request-id", &req_id),
|
|
1197
|
+
],
|
|
1198
|
+
br#"{"error":"Home is unreachable over Remote Link","code":"home_unreachable","message":"Home is unreachable over Remote Link"}"#,
|
|
1199
|
+
);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
let resp_head = match read_response_head(&mut upstream.reader) {
|
|
1203
|
+
Ok(h) => h,
|
|
1204
|
+
Err(FramingError::HeadersTooLarge) => {
|
|
1205
|
+
return write_json_response(
|
|
1206
|
+
client_writer,
|
|
1207
|
+
502,
|
|
1208
|
+
"Bad Gateway",
|
|
1209
|
+
&[],
|
|
1210
|
+
br#"{"error":"link relay response headers too large"}"#,
|
|
1211
|
+
);
|
|
1212
|
+
}
|
|
1213
|
+
Err(_) => {
|
|
1214
|
+
return write_json_response(
|
|
1215
|
+
client_writer,
|
|
1216
|
+
503,
|
|
1217
|
+
"Service Unavailable",
|
|
1218
|
+
&[
|
|
1219
|
+
("Retry-After", "1"),
|
|
1220
|
+
("x-opencodex-link-request-id", &req_id),
|
|
1221
|
+
],
|
|
1222
|
+
br#"{"error":"Home is unreachable over Remote Link","code":"home_unreachable","message":"Home is unreachable over Remote Link"}"#,
|
|
1223
|
+
);
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
|
|
1227
|
+
let Ok(up_framing) = response_framing(resp_head.status, &req.method, &resp_head.headers) else {
|
|
1228
|
+
return write_json_response(
|
|
1229
|
+
client_writer,
|
|
1230
|
+
502,
|
|
1231
|
+
"Bad Gateway",
|
|
1232
|
+
&[],
|
|
1233
|
+
br#"{"error":"invalid upstream framing"}"#,
|
|
1234
|
+
);
|
|
1235
|
+
};
|
|
1236
|
+
|
|
1237
|
+
let is_sse = is_sse_headers(&resp_head.headers);
|
|
1238
|
+
if !is_sse
|
|
1239
|
+
&& let BodyFraming::ContentLength(len) = up_framing
|
|
1240
|
+
&& len > response_cap_bytes
|
|
1241
|
+
{
|
|
1242
|
+
return write_json_response(
|
|
1243
|
+
client_writer,
|
|
1244
|
+
502,
|
|
1245
|
+
"Bad Gateway",
|
|
1246
|
+
&[],
|
|
1247
|
+
br#"{"error":"link relay response body too large"}"#,
|
|
1248
|
+
);
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
let sanitized_resp = sanitize_response_headers(&resp_head.headers);
|
|
1252
|
+
let reason = if resp_head.reason.is_empty() {
|
|
1253
|
+
"OK"
|
|
1254
|
+
} else {
|
|
1255
|
+
resp_head.reason.as_str()
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1258
|
+
if req.method == "HEAD"
|
|
1259
|
+
|| (100..200).contains(&resp_head.status)
|
|
1260
|
+
|| resp_head.status == 204
|
|
1261
|
+
|| resp_head.status == 304
|
|
1262
|
+
{
|
|
1263
|
+
write!(client_writer, "HTTP/1.1 {} {reason}\r\n", resp_head.status)?;
|
|
1264
|
+
for (k, v) in &sanitized_resp {
|
|
1265
|
+
write!(client_writer, "{k}: {v}\r\n")?;
|
|
1266
|
+
}
|
|
1267
|
+
write!(client_writer, "Connection: close\r\n\r\n")?;
|
|
1268
|
+
return client_writer.flush();
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
write!(client_writer, "HTTP/1.1 {} {reason}\r\n", resp_head.status)?;
|
|
1272
|
+
for (k, v) in &sanitized_resp {
|
|
1273
|
+
write!(client_writer, "{k}: {v}\r\n")?;
|
|
1274
|
+
}
|
|
1275
|
+
write!(
|
|
1276
|
+
client_writer,
|
|
1277
|
+
"Transfer-Encoding: chunked\r\nConnection: close\r\n\r\n"
|
|
1278
|
+
)?;
|
|
1279
|
+
client_writer.flush()?;
|
|
1280
|
+
|
|
1281
|
+
let cap = if is_sse {
|
|
1282
|
+
None
|
|
1283
|
+
} else {
|
|
1284
|
+
Some(response_cap_bytes)
|
|
1285
|
+
};
|
|
1286
|
+
let _ = stream_upstream_to_chunked_client(&mut upstream.reader, client_writer, up_framing, cap);
|
|
1287
|
+
Ok(())
|
|
1288
|
+
}
|