-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebdav.go
executable file
·163 lines (146 loc) · 4.35 KB
/
webdav.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"log/syslog"
"net/http"
"os"
"runtime"
"strings"
"time"
"golang.org/x/net/webdav"
)
const _version_ = "Rel_20220514a"
var (
httpPort = flag.Int("p", 6200, "http port (plain)")
httpsPort = flag.Int("ps", 6201, "https port (tls)")
poll = flag.Int("poll", 30, "how often to poll runtime stats")
insecure = flag.Bool("insecure", false, "disable TLS")
anon = flag.Bool("anon", false, "anonymous connections allowed (user auth disabled)")
monitor = flag.Bool("monitor", false, "enable metric logging; memory, heap, numGC, etc")
both = flag.Bool("both", false, "run an http server and https server")
version = flag.Bool("v", false, "show version number")
quiet = flag.Bool("quiet", false, "only log errors")
cert = flag.String("cert", "cert.pem", "path to your cert")
key = flag.String("key", "key.pem", "path to your key")
dir = flag.String("dir", "./", "Directory to serve from. Default is CWD")
logPath = flag.String("log", "webdav.log", "syslog server or /path/to/file to log to")
uniq = flag.String("uniq", "__DAV__", "if using syslog, a unique process name for easier debugging")
)
type Profile struct {
Alloc,
TotalAlloc,
MemoryAlloc,
System,
Free,
Objects,
TotalPauses uint64
NGC uint32
NumCPU int
}
func main() {
flag.Parse()
// print the version if so desired
if *version {
fmt.Printf("\nwebdav fileserver version: %v\n", _version_)
os.Exit(0)
}
// if the user supplied -log arg contains an "@", we treat it as a remote logging path
if strings.Contains(*logPath, "@") {
addr := strings.Split(*logPath, "@")
logger, e := syslog.Dial(addr[0], addr[1],
syslog.LOG_WARNING|syslog.LOG_DAEMON, *uniq)
check(e)
log.SetOutput(logger)
} else {
// otherwise we treat the arg like a file path
logger, e := os.OpenFile(*logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
check(e)
defer logger.Close()
log.SetPrefix(fmt.Sprintf("%v: ", *uniq))
log.SetOutput(logger)
}
// if the user wants to poll the runtime stats, start in `background` (so to speak)
if *monitor {
go monitorRuntimeProfile()
}
svr := &webdav.Handler{
FileSystem: webdav.Dir(*dir),
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Printf("-> %s: %s, ERROR->: %s on %v", r.Method, r.URL, err, r.RemoteAddr)
}
if !*quiet {
log.Printf("-> %s: %s -> %v", r.Method, r.URL, r.RemoteAddr)
}
},
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if !*anon {
uname, pwd, _ := r.BasicAuth()
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
if uname == os.Getenv("DUSR") && pwd == os.Getenv("DAT") {
if !*quiet {
log.Printf("recieved an authenticated connection from -> %v...starting server..", r.RemoteAddr)
}
w.Header().Set("Timeout", "86399")
svr.ServeHTTP(w, r)
} else {
log.Printf("recieved an attempted connection from -> %v, but no credentials were provided...", r.RemoteAddr)
w.WriteHeader(401)
w.Write([]byte("failed to authenticate; access denied."))
}
} else {
svr.ServeHTTP(w, r)
}
})
if !*insecure {
if _, err := os.Stat(*cert); err != nil {
fmt.Printf("no cert located at: %v\n", *cert)
os.Exit(1)
}
if _, er := os.Stat(*key); er != nil {
fmt.Printf("no key located at: %v\n", *key)
os.Exit(1)
}
if *both {
go http.ListenAndServeTLS(fmt.Sprintf(":%d", *httpsPort), *cert, *key, nil)
http.ListenAndServe(fmt.Sprintf(":%d", *httpPort), nil)
}
http.ListenAndServeTLS(fmt.Sprintf(":%d", *httpsPort), *cert, *key, nil)
}
if *insecure {
if err := http.ListenAndServe(fmt.Sprintf(":%d", *httpPort), nil); err != nil {
fmt.Println(err)
log.Fatalf("error with webdav server (http port: %v): %v", *httpPort, err)
}
}
}
func monitorRuntimeProfile() {
var p Profile
var stats runtime.MemStats
for {
<-time.After(
time.Duration(*poll) * time.Second)
runtime.ReadMemStats(&stats)
p.Alloc = stats.Alloc
p.TotalAlloc = stats.TotalAlloc
p.MemoryAlloc = stats.Mallocs
p.Free = stats.Frees
p.Objects = p.MemoryAlloc - p.Free
// GC stuff
p.NumCPU = runtime.NumCPU()
p.NGC = stats.NumGC
profile, _ := json.Marshal(p)
log.Println(string(profile))
}
}
func check(e error) {
if e != nil {
fmt.Printf("encountered an error!\t%v\n", e)
os.Exit(1)
}
}