Initial Commit
This commit is contained in:
commit
7e7b6d37f1
15 files changed
+2313
No files matched your search
@@ -0,0 +1,4 @@
|
|||||||
|
dist/
|
||||||
|
node_modules/
|
||||||
|
.idea/
|
||||||
|
.env
|
||||||
Generated
+1875
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"name": "cdn-resolver",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "CDN Server for cdn.itz.sh",
|
||||||
|
"license": "MIT",
|
||||||
|
"author": "itzMiney",
|
||||||
|
"type": "commonjs",
|
||||||
|
"main": "dist/server.js",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"start": "tsx src/server.ts",
|
||||||
|
"dev": "tsx watch src/server.ts",
|
||||||
|
"check": "tsc --noEmit"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"dotenv": "^18.0.9",
|
||||||
|
"express": "^5.3.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/express": "^5.0.6",
|
||||||
|
"@types/node": "^26.6.5",
|
||||||
|
"tsx": "^4.23.15",
|
||||||
|
"typescript": "^7.0.2"
|
||||||
|
},
|
||||||
|
"allowScripts": {
|
||||||
|
"esbuild@0.28.2": true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { Router } from 'express';
|
||||||
|
import {CdnResolver} from "./services/resolver.service";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.get('/{*splat}', (req, res) => {
|
||||||
|
return CdnResolver.handleRequest(req.path, req.query, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
export const cdnRouter = router;
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import 'dotenv/config';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
const STORAGE_ROOT = process.env.STORAGE_ROOT || '/srv/storage/cdn';
|
||||||
|
|
||||||
|
export const CONFIG = {
|
||||||
|
HOST: process.env.HOST || 'localhost',
|
||||||
|
PORT: Number(process.env.PORT) || 6020,
|
||||||
|
ROOT_DOMAIN: process.env.ROOT_DOMAIN || 'http://localhost',
|
||||||
|
STORAGE_ROOT,
|
||||||
|
STORAGE_DIR: path.join(STORAGE_ROOT, 'cdn'),
|
||||||
|
THUMB_DIR: path.join(STORAGE_ROOT, 'cache/thumbnails'),
|
||||||
|
RESIZE_DIR: path.join(STORAGE_ROOT, 'cache/resized'),
|
||||||
|
SUPPORTED_IMAGES: new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif']),
|
||||||
|
SUPPORTED_VIDEOS: new Set(['.mp4', '.webm', '.mov', '.mkv'])
|
||||||
|
};
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=6020
|
||||||
|
ROOT_DOMAIN=https://cdn.itz.sh
|
||||||
|
STORAGE_ROOT=/srv/storage
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import './config/config';
|
||||||
|
import express from 'express';
|
||||||
|
import { CONFIG } from './config/config';
|
||||||
|
import { cdnRouter } from './cdn.routes';
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use('/', cdnRouter);
|
||||||
|
|
||||||
|
app.listen(CONFIG.PORT, CONFIG.HOST, () => {
|
||||||
|
console.log(`CDN server listening on 0.0.0.0:${CONFIG.PORT}`);
|
||||||
|
});
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
export interface DiscordButton {
|
||||||
|
label: string;
|
||||||
|
url: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DiscordFileEmbedConfig {
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
currentUrl: string;
|
||||||
|
filename: string;
|
||||||
|
fileSize?: string;
|
||||||
|
thumbnailUrl?: string;
|
||||||
|
buttons?: DiscordButton[];
|
||||||
|
siteColor?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class EmbedService {
|
||||||
|
static generateFileComponentPayload(config: DiscordFileEmbedConfig): object {
|
||||||
|
const siteColor = config.siteColor || '#0095a4';
|
||||||
|
const containerComponents: any[] = [];
|
||||||
|
|
||||||
|
const section: any = {
|
||||||
|
type: 9,
|
||||||
|
components: [
|
||||||
|
{
|
||||||
|
type: 10,
|
||||||
|
content: `### [${config.title}](${config.currentUrl})\n${config.description}`
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
if (config.thumbnailUrl) {
|
||||||
|
section.accessory = {
|
||||||
|
type: 11,
|
||||||
|
media: { url: config.thumbnailUrl }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
containerComponents.push(section);
|
||||||
|
|
||||||
|
containerComponents.push({
|
||||||
|
type: 13,
|
||||||
|
file: {
|
||||||
|
url: `attachment://${config.filename}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config.buttons && config.buttons.length > 0) {
|
||||||
|
containerComponents.push({
|
||||||
|
type: 1,
|
||||||
|
components: config.buttons.map(btn => ({
|
||||||
|
type: 2,
|
||||||
|
label: btn.label,
|
||||||
|
style: 5,
|
||||||
|
url: btn.url
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
flags: 32768,
|
||||||
|
component: {
|
||||||
|
type: 17,
|
||||||
|
accent_color: parseInt(siteColor.replace('#', ''), 16),
|
||||||
|
components: containerComponents
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import crypto from 'crypto';
|
||||||
|
import { execFileSync } from 'child_process';
|
||||||
|
import { THUMB_DIR, RESIZE_DIR } from '../config';
|
||||||
|
import { safeResolveIn } from '../utils/path.utils';
|
||||||
|
|
||||||
|
export class MediaService {
|
||||||
|
static getVideoDimensions(videoPath: string): { width: number; height: number } {
|
||||||
|
try {
|
||||||
|
const out = execFileSync('/usr/bin/ffprobe', [
|
||||||
|
'-v', 'error',
|
||||||
|
'-select_streams', 'v:0',
|
||||||
|
'-show_entries', 'stream=width,height',
|
||||||
|
'-of', 'csv=s=x:p=0',
|
||||||
|
videoPath
|
||||||
|
], { encoding: 'utf8' }).trim();
|
||||||
|
|
||||||
|
const [w, h] = out.split('x').map(Number);
|
||||||
|
if (w > 0 && h > 0) return { width: w, height: h };
|
||||||
|
} catch {}
|
||||||
|
return { width: 1280, height: 720 };
|
||||||
|
}
|
||||||
|
|
||||||
|
static getOrCreateVideoThumb(videoPath: string, relVideoUrlPath: string): string | null {
|
||||||
|
try {
|
||||||
|
const stat = fs.statSync(videoPath);
|
||||||
|
const parsedRel = path.posix.parse(relVideoUrlPath);
|
||||||
|
const cacheThumbPath = safeResolveIn(THUMB_DIR, path.posix.join(parsedRel.dir, `${parsedRel.name}.webp`));
|
||||||
|
|
||||||
|
if (!cacheThumbPath) return null;
|
||||||
|
|
||||||
|
const needsBuild = !fs.existsSync(cacheThumbPath) || fs.statSync(cacheThumbPath).mtimeMs < stat.mtimeMs;
|
||||||
|
|
||||||
|
if (needsBuild) {
|
||||||
|
fs.mkdirSync(path.dirname(cacheThumbPath), { recursive: true });
|
||||||
|
try {
|
||||||
|
execFileSync('/usr/bin/ffmpeg', [
|
||||||
|
'-ss', '00:00:03',
|
||||||
|
'-i', videoPath,
|
||||||
|
'-vframes', '1',
|
||||||
|
'-vf', 'scale=1280:720:force_original_aspect_ratio=decrease',
|
||||||
|
cacheThumbPath,
|
||||||
|
'-y'
|
||||||
|
], { stdio: 'ignore' });
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (!fs.existsSync(cacheThumbPath)) {
|
||||||
|
execFileSync('/usr/bin/ffmpeg', [
|
||||||
|
'-i', videoPath,
|
||||||
|
'-vframes', '1',
|
||||||
|
'-vf', 'scale=1280:720:force_original_aspect_ratio=decrease',
|
||||||
|
cacheThumbPath,
|
||||||
|
'-y'
|
||||||
|
], { stdio: 'ignore' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fs.existsSync(cacheThumbPath) ? cacheThumbPath : null;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to generate thumbnail:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static getResizedImage(filePath: string, reqPath: string, rawWidth: string, fileMtime: number, ext: string): string | null {
|
||||||
|
const width = Math.min(Math.max(parseInt(rawWidth, 10) || 0, 16), 2560);
|
||||||
|
if (!width) return null;
|
||||||
|
|
||||||
|
const cacheKey = crypto
|
||||||
|
.createHash('sha1')
|
||||||
|
.update(`${reqPath}:${fileMtime}:${width}`)
|
||||||
|
.digest('hex');
|
||||||
|
const cachedFile = path.join(RESIZE_DIR, `${cacheKey}${ext}`);
|
||||||
|
|
||||||
|
if (!fs.existsSync(cachedFile)) {
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(RESIZE_DIR, { recursive: true });
|
||||||
|
execFileSync('/usr/bin/convert', [
|
||||||
|
filePath,
|
||||||
|
'-coalesce',
|
||||||
|
'-resize', `${width}x>`,
|
||||||
|
cachedFile
|
||||||
|
], { stdio: 'ignore' });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to resize image:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fs.existsSync(cachedFile) ? cachedFile : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import { Response } from 'express';
|
||||||
|
import { STORAGE_DIR, ROOT_DOMAIN, SUPPORTED_VIDEOS, SUPPORTED_IMAGES } from '../config';
|
||||||
|
import { safeResolveIn } from '../utils/path.utils';
|
||||||
|
import { MediaService } from './media.service';
|
||||||
|
import { EmbedService } from './embed.service';
|
||||||
|
import { send403, send404, renderVideoEmbed } from '../views/templates';
|
||||||
|
|
||||||
|
export class CdnResolver {
|
||||||
|
static handleRequest(reqPath: string, query: any, res: Response) {
|
||||||
|
let filePath = safeResolveIn(STORAGE_DIR, reqPath);
|
||||||
|
|
||||||
|
if (!filePath) {
|
||||||
|
return send403(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fs.existsSync(filePath)) {
|
||||||
|
return send404(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
const stat = fs.statSync(filePath);
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
return send403(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ext = path.extname(filePath).toLowerCase();
|
||||||
|
const filename = path.basename(filePath);
|
||||||
|
const encodedPath = reqPath.split('/').map(encodeURIComponent).join('/');
|
||||||
|
const displayUrl = `${ROOT_DOMAIN}${encodedPath}`;
|
||||||
|
|
||||||
|
// --- A. IMAGES: Raw inline passthrough or dynamic resize ---
|
||||||
|
if (SUPPORTED_IMAGES.has(ext)) {
|
||||||
|
const rawWidth = (query.w || query.width) as string | undefined;
|
||||||
|
if (rawWidth) {
|
||||||
|
const cachedFile = MediaService.getResizedImage(filePath, reqPath, rawWidth, stat.mtimeMs, ext);
|
||||||
|
if (cachedFile) {
|
||||||
|
res.setHeader('Cache-Control', 'public, max-age=2592000');
|
||||||
|
return res.sendFile(cachedFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res.sendFile(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- B. VIDEOS: Existing video web player / raw handling ---
|
||||||
|
if (SUPPORTED_VIDEOS.has(ext)) {
|
||||||
|
if (query.raw === '1') {
|
||||||
|
return res.sendFile(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
MediaService.getOrCreateVideoThumb(filePath, reqPath);
|
||||||
|
const { width, height } = MediaService.getVideoDimensions(filePath);
|
||||||
|
|
||||||
|
const parsedUrl = path.posix.parse(reqPath);
|
||||||
|
const webpUrlPath = path.posix.join(parsedUrl.dir, `${parsedUrl.name}.webp`);
|
||||||
|
const imageUrl = `${ROOT_DOMAIN}${webpUrlPath.split('/').map(encodeURIComponent).join('/')}`;
|
||||||
|
const rawVideoUrl = `${displayUrl}?raw=1`;
|
||||||
|
const mimeType = ext === '.webm' ? 'video/webm' : 'video/mp4';
|
||||||
|
|
||||||
|
res.setHeader('Cache-Control', 'no-store');
|
||||||
|
return res.send(renderVideoEmbed({
|
||||||
|
filename,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
displayUrl,
|
||||||
|
rawVideoUrl,
|
||||||
|
imageUrl,
|
||||||
|
mimeType
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- C. GENERAL FILES: Discord File Components v2 ---
|
||||||
|
if (query.discord_embed === '1') {
|
||||||
|
const fileSizeMB = (stat.size / 1024 / 1024).toFixed(2);
|
||||||
|
const payload = EmbedService.generateFileComponentPayload({
|
||||||
|
title: filename,
|
||||||
|
description: `File size: ${fileSizeMB} MB`,
|
||||||
|
currentUrl: displayUrl,
|
||||||
|
filename: filename,
|
||||||
|
buttons: [
|
||||||
|
{ label: 'Download Raw', url: `${displayUrl}?raw=1` },
|
||||||
|
{ label: 'View on Web', url: displayUrl }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
res.setHeader('Content-Type', 'application/json');
|
||||||
|
return res.send(JSON.stringify(payload, null, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default catch-all for raw non-media file requests
|
||||||
|
return res.sendFile(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
export function safeResolveIn(baseDir: string, reqPath: string): string | null {
|
||||||
|
try {
|
||||||
|
const decoded = decodeURIComponent(reqPath);
|
||||||
|
const resolved = path.resolve(baseDir, '.' + decoded);
|
||||||
|
if (!resolved.startsWith(baseDir + path.sep) && resolved !== baseDir) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return resolved;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" style="background: #71222b; display: flex; min-height: 100vh; justify-content: center; align-items: center; text-align: center; z-index: 10;">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>403 Forbidden</title>
|
||||||
|
</head>
|
||||||
|
<body style="margin: 0; font-family: Roboto, Helvetica Neue, sans-serif;">
|
||||||
|
<h1 style="font-size: 10rem; margin: 0; font-weight: 700; color: white;">403</h1>
|
||||||
|
<p style="font-size: 1.5rem; margin: 0; font-weight: 400; color: white;">You shall not pass!</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" style="background: #752d36; display: flex; min-height: 100vh; justify-content: center; align-items: center; text-align: center; z-index: 10;">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>404 Not found</title>
|
||||||
|
</head>
|
||||||
|
<body style="margin: 0; font-family: Roboto, Helvetica Neue, sans-serif;">
|
||||||
|
<h1 style="font-size: 10rem; margin: 0; font-weight: 700; color: white;">404</h1>
|
||||||
|
<p style="font-size: 1.5rem; margin: 0; font-weight: 400; color: white;">These are not the files you are looking for</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import { Response } from 'express';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
export function send403(res: Response) {
|
||||||
|
return res.status(403).sendFile(path.join(__dirname, '../views/403.html'));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function send404(res: Response) {
|
||||||
|
return res.status(404).sendFile(path.join(__dirname, '../views/404.html'));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function renderVideoEmbed(data: {
|
||||||
|
filename: string;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
displayUrl: string;
|
||||||
|
rawVideoUrl: string;
|
||||||
|
imageUrl: string;
|
||||||
|
mimeType: string;
|
||||||
|
}): string {
|
||||||
|
return `<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta property="og:title" content="${data.filename.replace(/"/g, '"')}" />
|
||||||
|
<meta property="og:site_name" content="itzMiney Filehost" />
|
||||||
|
<meta property="og:type" content="video.other" />
|
||||||
|
<meta property="og:url" content="${data.displayUrl}" />
|
||||||
|
<meta property="og:video" content="${data.rawVideoUrl}" />
|
||||||
|
<meta property="og:video:url" content="${data.rawVideoUrl}" />
|
||||||
|
<meta property="og:video:type" content="${data.mimeType}" />
|
||||||
|
<meta property="og:video:width" content="${data.width}" />
|
||||||
|
<meta property="og:video:height" content="${data.height}" />
|
||||||
|
<meta property="og:image" content="${data.imageUrl}" />
|
||||||
|
<meta property="og:image:width" content="${data.width}" />
|
||||||
|
<meta property="og:image:height" content="${data.height}" />
|
||||||
|
<meta name="theme-color" content="#0095a4" />
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
width: 100vw;
|
||||||
|
height: 100vh;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
background-color: #0d1117;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
video {
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 100vh;
|
||||||
|
object-fit: contain;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<video controls autoplay muted playsinline src="${data.rawVideoUrl}" type="${data.mimeType}"></video>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2022",
|
||||||
|
"module": "commonjs",
|
||||||
|
"rootDir": "./src",
|
||||||
|
"outDir": "./dist",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true
|
||||||
|
},
|
||||||
|
"include": ["src/**/*"]
|
||||||
|
}
|
||||||
Reference in new issue
Block a user