ryu-js / index.js
randydev's picture
Update index.js
6c8d3a7 verified
raw
history blame
5.31 kB
const express = require('express')
const app = express()
const port = 7860
const axios = require('axios')
const { connectToMongoDB } = require('./database');
const startup = async () => {
try {
console.log("Starting application...");
await connectToMongoDB();
console.log("MongoDB connected successfully.");
} catch (error) {
console.error("Error during startup:", error.message);
process.exit(1);
}
};
const startServer = async () => {
await startup();
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
};
const IPAddressAndUpdate = async (ip) => {
try {
const db = await connectToMongoDB();
const collection = db.collection("FastJsAPI");
const filter = { ip: ip };
const update = { $set: { ip: ip } };
const result = await collection.updateOne(filter, update, { upsert: true });
if (result.upsertedCount > 0) {
console.log("Inserted a new IP address:", ip);
} else {
console.log("Updated an existing IP address:", ip);
}
} catch (error) {
console.error("Error updating IP address:", error.message);
}
};
const AddIpisBlocked = async (ip) => {
try {
const db = await connectToMongoDB();
const collection = db.collection("FastJsAPI");
const filter = { ip: ip };
const update = { $set: { blocked: true } };
const result = await collection.updateOne(filter, update, { upsert: true });
if (result.upsertedCount > 0) {
console.log("Inserted a new IP address:", ip);
} else {
console.log("Updated an existing IP address:", ip);
}
} catch (error) {
console.error("Error updating IP address:", error.message);
}
};
const CheckIsBlocked = async (ip) => {
try {
const db = await connectToMongoDB();
const collection = db.collection("FastJsAPI");
const filter = { ip: ip };
const FindIp = await collection.findOne(filter);
if (FindIp) {
console.log("IP found in the database:", FindIp);
return FindIp;
} else {
console.log("IP not found in the database");
return null;
}
} catch (error) {
console.error("Error checking IP:", error.message);
return null;
}
};
app.get('/', (req, res) => {
res.redirect('https://t.me/RendyProjects');
});
app.use(async (req, res, next) => {
const xForwardedFor = req.headers['x-forwarded-for'];
const xRealIP = req.headers['x-real-ip'];
const cfConnectingIP = req.headers['cf-connecting-ip'];
let realIP = req.ip;
if (xForwardedFor) {
realIP = xForwardedFor.split(',')[0].trim();
}
else if (xRealIP){
realIP = xRealIP;
}
else if (cfConnectingIP) {
realIP = cfConnectingIP;
}
req.realIP = realIP;
const isBlocked = await CheckIsBlocked(realIP)
if (isBlocked && isBlocked.blocked === true) {
return res.status(403).send("Access denied: IP is blocked");
}
if (req.path === '/env') {
console.log("check path /env");
await AddIpisBlocked(realIP)
return res.status(403).send("Access denied: IP is blocked..");
}
await IPAddressAndUpdate(realIP);
console.log(`Real IP address is: ${realIP}, header: ${xForwardedFor ? "x-forwarded-for" : xRealIP ? "x-real-ip" : cfConnectingIP ? "cf-connecting-ip" : "req.ip"} `);
await next();
});
app.get('/api/test', async (req, res) => {
try {
// console.log("Test", req)
res.send("Hello world")
} catch (error) {
res.status(401).json({error: error.message})
}
})
const payload = (prompt) => ({
model: "gpt-4o",
prompt: prompt,
top_p: 1,
logprobs: null,
stop: null
});
const OpenaiRes = async (prompt) => {
try {
const EncodeUrl = "aHR0cHM6Ly9vcGVuYWktZ3B0LnJlbWl4cHJvamVjdC5vcmcv"
let url;
try {
url = atob(EncodeUrl);
} catch (e) {
console.error("Could not decode the string! " + e);
}
const headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Accept-Language": "en",
"Connection": "keep-alive",
"Origin": "https://remix.ethereum.org",
"Referer": "https://remix.ethereum.org/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134"
};
const response = await axios.post(url, payload(prompt), { headers, timeout: 50000 });
if (response.status === 200) {
return response.data.choices[0].message.content + "\n\nPowered By xtdevs";
}
// console.log("Response:", response)
} catch (error) {
console.error("Error:", error.message);
return null;
}
};
app.get('/api/gpt-old', async (req, res) => {
try {
const query = req.query.query;
const results = await OpenaiRes(query);
res.json({ results });
} catch (error) {
res.status(401).json({ error: error.message });
}
});
startServer();