Skip to content

Commit 61eb155

Browse files
committed
add trust proxy option
1 parent cbbba3e commit 61eb155

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ All options accepted by the `ShareXServer` constructor:
3535
- `fileListing` (string | false, default `files`) - Path to file listing of uploads. Set to `false` to disable the listing.
3636
- `debug` (boolean, default `false`) - Enable verbose debug logging to the console.
3737
- `forceHttps` (boolean, default `false`) - Force HTTPS for return URL (useful when running behind reverse proxy)
38+
- `trustProxy` (boolean, default `false`) - Sets Express trust proxy to true; Useful for getting your X-Forwarded-For IP from reverse proxy when running with debug enabled; Also enables forceHttps if not explicitly set to a boolean
3839

3940
## 🗒️ Usage notes:
4041

@@ -48,7 +49,7 @@ All options accepted by the `ShareXServer` constructor:
4849
- Endpoint: `POST /api/upload` (multipart form, field name `file`).
4950
- Auth: include header `x-password: <your-password>`.
5051
- Success response: JSON containing a `url` pointing to the uploaded file.
51-
- Error response: JSON containing an `error` property with status code 400 for missing file and 401 for missing/incorrect password.
52+
- Error response: JSON containing an `error` property with status code 400 for missing file and 401 for missing/incorrect password.
5253

5354
Example (PowerShell / pwsh):
5455

@@ -81,7 +82,7 @@ curl -X POST "http://localhost:8080/api/upload" \
8182
- Purpose: When `enableSxcu` is `true`, will return a .sxcu file to be used with ShareX.
8283
8384
> **Note:** All endpoints take the configured `baseUrl` into account.
84-
> This means that every URL, will be prefixed with the `baseUrl` you have configured. (example: `GET /baseUrl/files`)
85+
> This means that every URL will be prefixed with the `baseUrl` you have configured. (example: `GET /baseUrl/files`)
8586
8687
---
8788
@@ -90,9 +91,4 @@ curl -X POST "http://localhost:8080/api/upload" \
9091
- Filenames are generated as: `${nanoid(this.filenameLength)}.{ext}` where `ext` is the uploaded file's original extension.
9192
- A collision safeguard checks whether a file with the generated name already exists and regenerates once if necessary. The probability of collision is [extremely low](https://zelark.github.io/nano-id-cc/) with `nanoid`.
9293
93-
9494
---
95-
96-
## 📌 Documentation notice
97-
98-
This README was produced with the assistance of an AI.

docker/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ ENV BASE_URL=/
1919
ENV SAVE_PATH=/uploads
2020
ENV FILENAME_LENGTH=10
2121
ENV ENABLE_SXCU=TRUE
22+
ENV TRUST_PROXY=TRUE
2223
ENV DEBUG=FALSE
23-
ENV FORCE_HTTPS=FALSE
24+
ENV FORCE_HTTPS=
2425
ENV FILE_LISTING=/files
2526

2627
CMD ["node", "index.js"]

docker/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ The ShareX Server Docker image can be configured via the following environment v
5757
- **FORCE_HTTPS**
5858

5959
- Description: Force HTTPS in return URL (useful when running behind reverse proxy)
60-
- Default: `false`
60+
- Default: ` `
6161
- Example: `FORCE_HTTPS=true`
6262

63+
- **TRUST_PROXY**
64+
65+
- Description: Sets Express trust proxy to true; Useful for getting your X-Forwarded-For IP from reverse proxy when running with debug enabled; Also enables forceHttps if not explicitly set to a boolean
66+
- Default: `false`
67+
- Example: `TRUST_PROXY=true`
68+
6369
- **DEBUG**
6470

6571
- Description: Enables debug logging. Set to `true` to log detailed server activity.

docker/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ new ShareXServer({
77
port: process.env.PORT,
88
fileLength: process.env.LENGTH,
99
enableSxcu: JSON.parse(process.env.ENABLE_SXCU.toLowerCase()) ?? true,
10-
forceHttps: JSON.parse(process.env.FORCE_HTTPS.toLowerCase()) ?? false,
11-
fileListing: process.env.FILE_LISTING.toLowerCase() == "false" ? false : process.env.FILE_LISTING,
10+
forceHttps: JSON.parse(process.env.FORCE_HTTPS.toLowerCase()) ?? null,
11+
trustProxy: JSON.parse(process.env.TRUST_PROXY.toLowerCase()) ?? false,
12+
fileListing:
13+
process.env.FILE_LISTING.toLowerCase() == "false"
14+
? false
15+
: process.env.FILE_LISTING,
1216
debug: JSON.parse(process.env.DEBUG.toLowerCase()) ?? false,
1317
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sharex-server",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "ShareX image and file hosting using Node.js",
55
"main": "dist/index.js",
66
"scripts": {

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class ShareXServer {
2929
fileListing = "files",
3030
debug = false,
3131
forceHttps = false,
32+
trustProxy,
3233
}: SharexServerOptions) {
3334
this.port = port;
3435
// Ensure baseUrl starts and ends with /
@@ -46,6 +47,14 @@ export class ShareXServer {
4647
: fileListing
4748
: false;
4849

50+
if (trustProxy) {
51+
if (this.forceHttps !== true && this.forceHttps !== false) {
52+
this.forceHttps = true;
53+
}
54+
55+
this.#server.set("trust proxy", true);
56+
}
57+
4958
if (!password) {
5059
console.error(
5160
"[Error] A password must be provided to start the server."
@@ -293,4 +302,11 @@ export interface SharexServerOptions {
293302
debug?: boolean;
294303
/** Force HTTPS in return URL (useful when running behind reverse proxy) */
295304
forceHttps?: boolean;
305+
/** Sets Express trust proxy to true
306+
*
307+
* Useful for getting your X-Forwarded-For IP from reverse proxy
308+
*
309+
* Also enables forceHttps if not explicitly set to a boolean
310+
*/
311+
trustProxy?: boolean;
296312
}

0 commit comments

Comments
 (0)