-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
354 lines (307 loc) · 10.8 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/patrickhener/goshs/ca"
"github.com/patrickhener/goshs/httpserver"
"github.com/patrickhener/goshs/logger"
"github.com/patrickhener/goshs/update"
"github.com/patrickhener/goshs/utils"
)
const goshsVersion = "v1.0.1"
var (
port = 8000
ip = "0.0.0.0"
cli = false
webroot = "."
ssl = false
selfsigned = false
letsencrypt = false
myKey = ""
myCert = ""
myP12 = ""
basicAuth = ""
certAuth = ""
webdav = false
webdavPort = 8001
uploadOnly = false
readOnly = false
verbose = false
silent = false
dropuser = ""
leEmail = ""
leDomains = ""
leHTTPPort = "80"
leTLSPort = "443"
embedded = false
output = ""
)
// Man page
func usage() func() {
return func() {
fmt.Printf(`
goshs %s
Usage: %s [options]
Web server options:
-i, --ip The ip/if-name to listen on (default: 0.0.0.0)
-p, --port The port to listen on (default: 8000)
-d, --dir The web root directory (default: current working path)
-w, --webdav Also serve using webdav protocol (default: false)
-wp, --webdav-port The port to listen on for webdav (default: 8001)
-ro, --read-only Read only mode, no upload possible (default: false)
-uo, --upload-only Upload only mode, no download possible (default: false)
-si, --silent Running without dir listing (default: false)
-c, --cli Enable cli (only with auth and tls) (default: false)
-e, --embedded Show embedded files in UI (default: false)
-o, --output Write output to logfile (default: false)
TLS options:
-s, --ssl Use TLS
-ss, --self-signed Use a self-signed certificate
-sk, --server-key Path to server key
-sc, --server-cert Path to server certificate
-p12, --pkcs12 Path to server p12
-sl, --lets-encrypt Use Let's Encrypt as certification service
-sld, --le-domains Domain(s) to request from Let's Encrypt (comma separated list)
-sle, --le-email Email to use with Let's Encrypt
-slh, --le-http Port to use for Let's Encrypt HTTP Challenge (default: 80)
-slt, --le-tls Port to use for Let's Encrypt TLS ALPN Challenge (default: 443)
Authentication options:
-b, --basic-auth Use basic authentication (user:pass - user can be empty)
-ca, --cert-auth Use certificate based authentication - provide ca certificate
-H, --hash Hash a password for file based ACLs
Misc options:
-u --user Drop privs to user (unix only) (default: current user)
--update Update goshs to most recent version
-V --verbose Activate verbose log output (default: false)
-v Print the current goshs version
Usage examples:
Start with default values: ./goshs
Start with wevdav support: ./goshs -w
Start with different port: ./goshs -p 8080
Start with self-signed cert: ./goshs -s -ss
Start with let's encrypt: ./goshs -s -sl -sle [email protected] -sld your.domain.com,your.seconddomain.com
Start with custom cert: ./goshs -s -sk <path to key> -sc <path to cert>
Start with basic auth: ./goshs -b secret-user:$up3r$3cur3
Start with basic auth empty user: ./goshs -b :$up3r$3cur3
Start with cli enabled: ./goshs -b secret-user:$up3r$3cur3 -s -ss -c
`, goshsVersion, os.Args[0])
}
}
func flags() (*bool, *bool, *bool, *bool) {
wd, _ := os.Getwd()
flag.StringVar(&ip, "i", ip, "ip")
flag.StringVar(&ip, "ip", ip, "ip")
flag.IntVar(&port, "p", port, "port")
flag.IntVar(&port, "port", port, "port")
flag.StringVar(&webroot, "d", wd, "web root")
flag.StringVar(&webroot, "dir", wd, "web root")
flag.BoolVar(&ssl, "s", ssl, "tls")
flag.BoolVar(&ssl, "ssl", ssl, "tls")
flag.BoolVar(&selfsigned, "ss", selfsigned, "self-signed")
flag.BoolVar(&selfsigned, "self-signed", selfsigned, "self-signed")
flag.StringVar(&myKey, "sk", myKey, "server key")
flag.StringVar(&myKey, "server-key", myKey, "server key")
flag.StringVar(&myCert, "sc", myCert, "server cert")
flag.StringVar(&myCert, "server-cert", myCert, "server cert")
flag.StringVar(&myP12, "p12", myP12, "server p12")
flag.StringVar(&myP12, "pkcs12", myP12, "server p12")
flag.StringVar(&basicAuth, "b", basicAuth, "basic auth")
flag.StringVar(&basicAuth, "basic-auth", basicAuth, "basic auth")
flag.StringVar(&certAuth, "ca", certAuth, "cert auth")
flag.StringVar(&certAuth, "cert-auth", certAuth, "cert auth")
flag.BoolVar(&webdav, "w", webdav, "enable webdav")
flag.BoolVar(&webdav, "webdav", webdav, "enable webdav")
flag.IntVar(&webdavPort, "wp", webdavPort, "webdav port")
flag.IntVar(&webdavPort, "webdav-port", webdavPort, "webdav port")
flag.BoolVar(&uploadOnly, "uo", uploadOnly, "upload only")
flag.BoolVar(&uploadOnly, "upload-only", uploadOnly, "upload only")
flag.BoolVar(&readOnly, "ro", readOnly, "read only")
flag.BoolVar(&readOnly, "read-only", readOnly, "read only")
flag.BoolVar(&verbose, "V", verbose, "verbose")
flag.BoolVar(&verbose, "verbose", verbose, "verbose")
flag.BoolVar(&silent, "si", silent, "silent")
flag.BoolVar(&silent, "silent", silent, "silent")
flag.StringVar(&dropuser, "u", dropuser, "user")
flag.StringVar(&dropuser, "user", dropuser, "user")
flag.BoolVar(&cli, "c", cli, "cli")
flag.BoolVar(&cli, "cli", cli, "cli")
flag.BoolVar(&letsencrypt, "sl", letsencrypt, "letsencrypt")
flag.BoolVar(&letsencrypt, "lets-encrypt", letsencrypt, "letsencrypt")
flag.StringVar(&leDomains, "sld", leDomains, "")
flag.StringVar(&leDomains, "le-domains", leDomains, "")
flag.StringVar(&leEmail, "sle", leEmail, "")
flag.StringVar(&leEmail, "le-email", leEmail, "")
flag.StringVar(&leHTTPPort, "slh", leHTTPPort, "")
flag.StringVar(&leHTTPPort, "le-http", leHTTPPort, "")
flag.StringVar(&leTLSPort, "slt", leTLSPort, "")
flag.StringVar(&leTLSPort, "le-tls", leTLSPort, "")
flag.BoolVar(&embedded, "e", embedded, "")
flag.BoolVar(&embedded, "embedded", embedded, "")
flag.StringVar(&output, "o", output, "")
flag.StringVar(&output, "output", output, "")
updateGoshs := flag.Bool("update", false, "update")
hash := flag.Bool("H", false, "hash")
hashLong := flag.Bool("hash", false, "hash")
version := flag.Bool("v", false, "goshs version")
flag.Usage = usage()
flag.Parse()
return hash, hashLong, version, updateGoshs
}
func resolveInterface() {
// Check if interface name was provided as -i
// If so, resolve to ip address of interface
if !strings.Contains(ip, ".") {
addr, err := utils.GetInterfaceIpv4Addr(ip)
if err != nil {
logger.Fatal(err)
os.Exit(-1)
}
if addr == "" {
logger.Fatal("IP address cannot be found for provided interface")
os.Exit(-1)
}
ip = addr
}
}
func sanityChecks() {
// Sanity check for upload only vs read only
if uploadOnly && readOnly {
logger.Fatal("You can only select either 'upload only' or 'read only', not both.")
}
// Sanity check if cli mode is combined with auth and tls
if cli && (!ssl || basicAuth == "") {
if cli && (!ssl || certAuth == "") {
logger.Fatal("With cli mode you need to enable basic/cert auth and tls for security reasons.")
}
}
// Sanity check if CA mode enabled you will also need TLS enabled in some way
if certAuth != "" && !ssl {
logger.Fatal("To use certificate based authentication with a CA cert you will need tls in any mode (-ss, -sk/-sc, -p12, -sl)")
}
}
// Flag handling
func init() {
wd, _ := os.Getwd()
// flags
hash, hashLong, version, updateGoshs := flags()
if *updateGoshs {
err := update.UpdateTool(goshsVersion)
if err != nil {
logger.Fatalf("Failed to update tool: %+v", err)
}
}
if *version {
fmt.Printf("goshs version is: %+v\n", goshsVersion)
os.Exit(0)
}
if *hash || *hashLong {
utils.HashPassword()
os.Exit(1)
}
// Resolve Interface
resolveInterface()
// Sanity checks
sanityChecks()
if webdav {
logger.Warn("upload/read-only mode deactivated due to use of 'webdav' mode")
uploadOnly = false
readOnly = false
}
// Abspath for webroot
var err error
// Trim trailing / for linux/mac and \ for windows
webroot = strings.TrimSuffix(webroot, "/")
webroot = strings.TrimSuffix(webroot, "\\")
if !filepath.IsAbs(webroot) {
webroot, err = filepath.Abs(filepath.Join(wd, webroot))
if err != nil {
logger.Fatalf("Webroot cannot be constructed: %+v", err)
}
}
}
// Sanity checks if basic auth has the right format
func parseBasicAuth() (string, string) {
auth := strings.SplitN(basicAuth, ":", 2)
if len(auth) < 2 {
fmt.Println("Wrong basic auth format. Please provide user:password separated by a colon")
os.Exit(-1)
}
user := auth[0]
pass := auth[1]
return user, pass
}
func main() {
if yes, out := update.CheckForUpdates(goshsVersion); yes {
logger.Warnf("There is a newer Version (%s) of goshs available. Run --update to update goshs.", out)
} else {
if out != "" {
logger.Warnf("Failed to check for updates: %+v", out)
} else {
logger.Infof("You are running the newest version (%s) of goshs", goshsVersion)
}
}
user := ""
pass := ""
// check for basic auth
if basicAuth != "" {
user, pass = parseBasicAuth()
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// If Let's Encrypt is in play we need to fetch the key and cert, write them to disc and set their path in MyKey and MyCert
if letsencrypt {
ca.GetLECertificateAndKey(leEmail, strings.Split(leDomains, ","), leHTTPPort, leTLSPort)
myCert = "cert"
myKey = "key"
}
// If a logpath/-file is provided via -o/--output set the multiwriter to output both
if output != "" {
if !filepath.IsAbs(output) {
// If the provided path is not an abspath then merge with CWD
wd, _ := os.Getwd()
output = filepath.Join(wd, output)
}
logFile, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
logger.Panicf("Cannot open file to write output logfile: %s - %+v", output, err)
}
multiWriter := io.MultiWriter(os.Stdout, logFile)
logger.LogFile(multiWriter)
}
// Setup the custom file server
server := &httpserver.FileServer{
IP: ip,
Port: port,
CLI: cli,
Webroot: webroot,
SSL: ssl,
SelfSigned: selfsigned,
LetsEncrypt: letsencrypt,
MyCert: myCert,
MyKey: myKey,
MyP12: myP12,
User: user,
Pass: pass,
CACert: certAuth,
DropUser: dropuser,
UploadOnly: uploadOnly,
ReadOnly: readOnly,
Silent: silent,
Embedded: embedded,
Verbose: verbose,
Version: goshsVersion,
}
go server.Start("web")
if webdav {
server.WebdavPort = webdavPort
go server.Start("webdav")
}
<-done
logger.Infof("Received CTRL+C, exiting...")
}