Spaces:
Running
Running
refactor: improve request patching logic in cloudflare-proxy and force IPv4 for Telegram configuration
Browse files- cloudflare-proxy.js +31 -34
- start.sh +6 -5
cloudflare-proxy.js
CHANGED
|
@@ -64,38 +64,39 @@ if (PROXY_URL) {
|
|
| 64 |
};
|
| 65 |
|
| 66 |
const patch = (original, originalModuleName) => {
|
| 67 |
-
return function patchedRequest(
|
| 68 |
-
let
|
| 69 |
-
let
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
hostname
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
-
} else
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
headers = options.headers || {};
|
| 85 |
-
} else if (options && typeof options === "object") {
|
| 86 |
-
hostname =
|
| 87 |
-
options.hostname ||
|
| 88 |
-
(options.host ? String(options.host).split(":")[0] : "");
|
| 89 |
-
path = options.path || "/";
|
| 90 |
-
headers = options.headers || {};
|
| 91 |
}
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
const shouldProxy = shouldProxyHost(hostname);
|
| 94 |
-
const alreadyProxied =
|
| 95 |
-
options && typeof options === "object" && options._proxied;
|
| 96 |
const hasTargetHeader =
|
| 97 |
-
headers
|
| 98 |
-
(headers["x-target-host"] || headers["X-Target-Host"]);
|
| 99 |
|
| 100 |
if (shouldProxy && !alreadyProxied && !hasTargetHeader) {
|
| 101 |
if (DEBUG) {
|
|
@@ -104,11 +105,7 @@ if (PROXY_URL) {
|
|
| 104 |
);
|
| 105 |
}
|
| 106 |
|
| 107 |
-
const newOptions =
|
| 108 |
-
typeof options === "string" || options instanceof URL
|
| 109 |
-
? { protocol: "https:", path }
|
| 110 |
-
: { ...options };
|
| 111 |
-
|
| 112 |
newOptions._proxied = true;
|
| 113 |
newOptions.protocol = "https:";
|
| 114 |
newOptions.hostname = proxy.hostname;
|
|
@@ -118,7 +115,7 @@ if (PROXY_URL) {
|
|
| 118 |
delete newOptions.agent;
|
| 119 |
|
| 120 |
newOptions.headers = {
|
| 121 |
-
...(
|
| 122 |
host: proxy.host,
|
| 123 |
"x-target-host": hostname,
|
| 124 |
};
|
|
@@ -130,7 +127,7 @@ if (PROXY_URL) {
|
|
| 130 |
return originalHttpsRequest.call(https, newOptions, callback);
|
| 131 |
}
|
| 132 |
|
| 133 |
-
return original.call(this,
|
| 134 |
};
|
| 135 |
};
|
| 136 |
|
|
|
|
| 64 |
};
|
| 65 |
|
| 66 |
const patch = (original, originalModuleName) => {
|
| 67 |
+
return function patchedRequest(arg1, arg2, arg3) {
|
| 68 |
+
let options = {};
|
| 69 |
+
let callback;
|
| 70 |
+
|
| 71 |
+
if (typeof arg1 === "string" || arg1 instanceof URL) {
|
| 72 |
+
const url = typeof arg1 === "string" ? new URL(arg1) : arg1;
|
| 73 |
+
options = {
|
| 74 |
+
protocol: url.protocol,
|
| 75 |
+
hostname: url.hostname,
|
| 76 |
+
port: url.port,
|
| 77 |
+
path: url.pathname + url.search,
|
| 78 |
+
};
|
| 79 |
+
if (typeof arg2 === "object" && arg2 !== null) {
|
| 80 |
+
options = { ...options, ...arg2 };
|
| 81 |
+
callback = arg3;
|
| 82 |
+
} else {
|
| 83 |
+
callback = arg2;
|
| 84 |
}
|
| 85 |
+
} else {
|
| 86 |
+
options = { ...arg1 };
|
| 87 |
+
callback = arg2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
+
const hostname =
|
| 91 |
+
options.hostname ||
|
| 92 |
+
(options.host ? String(options.host).split(":")[0] : "");
|
| 93 |
+
const path = options.path || "/";
|
| 94 |
+
const headers = options.headers || {};
|
| 95 |
+
|
| 96 |
const shouldProxy = shouldProxyHost(hostname);
|
| 97 |
+
const alreadyProxied = options._proxied;
|
|
|
|
| 98 |
const hasTargetHeader =
|
| 99 |
+
headers["x-target-host"] || headers["X-Target-Host"];
|
|
|
|
| 100 |
|
| 101 |
if (shouldProxy && !alreadyProxied && !hasTargetHeader) {
|
| 102 |
if (DEBUG) {
|
|
|
|
| 105 |
);
|
| 106 |
}
|
| 107 |
|
| 108 |
+
const newOptions = { ...options };
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
newOptions._proxied = true;
|
| 110 |
newOptions.protocol = "https:";
|
| 111 |
newOptions.hostname = proxy.hostname;
|
|
|
|
| 115 |
delete newOptions.agent;
|
| 116 |
|
| 117 |
newOptions.headers = {
|
| 118 |
+
...(options.headers || {}),
|
| 119 |
host: proxy.host,
|
| 120 |
"x-target-host": hostname,
|
| 121 |
};
|
|
|
|
| 127 |
return originalHttpsRequest.call(https, newOptions, callback);
|
| 128 |
}
|
| 129 |
|
| 130 |
+
return original.call(this, arg1, arg2, arg3);
|
| 131 |
};
|
| 132 |
};
|
| 133 |
|
start.sh
CHANGED
|
@@ -354,17 +354,18 @@ fi
|
|
| 354 |
# Telegram (supports multiple user IDs, comma-separated)
|
| 355 |
if [ -n "${TELEGRAM_BOT_TOKEN:-}" ]; then
|
| 356 |
CONFIG_JSON=$(echo "$CONFIG_JSON" | jq '.plugins.entries.telegram = {"enabled": true}')
|
| 357 |
-
export TELEGRAM_BOT_TOKEN="$TELEGRAM_BOT_TOKEN"
|
| 358 |
export OPENCLAW_TELEGRAM_DISABLE_AUTO_SELECT_FAMILY=1
|
| 359 |
export OPENCLAW_TELEGRAM_DNS_RESULT_ORDER=ipv4first
|
| 360 |
-
|
|
|
|
|
|
|
|
|
|
| 361 |
.channels.telegram.enabled = true
|
| 362 |
| .channels.telegram.botToken = $token
|
| 363 |
| .channels.telegram.commands.native = false
|
| 364 |
| .channels.telegram.timeoutSeconds = 60
|
| 365 |
-
|
|
| 366 |
-
# | .channels.telegram.network.autoSelectFamily = false
|
| 367 |
-
# | .channels.telegram.network.dnsResultOrder = "ipv4first"
|
| 368 |
| .channels.telegram.retry = {
|
| 369 |
"attempts": 5,
|
| 370 |
"minDelayMs": 800,
|
|
|
|
| 354 |
# Telegram (supports multiple user IDs, comma-separated)
|
| 355 |
if [ -n "${TELEGRAM_BOT_TOKEN:-}" ]; then
|
| 356 |
CONFIG_JSON=$(echo "$CONFIG_JSON" | jq '.plugins.entries.telegram = {"enabled": true}')
|
| 357 |
+
export TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN//[[:space:]]/}"
|
| 358 |
export OPENCLAW_TELEGRAM_DISABLE_AUTO_SELECT_FAMILY=1
|
| 359 |
export OPENCLAW_TELEGRAM_DNS_RESULT_ORDER=ipv4first
|
| 360 |
+
# Force ipv4 for Telegram specifically as HF IPv6 often times out
|
| 361 |
+
export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--dns-result-order=ipv4first"
|
| 362 |
+
|
| 363 |
+
CONFIG_JSON=$(echo "$CONFIG_JSON" | jq --arg token "$TELEGRAM_BOT_TOKEN" '
|
| 364 |
.channels.telegram.enabled = true
|
| 365 |
| .channels.telegram.botToken = $token
|
| 366 |
| .channels.telegram.commands.native = false
|
| 367 |
| .channels.telegram.timeoutSeconds = 60
|
| 368 |
+
| .channels.telegram.apiRoot = "https://api.telegram.org"
|
|
|
|
|
|
|
| 369 |
| .channels.telegram.retry = {
|
| 370 |
"attempts": 5,
|
| 371 |
"minDelayMs": 800,
|