forked from YanxinTang/clipboard-online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
403 lines (350 loc) · 9.83 KB
/
server.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"image/png"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/YanxinTang/clipboard-online/utils"
"github.com/gin-gonic/gin"
"github.com/lxn/walk"
"github.com/sirupsen/logrus"
"golang.org/x/image/bmp"
)
const (
apiVersion = "1"
)
func setupRoute(engin *gin.Engine) {
engin.Use(clientName(), logger(), gin.Recovery(), apiVersionChecker(), auth())
engin.GET("/", getHandler)
engin.POST("/", setHandler)
engin.NoRoute(notFoundHandler)
}
func clientName() gin.HandlerFunc {
return func(c *gin.Context) {
urlEncodedClientName := c.GetHeader("X-Client-Name")
clientName, err := url.PathUnescape(urlEncodedClientName)
if err != nil || clientName == "" {
clientName = "匿名设备"
}
c.Set("clientName", clientName)
c.Next()
}
}
func apiVersionChecker() gin.HandlerFunc {
return func(c *gin.Context) {
version := c.GetHeader("X-API-Version")
if version == apiVersion {
c.Next()
return
}
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "接口版本不匹配,请升级您的捷径",
})
}
}
func auth() gin.HandlerFunc {
return func(c *gin.Context) {
if app.config.Authkey == "" {
c.Next()
return
}
reqAuth := c.GetHeader("X-Auth")
timestamp := time.Now().Unix()
timeKey := timestamp / app.config.AuthkeyExpiredTimeout
authCodeRaw := app.config.Authkey + "." + strconv.FormatInt(timeKey, 10)
authCodeHash := md5.Sum([]byte(authCodeRaw))
authCodeString := hex.EncodeToString(authCodeHash[:])
if authCodeString == reqAuth {
c.Next()
return
}
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "操作被拒绝:Authkey 验证失败",
})
}
}
func logger() gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Request.URL.Path
start := time.Now()
c.Next()
duration := time.Since(start)
clientIP := c.ClientIP()
statusCode := c.Writer.Status()
clientName := c.GetString("clientName")
requestLogger := log.WithFields(logrus.Fields{
"method": c.Request.Method,
"statusCode": statusCode,
"clientIP": clientIP,
"path": path,
"duration": duration,
"clientName": clientName,
})
if statusCode >= http.StatusInternalServerError {
requestLogger.Error()
} else if statusCode >= http.StatusBadRequest {
requestLogger.Warn()
} else {
requestLogger.Info()
}
}
}
type ResponseFile struct {
Name string `json:"name"`
Content string `json:"content"`
}
type ResponseFiles []ResponseFile
func getHandler(c *gin.Context) {
contentType, err := utils.Clipboard().ContentType()
if err != nil {
log.WithError(err).Info("failed to get content type of clipboard")
c.Status(http.StatusBadRequest)
return
}
if contentType == utils.TypeText {
str, err := walk.Clipboard().Text()
if err != nil {
c.Status(http.StatusBadRequest)
log.WithError(err).Warn("failed to get clipboard")
return
}
log.Info("get clipboard text")
c.JSON(http.StatusOK, gin.H{
"type": "text",
"data": str,
})
defer sendCopyNotification(log, c.GetString("clientName"), str)
return
}
if contentType == utils.TypeBitmap {
bmpBytes, err := utils.Clipboard().Bitmap()
if err != nil {
log.WithError(err).Warn("failed to get bmp bytes from clipboard")
}
bmpBytesReader := bytes.NewReader(bmpBytes)
bmpImage, err := bmp.Decode(bmpBytesReader)
if err != nil {
log.WithError(err).Warn("failed to decode bmp")
c.JSON(http.StatusBadRequest, gin.H{"error": "无法获取剪切板内容"})
return
}
pngBytesBuffer := new(bytes.Buffer)
if err = png.Encode(pngBytesBuffer, bmpImage); err != nil {
log.WithError(err).Warn("failed to encode bmp as png")
}
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无法获取剪切板内容"})
return
}
responseFiles := make([]ResponseFile, 0, 1)
responseFiles = append(responseFiles, ResponseFile{
"clipboard.png",
base64.StdEncoding.EncodeToString(pngBytesBuffer.Bytes()),
})
c.JSON(http.StatusOK, gin.H{
"type": "file",
"data": responseFiles,
})
defer sendCopyNotification(log, c.GetString("clientName"), "[图片媒体] 被复制")
return
}
if contentType == utils.TypeFile {
// get path of files from clipboard
filenames, err := utils.Clipboard().Files()
if err != nil {
log.WithError(err).Warn("failed to get path of files from clipboard")
c.Status(http.StatusBadRequest)
return
}
responseFiles := make([]ResponseFile, 0, len(filenames))
for _, path := range filenames {
base64, err := readBase64FromFile(path)
if err != nil {
log.WithError(err).WithField("filepath", path).Warning("read base64 from file failed")
continue
}
responseFiles = append(responseFiles, ResponseFile{filepath.Base(path), base64})
}
log.Info("get clipboard files")
c.JSON(http.StatusOK, gin.H{
"type": "file",
"data": responseFiles,
})
defer sendCopyNotification(log, c.GetString("clientName"), "[文件] 被复制")
return
}
c.JSON(http.StatusBadRequest, gin.H{"error": "无法识别剪切板内容"})
}
func readBase64FromFile(path string) (string, error) {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(fileBytes), nil
}
// Set clipboard handler
// TextBody is a struct of request body when iOS send files to windows
type TextBody struct {
Text string `json:"data"`
}
func setHandler(c *gin.Context) {
if !app.config.ReserveHistory {
cleanTempFiles()
}
contentType := c.GetHeader("X-Content-Type")
if contentType == utils.TypeText {
setTextHandler(c)
return
}
setFileHandler(c)
}
func setTextHandler(c *gin.Context) {
var body TextBody
if err := c.ShouldBindJSON(&body); err != nil {
log.WithError(err).Warn("failed to bind text body")
c.Status(http.StatusBadRequest)
return
}
if err := utils.Clipboard().SetText(body.Text); err != nil {
log.WithError(err).Warn("failed to set clipboard")
c.Status(http.StatusBadRequest)
return
}
var notify string = "粘贴内容为空"
if body.Text != "" {
notify = body.Text
}
defer sendPasteNotification(log, c.GetString("clientName"), notify)
log.WithField("text", body.Text).Info("set clipboard text")
c.Status(http.StatusOK)
}
// FileBody is a struct of request body when iOS send files to windows
type FileBody struct {
Files []File `json:"data"`
}
// File is a struct represtents request file
type File struct {
Name string `json:"name"` // filename
Base64 string `json:"base64"`
_bytes []byte `json:"-"` // don't use this directly. use *File.Bytes() to get bytes
}
// Bytes returns byte slice of file
func (f *File) Bytes() ([]byte, error) {
if len(f._bytes) > 0 {
return f._bytes, nil
}
fileBytes, err := base64.StdEncoding.DecodeString(f.Base64)
if err != nil {
return []byte{}, nil
}
f._bytes = fileBytes
return fileBytes, nil
}
func setFileHandler(c *gin.Context) {
contentType := c.GetHeader("X-Content-Type")
var body FileBody
if err := c.ShouldBindJSON(&body); err != nil {
log.WithError(err).Warn("failed to bind file body")
c.Status(http.StatusBadRequest)
return
}
paths := make([]string, 0, len(body.Files))
for _, file := range body.Files {
if file.Name == "-" && file.Base64 == "-" {
continue
}
path := utils.LatestFilename(app.GetTempFilePath(file.Name))
fileBytes, err := file.Bytes()
if err != nil {
log.WithField("filename", file.Name).Warn("failed to read file bytes")
continue
}
if err := newFile(path, fileBytes); err != nil {
log.WithError(err).WithField("path", path).Warn("failed to create file")
continue
}
paths = append(paths, path)
}
if app.config.ReserveHistory {
// clean paths in _filename.txt
setLastFilenames(nil)
} else {
// write paths to file
setLastFilenames(paths)
}
if err := utils.Clipboard().SetFiles(paths); err != nil {
log.WithError(err).Warn("failed to set clipboard")
c.Status(http.StatusBadRequest)
return
}
var notify string
if contentType == utils.TypeMedia {
notify = "[图片媒体] 已复制到剪贴板"
} else {
notify = "[文件] 已复制到剪贴板"
}
defer sendPasteNotification(log, c.GetString("clientName"), notify)
log.WithField("paths", paths).Info("set clipboard file")
c.Status(http.StatusOK)
}
func notFoundHandler(c *gin.Context) {
requestLogger := log.WithFields(logrus.Fields{"user_ip": c.Request.RemoteAddr})
requestLogger.Info("404 not found")
c.Status(http.StatusNotFound)
}
func sendCopyNotification(logger *logrus.Logger, client, notify string) {
if app.config.Notify.Copy {
sendNotification(logger, "复制", client, notify)
}
}
func sendPasteNotification(logger *logrus.Logger, client, notify string) {
if app.config.Notify.Paste {
sendNotification(logger, "粘贴", client, notify)
}
}
func sendNotification(logger *logrus.Logger, action, client, notify string) {
if notify == "" {
notify = action + "内容为空"
}
title := fmt.Sprintf("%s自 %s", action, client)
if err := app.ni.ShowInfo(title, notify); err != nil {
logger.WithError(err).WithField("notify", notify).Warn("failed to send notification")
}
}
func setLastFilenames(filenames []string) {
path := app.GetTempFilePath("_filename.txt")
allFilenames := strings.Join(filenames, "\n")
_ = ioutil.WriteFile(path, []byte(allFilenames), os.ModePerm)
}
func newFile(path string, bytes []byte) error {
return ioutil.WriteFile(path, bytes, 0644)
}
func cleanTempFiles() {
path := app.GetTempFilePath("_filename.txt")
if utils.IsExistFile(path) {
file, err := os.Open(path)
if err != nil {
log.WithError(err).WithField("path", path).Warn("failed to open temp file")
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
delPath := scanner.Text()
if err = os.Remove(delPath); err != nil {
log.WithError(err).WithField("delPath", delPath).Warn("failed to delete specify path")
}
}
}
}