kefu/controller/v2/ip_auths.go

118 lines
2.9 KiB
Go
Raw Permalink Normal View History

2024-12-10 02:50:12 +00:00
package v2
import (
"fmt"
"github.com/gin-gonic/gin"
"kefu/common"
"kefu/models"
"kefu/tools"
"log"
"net/url"
"time"
)
func GetIpAuth(c *gin.Context) {
ip := c.ClientIP()
host := c.Request.Referer()
log.Println("ipAuth:", ip, host)
ipModel := models.FindServerIpAddress(ip)
if ipModel.ID == 0 {
ipModel = models.CreateIpAuth(ip, "", host, "", "2006-01-02")
}
c.JSON(200, gin.H{
"code": 400,
"msg": "error",
})
}
func GetIsBindOfficial(c *gin.Context) {
ip := c.ClientIP()
ipModel := models.FindServerIpAddress(ip)
if ipModel.ID == 0 || ipModel.Status != 1 || ipModel.Phone == "" {
c.Writer.Write([]byte("error"))
return
}
user := &models.User{
Tel: ipModel.Phone,
}
var result models.User
result = user.GetOneUser("*")
//查看过期时间
nowSecond := time.Now().Unix()
expireSecond := result.ExpiredAt.Unix()
if result.ID == 0 || expireSecond < nowSecond {
c.Writer.Write([]byte("error"))
logContent := fmt.Sprintf("'%s' 远程验证失败或过期:%s", result.Name, result.ExpiredAt.Format("2006-01-02 15:04:05"))
go models.CreateFlyLog(result.EntId, ip, logContent, "user")
return
}
c.Writer.Write([]byte("success"))
}
//远程请求
func PostBindOfficial(c *gin.Context) {
api := "https://gofly.v1kf.com/2/officialBindIp"
phone := c.PostForm("phone")
password := c.PostForm("password")
host := c.Request.Host
data := url.Values{}
data.Set("phone", phone)
data.Set("password", password)
data.Set("host", host)
res, err := tools.PostForm(api, data)
if err != nil {
log.Println("绑定官网账户发送认证连接错误")
}
log.Println("bindOfficial:", c.ClientIP(), host)
c.Writer.Write([]byte(res))
}
//官网绑定
func PostOfficialBindIp(c *gin.Context) {
ip := c.ClientIP()
phone := c.PostForm("phone")
password := c.PostForm("password")
host := c.PostForm("host")
user := &models.User{
Tel: phone,
}
var result models.User
result = user.GetOneUser("*")
md5Pass := tools.Md5(password)
//查看过期时间
nowSecond := time.Now().Unix()
expireSecond := result.ExpiredAt.Unix()
if result.ID == 0 || result.Password != md5Pass || expireSecond < nowSecond {
c.Writer.Write([]byte("error"))
return
}
ipModel := models.FindServerIpAddress(ip)
code := makeAuthCode(host, phone)
if ipModel.ID == 0 {
models.CreateIpAuth(ip, phone, host, code, "2006-01-02")
} else if ipModel.Status != 1 {
c.Writer.Write([]byte("error"))
return
} else {
models.UpdateIpAddress(ip, phone, code, host)
}
c.Writer.Write([]byte("success"))
return
}
func makeAuthCode(host, phone string) string {
publicKey := common.RsaPublicKey
privateKey := common.RsaPrivateKey
rsa := tools.NewRsa(publicKey, privateKey)
content := fmt.Sprintf("{\"host\":\"%s\",\"phone\":\"%s\"}", host, phone)
res, err := rsa.Encrypt([]byte(content))
res = []byte(tools.Base64Encode(string(res)))
if err != nil {
return ""
}
return string(res)
}