option for trustXForwardedFor

This commit is contained in:
mappu 2017-10-15 19:09:14 +13:00
parent 27cf4cf0c0
commit 3ca73e3221
3 changed files with 17 additions and 8 deletions

View File

@ -20,9 +20,10 @@ type ServerPublicProperties struct {
} }
type ServerOptions struct { type ServerOptions struct {
DataDirectory string DataDirectory string
DBPath string DBPath string
BandwidthLimit int64 BandwidthLimit int64
TrustXForwardedFor bool
ServerPublicProperties ServerPublicProperties
} }
@ -75,7 +76,13 @@ func (this *Server) handleAbout(w http.ResponseWriter) {
this.serveJsonObject(w, this.opts.ServerPublicProperties) this.serveJsonObject(w, this.opts.ServerPublicProperties)
} }
func remoteIP(r *http.Request) string { func (this *Server) remoteIP(r *http.Request) string {
if this.opts.TrustXForwardedFor {
if xff := r.Header.Get("X-Forwarded-For"); len(xff) > 0 {
return xff
}
}
return strings.TrimRight(strings.TrimRight(r.RemoteAddr, "0123456789"), ":") return strings.TrimRight(strings.TrimRight(r.RemoteAddr, "0123456789"), ":")
} }

View File

@ -18,13 +18,15 @@ func main() {
appTitle := flag.String("title", "contented", "Title used in web interface") appTitle := flag.String("title", "contented", "Title used in web interface")
maxUploadMb := flag.Int("max", 8, "Maximum size of uploaded files in MiB (set zero for unlimited)") maxUploadMb := flag.Int("max", 8, "Maximum size of uploaded files in MiB (set zero for unlimited)")
maxUploadSpeed := flag.Int("speed", 0, "Maximum upload speed in bytes/sec (set zero for unlimited)") maxUploadSpeed := flag.Int("speed", 0, "Maximum upload speed in bytes/sec (set zero for unlimited)")
trustXForwardedFor := flag.Bool("trustXForwardedFor", false, "Trust X-Forwarded-For reverse proxy headers")
flag.Parse() flag.Parse()
svr, err := contented.NewServer(&contented.ServerOptions{ svr, err := contented.NewServer(&contented.ServerOptions{
DataDirectory: *dataDir, DataDirectory: *dataDir,
DBPath: *dbPath, DBPath: *dbPath,
BandwidthLimit: int64(*maxUploadSpeed), BandwidthLimit: int64(*maxUploadSpeed),
TrustXForwardedFor: *trustXForwardedFor,
ServerPublicProperties: contented.ServerPublicProperties{ ServerPublicProperties: contented.ServerPublicProperties{
AppTitle: *appTitle, AppTitle: *appTitle,
MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024, MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024,

View File

@ -41,7 +41,7 @@ func (this *Server) handleUpload(w http.ResponseWriter, r *http.Request) {
return return
} }
path, err := this.handleUploadFile(f, fhs, remoteIP(r)) path, err := this.handleUploadFile(f, fhs, this.remoteIP(r))
if err != nil { if err != nil {
log.Printf("%s Upload failed: %s\n", r.RemoteAddr, err.Error()) log.Printf("%s Upload failed: %s\n", r.RemoteAddr, err.Error())
http.Error(w, "Upload failed", 500) http.Error(w, "Upload failed", 500)