-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (178 loc) · 4.66 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
package main
import (
"embed"
"flag"
"fmt"
"html/template"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
qrcode "github.com/skip2/go-qrcode"
)
var (
rootDir string
//go:embed index.html
indexHTML embed.FS
)
type Auth struct {
Username string
Password string
}
type File struct {
Name string
ModTime time.Time
Size int64
IsDir bool
}
type Directory struct {
ParentUrl string
Files []File
}
func getOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
func fileHandler(w http.ResponseWriter, r *http.Request) {
//Do not cache
r.Header.Del("If-Modified-Since")
r.Header.Del("If-None-Match")
// Disable Client Cache
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
// r.URL.Path is already cleaned by the http server, so there is no '../' in url
// we can safely append it to the rootDir
path := rootDir + r.URL.Path
if strings.HasSuffix(path, "/") {
// if the path is a directory, list the files and directories in it
files, _ := filepath.Glob(path + "*")
// write the index page, list file and directorys, show filename, last modified time, size in table
var dir Directory
dir.ParentUrl = r.URL.Path
for _, file := range files {
_, filename := filepath.Split(file)
if strings.HasPrefix(filename, ".") {
continue
}
info, err := os.Stat(file)
if err != nil {
continue
}
dir.Files = append(dir.Files, File{
Name: filename,
ModTime: info.ModTime(),
Size: info.Size(),
IsDir: info.IsDir(),
})
}
tmpl, _ := template.ParseFS(indexHTML, "index.html")
err := tmpl.Execute(w, dir)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else if strings.HasSuffix(path, "index.html") {
// Avoid redirecting '/index.html' to '/'
f, err := os.Open(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.ServeContent(w, r, path, time.Now(), f)
} else {
// Other files, just serve it
http.ServeFile(w, r, path)
}
}
func logHandler(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
lrw := NewLoggingResponseWriter(w)
h(lrw, r)
end := time.Now()
fmt.Printf("%s %s %s %s %d %d\n",
end.Format("2006/01/02 15:04:05"),
r.RemoteAddr,
r.Method,
r.URL.Path,
lrw.statusCode,
end.Sub(start).Microseconds(),
)
}
}
func basicAuthHandler(h http.HandlerFunc, auth Auth) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || user != auth.Username || pass != auth.Password {
w.Header().Set("WWW-Authenticate", `Basic realm="Please enter your username and password"`)
http.Error(w, "Unauthorized.", http.StatusUnauthorized)
return
}
h(w, r)
}
}
type LoggingResponseWriter struct {
http.ResponseWriter
statusCode int
}
func NewLoggingResponseWriter(w http.ResponseWriter) *LoggingResponseWriter {
return &LoggingResponseWriter{w, http.StatusOK}
}
func (lrw *LoggingResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}
func main() {
ipStr := flag.String("ip", "", "the ip to bind, default is the outbound ip address")
port := flag.Int("port", 8080, "the port to listen on")
dir := flag.String("dir", "", "the directroy to serve, default is current directory")
authStr := flag.String("auth", "", "Basic auth credentials in the format 'username:password'")
flag.Parse()
if *dir == "" {
*dir, _ = os.Getwd()
}
var ip net.IP
if *ipStr != "" {
ip = net.ParseIP(*ipStr)
if ip == nil {
fmt.Printf("%s is not a valid ip address\n", ip)
os.Exit(1)
}
} else {
ip = getOutboundIP()
}
url := fmt.Sprintf("http://%s:%d", ip.String(), *port)
rootDir, _ = filepath.Abs(*dir)
fmt.Printf("Visit %s by clicking:\n%s\n", rootDir, url)
fmt.Println("Or you can scan the qrcode below:")
fmt.Println()
q, _ := qrcode.New(url, qrcode.Highest)
q.DisableBorder = true
art := q.ToSmallString(true)
fmt.Println(art)
fmt.Println()
if *authStr != "" {
parts := strings.SplitN(*authStr, ":", 2)
if len(parts) != 2 {
fmt.Println("Invalid auth credentials")
os.Exit(1)
}
auth := Auth{
Username: parts[0],
Password: parts[1],
}
http.HandleFunc("/", basicAuthHandler(logHandler(fileHandler), auth))
} else {
http.HandleFunc("/", logHandler(fileHandler))
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}