/
user.go
107 lines (91 loc) · 2.33 KB
/
user.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
package model
import (
"crypto/md5"
"encoding/hex"
"errors"
"net"
"net/http"
"strings"
"github.com/praserx/ipconv"
"gorm.io/gorm"
"github.com/cylonchau/firewalld-gateway/config"
"github.com/cylonchau/firewalld-gateway/server/apis"
)
const Secret = "com.github.cylonchau"
type User struct {
gorm.Model
Username string `gorm:"index;type:varchar(20)"`
Password string `gorm:"type:varchar(32)"`
LoginIP int `json:"login_ip" gorm:"type:int"`
}
func QueryUserWithUsername(username string) (User, error) {
user := User{}
result := DB.Select("id", "username", "password").Where("username = ?", username).Find(&user)
if result.Error == nil {
return user, nil
}
return User{}, result.Error
}
func QueryUserWithUID(uid int64) (User, error) {
user := User{}
result := DB.Select("id", "username").Where("id = ?", uid).Find(&user)
if result.Error == nil {
return user, nil
}
return User{}, result.Error
}
func CreateUser(userQuery *apis.UserQuery) (enconterError error) {
if checkUserExist(userQuery.Username) {
user := &User{
Username: userQuery.Username,
Password: EncryptPassword(userQuery.Password),
}
result := DB.Create(user)
if enconterError = result.Error; enconterError == nil {
return nil
}
} else {
enconterError = apis.ErrUserExist
}
return enconterError
}
func LastLogin(uid int64, ip uint32) bool {
result := DB.Model(&User{}).Where("id = ?", uid).Update("login_ip", ip)
if result.Error == nil {
return true
}
return false
}
func EncryptPassword(p string) string {
h := md5.New()
h.Write([]byte(Secret + config.CONFIG.AppName))
h.Sum([]byte(p))
return hex.EncodeToString(h.Sum([]byte(p)))
}
func checkUserExist(username string) bool {
result := DB.Where("username = ?", username).First(&User{})
if result.Error != gorm.ErrRecordNotFound || result.RowsAffected > 0 {
return false
}
return true
}
func GetRequestIP(r *http.Request) (uint32, error) {
ip := r.Header.Get("X-Real-IP")
if net.ParseIP(ip) != nil {
return 0, nil
}
ip = r.Header.Get("X-Forward-For")
for _, i := range strings.Split(ip, ",") {
if net.ParseIP(i) != nil {
return 0, nil
}
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return 0, err
}
if ip, version, err := ipconv.ParseIP(ip); err != nil && version == 4 {
return ipconv.IPv4ToInt(ip)
}
return 0, errors.New("no valid ip found")
}