-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (179 loc) · 5.14 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
package main
import (
"embed"
"fmt"
"html/template"
"io"
"log/slog"
"net"
"net/http"
"os"
"path/filepath"
"github.com/common-nighthawk/go-figure"
"github.com/spf13/pflag"
)
type FileInfo struct {
Name string
}
var (
//go:embed index.gohtml
homePage embed.FS
//go:embed favicon.ico
favicon []byte
)
var (
port = pflag.IntP("port", "p", 8080, "Listen addr")
root = pflag.String("root", "./", "The root path to serve")
onlyLocal = pflag.Bool("local", true, "Only listening on local machine")
onlyIntranet = pflag.Bool("intranet", false, "Only listening on intranet")
advance = pflag.Bool("advance", false, "Use advance mode")
)
var (
serveRoot = ""
)
func main() {
var err error
figure.NewFigure("FileSharer", "standard", true).Print()
fmt.Println()
pflag.Parse()
if filepath.IsAbs(*root) {
serveRoot = *root
} else {
pwd, err := os.Getwd()
if err != nil {
slog.With("err", err).Error("get current path error")
return
}
serveRoot = filepath.Join(pwd, *root)
}
var listenAddrs string
switch {
case *onlyLocal:
listenAddrs = fmt.Sprintf("127.0.0.1:%d", *port)
fmt.Printf("listening on: http://127.0.0.1:%d\n", *port)
case *onlyIntranet:
localIP := GetLocalIP()
listenAddrs = fmt.Sprintf("%s:%d", localIP, *port)
fmt.Printf("listening on: http://%s:%d\n", localIP, *port)
default:
interfaces, err := net.Interfaces()
if err != nil {
panic(fmt.Errorf("listing net interfaces: %w", err))
}
for _, i := range interfaces {
var addrs []net.Addr
addrs, err = i.Addrs()
if err != nil {
panic(fmt.Errorf("i.Addrs: %w", err))
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
fmt.Printf("listening on: http://%s:%d\n", ip, *port)
}
}
listenAddrs = fmt.Sprintf(":%d", *port)
}
if *advance {
http.HandleFunc("/", home)
http.HandleFunc("/favicon.ico", ico)
http.HandleFunc("/uploadFile", uploadFile)
http.HandleFunc("/downloadFile", downloadFile)
if err = http.ListenAndServe(listenAddrs, LoggerMiddleware(http.DefaultServeMux)); err != nil {
slog.With("err", err).Error("start file sharer error")
return
}
} else {
if err = http.ListenAndServe(listenAddrs, http.FileServer(http.Dir(*root))); err != nil {
slog.With("err", err).Error("start file sharer error")
return
}
}
}
func LoggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
clientIP, _ := GetClientIP(r)
slog.Info(fmt.Sprintf("%s %s from %s", r.Method, r.RequestURI, clientIP))
})
}
func ico(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
func home(w http.ResponseWriter, r *http.Request) {
tpl, err := template.ParseFS(homePage, "index.gohtml")
if err != nil {
slog.With("err", err, "uri", r.RequestURI).Error("index page not found in pages cache")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
files, err := os.ReadDir(serveRoot)
if err != nil {
slog.With("err", err, "uri", r.RequestURI).Error("ReadDir error")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var fileInfos []FileInfo
for _, file := range files {
fileInfos = append(fileInfos, FileInfo{Name: file.Name()})
}
data := struct{ Files []FileInfo }{fileInfos}
if err := tpl.Execute(w, data); err != nil {
slog.With("err", err, "uri", r.RequestURI).Error("Execute tpl error")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func uploadFile(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(1024000)
if err != nil {
slog.With("err", err).Error("ParseMultipartForm error")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
files := r.MultipartForm.File["files"]
for _, fileHeader := range files {
file, err := fileHeader.Open()
if err != nil {
slog.With("err", err).Error("Open file error")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
out, err := os.Create(filepath.Join(serveRoot, fileHeader.Filename))
if err != nil {
slog.With("err", err).Error("Create file error")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
slog.With("err", err).Error("Copy file error")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
slog.With("f", fileHeader.Filename).Info("upload file success")
}
fmt.Fprintf(w, `{"code":0,"msg":"Upload file success!"}`)
}
func downloadFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fileName := r.URL.Query().Get("file")
filePath := filepath.Join(serveRoot, fileName)
_, err := os.Stat(filePath)
if err != nil {
slog.With("err", err).Error("Open file error")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(filePath))
http.ServeFile(w, r, filePath)
}