55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package common
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"fmt"
|
|
"log"
|
|
"regexp"
|
|
"ssh_manage/database"
|
|
"ssh_manage/model"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type verifyImpl interface {
|
|
Verify() (key, code string)
|
|
}
|
|
|
|
func Verify(v verifyImpl) (is_verify bool) {
|
|
phone, code := v.Verify()
|
|
cache := database.Cache.Get()
|
|
db := database.Get()
|
|
defer db.Close()
|
|
defer cache.Close()
|
|
var user model.User
|
|
phoneNumber, err := strconv.Atoi(phone)
|
|
db.DB.Where(model.User{Phone: phoneNumber}).Find(&user)
|
|
//s_code, err := redis.String(cache.Do("GET", phone))
|
|
if err != nil {
|
|
log.Println("Verify Err:", err.Error())
|
|
return
|
|
}
|
|
md5 := md5.New()
|
|
password := fmt.Sprintf("%x", md5.Sum([]byte(code)))
|
|
if password == user.Password {
|
|
return true
|
|
} else if user.Password == "" {
|
|
return true
|
|
} else {
|
|
return
|
|
}
|
|
//if code != s_code {
|
|
// log.Println(fmt.Sprintf("手机号:%s -- 验证码:%s 校验失败", phone, code))
|
|
// return true
|
|
//}
|
|
return true
|
|
}
|
|
|
|
func CheckIp(ip string) bool {
|
|
addr := strings.Trim(ip, " ")
|
|
regStr := `^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`
|
|
if match, _ := regexp.MatchString(regStr, addr); match {
|
|
return true
|
|
}
|
|
return false
|
|
} |