-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
189 lines (147 loc) · 4.3 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
/*
coolhttpd: static file server
author: boot-error
*/
package main
import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
)
type Config struct {
Host string
Port uint
Username string
Passwd string
Directory string
}
type File struct {
Name string `json:"name"`
Path string `json:"path"`
IsDir bool `json:"isdir"`
Modified string `json:"modified"`
Size int64 `json:"size"`
Contents []File `json:"contents"`
}
func getDirList(rootdir string) (f File, err error) {
var fdata os.FileInfo
fdata, err = os.Stat(rootdir)
if err != nil {
fmt.Println(err)
return
}
f.Name = fdata.Name()
f.Path = rootdir
f.Size = fdata.Size()
f.Modified = fdata.ModTime().Format(time.UnixDate)
if fdata.IsDir() {
f.IsDir = true
files, _ := ioutil.ReadDir(rootdir)
for _, file := range files {
fc, _ := getDirList(path.Join(rootdir, file.Name()))
f.Contents = append(f.Contents, fc)
}
}
return
}
func renderDirectory(f File, w http.ResponseWriter) {
fmt.Fprintf(w, "<details style=\"margin-left:10px;\">")
fmt.Fprintf(w, "<summary>%s</summary>", f.Name)
for _, cf := range f.Contents {
if cf.IsDir {
renderDirectory(cf, w)
} else {
resourceURL := path.Join("/file/", strings.TrimPrefix(cf.Path, config.Directory))
fmt.Fprintf(w, "<p style=\"margin: 0px;\"><a href=\"%s\" style=\" margin-left:10px\">%s</a></p>", resourceURL, cf.Name)
}
}
fmt.Fprintf(w, "</details>")
}
func directoryListingHandler(w http.ResponseWriter, r *http.Request) {
f, err := getDirList(config.Directory)
w.Header().Add("Content-Type", "text/html; charset=utf-8")
if err != nil {
http.Error(w, "Error while indexing directory", http.StatusInternalServerError)
}
if f.IsDir {
fmt.Fprintf(w, "<h3>Directory %s</h3>", f.Name)
renderDirectory(f, w)
} else {
http.Redirect(w, r, f.Name, http.StatusTemporaryRedirect)
}
}
func fileServingHandler(w http.ResponseWriter, r *http.Request) {
// fmt.Printf("Serving file %s\n", path.Join(config.Directory, strings.TrimPrefix(r.URL.Path, "/file/")))
http.ServeFile(w, r, path.Join(config.Directory, strings.TrimPrefix(r.URL.Path, "/file/")))
}
func AuthMiddleware(handlerFunc http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
authParams, chk := r.Header["Authorization"]
if !chk {
w.Header().Add("WWW-authenticate", "Basic")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
authData := strings.Split(authParams[0], " ")
if authData[0] == "Basic" {
decodedAuthData, err := base64.StdEncoding.DecodeString(authData[1])
if err != nil {
fmt.Println(err)
http.Error(w, "Unauthorized, Failed to decode auth credentials", http.StatusUnauthorized)
return
}
authValues := strings.Split(string(decodedAuthData), ":")
if authValues[0] == config.Username && authValues[1] == config.Passwd {
handlerFunc(w, r)
} else {
fmt.Println(authValues)
http.Error(w, "Unauthorized, wrong credentials", http.StatusUnauthorized)
return
}
} else {
fmt.Println(authData[0])
http.Error(w, "Unauthorized, Couldn't understand auth header", http.StatusUnauthorized)
return
}
}
}
func LoggingMiddleware(handlerFunc http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handlerFunc(w, r)
fmt.Println(
r.RemoteAddr,
r.Method,
r.Proto,
r.Header["User-Agent"],
r.URL,
)
}
}
var config = Config{
Username: "test",
Passwd: "123",
Directory: ".",
Host: "0.0.0.0",
Port: 8000,
}
func setupConfig() {
flag.StringVar(&config.Username, "username", config.Username, "Username for the Basic Authentication")
flag.StringVar(&config.Passwd, "passwd", config.Passwd, "Password for the Basc Authentication")
flag.StringVar(&config.Host, "host", config.Host, "ip address for the server")
flag.UintVar(&config.Port, "port", config.Port, "server port")
flag.StringVar(&config.Directory, "dir", config.Directory, "directory to host")
flag.Parse()
}
func main() {
setupConfig()
http.Handle("/", LoggingMiddleware(AuthMiddleware(directoryListingHandler)))
http.Handle("/file/", LoggingMiddleware(AuthMiddleware(fileServingHandler)))
http.ListenAndServe(strings.Join([]string{config.Host, strconv.Itoa(int(config.Port))}, ":"), nil)
}