File size: 5,113 Bytes
0d44546
cfe32b0
 
0d44546
 
 
 
 
 
 
e0f2f99
cfe32b0
 
e0f2f99
 
 
 
 
 
 
 
0d44546
e0f2f99
cfe32b0
e0f2f99
 
 
cfe32b0
 
 
e0f2f99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfe32b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f2f99
 
cfe32b0
e0f2f99
cfe32b0
e0f2f99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfe32b0
e0f2f99
cfe32b0
e0f2f99
 
 
cfe32b0
e0f2f99
 
cfe32b0
e0f2f99
 
 
 
 
cfe32b0
e0f2f99
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import express from 'express';
import { chromium } from 'playwright-extra';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
import cors from 'cors';
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

chromium.use(StealthPlugin());

const app = express();
const PORT = process.env.PORT || 7860;

app.use(cors());
app.use(express.json({ limit: '50mb' }));
app.use('/files', express.static('public'));

const publicDir = path.join(__dirname, 'public');
await fs.mkdir(publicDir, { recursive: true }).catch(console.error);

function convertPlaywrightCode(code) {
    let converted = code;
    
    const replacements = [
        [/const\s+{\s*chromium\s*}\s*=\s*require\(['"](.*?playwright.*?)['"]\)/gi, 'const chromium = globalThis.chromium'],
        [/from\s+['"]playwright['"]/gi, ''],
        [/import\s+{\s*chromium\s*}\s+from\s+['"]playwright['"]/gi, 'const chromium = globalThis.chromium'],
    ];
    
    replacements.forEach(([pattern, replacement]) => {
        converted = converted.replace(pattern, replacement);
    });
    
    return converted;
}

app.post('/api/s-playwright', async (req, res) => {
    const { code, lang } = req.body;

    if (!code || !lang) {
        return res.status(400).json({
            success: false,
            error: 'Missing required fields: code and lang'
        });
    }

    const supportedLangs = ['javascript'];
    const normalizedLang = lang.toLowerCase();

    if (!supportedLangs.includes(normalizedLang)) {
        return res.status(400).json({
            success: false,
            error: `Unsupported language: "${lang}". Supported: ${supportedLangs.join(', ')}`
        });
    }

    let browser = null;

    try {
        const convertedCode = convertPlaywrightCode(code);
        
        globalThis.chromium = {
            launch: async (options = {}) => {
                return await chromium.launch({
                    headless: options.headless !== false,
                    args: [
                        '--disable-blink-features=AutomationControlled',
                        '--no-sandbox',
                        '--disable-setuid-sandbox',
                        '--disable-web-security',
                        '--disable-features=IsolateOrigins,site-per-process',
                        '--disable-dev-shm-usage',
                        '--disable-accelerated-2d-canvas',
                        '--disable-gpu',
                        '--window-size=1920,1080',
                        '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
                        ...(options.args || [])
                    ]
                });
            }
        };
        
        const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
        const executableCode = new AsyncFunction('chromium', 'publicDir', 'path', 'fs', convertedCode);
        
        const result = await executableCode(globalThis.chromium, publicDir, path, fs);

        const timestamp = Date.now();
        const baseUrl = `${req.protocol}://${req.get('host')}`;
        const screenshotFiles = await fs.readdir(publicDir);
        const recentFiles = screenshotFiles
            .filter(f => f.startsWith('screenshot-') && f.endsWith('.png'))
            .map(f => ({
                name: f,
                publicURL: `${baseUrl}/files/${f}`
            }));

        res.json({
            success: true,
            data: {
                result: result,
                files: recentFiles,
                timestamp: timestamp
            }
        });

    } catch (error) {
        res.status(500).json({
            success: false,
            error: error.message,
            stack: error.stack
        });
    } finally {
        if (browser) {
            try {
                await browser.close();
            } catch (e) {
                console.error('Error closing browser:', e.message);
            }
        }
    }
});

app.get('/', (req, res) => {
    res.json({
        message: 'Playwright Stealth API is running',
        endpoints: {
            'POST /api/s-playwright': 'Execute playwright code with stealth mode'
        },
        features: [
            'Advanced Anti-Fingerprinting',
            'Stealth Plugin Enabled',
            'Human-like Behavior',
            'Cloudflare WAF Bypass',
            'Auto Playwright code execution'
        ]
    });
});

app.listen(PORT, '0.0.0.0', () => {
    console.log(`🚀 Playwright Stealth API running on port ${PORT}`);
    console.log(`📝 Endpoint: POST /api/s-playwright`);
    console.log(`🎭 Features: Anti-fingerprinting, WAF bypass, stealth mode`);
});

process.on('SIGTERM', async () => {
    console.log('SIGTERM received, shutting down gracefully...');
    process.exit(0);
});

process.on('SIGINT', async () => {
    console.log('SIGINT received, shutting down gracefully...');
    process.exit(0);
});