somratpro commited on
Commit
16622a0
·
1 Parent(s): d80a47a

refactor: improve request patching logic in cloudflare-proxy and force IPv4 for Telegram configuration

Browse files
Files changed (2) hide show
  1. cloudflare-proxy.js +31 -34
  2. 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(options, callback) {
68
- let hostname = "";
69
- let path = "";
70
- let headers = {};
71
-
72
- if (typeof options === "string") {
73
- try {
74
- const parsed = new URL(options);
75
- hostname = parsed.hostname;
76
- path = parsed.pathname + parsed.search;
77
- } catch (e) {
78
- hostname = options.split('/')[0];
79
- path = '/' + options.split('/').slice(1).join('/');
 
 
 
 
80
  }
81
- } else if (options instanceof URL) {
82
- hostname = options.hostname;
83
- path = options.pathname + options.search;
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
- ...(newOptions.headers || {}),
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, options, callback);
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
- CONFIG_JSON=$(echo "$CONFIG_JSON" | jq --arg token "$TELEGRAM_BOT_TOKEN" --arg proxy_url "${CLOUDFLARE_PROXY_URL:-}" '
 
 
 
361
  .channels.telegram.enabled = true
362
  | .channels.telegram.botToken = $token
363
  | .channels.telegram.commands.native = false
364
  | .channels.telegram.timeoutSeconds = 60
365
- | (if $proxy_url != "" then .channels.telegram.apiRoot = $proxy_url else . end)
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,