Skip to content

Commit 72c48c5

Browse files
add scripts
1 parent 5ff59ee commit 72c48c5

File tree

7 files changed

+188
-27
lines changed

7 files changed

+188
-27
lines changed

internal/auth/http.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package auth
22

33
import (
44
"net/http"
5-
"strconv"
65
"strings"
76

87
"github.com/collapsinghierarchy/noisytransfer/internal/ids"
@@ -19,27 +18,3 @@ func RequireUUIDAppID(next http.Handler) http.Handler {
1918
next.ServeHTTP(w, r)
2019
})
2120
}
22-
23-
// EnforceUploadPolicyMax blocks oversize uploads early (413).
24-
// If maxBytes <= 0, the limiter is disabled (no size cap).
25-
func EnforceUploadPolicyMax(maxBytes int64) func(http.Handler) http.Handler {
26-
return func(next http.Handler) http.Handler {
27-
// disabled?
28-
if maxBytes <= 0 {
29-
return next
30-
}
31-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
32-
switch r.Method {
33-
case http.MethodPut, http.MethodPost, http.MethodPatch:
34-
if cl := r.Header.Get("Content-Length"); cl != "" {
35-
if n, err := strconv.ParseInt(cl, 10, 64); err == nil && n > maxBytes {
36-
http.Error(w, "payload too large", http.StatusRequestEntityTooLarge)
37-
return
38-
}
39-
}
40-
r.Body = http.MaxBytesReader(w, r.Body, maxBytes)
41-
}
42-
next.ServeHTTP(w, r)
43-
})
44-
}
45-
}

internal/httpx/policy.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package httpx
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
)
7+
8+
// EnforceUploadPolicyMax blocks oversize uploads early (413).
9+
// If maxBytes <= 0, the limiter is disabled (no size cap).
10+
func EnforceUploadPolicyMax(maxBytes int64) func(http.Handler) http.Handler {
11+
return func(next http.Handler) http.Handler {
12+
if maxBytes <= 0 {
13+
return next
14+
}
15+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16+
switch r.Method {
17+
case http.MethodPut, http.MethodPost, http.MethodPatch:
18+
if cl := r.Header.Get("Content-Length"); cl != "" {
19+
if n, err := strconv.ParseInt(cl, 10, 64); err == nil && n > maxBytes {
20+
http.Error(w, "payload too large", http.StatusRequestEntityTooLarge)
21+
return
22+
}
23+
}
24+
r.Body = http.MaxBytesReader(w, r.Body, maxBytes)
25+
}
26+
next.ServeHTTP(w, r)
27+
})
28+
}
29+
}

