summaryrefslogtreecommitdiffstats
path: root/internal/handler/submit.go
blob: 282248882ad2d329cd9fd0bc17d1fab2d2c0b521 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package handler

import (
	"net/http"
	"nymdrop/internal/pow"
	"nymdrop/internal/relay"
)

type Submit struct {
	relay *relay.Relay
	pow   *pow.Verifier
}

func NewSubmit(r *relay.Relay, p *pow.Verifier) *Submit {
	return &Submit{relay: r, pow: p}
}

func (s *Submit) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}

	// Proof-of-work computed client-side (X-Nymdrop-Pow: "<unix-ts>:<nonce>").
	// Rejects unproven, expired, and replayed stamps before touching the relay.
	if err := s.pow.Verify(r.Header.Get("X-Nymdrop-Pow")); err != nil {
		// Generic error — don't reveal which check failed.
		http.Error(w, "error", http.StatusBadRequest)
		return
	}

	r.Body = http.MaxBytesReader(w, r.Body, 10*1024*1024)
	defer r.Body.Close()

	if err := s.relay.Forward(r.Body); err != nil {
		// Generic error — reveal nothing about internals.
		http.Error(w, "error", http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Write([]byte("ok"))
}