-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
304 lines (260 loc) · 7.69 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
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
package main
import (
"context"
"flag"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/russross/blackfriday/v2"
"golang.org/x/sync/semaphore"
"golang.org/x/time/rate"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
)
var configFile string
type ConfigModel struct {
Ip string `yaml:"ip"`
Port uint16 `yaml:"port"`
RootPath string `yaml:"rootPath"`
Password string `yaml:"password"`
DocFile string `yaml:"docFile"`
MaxFileSize int64 `yaml:"maxFileSize"`
MaxStorageSize int64 `yaml:"maxStorageSize"`
MaxConcurrency int64 `yaml:"maxConcurrency"`
MaxQueuing int64 `yaml:"maxQueuing"`
MaxLimit int64 `yaml:"maxLimit"`
MaxBurst int64 `yaml:"maxBurst"`
}
var config ConfigModel
func readInConfig(filename string) {
data, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
panic(err)
}
}
var uploadTaskMap sync.Map
type uploadTask struct {
uploadpath string
localpath string
totalSize int64
writtenSize int64
createAt time.Time
}
var EstimateStorageSize int64
func initRootPath() {
if !strings.HasPrefix(config.RootPath, "/var/") {
panic("rootPath must start with /var/")
}
os.MkdirAll(config.RootPath, os.ModePerm)
go func() {
for {
time.Sleep(time.Second * 5)
var total int64 = 0
filepath.Walk(config.RootPath, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() {
total += info.Size()
}
return nil
})
atomic.StoreInt64(&EstimateStorageSize, total)
}
}()
}
func main() {
flag.StringVar(&configFile, "c", "fileserver.yaml", "config file")
flag.Parse()
readInConfig(configFile)
initRootPath()
doc, err := os.ReadFile(config.DocFile)
if err != nil {
panic(err)
}
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
engine.Use(concurrencyLimit(), rateLimit(), preCheck())
engine.Static("/static", config.RootPath)
engine.POST("/upload/*path", func(c *gin.Context) {
body := c.Request.Body
defer body.Close()
targetPath := c.Param("path")
if targetPath[len(targetPath)-1] == '/' {
c.String(http.StatusBadRequest, "[ERR] Must provide a complete file path, not only directory\n")
return
}
localPath := config.RootPath + targetPath
stat, err := os.Stat(targetPath)
if err == nil && stat.IsDir() {
c.String(http.StatusBadRequest, "[ERR] Provided path is an existing directory\n")
return
}
task := uploadTask{
uploadpath: targetPath,
localpath: localPath,
totalSize: c.Request.ContentLength,
writtenSize: 0,
createAt: time.Now(),
}
if v, exists := uploadTaskMap.LoadOrStore(targetPath, &task); exists {
c.String(http.StatusForbidden, fmt.Sprintf("[ERR] Provided path is being upload now, start at %s\n", v.(*uploadTask).createAt))
return
}
defer uploadTaskMap.Delete(targetPath)
os.MkdirAll(filepath.Dir(localPath), os.ModePerm)
targetFile, err := os.Create(localPath)
if err != nil {
c.String(http.StatusInternalServerError, "[ERR] Cannot create target file on server\n")
return
}
defer targetFile.Close()
buf := make([]byte, 65536)
finish := false
for !finish {
n, err := body.Read(buf)
if err != nil {
if err != io.EOF {
c.String(http.StatusInternalServerError, fmt.Sprintf("[ERR] Read target file from client error: %s\n", err.Error()))
return
} else {
finish = true
}
}
task.writtenSize += int64(n)
written, err := targetFile.Write(buf[:n])
if err != nil || written != n {
c.String(http.StatusInternalServerError, fmt.Sprintf("[ERR] Write target file error: %s\n", err.Error()))
return
}
}
c.String(http.StatusOK, fmt.Sprintf("[OK] %s upload finish\n", targetPath))
})
engine.GET("/progress/*path", func(c *gin.Context) {
targetPath := c.Param("path")
v, exists := uploadTaskMap.Load(targetPath)
if !exists {
c.Status(http.StatusNotFound)
return
}
taskP := v.(*uploadTask)
if taskP == nil {
c.Status(http.StatusNotFound)
return
}
task := *taskP
cost := time.Since(task.createAt).Seconds()
speed := float64(task.writtenSize) / (1024 * 1024) / cost
estimatedCost := cost * (float64(task.totalSize) / float64(task.writtenSize))
c.String(http.StatusOK, fmt.Sprintf("%.2f%% [%.1f s / %.1f s] [%d B / %d B] %.2f MB/s\n", float64(task.writtenSize)*100/float64(task.totalSize), cost, estimatedCost, task.writtenSize, task.totalSize, speed))
})
engine.GET("/list/*path", func(c *gin.Context) {
path := c.Param("path")
localPath := config.RootPath + path
info, err := os.Stat(localPath)
if err != nil {
c.String(http.StatusNotFound, "[ERR] Not found\n")
return
}
var html strings.Builder
if !info.IsDir() {
html.WriteString(fmt.Sprintf("<a href=\"/static%s\" style=\"color: black; text-decoration: underline;\" download>%s</a><br>", path, info.Name()))
c.Data(http.StatusOK, "html", []byte(html.String()))
return
}
entries, err := os.ReadDir(localPath)
if err != nil {
c.String(http.StatusNotFound, "[ERR] Not found\n")
return
}
for _, entry := range entries {
if !entry.IsDir() {
html.WriteString(fmt.Sprintf("<a href=\"/static%s/%s\" style=\"color: black; text-decoration: underline;\" download>%s</a><br>", path, entry.Name(), entry.Name()))
} else {
html.WriteString(fmt.Sprintf("<a href=\"/list%s/%s\" style=\"color: blue; text-decoration: underline; font-weight: bold\">%s</a><br>", path, entry.Name(), entry.Name()))
}
}
c.Data(http.StatusOK, "html", []byte(html.String()))
})
engine.DELETE("/delete/*path", func(c *gin.Context) {
targetPath := c.Param("path")
if targetPath[len(targetPath)-1] == '/' {
c.String(http.StatusBadRequest, "[ERR] Must provide a complete file path, not only directory\n")
return
}
if v, exists := uploadTaskMap.Load(targetPath); exists {
c.String(http.StatusForbidden, fmt.Sprintf("[ERR] Provided path is being upload now, start at %s\n", v.(*uploadTask).createAt))
return
}
localPath := config.RootPath + targetPath
_, err := os.Stat(localPath)
if err != nil {
c.String(http.StatusNotFound, "[ERR] Provided path is not exists\n")
return
}
os.RemoveAll(localPath)
c.String(http.StatusOK, fmt.Sprintf("[OK] %s delete finish\n", targetPath))
})
engine.GET("/", func(c *gin.Context) {
c.Data(http.StatusOK, "text/html; charset=utf-8", blackfriday.Run(doc))
})
if err := engine.Run(fmt.Sprintf("%s:%d", config.Ip, config.Port)); err != nil {
panic(err)
}
}
func rateLimit() gin.HandlerFunc {
limiter := rate.NewLimiter(rate.Limit(config.MaxLimit), int(config.MaxBurst))
return func(c *gin.Context) {
if !limiter.Allow() {
c.String(http.StatusForbidden, "[ERR] Request too frequent\n")
return
}
c.Next()
}
}
func concurrencyLimit() gin.HandlerFunc {
sem := semaphore.NewWeighted(config.MaxConcurrency)
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(config.MaxQueuing))
defer cancel()
if err := sem.Acquire(ctx, 1); err != nil {
c.String(http.StatusForbidden, "[ERR] Service busy\n")
return
}
c.Next()
sem.Release(1)
}
}
func preCheck() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.Method == "GET" {
c.Next()
return
}
if c.GetHeader("password") != config.Password {
c.String(http.StatusForbidden, "[ERR] Password invalid\n")
c.Abort()
return
}
clen := c.Request.ContentLength
if clen > config.MaxFileSize<<20 {
c.String(http.StatusForbidden, "[ERR] Uploaded file too large\n")
c.Abort()
return
}
if atomic.LoadInt64(&EstimateStorageSize)+clen > config.MaxStorageSize<<20 {
c.String(http.StatusForbidden, "[ERR] No disk space for this file\n")
c.Abort()
return
}
c.Next()
}
}