| #!/usr/bin/env node |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import fs from "node:fs"; |
| import path from "node:path"; |
|
|
| const home = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user"; |
| const stateDir = path.join(home, ".openclaw"); |
| const configPath = path.join(stateDir, "openclaw.json"); |
|
|
| |
| function readGatewayToken() { |
| const fromEnv = process.env.OPENCLAW_GATEWAY_TOKEN?.trim(); |
| if (fromEnv) return fromEnv; |
| const filePath = process.env.OPENCLAW_GATEWAY_TOKEN_FILE?.trim(); |
| if (filePath && fs.existsSync(filePath)) { |
| try { |
| return fs.readFileSync(filePath, "utf-8").trim(); |
| } catch { |
| return ""; |
| } |
| } |
| return ""; |
| } |
|
|
| const defaultModel = |
| process.env.OPENCLAW_HF_DEFAULT_MODEL?.trim() || "huggingface/deepseek-ai/DeepSeek-R1"; |
| const gatewayToken = readGatewayToken(); |
| const gatewayPassword = process.env.OPENCLAW_GATEWAY_PASSWORD?.trim(); |
|
|
| |
| const DEFAULT_HF_TRUSTED_PROXY_IPS = [ |
| "10.16.4.123", |
| "10.16.34.155", |
| "10.20.1.9", |
| "10.20.1.222", |
| "10.20.26.157", |
| "10.20.31.87", |
| ]; |
| const trustedProxiesRaw = process.env.OPENCLAW_GATEWAY_TRUSTED_PROXIES?.trim(); |
| const trustedProxies = |
| trustedProxiesRaw && trustedProxiesRaw.length > 0 |
| ? trustedProxiesRaw.split(",").map((s) => s.trim()).filter(Boolean) |
| : DEFAULT_HF_TRUSTED_PROXY_IPS; |
| |
| const allowedOriginsRaw = process.env.OPENCLAW_CONTROL_UI_ALLOWED_ORIGINS?.trim(); |
| const allowedOrigins = allowedOriginsRaw |
| ? allowedOriginsRaw.split(",").map((s) => s.trim()).filter(Boolean) |
| : []; |
|
|
| let config = {}; |
| if (fs.existsSync(configPath)) { |
| try { |
| config = JSON.parse(fs.readFileSync(configPath, "utf-8")); |
| } catch { |
| |
| } |
| } |
|
|
| if (!config.agents) config.agents = {}; |
| if (!config.agents.defaults) config.agents.defaults = {}; |
| if (!config.agents.defaults.model) config.agents.defaults.model = {}; |
| config.agents.defaults.model.primary = defaultModel; |
|
|
| |
| const useTokenAuth = Boolean(gatewayToken); |
| const usePasswordAuth = Boolean(gatewayPassword) && !useTokenAuth; |
| if (useTokenAuth || usePasswordAuth) { |
| if (!config.gateway) config.gateway = {}; |
| if (!config.gateway.auth) config.gateway.auth = {}; |
| if (useTokenAuth) { |
| config.gateway.auth.mode = "token"; |
| config.gateway.auth.token = gatewayToken; |
| } else { |
| config.gateway.auth.mode = "password"; |
| config.gateway.auth.password = gatewayPassword; |
| } |
| } |
| |
| if (useTokenAuth || usePasswordAuth) { |
| if (!config.gateway) config.gateway = {}; |
| if (!config.gateway.controlUi) config.gateway.controlUi = {}; |
| config.gateway.controlUi.dangerouslyDisableDeviceAuth = true; |
| } |
|
|
| |
| if (!config.gateway) config.gateway = {}; |
| config.gateway.trustedProxies = trustedProxies; |
|
|
| if (allowedOrigins.length > 0) { |
| if (!config.gateway) config.gateway = {}; |
| if (!config.gateway.controlUi) config.gateway.controlUi = {}; |
| config.gateway.controlUi.allowedOrigins = allowedOrigins; |
| } |
|
|
| fs.mkdirSync(stateDir, { recursive: true }); |
| fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8"); |
|
|
| |
| const authKind = useTokenAuth ? "token" : usePasswordAuth ? "password" : "none"; |
| const parts = [ |
| `token_present=${useTokenAuth ? "1" : "0"}`, |
| `password_present=${usePasswordAuth ? "1" : "0"}`, |
| `auth=${authKind}`, |
| `trustedProxies=${trustedProxies.length}`, |
| `allowedOrigins=${allowedOrigins.length}`, |
| ]; |
| console.log(`[openclaw-hf-setup] ${parts.join(" ")} -> ${configPath}`); |
| if (authKind === "none") { |
| console.warn( |
| "[openclaw-hf-setup] No auth set. Add OPENCLAW_GATEWAY_TOKEN or OPENCLAW_GATEWAY_PASSWORD in Space Secrets, then restart.", |
| ); |
| } |
|
|