trust proxy
This commit is contained in:
@@ -2,6 +2,12 @@
|
|||||||
HOST=0.0.0.0
|
HOST=0.0.0.0
|
||||||
PORT=3000
|
PORT=3000
|
||||||
|
|
||||||
|
# Public base URL of the site (the domain your users see, including scheme,
|
||||||
|
# WITHOUT a trailing slash). When set, share links returned by the API and the
|
||||||
|
# dashboard are absolute URLs (e.g. https://files.example.com/xmK5lLMRErlN).
|
||||||
|
# Leave unset to keep relative paths (the browser host is used).
|
||||||
|
PUBLIC_BASE_URL=https://files.example.com
|
||||||
|
|
||||||
# Storage: persistent filesystem directory where uploaded files and
|
# Storage: persistent filesystem directory where uploaded files and
|
||||||
# metadata live. Files survive restarts.
|
# metadata live. Files survive restarts.
|
||||||
STORAGE_DIR=./storage
|
STORAGE_DIR=./storage
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
cfg = config.services.downloadwf;
|
cfg = config.services.download;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.services.downloadwf = {
|
options.services.download = {
|
||||||
enable = lib.mkEnableOption "the File Drop private file host";
|
enable = lib.mkEnableOption "the File Drop private file host";
|
||||||
|
|
||||||
user = lib.mkOption {
|
user = lib.mkOption {
|
||||||
@@ -25,7 +25,7 @@ in
|
|||||||
storageDir = lib.mkOption {
|
storageDir = lib.mkOption {
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
default = "/srv/download/storage";
|
default = "/srv/download/storage";
|
||||||
description = "Where files and metadata are persisted.";
|
description = "Persistent directory where files and metadata are stored.";
|
||||||
};
|
};
|
||||||
|
|
||||||
host = lib.mkOption {
|
host = lib.mkOption {
|
||||||
@@ -52,7 +52,25 @@ in
|
|||||||
description = "Milliseconds before an uploaded file expires and is deleted.";
|
description = "Milliseconds before an uploaded file expires and is deleted.";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
publicBaseUrl = lib.mkOption {
|
||||||
|
type = lib.types.nullOr lib.types.str;
|
||||||
|
default = null;
|
||||||
|
description = "Public domain of the site (e.g. https://files.example.com).";
|
||||||
|
};
|
||||||
|
|
||||||
trustProxy = lib.mkEnableOption "trust of X-Forwarded-* headers (behind a reverse proxy)";
|
trustProxy = lib.mkEnableOption "trust of X-Forwarded-* headers (behind a reverse proxy)";
|
||||||
|
|
||||||
|
repoUrl = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
default = "https://github.com/Kisakay/download.git";
|
||||||
|
description = "Git repository to clone/pull from.";
|
||||||
|
};
|
||||||
|
|
||||||
|
deployOnBoot = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = true;
|
||||||
|
description = "Pull the repo at boot (like the nightcord-deploy pattern).";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
@@ -60,11 +78,39 @@ in
|
|||||||
"d ${cfg.storageDir} 0755 ${cfg.user} ${cfg.user} -"
|
"d ${cfg.storageDir} 0755 ${cfg.user} ${cfg.user} -"
|
||||||
];
|
];
|
||||||
|
|
||||||
systemd.services.downloadwf = {
|
systemd.services.download-deploy = {
|
||||||
description = "File Drop private file host";
|
description = "Update download from git";
|
||||||
wantedBy = [ "multi-user.target" ];
|
|
||||||
after = [ "network-online.target" ];
|
after = [ "network-online.target" ];
|
||||||
wants = [ "network-online.target" ];
|
wants = [ "network-online.target" ];
|
||||||
|
wantedBy = lib.mkIf cfg.deployOnBoot [ "multi-user.target" ];
|
||||||
|
|
||||||
|
path = with pkgs; [ git bun ];
|
||||||
|
|
||||||
|
serviceConfig = {
|
||||||
|
Type = "oneshot";
|
||||||
|
User = cfg.user;
|
||||||
|
WorkingDirectory = cfg.workingDir;
|
||||||
|
Environment = [ "HOME=/home/${cfg.user}" ];
|
||||||
|
ExecStartPost = "+${pkgs.systemd}/bin/systemctl restart download.service";
|
||||||
|
};
|
||||||
|
|
||||||
|
script = ''
|
||||||
|
set -euo pipefail
|
||||||
|
if [ ! -d "${cfg.workingDir}/.git" ]; then
|
||||||
|
git clone ${cfg.repoUrl} ${cfg.workingDir}
|
||||||
|
else
|
||||||
|
git -C ${cfg.workingDir} pull --ff-only
|
||||||
|
fi
|
||||||
|
cd ${cfg.workingDir}
|
||||||
|
bun install
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
systemd.services.download = {
|
||||||
|
description = "File Drop private file host";
|
||||||
|
after = [ "network-online.target" ];
|
||||||
|
wants = [ "network-online.target" ];
|
||||||
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
|
||||||
path = with pkgs; [ bun ];
|
path = with pkgs; [ bun ];
|
||||||
|
|
||||||
@@ -73,22 +119,23 @@ in
|
|||||||
User = cfg.user;
|
User = cfg.user;
|
||||||
Group = cfg.user;
|
Group = cfg.user;
|
||||||
WorkingDirectory = cfg.workingDir;
|
WorkingDirectory = cfg.workingDir;
|
||||||
Environment = [
|
Environment =
|
||||||
"HOME=/home/${cfg.user}"
|
[
|
||||||
"PORT=${toString cfg.port}"
|
"HOME=/home/${cfg.user}"
|
||||||
"HOST=${cfg.host}"
|
"PORT=${toString cfg.port}"
|
||||||
"STORAGE_DIR=${cfg.storageDir}"
|
"HOST=${cfg.host}"
|
||||||
"MAX_FILE_SIZE=${cfg.maxFileSize}"
|
"STORAGE_DIR=${cfg.storageDir}"
|
||||||
"FILE_TTL_MS=${toString cfg.fileTtlMs}"
|
"MAX_FILE_SIZE=${cfg.maxFileSize}"
|
||||||
] ++ lib.optionals cfg.trustProxy [ "TRUST_PROXY=1" ];
|
"FILE_TTL_MS=${toString cfg.fileTtlMs}"
|
||||||
|
]
|
||||||
|
++ lib.optionals (cfg.publicBaseUrl != null) [ "PUBLIC_BASE_URL=${cfg.publicBaseUrl}" ]
|
||||||
|
++ lib.optionals cfg.trustProxy [ "TRUST_PROXY=1" ];
|
||||||
|
|
||||||
# no runtime deps, so no `bun install` needed at startup
|
|
||||||
ExecStart = "${pkgs.bun}/bin/bun src/server.ts";
|
ExecStart = "${pkgs.bun}/bin/bun src/server.ts";
|
||||||
Restart = "on-failure";
|
Restart = "on-failure";
|
||||||
RestartSec = 3;
|
RestartSec = 3;
|
||||||
TimeoutStopSec = 30;
|
TimeoutStopSec = 30;
|
||||||
|
|
||||||
# hardening
|
|
||||||
NoNewPrivileges = true;
|
NoNewPrivileges = true;
|
||||||
PrivateTmp = true;
|
PrivateTmp = true;
|
||||||
ProtectSystem = "full";
|
ProtectSystem = "full";
|
||||||
+15
-1
@@ -6,6 +6,7 @@ export interface Config {
|
|||||||
storageDir: string;
|
storageDir: string;
|
||||||
maxFileSize: number;
|
maxFileSize: number;
|
||||||
fileTtlMs: number;
|
fileTtlMs: number;
|
||||||
|
publicBaseUrl?: string;
|
||||||
rateLimitEnabled: boolean;
|
rateLimitEnabled: boolean;
|
||||||
rateLimitMax: number;
|
rateLimitMax: number;
|
||||||
rateLimitWindowMs: number;
|
rateLimitWindowMs: number;
|
||||||
@@ -69,15 +70,28 @@ export function loadConfig(env: Record<string, string | undefined> = process.env
|
|||||||
throw new Error(`Invalid RATE_LIMIT_WINDOW_MS: ${env.RATE_LIMIT_WINDOW_MS}`);
|
throw new Error(`Invalid RATE_LIMIT_WINDOW_MS: ${env.RATE_LIMIT_WINDOW_MS}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const trustProxy = boolFromEnv(env.TRUST_PROXY, false);
|
||||||
|
|
||||||
|
let publicBaseUrl: string | undefined;
|
||||||
|
const rawBase = env.PUBLIC_BASE_URL?.trim();
|
||||||
|
if (rawBase) {
|
||||||
|
const normalized = rawBase.replace(/\/+$/, "");
|
||||||
|
if (!/^https?:\/\/[^\s/]+$/.test(normalized)) {
|
||||||
|
throw new Error(`Invalid PUBLIC_BASE_URL: "${rawBase}"`);
|
||||||
|
}
|
||||||
|
publicBaseUrl = normalized;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
storageDir,
|
storageDir,
|
||||||
maxFileSize,
|
maxFileSize,
|
||||||
fileTtlMs,
|
fileTtlMs,
|
||||||
|
publicBaseUrl,
|
||||||
rateLimitEnabled: boolFromEnv(env.RATE_LIMIT_ENABLED, true),
|
rateLimitEnabled: boolFromEnv(env.RATE_LIMIT_ENABLED, true),
|
||||||
rateLimitMax,
|
rateLimitMax,
|
||||||
rateLimitWindowMs,
|
rateLimitWindowMs,
|
||||||
trustProxy: boolFromEnv(env.TRUST_PROXY, false),
|
trustProxy,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ export async function handleUpload(
|
|||||||
storage: Storage,
|
storage: Storage,
|
||||||
maxFileSize: number,
|
maxFileSize: number,
|
||||||
ttlMs: number,
|
ttlMs: number,
|
||||||
|
publicBaseUrl?: string,
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const contentType = req.headers.get("content-type");
|
const contentType = req.headers.get("content-type");
|
||||||
const boundary = contentType ? getBoundary(contentType) : null;
|
const boundary = contentType ? getBoundary(contentType) : null;
|
||||||
@@ -77,8 +78,9 @@ export async function handleUpload(
|
|||||||
expiresAt: new Date(Date.now() + ttlMs).toISOString(),
|
expiresAt: new Date(Date.now() + ttlMs).toISOString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const url = publicBaseUrl ? `${publicBaseUrl}/${key}` : `/${key}`;
|
||||||
log("info", `upload ok id=${id} size=${size} name=${JSON.stringify(originalName)}`);
|
log("info", `upload ok id=${id} size=${size} name=${JSON.stringify(originalName)}`);
|
||||||
return json({ id, key, url: `/${key}`, originalName, size, protected: true });
|
return json({ id, key, url, originalName, size, protected: true });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await storage.delete(id);
|
await storage.delete(id);
|
||||||
await storage.deletePartial(id);
|
await storage.deletePartial(id);
|
||||||
|
|||||||
+1
-1
@@ -108,7 +108,7 @@ async function route(req: Request, srv: Server<undefined>): Promise<Response> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (path === "/api/upload" && method === "POST") {
|
if (path === "/api/upload" && method === "POST") {
|
||||||
return handleUpload(req, storage, cfg.maxFileSize, cfg.fileTtlMs);
|
return handleUpload(req, storage, cfg.maxFileSize, cfg.fileTtlMs, cfg.publicBaseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.startsWith("/api/auth/") && method === "POST") {
|
if (path.startsWith("/api/auth/") && method === "POST") {
|
||||||
|
|||||||
Reference in New Issue
Block a user