-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
769 lines (656 loc) · 25.5 KB
/
Copy pathserver.js
File metadata and controls
769 lines (656 loc) · 25.5 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
const express = require('express');
const cors = require('cors');
const http = require('http');
const socketIo = require('socket.io');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const paypal = require('@paypal/checkout-server-sdk');
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const RealBotEngine = require('./real-bot-engine');
// Initialize Express app
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
app.use(cors());
app.use(express.json());
app.use(express.static('.'));
// Initialize PayPal
let paypalClient = null;
if (process.env.PAYPAL_CLIENT_ID && process.env.PAYPAL_CLIENT_SECRET) {
const environment = process.env.NODE_ENV === 'production'
? new paypal.core.LiveEnvironment(process.env.PAYPAL_CLIENT_ID, process.env.PAYPAL_CLIENT_SECRET)
: new paypal.core.SandboxEnvironment(process.env.PAYPAL_CLIENT_ID, process.env.PAYPAL_CLIENT_SECRET);
paypalClient = new paypal.core.PayPalHttpClient(environment);
}
// In-memory storage (replace with database in production)
let users = new Map();
let bots = new Map();
let withdrawals = new Map();
// Generate unique user ID
function generateUserId() {
return 'user_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
}
// REAL earning tracking from affiliate APIs
async function trackAffiliateEarnings(userId) {
const user = users.get(userId);
if (!user) return { total: 0, breakdown: {} };
try {
// Use the real bot engine to get actual earnings
const realEarnings = await botEngine.trackAffiliateEarnings();
return {
total: realEarnings.total || 0,
breakdown: {
amazon: realEarnings.amazon || 0,
clickbank: realEarnings.clickbank || 0,
ebay: realEarnings.ebay || 0
},
timestamp: Date.now(),
source: 'real-api'
};
} catch (error) {
console.error('Error tracking real affiliate earnings:', error);
return { total: 0, breakdown: {}, error: error.message };
}
}
// Real crypto balance fetching
async function getCryptoBalance(type, address) {
try {
let balance = 0;
let usdRate = 0;
switch(type.toLowerCase()) {
case 'btc':
// Use BlockCypher API for real Bitcoin balance
const btcResponse = await fetch(`https://api.blockcypher.com/v1/btc/main/addrs/${address}/balance`);
const btcData = await btcResponse.json();
balance = btcData.balance / 100000000; // Convert satoshis to BTC
// Get BTC price from CoinGecko
const btcPriceResponse = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd');
const btcPriceData = await btcPriceResponse.json();
usdRate = btcPriceData.bitcoin.usd;
break;
case 'eth':
// Use Etherscan API for real Ethereum balance
const ethResponse = await fetch(`https://api.etherscan.io/api?module=account&action=balance&address=${address}&tag=latest`);
const ethData = await ethResponse.json();
balance = parseInt(ethData.result) / 1e18; // Convert wei to ETH
// Get ETH price from CoinGecko
const ethPriceResponse = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd');
const ethPriceData = await ethPriceResponse.json();
usdRate = ethPriceData.ethereum.usd;
break;
default:
// For other cryptocurrencies, return mock data
balance = Math.random() * 1;
usdRate = 100 + Math.random() * 1000;
}
return { balance, usdRate, usdValue: balance * usdRate };
} catch (error) {
console.error(`Error fetching ${type} balance:`, error);
return { balance: 0, usdRate: 0, usdValue: 0 };
}
}
// Real payment processing
async function processWithdrawal(withdrawalData) {
try {
let paymentResult;
switch (withdrawalData.method) {
case 'stripe':
// Real Stripe payment processing
const stripePaymentIntent = await stripe.paymentIntents.create({
amount: Math.round(withdrawalData.amount * 100), // Convert to cents
currency: 'usd',
payment_method: withdrawalData.paymentMethodId,
confirmation_method: 'manual',
confirm: true,
return_url: `${process.env.FRONTEND_URL}/withdrawal-complete`
});
paymentResult = {
success: stripePaymentIntent.status === 'succeeded',
transactionId: stripePaymentIntent.id,
fee: withdrawalData.amount * 0.029 + 0.30, // Stripe fees
processingTime: 'instant'
};
break;
case 'paypal':
// Real PayPal payment processing
if (!paypalClient) {
throw new Error('PayPal not configured');
}
const request = new paypal.orders.OrdersCreateRequest();
request.prefer("return=representation");
request.requestBody({
intent: 'CAPTURE',
purchase_units: [{
amount: {
currency_code: 'USD',
value: withdrawalData.amount.toFixed(2)
}
}]
});
const order = await paypalClient.execute(request);
// Capture the payment
const captureRequest = new paypal.orders.OrdersCaptureRequest(order.result.id);
captureRequest.requestBody({});
const capture = await paypalClient.execute(captureRequest);
paymentResult = {
success: capture.result.status === 'COMPLETED',
transactionId: capture.result.id,
fee: withdrawalData.amount * 0.0349 + 0.30, // PayPal fees
processingTime: 'instant'
};
break;
case 'bank':
// Bank transfer (would integrate with banking APIs like Plaid)
paymentResult = {
success: true,
transactionId: 'bank_' + Date.now(),
fee: 0,
processingTime: '1-3 business days'
};
break;
default:
throw new Error('Unsupported payment method');
}
return paymentResult;
} catch (error) {
console.error('Payment processing error:', error);
throw error;
}
}
// REAL Bot Engine with actual API integrations (USING REAL eBay CREDENTIALS)
const botEngine = new RealBotEngine({
amazon: {
access_key: process.env.AMAZON_ACCESS_KEY,
secret_key: process.env.AMAZON_SECRET_KEY,
affiliate_tag: process.env.AMAZON_ASSOCIATES_TAG || 'your-tag-20'
},
twitter: {
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token: process.env.TWITTER_ACCESS_TOKEN,
access_secret: process.env.TWITTER_ACCESS_SECRET
},
facebook: {
access_token: process.env.FACEBOOK_ACCESS_TOKEN,
page_id: process.env.FACEBOOK_PAGE_ID
},
instagram: {
access_token: process.env.INSTAGRAM_ACCESS_TOKEN,
user_id: process.env.INSTAGRAM_USER_ID
},
tiktok: {
client_key: process.env.TIKTOK_CLIENT_KEY,
client_secret: process.env.TIKTOK_CLIENT_SECRET,
access_token: process.env.TIKTOK_ACCESS_TOKEN
},
pinterest: {
access_token: process.env.PINTEREST_ACCESS_TOKEN,
board_id: process.env.PINTEREST_BOARD_ID
},
clickbank: {
api_key: process.env.CLICKBANK_API_KEY,
nickname: process.env.CLICKBANK_NICKNAME
},
ebay: {
access_token: process.env.EBAY_ACCESS_TOKEN,
client_id: process.env.EBAY_CLIENT_ID,
app_id: process.env.EBAY_CLIENT_ID,
dev_id: process.env.EBAY_DEV_ID,
cert_id: process.env.EBAY_CERT_ID
}
});
console.log('✅ Real eBay API credentials loaded from environment');
// Enhanced bot management with real posting
class BotManager {
constructor() {
this.activeBots = new Map();
this.botIntervals = new Map();
this.userConfigs = new Map();
}
async startBot(userId, platform, keywords = []) {
const user = users.get(userId);
if (!user || !user.activeBots) user.activeBots = {};
user.activeBots[platform] = true;
users.set(userId, user);
// Store user keywords/config
this.userConfigs.set(`${userId}_${platform}`, {
keywords: keywords.length > 0 ? keywords : ['trending', 'popular', 'bestseller'],
platforms: [platform]
});
// Start the bot cycle
const interval = setInterval(() => {
this.runRealBotCycle(userId, platform);
}, 60000); // Run every minute
this.botIntervals.set(`${userId}_${platform}`, interval);
// Emit to user
io.to(userId).emit('bot_activity', {
platform: platform,
message: 'Real bot started - posting to social media',
timestamp: Date.now()
});
return true;
}
stopBot(userId, platform) {
const user = users.get(userId);
if (user && user.activeBots) {
user.activeBots[platform] = false;
users.set(userId, user);
}
// Clear interval
const intervalKey = `${userId}_${platform}`;
if (this.botIntervals.has(intervalKey)) {
clearInterval(this.botIntervals.get(intervalKey));
this.botIntervals.delete(intervalKey);
}
// Emit to user
io.to(userId).emit('bot_activity', {
platform: platform,
message: 'Bot stopped',
timestamp: Date.now()
});
return true;
}
async runRealBotCycle(userId, platform) {
const user = users.get(userId);
if (!user || !user.activeBots || !user.activeBots[platform]) return;
const config = this.userConfigs.get(`${userId}_${platform}`);
if (!config) return;
try {
// Emit real processing activity
io.to(userId).emit('bot_activity', {
platform: platform,
message: 'Fetching real products from Amazon...',
timestamp: Date.now()
});
// Run real bot cycle with actual APIs
const keywords = config.keywords;
const searchResults = [];
for (const keyword of keywords) {
try {
const results = await botEngine.runBotCycle(keyword, config.platforms);
searchResults.push(...results);
// Rate limiting between keyword searches
await new Promise(resolve => setTimeout(resolve, 5000));
} catch (error) {
console.error(`Error processing keyword "${keyword}":`, error);
}
}
// Track real earnings
const earnings = await botEngine.trackAffiliateEarnings();
if (earnings.total > 0) {
// Update user's real earnings
user.totalEarnings = (user.totalEarnings || 0) + earnings.total;
user.availableBalance = (user.availableBalance || 0) + earnings.total;
user.totalRevenue = (user.totalRevenue || 0) + earnings.total;
user.totalProfit = (user.totalProfit || 0) + (earnings.total * 0.8); // 20% platform fee
users.set(userId, user);
// Emit real earning update
io.to(userId).emit('earning_update', {
source: platform,
amount: earnings.total,
breakdown: earnings,
timestamp: Date.now()
});
io.to(userId).emit('bot_activity', {
platform: platform,
message: `REAL EARNINGS: $${earnings.total.toFixed(2)} from ${searchResults.length} posts`,
timestamp: Date.now()
});
}
io.to(userId).emit('bot_activity', {
platform: platform,
message: `Real cycle completed. Posted ${searchResults.length} products to ${config.platforms.join(', ')}`,
results: searchResults,
timestamp: Date.now()
});
} catch (error) {
console.error(`Real bot cycle error for ${platform}:`, error);
io.to(userId).emit('bot_activity', {
platform: platform,
message: 'Real cycle failed - retrying in 5 minutes',
error: error.message,
timestamp: Date.now()
});
}
}
// Manual trigger for immediate execution
async runManualCycle(userId, platform, keyword) {
const config = this.userConfigs.get(`${userId}_${platform}`);
if (!config) {
throw new Error('Bot not started - start the bot first');
}
try {
io.to(userId).emit('bot_activity', {
platform: platform,
message: `Manual run: searching "${keyword}"`,
timestamp: Date.now()
});
const results = await botEngine.runBotCycle(keyword, [platform]);
const earnings = await botEngine.trackAffiliateEarnings();
return {
success: true,
posts: results,
earnings: earnings.total,
timestamp: Date.now()
};
} catch (error) {
throw error;
}
}
}
const botManager = new BotManager();
// API Routes
// Get user data
app.get('/api/user-data', (req, res) => {
const userId = req.query.userId || 'default_user';
if (!users.has(userId)) {
// Create new user
const newUser = {
id: userId,
totalEarnings: 0,
availableBalance: 0,
totalRevenue: 0,
totalProfit: 0,
activeBots: {},
botStats: {
processed: 0,
posted: 0,
earnings: 0,
errors: 0
},
cryptoWallets: [],
withdrawalHistory: [],
activities: []
};
users.set(userId, newUser);
}
res.json(users.get(userId));
});
// Track earnings
app.post('/api/track-earnings', async (req, res) => {
const { userId } = req.body;
try {
const earnings = await trackAffiliateEarnings(userId);
// Update user earnings
const user = users.get(userId);
if (user && earnings.length > 0) {
earnings.forEach(earning => {
user.totalEarnings += earning.amount;
user.availableBalance += earning.amount;
user.totalRevenue += earning.amount;
user.totalProfit += earning.amount * 0.8; // 20% platform fee
// Emit to user
io.to(userId).emit('earning_update', earning);
});
users.set(userId, user);
}
res.json({ success: true, earnings });
} catch (error) {
console.error('Error tracking earnings:', error);
res.json({ success: false, error: error.message });
}
});
// Crypto balance endpoint
app.get('/api/crypto-balance/:type/:address', async (req, res) => {
const { type, address } = req.params;
try {
const result = await getCryptoBalance(type, address);
res.json(result);
} catch (error) {
console.error('Error fetching crypto balance:', error);
res.json({ balance: 0, usdRate: 0, usdValue: 0 });
}
});
// Real bot control endpoints
app.post('/api/bot/:platform/:action', async (req, res) => {
const { platform, action } = req.params;
const { userId, keywords } = req.body;
try {
let success = false;
if (action === 'start') {
success = await botManager.startBot(userId, platform, keywords || []);
} else if (action === 'stop') {
success = botManager.stopBot(userId, platform);
}
res.json({ success, platform, action, message: `Real ${platform} bot ${action}ed` });
} catch (error) {
console.error('Real bot control error:', error);
res.json({ success: false, error: error.message });
}
});
// Run manual bot cycle
app.post('/api/bot/:platform/cycle', async (req, res) => {
const { platform } = req.params;
const { userId, keyword } = req.body;
try {
const result = await botManager.runManualCycle(userId, platform, keyword || 'trending products');
res.json(result);
} catch (error) {
console.error('Manual bot cycle error:', error);
res.json({ success: false, error: error.message });
}
});
// Get real bot status
app.get('/api/bot/:platform/status', (req, res) => {
const { platform } = req.params;
const { userId } = req.query;
const user = users.get(userId);
const isActive = user && user.activeBots && user.activeBots[platform];
const config = botManager.userConfigs.get(`${userId}_${platform}`);
res.json({
platform,
active: isActive,
config: config || null,
keywords: config ? config.keywords : [],
lastRun: Date.now()
});
});
// Withdrawal endpoint
app.post('/api/withdraw', async (req, res) => {
const withdrawalData = req.body;
const withdrawalId = 'withdraw_' + Date.now();
try {
// Process the actual payment
const paymentResult = await processWithdrawal(withdrawalData);
if (paymentResult.success) {
// Update user balance
const user = users.get(withdrawalData.userId);
if (user) {
user.availableBalance -= withdrawalData.amount;
users.set(withdrawalData.userId, user);
}
// Add to withdrawal history
const withdrawal = {
id: withdrawalId,
userId: withdrawalData.userId,
amount: withdrawalData.amount,
method: withdrawalData.method,
date: new Date().toISOString(),
status: 'completed',
transactionId: paymentResult.transactionId,
fee: paymentResult.fee,
processingTime: paymentResult.processingTime
};
withdrawals.set(withdrawalId, withdrawal);
// Update user's withdrawal history
if (user) {
if (!user.withdrawalHistory) user.withdrawalHistory = [];
user.withdrawalHistory.unshift(withdrawal);
users.set(withdrawalData.userId, user);
}
// Emit to user
io.to(withdrawalData.userId).emit('withdrawal_status', {
withdrawalId: withdrawalId,
status: 'completed',
amount: withdrawalData.amount,
transactionId: paymentResult.transactionId
});
res.json({ success: true, withdrawalId, transactionId: paymentResult.transactionId });
} else {
throw new Error('Payment processing failed');
}
} catch (error) {
console.error('Withdrawal error:', error);
// Emit failure to user
io.to(withdrawalData.userId).emit('withdrawal_status', {
withdrawalId: withdrawalId,
status: 'failed',
amount: withdrawalData.amount,
error: error.message
});
res.json({ success: false, error: error.message });
}
});
// Save settings
app.post('/api/save-settings', (req, res) => {
const { userId, paymentSettings, affiliateSettings } = req.body;
try {
const user = users.get(userId);
if (user) {
user.paymentSettings = paymentSettings;
user.affiliateSettings = affiliateSettings;
users.set(userId, user);
}
res.json({ success: true });
} catch (error) {
console.error('Save settings error:', error);
res.json({ success: false, error: error.message });
}
});
// Test connections
app.post('/api/test-connections', async (req, res) => {
const { userId } = req.body;
try {
const user = users.get(userId);
const results = {
stripe: false,
paypal: false,
amazon: false,
clickbank: false
};
// Test Stripe connection
if (user?.paymentSettings?.stripeAccountId) {
try {
const account = await stripe.accounts.retrieve(user.paymentSettings.stripeAccountId);
results.stripe = account.charges_enabled;
} catch (error) {
console.log('Stripe test failed:', error.message);
}
}
// Test PayPal connection
if (user?.paymentSettings?.paypalEmail) {
// PayPal connection test would go here
results.paypal = true; // Mock for now
}
// Test affiliate connections
if (user?.affiliateSettings?.amazonTag) {
results.amazon = true; // Mock for now
}
if (user?.affiliateSettings?.clickbankApiKey) {
results.clickbank = true; // Mock for now
}
res.json({ success: true, results });
} catch (error) {
console.error('Test connections error:', error);
res.json({ success: false, error: error.message });
}
});
// Socket.IO connection handling
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
socket.on('join_user', (userId) => {
socket.join(userId);
console.log(`User ${userId} joined room`);
});
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
});
});
// Background earnings tracking
setInterval(async () => {
for (const [userId, user] of users) {
try {
const earnings = await trackAffiliateEarnings(userId);
if (earnings.length > 0) {
earnings.forEach(earning => {
user.totalEarnings += earning.amount;
user.availableBalance += earning.amount;
user.totalRevenue += earning.amount;
user.totalProfit += earning.amount * 0.8;
// Emit earning update
io.to(userId).emit('earning_update', earning);
});
users.set(userId, user);
}
} catch (error) {
console.error(`Background earning tracking error for user ${userId}:`, error);
}
}
}, 30000); // Every 30 seconds
// Check if server is already running
const net = require('net');
let serverStarted = false;
function isPortInUse(port) {
return new Promise((resolve) => {
const server = net.createServer();
server.listen(port, (err) => {
if (err) {
resolve(true); // Port is in use
} else {
server.once('close', () => resolve(false));
server.close();
}
});
server.on('error', () => resolve(true));
});
}
// Server startup endpoint
app.post('/start-server', async (req, res) => {
try {
const port = process.env.PORT || 3000;
const inUse = await isPortInUse(port);
if (!inUse) {
console.log('Port available, server can start');
res.json({ success: true, message: 'Server ready to start' });
} else {
console.log('Port already in use, server may already be running');
res.json({ success: true, message: 'Server may already be running' });
}
} catch (error) {
console.error('Error checking server status:', error);
res.json({ success: false, error: error.message });
}
});
app.get('/server-status', async (req, res) => {
const port = process.env.PORT || 3000;
const inUse = await isPortInUse(port);
res.json({
running: inUse,
port: port,
timestamp: Date.now()
});
});
// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`🚀 Real Money Earning Server running on port ${PORT}`);
console.log(`💰 Ready to process real payments and track actual earnings!`);
console.log(`🌐 Open: http://localhost:${PORT}/launch.html to start earning!`);
console.log(`🎯 Or open: http://localhost:${PORT} for direct dashboard access`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('Shutting down server...');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});
module.exports = { app, server, io };