somratpro commited on
Commit
529ca98
·
1 Parent(s): 7cd1716

refactor: simplify gateway connection and update handshake parameters in wa-guardian.js

Browse files
Files changed (1) hide show
  1. wa-guardian.js +14 -27
wa-guardian.js CHANGED
@@ -12,12 +12,10 @@ const path = require("path");
12
  const { WebSocket } = require('/home/node/.openclaw/openclaw-app/node_modules/ws');
13
  const { randomUUID } = require('node:crypto');
14
 
15
- const GATEWAY_WS_URL = "ws://127.0.0.1:7860";
16
- const GATEWAY_ORIGIN = "http://127.0.0.1:7860";
17
  const GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || "huggingclaw";
18
  const CHECK_INTERVAL = 5000;
19
  const WAIT_TIMEOUT = 120000;
20
- const AUTH_FAILURE_COOLDOWN = 5 * 60 * 1000;
21
  const POST_515_NO_LOGOUT_MS = 90 * 1000;
22
  const RESET_MARKER_PATH = path.join(
23
  process.env.HOME || "/home/node",
@@ -28,8 +26,6 @@ const RESET_MARKER_PATH = path.join(
28
 
29
  let isWaiting = false;
30
  let hasShownWaitMessage = false;
31
- let authFailureUntil = 0;
32
- let authFailureLogged = false;
33
  let last515At = 0;
34
 
35
  function extractErrorMessage(msg) {
@@ -52,12 +48,7 @@ function writeResetMarker() {
52
 
53
  async function createConnection() {
54
  return new Promise((resolve, reject) => {
55
- const ws = new WebSocket(
56
- `${GATEWAY_WS_URL}/?token=${encodeURIComponent(GATEWAY_TOKEN)}`,
57
- {
58
- headers: { Origin: GATEWAY_ORIGIN },
59
- },
60
- );
61
  let resolved = false;
62
 
63
  ws.on("message", (data) => {
@@ -71,10 +62,17 @@ async function createConnection() {
71
  params: {
72
  minProtocol: 3,
73
  maxProtocol: 3,
 
 
 
 
 
 
 
74
  auth: { token: GATEWAY_TOKEN },
75
- client: { id: "huggingclaw-wa-guardian", platform: "web", mode: "ui", version: "1.0.0" },
76
- scopes: ["operator.admin", "operator.pairing", "operator.read", "operator.write"],
77
- }
78
  }));
79
  return;
80
  }
@@ -119,13 +117,10 @@ async function callRpc(ws, method, params) {
119
 
120
  async function checkStatus() {
121
  if (isWaiting) return;
122
- if (Date.now() < authFailureUntil) return;
123
 
124
  let ws;
125
  try {
126
  ws = await createConnection();
127
- authFailureUntil = 0;
128
- authFailureLogged = false;
129
 
130
  // Check if WhatsApp channel exists and its status
131
  const statusRes = await callRpc(ws, "channels.status", {});
@@ -205,15 +200,7 @@ async function checkStatus() {
205
  }
206
 
207
  } catch (e) {
208
- const message = e && e.message ? e.message : "";
209
- if (/unauthorized|authentication|too many failed/i.test(message)) {
210
- authFailureUntil = Date.now() + AUTH_FAILURE_COOLDOWN;
211
- if (!authFailureLogged) {
212
- console.log(`[guardian] Authentication failed (${message}). Pausing guardian retries for ${AUTH_FAILURE_COOLDOWN / 60000} minutes.`);
213
- authFailureLogged = true;
214
- }
215
- }
216
- // Normal timeout or gateway starting up
217
  } finally {
218
  isWaiting = false;
219
  if (ws) ws.close();
@@ -222,4 +209,4 @@ async function checkStatus() {
222
 
223
  console.log("[guardian] ⚔️ WhatsApp Guardian active. Monitoring pairing status...");
224
  setInterval(checkStatus, CHECK_INTERVAL);
225
- setTimeout(checkStatus, 10000);
 
12
  const { WebSocket } = require('/home/node/.openclaw/openclaw-app/node_modules/ws');
13
  const { randomUUID } = require('node:crypto');
14
 
15
+ const GATEWAY_URL = "ws://127.0.0.1:7860";
 
16
  const GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || "huggingclaw";
17
  const CHECK_INTERVAL = 5000;
18
  const WAIT_TIMEOUT = 120000;
 
19
  const POST_515_NO_LOGOUT_MS = 90 * 1000;
20
  const RESET_MARKER_PATH = path.join(
21
  process.env.HOME || "/home/node",
 
26
 
27
  let isWaiting = false;
28
  let hasShownWaitMessage = false;
 
 
29
  let last515At = 0;
30
 
31
  function extractErrorMessage(msg) {
 
48
 
49
  async function createConnection() {
50
  return new Promise((resolve, reject) => {
51
+ const ws = new WebSocket(GATEWAY_URL);
 
 
 
 
 
52
  let resolved = false;
53
 
54
  ws.on("message", (data) => {
 
62
  params: {
63
  minProtocol: 3,
64
  maxProtocol: 3,
65
+ client: {
66
+ id: "gateway-client",
67
+ version: "1.0.0",
68
+ platform: "linux",
69
+ mode: "backend",
70
+ },
71
+ caps: [],
72
  auth: { token: GATEWAY_TOKEN },
73
+ role: "operator",
74
+ scopes: ["operator.admin"],
75
+ },
76
  }));
77
  return;
78
  }
 
117
 
118
  async function checkStatus() {
119
  if (isWaiting) return;
 
120
 
121
  let ws;
122
  try {
123
  ws = await createConnection();
 
 
124
 
125
  // Check if WhatsApp channel exists and its status
126
  const statusRes = await callRpc(ws, "channels.status", {});
 
200
  }
201
 
202
  } catch (e) {
203
+ // Normal timeout or gateway starting up; retry on the next interval.
 
 
 
 
 
 
 
 
204
  } finally {
205
  isWaiting = false;
206
  if (ws) ws.close();
 
209
 
210
  console.log("[guardian] ⚔️ WhatsApp Guardian active. Monitoring pairing status...");
211
  setInterval(checkStatus, CHECK_INTERVAL);
212
+ setTimeout(checkStatus, 15000);