-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
344 lines (321 loc) · 8.1 KB
/
serve.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
/*
来源:https://github.com/Alexendoo/serve
有历史记录good
USAGE:
serve [OPTION]... [DIR]...
OPTIONS:
--host -- bind to host (default: localhost)
-i, --index -- serve all paths to index if file not found
--no-list -- disable directory listings
-p, --port -- bind to port (default: 8080)
-v, --verbose -- display requests and responses
*/
package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"path"
"path/filepath"
"strings"
"syscall"
"time"
)
var (
host string
port string
index string
noList bool
verbose bool
version = "HEAD"
htmlTmpl = template.Must(template.New("html").Parse(html))
)
const (
html = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body {
font-size: 14px;
font-family: consolas, "Liberation Mono", "DejaVu Sans Mono", Menlo, monospace;
}
a {
display: block;
color: blue;
text-decoration: none;
}
a:hover {
background-color: #f3f3f3;
}
.req-path {
color: #bbb;
}
</style>
</head>
<body>
{{range .}}
<h3>
<span class="local-path">{{.LocalPath}}</span><span class="req-path">{{.RequestPath}}</span>
</h3>
{{range .Entries}}
<a class="entry{{if .IsDir}} dir{{end}}" href="{{.Link}}">{{.Name}}{{if .IsDir}}/{{end}}</a>
{{end}}
{{end}}
</body>
`
usage = `
NAME:
Serve - HTTP server for files spanning multiple directories
USAGE:
%s [OPTION]... [DIR]...
VERSION:
%s
OPTIONS:
--host -- bind to host (default: localhost)
-i, --index -- serve all paths to index if file not found
--no-list -- disable directory listings
-p, --port -- bind to port (default: 8080)
-v, --verbose -- display requests and responses
`
)
func main() {
// handle interrupts (0 exit on ctrl + c)
c := make(chan os.Signal, 2)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-c
os.Exit(0)
}()
// Log just the timestamp + message
log.SetFlags(log.Ltime)
flags := getFlags()
serve(flags)
}
// getFlags returns the command line flags passed to the serve binary
func getFlags() *flag.FlagSet {
flags := flag.NewFlagSet("flags", flag.ContinueOnError)
flags.Usage = func() {
usageName := filepath.Base(os.Args[0])
fmt.Printf(usage, usageName, version)
}
flags.StringVar(&port, "port", "80", "")
flags.StringVar(&port, "p", "80", "")
flags.StringVar(&host, "host", "0.0.0.0", "")
flags.StringVar(&index, "index", "", "")
flags.StringVar(&index, "i", "", "")
flags.BoolVar(&noList, "no-list", false, "")
flags.BoolVar(&verbose, "verbose", true, "")
flags.BoolVar(&verbose, "v", true, "")
err := flags.Parse(os.Args[1:])
if err == flag.ErrHelp {
os.Exit(0)
}
if err != nil {
os.Exit(1)
}
return flags
}
func serve(flags *flag.FlagSet) {
dirs := make([]string, flags.NArg())
for i := range dirs {
dirs[i] = flags.Arg(i)
}
if len(dirs) == 0 {
// serve from the current directory
dirs = []string{"."}
}
http.HandleFunc("/", makeHandler(dirs))
address := net.JoinHostPort(host, port)
data4:="starting on: http://"+address+"\r\n"
wlog(data4)
log.Printf("starting on: http://%s", address)
log.Fatal(http.ListenAndServe(address, nil))
}
func makeHandler(dirs []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logRequest(r)
server := fmt.Sprintf("serve/%s", version)
w.Header().Set("Server", server)
if !validRequest(r) {
http.Error(w, "invalid path", http.StatusBadRequest)
log.Printf("invalid path: %s", r.URL.Path)
return
}
if tryFiles(w, r, dirs) {
return
}
if len(index) > 0 && staticIndex(w, r) {
return
}
if !noList && tryDirs(w, r, dirs) {
return
}
http.NotFound(w, r)
}
}
func wlog(data string){
f, error := os.OpenFile("./run.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
if error != nil {
fmt.Println(error)
}
//写入字符串
//buf:=[]byte(data)
now := time.Now()
formatNow := now.Format("2006-01-02 15:04:05")
newdata:=formatNow+"\t"+data
f.WriteString(newdata)
f.Close()
}
func timenow()(string){
now := time.Now()
formatNow := now.Format("2006-01-02 15:04:05")
return formatNow
}
func logRequest(r *http.Request) {
if !verbose {
return
}
data := r.RemoteAddr+" -> "+r.Method+"\t"+r.RequestURI+"\t"+r.Proto+"\r\n"
wlog(data)
log.Printf("%s → %s %s %s", r.RemoteAddr, r.Method, r.RequestURI, r.Proto)
/*f, error := os.OpenFile("./run.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
if error != nil {
fmt.Println(error)
}*/
//写入字符串
//buf:=[]byte(data)
//f.WriteString(data)
//f.Close()
}
// validRequest returns false if the request is invalid: Contains ".."
func validRequest(r *http.Request) bool {
if !strings.Contains(r.URL.Path, "..") {
return true
}
for _, field := range strings.FieldsFunc(r.URL.Path, isSlashRune) {
if field == ".." {
return false
}
}
return true
}
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
func tryFiles(w http.ResponseWriter, r *http.Request, dirs []string) bool {
for _, dir := range dirs {
filePath := filepath.Join(dir, r.URL.Path)
indexPath := filepath.Join(filePath, "index.html")
if tryFile(w, r, filePath) || tryFile(w, r, indexPath) {
return true
}
}
return false
}
// tryFile attempts to serve a file at filePath to the provided ResponseWriter
func tryFile(w http.ResponseWriter, r *http.Request, filePath string) bool {
stat, statErr := os.Stat(filePath)
if statErr != nil || stat.IsDir() {
return false
}
file, fileErr := os.Open(filePath)
defer file.Close()
if fileErr != nil {
return false
}
if verbose {
filename, _ := filepath.Abs(filePath)
data2:=r.RemoteAddr+" <- "+filename+"\r\n"
wlog(data2)
log.Printf("%s ← %s", r.RemoteAddr, filename)
}
http.ServeContent(w, r, stat.Name(), stat.ModTime(), file)
return true
}
// staticIndex will attempt to serve the globally defined index file
func staticIndex(w http.ResponseWriter, r *http.Request) bool {
file, fileErr := os.Open(index)
defer file.Close()
stat, statErr := os.Stat(index)
if fileErr != nil || statErr != nil {
log.Println(fileErr)
return false
}
http.ServeContent(w, r, stat.Name(), stat.ModTime(), file)
return true
}
type dirList struct {
LocalPath string
RequestPath string
Entries []entry
}
type entry struct {
Name string
Link string
IsDir bool
}
// tryDirs will generate directory listings for any available directories,
// providing multiple in the case that there are several matching directories
//
// Example: `serve dir1 dir2` would list directory entries dir1 containing file1,
// and dir2 containing file2 and file3
// .
// ├── dir1
// │ └── file1
// └── dir2
// ├── file2
// └── file3
func tryDirs(w http.ResponseWriter, r *http.Request, dirs []string) bool {
dirLists := []dirList{}
found := false
for _, dir := range dirs {
dirPath := filepath.Join(dir, r.URL.Path)
dirInfo, err := ioutil.ReadDir(dirPath)
if err != nil {
continue
}
entries := []entry{
{
Name: "..",
Link: path.Join(r.URL.Path, ".."),
IsDir: true,
},
}
for _, file := range dirInfo {
entries = append(entries, entry{
Name: file.Name(),
Link: path.Join(r.URL.Path, file.Name()),
IsDir: file.IsDir(),
})
}
dirLists = append(dirLists, dirList{
LocalPath: dir,
RequestPath: r.URL.Path,
Entries: entries,
})
found = true
}
if found {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
logDirLists(r, dirLists)
htmlTmpl.Execute(w, dirLists)
}
return found
}
func logDirLists(r *http.Request, dirLists []dirList) {
if !verbose {
return
}
output := ""
for _, dir := range dirLists {
output += dir.LocalPath + "/, "
}
data3:=r.RemoteAddr+" <- "+output[:len(output)-2]+"\r\n"
wlog(data3)
log.Printf("%s ← %s", r.RemoteAddr, output[:len(output)-2])
}