forked from espebra/filebin2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
183 lines (157 loc) · 6.22 KB
/
main.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"flag"
"fmt"
_ "net/http/pprof"
"os"
"strconv"
//"github.com/espebra/filebin2/ds"
"github.com/GeertJohan/go.rice"
"github.com/dustin/go-humanize"
"github.com/espebra/filebin2/dbl"
"github.com/espebra/filebin2/ds"
"github.com/espebra/filebin2/geoip"
"github.com/espebra/filebin2/s3"
"math/rand"
"net/url"
"time"
)
var (
// Various
expirationFlag = flag.Int("expiration", 604800, "Bin expiration time in seconds since the last bin update")
tmpdirFlag = flag.String("tmpdir", os.TempDir(), "Directory for temporary files for upload and download")
baseURLFlag = flag.String("baseurl", "https://filebin.net", "The base URL to use. Required for self-hosted instances.")
requireApprovalFlag = flag.Bool("manual-approval", false, "Require manual admin approval of new bins before files can be downloaded.")
mmdbPathFlag = flag.String("mmdb", "", "The path to an mmdb formatted geoip database like GeoLite2-City.mmdb.")
// Limits
limitFileDownloadsFlag = flag.Uint64("limit-file-downloads", 0, "Limit the number of downloads per file. 0 disables this limit.")
limitStorageFlag = flag.String("limit-storage", "0", "Limit the storage capacity to use (examples: 100MB, 20GB, 2TB). 0 disables this limit.")
// HTTP
listenHostFlag = flag.String("listen-host", "127.0.0.1", "Listen host")
listenPortFlag = flag.Int("listen-port", 8080, "Listen port")
accessLogFlag = flag.String("access-log", "/var/log/filebin/access.log", "Path for access.log output")
proxyHeadersFlag = flag.Bool("proxy-headers", false, "Read client request information from proxy headers")
// Database
dbHostFlag = flag.String("db-host", os.Getenv("DATABASE_HOST"), "Database host")
dbPortFlag = flag.String("db-port", os.Getenv("DATABASE_PORT"), "Database port")
dbNameFlag = flag.String("db-name", os.Getenv("DATABASE_NAME"), "Name of the database")
dbUsernameFlag = flag.String("db-username", "", "Database username")
dbPasswordFlag = flag.String("db-password", "", "Database password")
// S3
s3EndpointFlag = flag.String("s3-endpoint", os.Getenv("S3_ENDPOINT"), "S3 endpoint")
s3BucketFlag = flag.String("s3-bucket", os.Getenv("S3_BUCKET"), "S3 bucket")
s3RegionFlag = flag.String("s3-region", os.Getenv("S3_REGION"), "S3 region")
s3AccessKeyFlag = flag.String("s3-access-key", "", "S3 access key")
s3SecretKeyFlag = flag.String("s3-secret-key", "", "S3 secret key")
s3TraceFlag = flag.Bool("s3-trace", false, "Enable S3 HTTP tracing for debugging")
s3SecureFlag = flag.Bool("s3-secure", true, "Use TLS when connecting to S3")
s3UrlTtlFlag = flag.String("s3-url-ttl", "1m", "The time to live for presigned S3 URLs, for example 30s or 5m")
// Lurker
lurkerIntervalFlag = flag.Int("lurker-interval", 300, "Lurker interval is the delay to sleep between each run in seconds")
logRetentionFlag = flag.Uint64("log-retention", 7, "The number of days to keep log entries before removed by the lurker.")
// Auth
adminUsernameFlag = flag.String("admin-username", "", "Admin username")
adminPasswordFlag = flag.String("admin-password", "", "Admin password")
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
flag.Parse()
// Set some default values that should not be exposed by flag.PrintDefaults()
if *dbUsernameFlag == "" {
*dbUsernameFlag = os.Getenv("DATABASE_USERNAME")
}
if *dbPasswordFlag == "" {
*dbPasswordFlag = os.Getenv("DATABASE_PASSWORD")
}
if *s3AccessKeyFlag == "" {
*s3AccessKeyFlag = os.Getenv("S3_ACCESS_KEY")
}
if *s3SecretKeyFlag == "" {
*s3SecretKeyFlag = os.Getenv("S3_SECRET_KEY")
}
if *adminUsernameFlag == "" {
*adminUsernameFlag = os.Getenv("ADMIN_USERNAME")
}
if *adminPasswordFlag == "" {
*adminPasswordFlag = os.Getenv("ADMIN_PASSWORD")
}
// mmdb path
geodb, err := geoip.Init(*mmdbPathFlag)
if err != nil {
fmt.Printf("Unable to load geoip database: %s\n", err.Error())
}
// Set database port to 5432 if not set or invalid
dbport, err := strconv.Atoi(*dbPortFlag)
if err != nil {
dbport = 5432
}
daoconn, err := dbl.Init(*dbHostFlag, dbport, *dbNameFlag, *dbUsernameFlag, *dbPasswordFlag)
if err != nil {
fmt.Printf("Unable to connect to the database: %s\n", err.Error())
os.Exit(2)
}
if err := daoconn.CreateSchema(); err != nil {
fmt.Printf("Unable to create Schema: %s\n", err.Error())
}
s3UrlTtl, err := time.ParseDuration(*s3UrlTtlFlag)
if err != nil {
fmt.Printf("Unable to parse --s3-url-ttl: %s\n", err.Error())
os.Exit(2)
}
fmt.Printf("TTL for presigned S3 URLs: %s\n", s3UrlTtl.String())
s3conn, err := s3.Init(*s3EndpointFlag, *s3BucketFlag, *s3RegionFlag, *s3AccessKeyFlag, *s3SecretKeyFlag, *s3SecureFlag, s3UrlTtl)
if err != nil {
fmt.Printf("Unable to initialize S3 connection: %s\n", err.Error())
os.Exit(2)
}
if *s3TraceFlag {
s3conn.SetTrace(*s3TraceFlag)
}
l := &Lurker{
dao: &daoconn,
s3: &s3conn,
}
// Start the lurker process
l.Init(*lurkerIntervalFlag, *logRetentionFlag)
l.Run()
staticBox := rice.MustFindBox("static")
templateBox := rice.MustFindBox("templates")
u, err := url.Parse(*baseURLFlag)
if err != nil {
fmt.Printf("Unable to parse the baseurl parameter: %s\n", *baseURLFlag)
os.Exit(2)
}
config := &ds.Config{
Expiration: *expirationFlag,
LimitFileDownloads: *limitFileDownloadsFlag,
HttpHost: *listenHostFlag,
HttpPort: *listenPortFlag,
HttpProxyHeaders: *proxyHeadersFlag,
HttpAccessLog: *accessLogFlag,
AdminUsername: *adminUsernameFlag,
AdminPassword: *adminPasswordFlag,
Tmpdir: *tmpdirFlag,
RequireApproval: *requireApprovalFlag,
BaseUrl: *u,
}
config.LimitStorageBytes, err = humanize.ParseBytes(*limitStorageFlag)
if err != nil {
fmt.Printf("Unable to parse the --limit-storage parameter: %s\n", *baseURLFlag)
os.Exit(2)
}
config.LimitStorageReadable = humanize.Bytes(config.LimitStorageBytes)
h := &HTTP{
staticBox: staticBox,
templateBox: templateBox,
dao: &daoconn,
s3: &s3conn,
geodb: &geodb,
config: config,
}
if err := h.Init(); err != nil {
fmt.Printf("Unable to start the HTTP server: %s\n", err.Error())
}
fmt.Printf("Uploaded files expiration: %s\n", config.ExpirationDuration.String())
// Start the http server
h.Run()
}