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 {
DataDirectory string
DBPath string
BandwidthLimit int64
DataDirectory string
DBPath string
BandwidthLimit int64
TrustXForwardedFor bool
ServerPublicProperties
}
@ -75,7 +76,13 @@ func (this *Server) handleAbout(w http.ResponseWriter) {
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"), ":")
}

View File

@ -18,13 +18,15 @@ func main() {
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)")
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()
svr, err := contented.NewServer(&contented.ServerOptions{
DataDirectory: *dataDir,
DBPath: *dbPath,
BandwidthLimit: int64(*maxUploadSpeed),
DataDirectory: *dataDir,
DBPath: *dbPath,
BandwidthLimit: int64(*maxUploadSpeed),
TrustXForwardedFor: *trustXForwardedFor,
ServerPublicProperties: contented.ServerPublicProperties{
AppTitle: *appTitle,
MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024,

View File

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