-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileserver.go
96 lines (82 loc) · 2.69 KB
/
fileserver.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
package main
import (
"fileserver/qr"
"fileserver/utils"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
rice "github.com/GeertJohan/go.rice"
"github.com/davecheney/mdns"
)
// 获取本机ip
func GetIP() string {
localIP := ""
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
localIP = ipnet.IP.String()
}
}
}
return localIP
}
// 设置访问的用户名,密码
func authentication(next http.Handler, username string, password string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if password != "" { //密码为空,那么就不验证了
if !ok || user != username || pass != password {
w.Header().Set("WWW-Authenticate", "Basic realm=\"Demo Test\"")
w.WriteHeader(http.StatusUnauthorized)
}
}
next.ServeHTTP(w, r)
})
}
func main() {
// 获取本机IP
localIP := GetIP()
// 获取监听端口,建议mac电脑大于1024,否则需要sudo
var port string
flag.StringVar(&port, "port", "3000", "指定要使用的端口,默认是3000,建议Mac电脑不要使用小于1024的端口,否则需要sudo --port=3000") // -port=9000输入
var username string
flag.StringVar(&username, "username", "admin", "指定要使用的用戶名,默认 --username=admin")
var password string
flag.StringVar(&password, "password", "", "指定要使用的密码,默认为空则不需要输入 --password=123")
var hostname string
flag.StringVar(&hostname, "hostname", "", "可以指定一個hostname来访问文件服务器 --hostname=fileserver.local")
flag.Parse()
box1 := rice.MustFindBox("views")
viewsFileServer := http.StripPrefix("/views/", http.FileServer(box1.HTTPBox()))
http.Handle("/layout/", viewsFileServer)
box := rice.MustFindBox("static")
staticFileServer := http.StripPrefix("/static/", http.FileServer(box.HTTPBox()))
http.Handle("/static/", staticFileServer)
// 自己复制一个fileServer然后照着修改!
fs1 := utils.FileServer(utils.Dir("./"))
http.Handle("/", authentication(http.StripPrefix("/", fs1), username, password))
sendURL := fmt.Sprintf("http://%s:%s", localIP, port)
if hostname != "" {
record := fmt.Sprintf("%s IN A %s", hostname, localIP)
mdns.Publish(record)
sendURL = fmt.Sprintf("http://%s:%s", hostname, port)
}
log.Println(sendURL, "Listening...")
qr.RenderString(sendURL)
if password != "" {
log.Println("Auth username:" + username + " password:" + password)
}
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Println(err.Error())
}
}