-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
234 lines (227 loc) · 5.37 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
package main
import (
"github.com/gin-gonic/gin"
"github.com/dgrijalva/jwt-go"
"time"
"log"
"github.com/lllwan/redirect/model"
"fmt"
)
var conf = model.Config
func Login(g *gin.Context){
username := g.DefaultPostForm("username", "")
password := g.DefaultPostForm("password", "")
if username == "" || password == ""{
g.JSON(403, gin.H{
"errcode": 403,
"errmsg": "用户名或密码不可为空!",
})
g.Abort()
return
}
var user model.Users
if err := model.DB.Where("username=? and password=?",
username, model.Convert(username, password)).First(&user).Error;err != nil{
g.JSON(403, gin.H{
"errcode": 403,
"errmsg": "登入失败!",
})
g.Abort()
return
}
token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
claims["iat"] = time.Now().Unix()
claims["user"] = username
claims["ip"] = g.Request.RemoteAddr
token.Claims = claims
tokenStr, err := token.SignedString([]byte(conf.SECRET))
if err != nil{
g.JSON(500, gin.H{
"errcode":500,
"errmsg":"内部错误:Token信息生成失败!",
})
log.Println(username, "认证成功,但是生成token失败!", err)
g.Abort()
return
}
g.JSON(200, gin.H{
"errcode":0,
"errmsg":"OK",
"token":tokenStr,
})
}
func Validate() gin.HandlerFunc{
return func(g *gin.Context) {
token := g.GetHeader("authorization")
if token == ""{
g.JSON(403, gin.H{
"errcode":403,
"errmsg":"请求非法!",
})
g.Abort()
return
}
auth,err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return []byte(conf.SECRET), nil
})
if !auth.Valid{
g.JSON(403, gin.H{
"errcode":403,
"errmsg":"Not authorized!",
"result":err.Error(),
})
g.Abort()
}
g.Set("claims", auth.Claims)
}
}
func UrlForward(g *gin.Context){
Host := g.Request.Host
var acl model.Acls
if err := model.DB.Where("domain=?",Host).First(&acl).Error;err != nil{
log.Println("数据库查询失败,Host:", g.Request.Host, err)
g.JSON(403, gin.H{
"errcode":403,
"errmsg":"数据查询失败!",
})
g.Abort()
return
}
switch acl.Method {
default:
g.HTML(200, "template.html", gin.H{
"title": acl.Title,
"count": acl.Count,
"countid": acl.Countid,
"keywords": acl.Keywords,
"description": acl.Description,
"favicon": acl.Favicon,
"url": acl.Url,
"path":g.Request.URL.Path,
})
case "301":
g.Redirect(301, acl.Url)
case "301all":
g.Redirect(301, acl.Url + g.Request.URL.Path)
}
}
func ChangePassword(g *gin.Context){
claims := g.MustGet("claims")
oldpassword := g.DefaultPostForm("oldpass", "")
password := g.DefaultPostForm("password", "")
if len([]rune(password)) >=8 {
var user model.Users
claiminfo := claims.(jwt.MapClaims)
username := claiminfo["user"].(string)
if err := model.DB.Where("username=? and password=?", username, model.Convert(username, oldpassword)).First(&user).Error;err != nil{
g.JSON(403, gin.H{
"errcode": 403,
"errmsg": "旧密码验证失败!",
})
g.Abort()
return
}
New := model.Convert(claiminfo["user"].(string), password)
err := model.DB.Model(&user).Where("username = ?", claiminfo["user"].(string)).Update("password", New).Error
if err != nil{
g.JSON(500, gin.H{
"errcode": 500,
"errmsg": fmt.Sprintf("密码修改失败,因为:%s", err.Error),
})
g.Abort()
return
}
g.JSON(200, gin.H{
"errcode": 200,
"errmsg": "OK",
})
} else {
g.JSON(403, gin.H{
"errcode": 403,
"errmsg": "密码位数必须大于8!",
})
}
}
func RemoveAcl(g *gin.Context){
domain := g.PostForm("domain")
if domain == ""{
g.JSON(403, gin.H{
"errcode": 403,
"errmsg": "缺少必需参数:domain",
})
g.Abort()
return
}
err := model.DB.Delete(model.Users{}, "domain = ?", domain)
if err != nil{
g.JSON(500, gin.H{
"errcode": 500,
"errmsg": fmt.Sprintf("删除失败:%s", err.Error),
})
g.Abort()
return
}
g.JSON(200, gin.H{
"errcode": 200,
"errmsg": "OK",
})
}
func CreateAcl(g *gin.Context){
domain := g.DefaultPostForm("domain", "")
url := g.DefaultPostForm("url", "")
keywords := g.DefaultPostForm("keywords", "")
description := g.DefaultPostForm("description", "")
favicon := g.DefaultPostForm("favicon", "")
method := g.DefaultPostForm("method", "hide")
comment := g.DefaultPostForm("comment", "")
count := g.DefaultPostForm("count", "")
countid := g.DefaultPostForm("countid", "")
title := g.DefaultPostForm("title", "")
claims := g.MustGet("claims")
if domain == "" || url == ""{
g.JSON(403, gin.H{
"errcode": 403,
"errmsg": "域名或目标URL为空!",
})
g.Abort()
return
}
Acl := model.Acls{
Domain:domain,
Url:url,
Keywords:keywords,
Description:description,
Favicon:favicon,
Method:method,
Comment:comment,
Count:count,
Countid:countid,
Title:title,
Username:claims.(jwt.MapClaims)["user"].(string),
}
if err := model.DB.Create(Acl).Error;err != nil{
g.JSON(500, gin.H{
"errcode": 500,
"errmsg": "内部错误, 数据添加失败!",
})
g.Abort()
return
}
g.JSON(200, gin.H{
"errcode": 200,
"errmsg": "OK",
})
}
func main(){
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.POST("/login", Login)
api := r.Group("/api", Validate())
api.POST("/ChangePassword", ChangePassword)
api.POST("/CreateAcl", CreateAcl)
api.POST("/RemoveAcl", CreateAcl)
r.GET("", UrlForward)
r.Run(conf.HTTP_BIND)
}