-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
114 lines (100 loc) · 1.91 KB
/
web.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
package main
import (
"embed"
"github.com/gin-gonic/gin"
"html/template"
"io/ioutil"
"net/http"
)
type resp struct {
Code int
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func (r resp) response(c *gin.Context) {
c.JSON(r.Code, resp{
Msg: r.Msg,
Data: r.Data,
})
c.Abort()
}
func Index(c *gin.Context) {
var res []*proxyItem
showProxyInfo(&res)
c.HTML(200, "index.html", map[string]interface{}{
"data": res,
})
}
func Delete(c *gin.Context) {
request := &proxyItem{}
err := c.ShouldBindJSON(request)
if err != nil {
resp{
Code: 400,
Msg: "参数解析失败",
Data: nil,
}.response(c)
return
}
err = deleteProxy(request.ListenAddr, request.ListenPort)
if err != nil {
resp{
Code: 400,
Msg: "删除失败: " + err.Error(),
Data: nil,
}.response(c)
return
}
resp{
Code: 200,
Msg: "删除完成",
Data: nil,
}.response(c)
return
}
func Create(c *gin.Context) {
request := &proxyItem{}
err := c.ShouldBindJSON(request)
if err != nil {
resp{
Code: 400,
Msg: "参数解析失败",
Data: nil,
}.response(c)
return
}
err = createProxy(request.ListenAddr, request.ListenPort, request.TargetAddr, request.TargetPort)
if err != nil {
resp{
Code: 400,
Msg: "创建失败: " + err.Error(),
Data: nil,
}.response(c)
return
}
resp{
Code: 200,
Msg: "创建完成",
Data: nil,
}.response(c)
return
}
//go:embed template
var tmpl embed.FS
//go:embed static
var static embed.FS
func StartService() {
templateFile, _ := template.ParseFS(tmpl, "template/*")
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
engine := gin.Default()
engine.SetHTMLTemplate(templateFile)
engine.StaticFS("/static", http.FS(static))
engine.GET("/", Index)
engine.POST("/delete", Delete)
engine.POST("/create", Create)
err := engine.Run("127.0.0.1:57391")
if err != nil {
ioutil.WriteFile("err.log", []byte(err.Error()), 0644)
}
}