aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: c72d585c23f56e74d75c484f3fe8329027e354b6 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main

import (
	"flag"
	"fmt"
	"git.clsr.net/gomf/gomf/storage"
	"math/rand"
	"net/http"
	"strings"
	"time"
)

var uploads *storage.Storage

var (
	uploadUrl     string
	uploadHost    string
	siteName      string
	contactMail   string
	abuseMail     string
	csp           string
	hsts          bool
	allowHtml     bool
	cors          bool
	redirectHttps bool
)

func handle(w http.ResponseWriter, r *http.Request) {
	if cors {
		w.Header().Set("Access-Control-Allow-Origin", "*")
	}
	if hsts {
		w.Header().Set("Strict-Transport-Security", "max-age=15552000")
	}
	if redirectHttps && r.TLS == nil && r.Host != "" {
		targ := &*r.URL
		targ.Host = r.Host
		targ.Scheme = "https"
		http.Redirect(w, r, targ.String(), http.StatusFound)
		return
	}
	if r.Method == http.MethodGet || r.Method == http.MethodPost || r.Method == http.MethodHead {
		for _, host := range strings.Split(uploadHost, ",") {
			if r.Host == host {
				handleFile(w, r)
				return
			}
		}
		http.DefaultServeMux.ServeHTTP(w, r)
	} else {
		w.Header().Set("Allow", "POST, HEAD, OPTIONS, GET")
		if r.Method != http.MethodOptions {
			http.Error(w, "The method is not allowed for the requested URL.", http.StatusMethodNotAllowed)
		}
	}
}

func main() {
	flag.StringVar(&uploadUrl, "upload-url", "", "URL to serve uploads from")
	flag.StringVar(&uploadHost, "upload-host", "", "comma-separated list of hosts to serve uploads on")
	flag.StringVar(&siteName, "name", "Gomf", "website name")
	flag.StringVar(&contactMail, "contact", "contact@example.com", "contact email address")
	flag.StringVar(&abuseMail, "abuse", "abuse@example.com", "abuse email address")
	flag.StringVar(&csp, "csp", "default-src 'none'; media-src 'self'", "the Content-Security-Policy header for files; blank to disable")
	flag.BoolVar(&hsts, "hsts", false, "enable HSTS")
	flag.BoolVar(&allowHtml, "allow-html", false, "serve (X)HTML uploads with (X)HTML filetypes")
	flag.BoolVar(&cors, "cors", false, "enable CORS and allow all origins")
	flag.BoolVar(&redirectHttps, "redirect-https", false, "redirect HTTP traffic to HTTPS")
	listenHttp := flag.String("http", "localhost:8080", "address to listen on for HTTP")
	listenHttps := flag.String("https", "", "address to listen on for HTTPS")
	cert := flag.String("cert", "", "path to TLS certificate (for HTTPS)")
	key := flag.String("key", "", "path to TLS key (for HTTPS)")
	maxSize := flag.Int64("max-size", storage.DefaultMaxSize, "max filesize in bytes")
	filterMime := flag.String("filter-mime", "application/x-dosexec,application/x-msdos-program", "comma-separated list of filtered MIME types")
	filterExt := flag.String("filter-ext", "exe,dll,msi,scr,com,pif", "comma-separated list of filtered file extensions")
	whitelist := flag.Bool("whitelist", false, "use filter as a whitelist instead of blacklist")
	grill := flag.Bool("grill", false, "enable grills")
	idLength := flag.Int("id-length", storage.DefaultIdLength, "length of uploaded file IDs")
	idCharset := flag.String("id-charset", "", "charset for uploaded file IDs (default lowercase letters a-z)")
	enableLog := flag.Bool("log", false, "enable logging")
	logIP := flag.Bool("log-ip", false, "log IP addresses")
	logIPHash := flag.Bool("log-ip-hash", false, "log hashed IP addresses")
	logUA := flag.Bool("log-ua", false, "log User-Agent headers")
	logUAHash := flag.Bool("log-ua-hash", false, "log hashed User-Agent headers")
	logReferer := flag.Bool("log-referer", false, "log Referer headers")
	logRefererHash := flag.Bool("log-referer-hash", false, "log hashed Referer headers")
	logHashSalt := flag.String("log-hash-salt", "", "salt to use for hashed log entries")
	proxyCount := flag.Int("proxy-count", 0, "count of trusted reverse proxies")

	flag.Parse()

	rand.Seed(time.Now().UnixNano())

	initWebsite()

	uploads = storage.NewStorage("upload")
	uploads.FilterExt = strings.Split(*filterExt, ",")
	for i := range uploads.FilterExt {
		uploads.FilterExt[i] = "." + uploads.FilterExt[i]
	}
	uploads.FilterMime = strings.Split(*filterMime, ",")
	uploads.Whitelist = *whitelist
	uploads.IdLength = *idLength
	uploads.MaxSize = *maxSize
	if *idCharset != "" {
		uploads.IdCharset = *idCharset
	}

	if !*enableLog {
		DefaultLogger = nil
	} else {
		DefaultLogger.LogIP = *logIP || *logIPHash
		DefaultLogger.LogUserAgent = *logUA || *logUAHash
		DefaultLogger.LogReferer = *logReferer || *logRefererHash
		DefaultLogger.HashIP = *logIPHash
		DefaultLogger.HashUserAgent = *logUAHash
		DefaultLogger.HashReferer = *logRefererHash
		DefaultLogger.HashSalt = *logHashSalt
		DefaultLogger.ProxyCount = *proxyCount
	}

	http.HandleFunc("/upload.php", handleUpload)
	http.Handle("/u/", http.StripPrefix("/u/", http.HandlerFunc(handleFile)))
	if *grill {
		http.HandleFunc("/grill.php", handleGrill)
	}

	if uploadUrl == "" {
		if uploadHost != "" {
			host := strings.Split(uploadHost, ",")[0]
			if *listenHttps != "" {
				uploadUrl = "https://" + host + "/"
			} else if *listenHttp != "" {
				uploadUrl = "http://" + host + "/"
			}
		} else {
			if *listenHttps != "" {
				uploadUrl = "https://" + *listenHttps + "/u/"
			} else if *listenHttp != "" {
				uploadUrl = "http://" + *listenHttp + "/u/"
			}
		}
		fmt.Printf("using %q as uploaded file URL\n", uploadUrl)
	}

	exit := true
	if *listenHttp != "" {
		exit = false
		fmt.Printf("listening on http://%s/\n", *listenHttp)
		go func() {
			panic(http.ListenAndServe(*listenHttp, http.HandlerFunc(handle)))
		}()
	}
	if *listenHttps != "" {
		exit = false
		fmt.Printf("listening on https://%s/\n", *listenHttps)
		go func() {
			panic(http.ListenAndServeTLS(*listenHttps, *cert, *key, http.HandlerFunc(handle)))
		}()
	}

	if !exit {
		select {}
	}
}