-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (88 loc) · 3.48 KB
/
index.js
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
import {
PangeaConfig,
DomainIntelService,
PangeaErrors,
} from "pangea-node-sdk";
import express from 'express';
import cors from 'cors';
import { createClient } from "@supabase/supabase-js";
import { config as envConfig } from "dotenv";
envConfig();
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
// Additional error handling or cleanup logic can go here
// Note: Continuing the application after an unhandled exception is risky
});
// Initialize Supabase client with your Supabase project URL and public API key
const supabase = createClient(process.env.SUPABASE_URL || "", process.env.SUPABASE_KEY || "");
const app = express();
const port = 5000; // You can change the port as needed
app.use(cors());
app.use(express.json());
const domain = process.env.PANGEA_DOMAIN;
const token = process.env.PANGEA_CLIENT_TOKEN;
const config = new PangeaConfig({ domain: domain });
const domainIntel = new DomainIntelService(String(token), config);
// Route to handle product submissions
app.post('/submit-tool', async (req, res) => {
const { productUrl, description, mail } = req.body;
const urlObject = new URL(productUrl);
const domainUrl = urlObject.hostname;
try {
const response = await domainIntel.reputation(domainUrl, { provider: "domaintools" });
if (response.status === "Success" &&
(response.result.data.verdict === "unknown" || response.result.data.verdict === "benign")) {
const supabaseResponse = await saveToSupabase(productUrl, description, mail);
if (supabaseResponse.error) {
console.error('Error saving data to Supabase:', supabaseResponse.error);
res.status(500).json({ error: 'Error in saving tool' });
} else {
console.log('Data saved to Supabase successfully:', supabaseResponse.data);
res.json({ success: 'Tool submitted successfully' });
}
} else {
console.log('Malicious domain detected. Data not saved.');
res.status(403).json({ error: 'Malicious domain detected. Data not saved.' });
}
} catch (e) {
console.error('Error checking domain reputation:', e.message);
res.status(500).json({ error: 'Error checking domain reputation' });
}
});
app.get('/get-all-tools', async (req, res) => {
try {
const { data, error } = await supabase.from('products').select('*');
if (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: 'Error fetching data' });
} else {
res.json(data);
}
} catch (error) {
console.error('Error:', error.message);
res.status(500).json({ error: 'Internal Server Error' });
}
});
const saveToSupabase = async (productUrl, description, mail) => {
try {
const { data, error } = await supabase
.from('products')
.upsert([
{
product_url: productUrl,
product_description: description,
user_mail: mail
},
]);
if (error) {
return { error: 'Error inserting data into Supabase' };
} else {
return { data: 'data inserted successfully' };
}
} catch (error) {
return { error: 'Error inserting data into Supabase' };
}
}
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});