Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- src/app.ts +54 -18
src/app.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as wppconnect from './index';
|
|
| 3 |
import dns from 'dns';
|
| 4 |
import fs from 'fs';
|
| 5 |
import path from 'path';
|
|
|
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const port = process.env.PORT || 7860;
|
|
@@ -11,7 +12,14 @@ let isInitializing = false;
|
|
| 11 |
let lastQR: { base64?: string, ascii?: string, timestamp?: number } | null = null;
|
| 12 |
let lastStatus: string = 'Starting';
|
| 13 |
let initError: string | null = null;
|
| 14 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
app.use(express.json());
|
| 17 |
app.use(express.urlencoded({ extended: true }));
|
|
@@ -19,11 +27,25 @@ app.use(express.urlencoded({ extended: true }));
|
|
| 19 |
const checkNetwork = (host: string): Promise<boolean> => {
|
| 20 |
return new Promise((resolve) => {
|
| 21 |
dns.lookup(host, (err, address) => {
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
});
|
| 28 |
});
|
| 29 |
};
|
|
@@ -37,7 +59,7 @@ app.get('/health', (req, res) => {
|
|
| 37 |
hasQR: !!lastQR,
|
| 38 |
initError: initError,
|
| 39 |
uptime: process.uptime(),
|
| 40 |
-
|
| 41 |
});
|
| 42 |
});
|
| 43 |
|
|
@@ -76,24 +98,38 @@ async function startWPP() {
|
|
| 76 |
isInitializing = true;
|
| 77 |
lastStatus = 'Waiting for Network';
|
| 78 |
|
| 79 |
-
//
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
lastStatus = 'Initializing Browser';
|
| 86 |
-
const sessionName = 'hf-session
|
| 87 |
|
| 88 |
try {
|
| 89 |
whatsappClient = await wppconnect.create({
|
| 90 |
session: sessionName,
|
| 91 |
catchQR: (base64Qr, asciiQR) => {
|
| 92 |
lastQR = { base64: base64Qr, ascii: asciiQR, timestamp: Date.now() };
|
| 93 |
-
|
| 94 |
},
|
| 95 |
statusFind: (statusSession) => {
|
| 96 |
lastStatus = statusSession;
|
|
|
|
| 97 |
if (statusSession === 'isLogged' || statusSession === 'inChat') lastQR = null;
|
| 98 |
},
|
| 99 |
headless: true,
|
|
@@ -105,24 +141,24 @@ async function startWPP() {
|
|
| 105 |
'--disable-setuid-sandbox',
|
| 106 |
'--disable-dev-shm-usage',
|
| 107 |
'--disable-gpu',
|
| 108 |
-
'--
|
| 109 |
],
|
| 110 |
},
|
| 111 |
tokenStore: 'file',
|
| 112 |
folderNameToken: './tokens',
|
| 113 |
autoClose: 0,
|
| 114 |
});
|
| 115 |
-
|
| 116 |
isInitializing = false;
|
| 117 |
} catch (error) {
|
| 118 |
-
|
| 119 |
initError = error.message;
|
| 120 |
isInitializing = false;
|
| 121 |
-
setTimeout(startWPP,
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
app.listen(port, () => {
|
| 126 |
-
|
| 127 |
startWPP();
|
| 128 |
});
|
|
|
|
| 3 |
import dns from 'dns';
|
| 4 |
import fs from 'fs';
|
| 5 |
import path from 'path';
|
| 6 |
+
import https from 'https';
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
const port = process.env.PORT || 7860;
|
|
|
|
| 12 |
let lastQR: { base64?: string, ascii?: string, timestamp?: number } | null = null;
|
| 13 |
let lastStatus: string = 'Starting';
|
| 14 |
let initError: string | null = null;
|
| 15 |
+
let logs: string[] = [];
|
| 16 |
+
|
| 17 |
+
const log = (msg: string) => {
|
| 18 |
+
const entry = `${new Date().toISOString()} - ${msg}`;
|
| 19 |
+
console.log(entry);
|
| 20 |
+
logs.push(entry);
|
| 21 |
+
if (logs.length > 50) logs.shift();
|
| 22 |
+
};
|
| 23 |
|
| 24 |
app.use(express.json());
|
| 25 |
app.use(express.urlencoded({ extended: true }));
|
|
|
|
| 27 |
const checkNetwork = (host: string): Promise<boolean> => {
|
| 28 |
return new Promise((resolve) => {
|
| 29 |
dns.lookup(host, (err, address) => {
|
| 30 |
+
if (err) {
|
| 31 |
+
log(`DNS Fail ${host}: ${err.message}`);
|
| 32 |
+
resolve(false);
|
| 33 |
+
} else {
|
| 34 |
+
log(`DNS Success ${host}: ${address}`);
|
| 35 |
+
resolve(true);
|
| 36 |
+
}
|
| 37 |
+
});
|
| 38 |
+
});
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
const checkHttps = (url: string): Promise<boolean> => {
|
| 42 |
+
return new Promise((resolve) => {
|
| 43 |
+
https.get(url, (res) => {
|
| 44 |
+
log(`HTTPS Success ${url}: ${res.statusCode}`);
|
| 45 |
+
resolve(true);
|
| 46 |
+
}).on('error', (e) => {
|
| 47 |
+
log(`HTTPS Fail ${url}: ${e.message}`);
|
| 48 |
+
resolve(false);
|
| 49 |
});
|
| 50 |
});
|
| 51 |
};
|
|
|
|
| 59 |
hasQR: !!lastQR,
|
| 60 |
initError: initError,
|
| 61 |
uptime: process.uptime(),
|
| 62 |
+
logs
|
| 63 |
});
|
| 64 |
});
|
| 65 |
|
|
|
|
| 98 |
isInitializing = true;
|
| 99 |
lastStatus = 'Waiting for Network';
|
| 100 |
|
| 101 |
+
// Try to reach some reliable hosts first
|
| 102 |
+
const hosts = ['google.com', 'huggingface.co', 'web.whatsapp.com'];
|
| 103 |
+
let reachable = false;
|
| 104 |
+
|
| 105 |
+
for (let i = 0; i < 20; i++) {
|
| 106 |
+
log(`Network check attempt ${i+1}`);
|
| 107 |
+
for (const host of hosts) {
|
| 108 |
+
if (await checkNetwork(host)) {
|
| 109 |
+
if (host === 'web.whatsapp.com') reachable = true;
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
if (reachable) break;
|
| 113 |
+
await new Promise(r => setTimeout(r, 15000));
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
if (!reachable) {
|
| 117 |
+
log('Warning: web.whatsapp.com still not resolvable, attempting WPP create anyway...');
|
| 118 |
}
|
| 119 |
|
| 120 |
lastStatus = 'Initializing Browser';
|
| 121 |
+
const sessionName = 'hf-session';
|
| 122 |
|
| 123 |
try {
|
| 124 |
whatsappClient = await wppconnect.create({
|
| 125 |
session: sessionName,
|
| 126 |
catchQR: (base64Qr, asciiQR) => {
|
| 127 |
lastQR = { base64: base64Qr, ascii: asciiQR, timestamp: Date.now() };
|
| 128 |
+
log('QR code generated');
|
| 129 |
},
|
| 130 |
statusFind: (statusSession) => {
|
| 131 |
lastStatus = statusSession;
|
| 132 |
+
log(`Status: ${statusSession}`);
|
| 133 |
if (statusSession === 'isLogged' || statusSession === 'inChat') lastQR = null;
|
| 134 |
},
|
| 135 |
headless: true,
|
|
|
|
| 141 |
'--disable-setuid-sandbox',
|
| 142 |
'--disable-dev-shm-usage',
|
| 143 |
'--disable-gpu',
|
| 144 |
+
'--disable-web-security'
|
| 145 |
],
|
| 146 |
},
|
| 147 |
tokenStore: 'file',
|
| 148 |
folderNameToken: './tokens',
|
| 149 |
autoClose: 0,
|
| 150 |
});
|
| 151 |
+
log('WPPConnect ready');
|
| 152 |
isInitializing = false;
|
| 153 |
} catch (error) {
|
| 154 |
+
log(`WPPConnect error: ${error.message}`);
|
| 155 |
initError = error.message;
|
| 156 |
isInitializing = false;
|
| 157 |
+
setTimeout(startWPP, 60000);
|
| 158 |
}
|
| 159 |
}
|
| 160 |
|
| 161 |
app.listen(port, () => {
|
| 162 |
+
log(`App listening on port ${port}`);
|
| 163 |
startWPP();
|
| 164 |
});
|