Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
update f2
Browse files Browse the repository at this point in the history
  • Loading branch information
hexahigh committed Mar 3, 2024
1 parent d1bbcd0 commit d8a55d0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 19 deletions.
6 changes: 3 additions & 3 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let uploadCount;
let ep = endpoint;
let doArchive = false;
let direct = true;
let direct = false;
let useS256 = false;
let shortenLinks = [];
Expand Down Expand Up @@ -365,10 +365,10 @@
</label>
<label
class="flex items-center mt-4"
title="Download directly from the endpoint instead of using the proxy"
title="Downloads directly from the primary server"
>
<input type="checkbox" bind:checked={direct} class="form-checkbox" />
<span class="ml-2">Direct download</span>
<span class="ml-2">Direct</span>
</label>
<p id="status" class="mt-4 text-center">{status}</p>
{#if uploadProgress > 0 && uploadProgress < 100}
Expand Down
100 changes: 84 additions & 16 deletions frontend/src/routes/f2/+server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,87 @@
import { endpoint } from '$lib/conf.js';

export async function GET({ url }) {
// Extract the HASH and the file extension from the url
const hash = url.searchParams.get('h') || '0';
const ext = url.searchParams.get('e') || 'bin';
const filename = url.searchParams.get('f') || 'file.bin';

// Construct the URL to the file
const fileUrl = `${endpoint}/get2/?h=${hash}&e=${ext}&f=${filename}`;

// Create a 301 redirect response
return new Response(null, {
status: 301,
headers: {
'Location': fileUrl
}
});
export async function GET({ url, headers }) {
// Extract the HASH and the file extension from the url
const hash = url.searchParams.get('h') || '0';
const ext = url.searchParams.get('e') || 'bin';
const filename = url.searchParams.get('f') || 'file.bin';

const servers = [
{
name: "NO1",
url: "https://pomf1.080609.xyz",
lat: "59.2083",
lon: "10.9484"
}
];

const ip = headers.get('x-forwarded-for') || 'unknown';

// Use ip-api.com to get the latitude and longitude of the IP address
const response = await fetch(`http://ip-api.com/json/${ip}`);
const data = await response.json();
const clientLat = data.lat;
const clientLon = data.lon;

// Function to calculate the distance between two points using the Haversine formula
function calculateDistance(lat1, lon1, lat2, lon2) {
const R = 6371; // Radius of the earth in km
const dLat = deg2rad(lat2 - lat1);
const dLon = deg2rad(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const d = R * c; // Distance in km
return d;
}

function deg2rad(deg) {
return deg * (Math.PI / 180);
}

// Find the closest server
let closestServer = servers[0];
let shortestDistance = calculateDistance(clientLat, clientLon, closestServer.lat, closestServer.lon);

for (let server of servers) {
const distance = calculateDistance(clientLat, clientLon, server.lat, server.lon);
if (distance < shortestDistance) {
closestServer = server;
shortestDistance = distance;
}
}

// Function to send a request with a timeout
async function fetchWithTimeout(url, options, timeout = 2000) {
return Promise.race([
fetch(url, options),
new Promise((_, reject) =>
setTimeout(() => reject(new Error('Timeout')), timeout)
)
]);
}

try {
// Send a request to the closest server's health endpoint
await fetchWithTimeout(`${closestServer.url}/health`);
} catch (error) {
// If the request fails or times out, switch to another server
console.log('Failed to reach the closest server, switching to another server...');
// Implement logic to switch to another server here
// For simplicity, let's just select the next server in the list
closestServer = servers.find(server => server.url !== closestServer.url) || servers[0];
}

// Construct the URL to the file on the selected server
const fileUrl = `${closestServer.url}/get2/?h=${hash}&e=${ext}&f=${filename}`;

// Create a 301 redirect response
return new Response(null, {
status: 301,
headers: {
'Location': fileUrl
}
});
}

0 comments on commit d8a55d0

Please sign in to comment.