Spaces:
Paused
Paused
Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const puppeteer = require('puppeteer');
|
| 3 |
+
const path = require('path');
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
|
| 7 |
+
app.use(express.static(path.join(__dirname, 'public')));
|
| 8 |
+
|
| 9 |
+
async function loginToFacebook(email, password) {
|
| 10 |
+
let browser;
|
| 11 |
+
try {
|
| 12 |
+
console.log("Launching browser...");
|
| 13 |
+
browser = await puppeteer.launch({
|
| 14 |
+
headless: true,
|
| 15 |
+
args: [
|
| 16 |
+
'--no-sandbox',
|
| 17 |
+
'--disable-gpu',
|
| 18 |
+
'--disable-software-rasterizer',
|
| 19 |
+
'--disable-dev-shm-usage'
|
| 20 |
+
]
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
const page = await browser.newPage();
|
| 24 |
+
console.log("Navigating to Facebook...");
|
| 25 |
+
await page.goto('https://www.facebook.com/');
|
| 26 |
+
|
| 27 |
+
console.log("Typing email and password...");
|
| 28 |
+
await page.type('#email', email, { delay: 100 });
|
| 29 |
+
await page.type('#pass', password, { delay: 100 });
|
| 30 |
+
|
| 31 |
+
await Promise.all([
|
| 32 |
+
page.click('[name="login"]'),
|
| 33 |
+
page.waitForNavigation({ waitUntil: 'networkidle0' }),
|
| 34 |
+
]);
|
| 35 |
+
|
| 36 |
+
const cookies = await page.cookies();
|
| 37 |
+
const loginFailed = await page.$('input[name="email"]');
|
| 38 |
+
if (loginFailed) {
|
| 39 |
+
await browser.close();
|
| 40 |
+
return { error: 'Wrong username or password. Please try again.' };
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
const cookieString = cookies.map(cookie => `${cookie.name}=${cookie.value}`).join('; ');
|
| 44 |
+
|
| 45 |
+
const jsonCookies = cookies.map(cookie => ({
|
| 46 |
+
domain: cookie.domain,
|
| 47 |
+
expirationDate: cookie.expires,
|
| 48 |
+
hostOnly: cookie.hostOnly,
|
| 49 |
+
httpOnly: cookie.httpOnly,
|
| 50 |
+
name: cookie.name,
|
| 51 |
+
path: cookie.path,
|
| 52 |
+
sameSite: cookie.sameSite,
|
| 53 |
+
secure: cookie.secure,
|
| 54 |
+
session: cookie.session,
|
| 55 |
+
storeId: cookie.storeId,
|
| 56 |
+
value: cookie.value
|
| 57 |
+
}));
|
| 58 |
+
|
| 59 |
+
const datrCookie = cookies.find(cookie => cookie.name === 'datr') || {};
|
| 60 |
+
|
| 61 |
+
const responseWithDatr = {
|
| 62 |
+
cookies: cookieString,
|
| 63 |
+
jsonCookies,
|
| 64 |
+
datr: datrCookie.value || null
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
await browser.close();
|
| 68 |
+
return responseWithDatr;
|
| 69 |
+
|
| 70 |
+
} catch (error) {
|
| 71 |
+
console.error('Error during Facebook login:', error);
|
| 72 |
+
if (browser) await browser.close();
|
| 73 |
+
throw error;
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
app.get('/appstate', async (req, res) => {
|
| 78 |
+
const { e: email, p: password } = req.query;
|
| 79 |
+
|
| 80 |
+
if (!email || !password) {
|
| 81 |
+
return res.status(400).json({ error: 'Email and password are required.' });
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
try {
|
| 85 |
+
console.log("Attempting to log in...");
|
| 86 |
+
const result = await loginToFacebook(email, password);
|
| 87 |
+
|
| 88 |
+
if (result.error) {
|
| 89 |
+
return res.status(400).json({ error: result.error });
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
console.log("Login successful. Returning cookies...");
|
| 93 |
+
return res.json(result);
|
| 94 |
+
} catch (error) {
|
| 95 |
+
console.error('Error during login:', error);
|
| 96 |
+
return res.status(500).json({ error: 'An error occurred during the login process.' });
|
| 97 |
+
}
|
| 98 |
+
});
|
| 99 |
+
|
| 100 |
+
app.get('/info', async (req, res) => {
|
| 101 |
+
try {
|
| 102 |
+
console.log("Launching Puppeteer to gather browser info...");
|
| 103 |
+
const browser = await puppeteer.launch({ headless: true });
|
| 104 |
+
const version = await browser.version();
|
| 105 |
+
const page = await browser.newPage();
|
| 106 |
+
const userAgent = await page.evaluate(() => navigator.userAgent);
|
| 107 |
+
await browser.close();
|
| 108 |
+
|
| 109 |
+
console.log("Successfully fetched browser info.");
|
| 110 |
+
res.json({
|
| 111 |
+
puppeteer_version: require("puppeteer/package.json").version,
|
| 112 |
+
browser_version: version,
|
| 113 |
+
user_agent: userAgent,
|
| 114 |
+
});
|
| 115 |
+
} catch (error) {
|
| 116 |
+
console.error("Failed to fetch system info:", error);
|
| 117 |
+
res.status(500).json({ error: "Failed to fetch system info" });
|
| 118 |
+
}
|
| 119 |
+
});
|
| 120 |
+
|
| 121 |
+
const PORT = process.env.PORT || 7860;
|
| 122 |
+
app.listen(PORT, () => {
|
| 123 |
+
console.log(`Server is running on http://localhost:${PORT}`);
|
| 124 |
+
});
|