internal/server/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,16 @@ func Build(p BuildParams, opt ...any) (*App, error) {
169169
// Mark rendezvous paired upon RTC established (same behavior spot as before).
170170
h.OnEstablished = func(appID string) { rv.MarkPaired(context.Background(), appID) }
171171

172-
// Objects API
173172
objMux := http.NewServeMux()
174173
apiSrv.Register(objMux)
175174
objHandler := http.Handler(objMux)
175+
176176
if !p.DevMode {
177-
objHandler = auth.RequireUUIDAppID(auth.EnforceUploadPolicyMax(p.UploadMaxBytes)(objHandler))
177+
objHandler = auth.RequireUUIDAppID(
178+
httpx.EnforceUploadPolicyMax(p.UploadMaxBytes)(objHandler),
179+
)
178180
}
181+
179182
mux.Handle("/objects", objHandler)
180183
mux.Handle("/objects/", objHandler)
181184

scripts/dev-local.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Run the server locally in DEV mode (looser WS origin, open CORS allowlist).
5+
# Usage:
6+
# ./scripts/dev-local.sh
7+
# Env overrides:
8+
# ADDR (default :1234)
9+
# BASE (default http://127.0.0.1:1234)
10+
# CORS (default http://127.0.0.1,http://localhost)
11+
# DATA_DIR (default ./data)
12+
# UPLOAD_MAX_MB (default 64) # per-request cap; set 0 to disable
13+
# TURN=1 (enable embedded TURN server for dev/testing)
14+
# NT_WORDLIST_FILE (optional path to custom wordlist)
15+
16+
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
17+
cd "$repo_root"
18+
19+
BIN_DIR="${BIN_DIR:-./bin}"
20+
BIN="$BIN_DIR/noisytransferd"
21+
mkdir -p "$BIN_DIR"
22+
23+
version="dev-local"
24+
commit="$(git rev-parse --short HEAD 2>/dev/null || echo unknown)"
25+
26+
echo "==> building noisytransferd ($version @ $commit)"
27+
GOFLAGS="${GOFLAGS:-}"
28+
go build $GOFLAGS -ldflags "-s -w -X main.version=$version -X main.commit=$commit" -o "$BIN" ./noisytransferd
29+
30+
export NT_DEV=1
31+
ADDR="${ADDR:-:1234}"
32+
BASE="${BASE:-http://127.0.0.1:1234}"
33+
CORS="${CORS:-http://127.0.0.1,http://localhost}"
34+
DATA_DIR="${DATA_DIR:-./data}"
35+
UPLOAD_MAX_MB="${UPLOAD_MAX_MB:-64}"
36+
37+
args=(
38+
-addr "$ADDR"
39+
-base "$BASE"
40+
-cors "$CORS"
41+
-data "$DATA_DIR"
42+
-upload_max_mb "$UPLOAD_MAX_MB"
43+
)
44+
if [[ "${TURN:-0}" != "0" ]]; then
45+
args+=(-turn)
46+
fi
47+
48+
echo "==> running noisytransferd ${args[*]}"
49+
exec "$BIN" "${args[@]}"

scripts/docker-prof.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Build and run the container locally.
5+
# Optional env:
6+
# TAG (default: local)
7+
# PORT (default: 1234)
8+
# DEV (default: 1)
9+
# BASE (default: http://127.0.0.1:1234)
10+
# CORS (default: http://127.0.0.1,http://localhost)
11+
12+
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
13+
cd "$repo_root"
14+
15+
TAG="${TAG:-local}"
16+
PORT="${PORT:-1234}"
17+
DEV="${DEV:-1}"
18+
BASE="${BASE:-http://127.0.0.1:1234}"
19+
CORS="${CORS:-http://127.0.0.1,http://localhost}"
20+
21+
version="${VERSION:-dev-docker}"
22+
commit="${COMMIT:-$(git rev-parse --short HEAD 2>/dev/null || echo unknown)}"
23+
24+
echo "==> building image noisytransferd:$TAG"
25+
docker build \
26+
--build-arg VERSION="$version" \
27+
--build-arg COMMIT="$commit" \
28+
-t noisytransferd:"$TAG" .
29+
30+
mkdir -p ./data
31+
32+
echo "==> running container on :$PORT"
33+
docker run --rm -it \
34+
-p "$PORT:1234" \
35+
-e NT_DEV="$DEV" \
36+
-e NT_BASE_URL="$BASE" \
37+
-e NT_CORS="$CORS" \
38+
-e NT_DATA_DIR="/data" \
39+
-v "$(pwd)/data:/data" \
40+
--name noisytransferd \
41+
noisytransferd:"$TAG"

scripts/prod-basic.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Run the server in a production-like way (foreground).
5+
# Reads variables from ./scripts/prod.env if present, then from environment.
6+
# Required:
7+
# BASE (e.g., https://relay.example)
8+
# CORS (e.g., https://app.example)
9+
# Optional:
10+
# ADDR (default :1234)
11+
# DATA_DIR (default ./data)
12+
# UPLOAD_MAX_MB (default 128)
13+
# TURN=1 to enable embedded TURN (dev/test only)
14+
15+
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
16+
cd "$repo_root"
17+
18+
if [[ -f "./scripts/prod.env" ]]; then
19+
set -a; source "./scripts/prod.env"; set +a
20+
fi
21+
22+
: "${BASE:?set BASE, e.g. https://relay.example}"
23+
: "${CORS:?set CORS, e.g. https://app.example}"
24+
25+
BIN_DIR="${BIN_DIR:-./bin}"
26+
BIN="$BIN_DIR/noisytransferd"
27+
mkdir -p "$BIN_DIR"
28+
29+
version="${VERSION:-$(git describe --tags --always 2>/dev/null || echo prod-build)}"
30+
commit="${COMMIT:-$(git rev-parse --short HEAD 2>/dev/null || echo unknown)}"
31+
32+
echo "==> building noisytransferd ($version @ $commit)"
33+
go build -ldflags "-s -w -X main.version=$version -X main.commit=$commit" -o "$BIN" ./noisytransferd
34+
35+
ADDR="${ADDR:-:1234}"
36+
DATA_DIR="${DATA_DIR:-./data}"
37+
UPLOAD_MAX_MB="${UPLOAD_MAX_MB:-128}"
38+
export NT_DEV="${NT_DEV:-0}"
39+
40+
args=(
41+
-addr "$ADDR"
42+
-base "$BASE"
43+
-cors "$CORS"
44+
-data "$DATA_DIR"
45+
-upload_max_mb "$UPLOAD_MAX_MB"
46+
)
47+
if [[ "${TURN:-0}" != "0" ]]; then
48+
args+=(-turn)
49+
fi
50+
51+
echo "==> running noisytransferd ${args[*]}"
52+
exec "$BIN" "${args[@]}"

scripts/prod.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Example environment file for scripts/prod-basic.sh and systemd unit.
2+
# Rename to scripts/prod.env and edit.
3+
BASE=https://relay.example
4+
CORS=https://app.example
5+
ADDR=:1234
6+
DATA_DIR=/var/lib/noisytransfer
7+
UPLOAD_MAX_MB=128
8+
NT_DEV=0
9+
# Optional embedded TURN for dev/test:
10+
# TURN=1
11+
# Optional alternative rendezvous wordlist:
12+
# NT_WORDLIST_FILE=/etc/noisytransfer/wordlist.txt

0 commit comments

Comments
 (0)