-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
302 lines (255 loc) · 6.91 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
package main
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/getlantern/systray"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"golang.design/x/clipboard"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"time"
)
const (
apiVersion = "1"
)
func RunHTTPServer() {
go func() {
engin := gin.New()
setupRoute(engin)
if err := engin.Run(":" + Gconfig.Port); err != nil {
log.Error().Msg("HTTP Server 启动失败 您的应用可能不能正常运行")
systray.Quit()
log.Error().Msg("failed to start http server")
return
}
}()
}
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 Gconfig.Authkey == "" {
c.Next()
return
}
reqAuth := c.GetHeader("X-Auth")
timestamp := time.Now().Unix()
timeKey := timestamp / Gconfig.Authkey_expired_timeout
authCodeRaw := Gconfig.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")
// // log.Info().Msgf("method %s \n statusCode: %s\n clientIP %s\n path: %s\n duration %s\n clientName %s \n", c.Request.Method, statusCode, clientIP, path, duration, clientName)
}
}
const (
TypeText = "text"
TypeFile = "file"
TypeMedia = "media"
TypeBitmap = "bitmap"
TypeUnknown = "unknown"
)
type ResponseFile struct {
Name string `json:"name"`
Content string `json:"content"`
}
type ResponseFiles []ResponseFile
func getHandler(c *gin.Context) {
if ServiceFlag == false {
c.Status(http.StatusBadRequest)
return
}
var b []byte
data_type := TypeText
b = clipboard.Read(clipboard.FmtText)
if b == nil {
b = clipboard.Read(clipboard.FmtImage)
data_type = TypeBitmap
}
log.Info().Msgf("The clipboard type is %s ", data_type)
if b == nil {
log.Error().Msg("failed to get content type of clipboard")
c.Status(http.StatusBadRequest)
return
}
if data_type == TypeText {
log.Info().Msg("get clipboard text")
c.JSON(http.StatusOK, gin.H{
"type": "text",
"data": string(b), // 将数据返回给客户端
})
return
}
if data_type == TypeBitmap {
responseFiles := make([]ResponseFile, 0, 1)
responseFiles = append(responseFiles, ResponseFile{
"clipboard.png",
base64.StdEncoding.EncodeToString(b),
})
c.JSON(http.StatusOK, gin.H{
"type": "file",
"data": responseFiles,
})
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 !Gconfig.ReserveHistory {
// cleanTempFiles()
// }
if ServiceFlag == false {
c.Status(http.StatusBadRequest)
return
}
contentType := c.GetHeader("X-Content-Type")
if contentType == TypeText {
setTextHandler(c)
return
}
setFileHandler(c)
}
func setTextHandler(c *gin.Context) {
var body TextBody
if err := c.ShouldBindJSON(&body); err != nil {
log.Error().AnErr("failed to bind text body", err)
c.Status(http.StatusBadRequest)
return
}
clipboard.Write(clipboard.FmtText, []byte(body.Text))
// if err := utils.Clipboard().SetText(body.Text); err != nil {
// log.Error().AnErr("failed to set clipboard", err)
// c.Status(http.StatusBadRequest)
// return
// }
log.Error().Any("set clipboard text", body)
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")
// 将发送过来的json解析到结构体上。
var body FileBody
if err := c.ShouldBindJSON(&body); err != nil {
log.Error().AnErr("failed to bind file body", err)
c.Status(http.StatusBadRequest)
return
}
// paths := make([]string, 0, len(body.Files)) // 创建一个空切片(数组)
// 枚举发送过来的base64编码的图片,或者文件
for _, file := range body.Files {
if file.Name == "-" && file.Base64 == "-" {
continue
}
fmt.Println(file.Name) //Clipboard 2024年3月2日 10.52.png
// 编译正则表达式
// re_bmp := regexp.MustCompile(`^[a-zA-Z0-9_]+\.bmp$`)
re_png := regexp.MustCompile(`.*\.png$`)
// 判断字符串是否匹配正则表达式
if re_png.MatchString(file.Name) {
log.Info().Msg("Recived Image")
} else {
log.Error().Any("仅仅支持png|bmp数据", file.Name)
c.Status(http.StatusBadRequest)
fmt.Println("仅仅支持png|bmp数据") //Clipboard 2024年3月2日 10.52.png
return
}
fileBytes, err := file.Bytes() // 在这里就完成了解码
if err != nil {
log.Error().AnErr("read buffer faild", err)
c.Status(http.StatusBadRequest)
return
}
clipboard.Write(clipboard.FmtImage, fileBytes)
log.Info().Msgf("set Recived %s to buffer faild", file.Name)
}
c.Status(http.StatusOK)
log.Info().Msg("success1")
}
func notFoundHandler(c *gin.Context) {
log.Error().Msgf("user_ip : %s", c.Request.RemoteAddr)
c.Status(http.StatusNotFound)
}