Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a939f7082 | ||
|
|
78bf3a7827 | ||
|
|
58139791f3 | ||
|
|
fd62c5cddb | ||
|
|
dadea265b2 |
@@ -1,2 +1,5 @@
|
||||
# core
|
||||
|
||||
go env -w GOPRIVATE=git.apinb.com/*
|
||||
go env -w GONOPROXY=git.apinb.com/*
|
||||
go env -w GOINSECURE=git.apinb.com/*
|
||||
go env -w GONOSUMDB=git.apinb.com/*
|
||||
@@ -1,7 +1,8 @@
|
||||
package errcode
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// header error code ,start:100
|
||||
@@ -88,14 +89,14 @@ var (
|
||||
)
|
||||
|
||||
func NewError(code int, msg string) error {
|
||||
return fiber.NewError(code, msg)
|
||||
return status.New(codes.Code(code), msg).Err()
|
||||
}
|
||||
|
||||
// custom error,status code:500
|
||||
func ErrFatal(msg string) error {
|
||||
return fiber.NewError(500, msg)
|
||||
func ErrFatal(code int, msg string) error {
|
||||
return status.New(codes.Code(code), msg).Err()
|
||||
}
|
||||
|
||||
func ErrNotFound(msg string) error {
|
||||
return fiber.NewError(404, msg+" Not Found")
|
||||
func ErrNotFound(code int, msg string) error {
|
||||
return status.New(codes.Code(code), msg).Err()
|
||||
}
|
||||
|
||||
3
go.sum
3
go.sum
@@ -673,12 +673,15 @@ google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6D
|
||||
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
|
||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY=
|
||||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
|
||||
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a h1:fwgW9j3vHirt4ObdHoYNwuO24BEZjSzbh+zPaNWoiY8=
|
||||
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a/go.mod h1:EMfReVxb80Dq1hhioy0sOsY9jCE46YDgHlJ7fWVUWRE=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97/go.mod h1:iargEX0SFPm3xcfMI0d1domjg0ZF4Aa0p2awqyxhvF0=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b h1:ZlWIi1wSK56/8hn4QcBp/j9M7Gt3U/3hZw3mC7vDICo=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:swOH3j0KzcDDgGUWr+SNpyTen5YrXjS3eyPzFYKc6lc=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
|
||||
|
||||
44
infra/fts.go
Normal file
44
infra/fts.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
)
|
||||
|
||||
var FTS fts
|
||||
|
||||
type fts struct {
|
||||
Endpoint string
|
||||
}
|
||||
|
||||
func (o *fts) SaveTo(buf *bytes.Buffer, haeder map[string]string) (string, error) {
|
||||
// 创建一个 POST 请求
|
||||
req, err := http.NewRequest("POST", o.Endpoint, buf)
|
||||
if err != nil {
|
||||
return "", errcode.NewError(110, "Request to the URL")
|
||||
}
|
||||
|
||||
// 设置请求头为二进制流
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
for key, val := range haeder {
|
||||
req.Header.Set(key, val)
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", errcode.NewError(111, "Client Do")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 读取响应体
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", errcode.NewError(112, "Read response body")
|
||||
}
|
||||
return string(respBody), nil
|
||||
}
|
||||
7
infra/handler.go
Normal file
7
infra/handler.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package infra
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
func Health(ctx *fiber.Ctx) error {
|
||||
return ctx.SendStatus(200)
|
||||
}
|
||||
16
infra/new.go
Normal file
16
infra/new.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
DB *gorm.DB
|
||||
RedisCache *redis.RedisClient
|
||||
)
|
||||
|
||||
func New(db *gorm.DB, redisCache *redis.RedisClient) {
|
||||
DB = db
|
||||
RedisCache = redisCache
|
||||
}
|
||||
54
infra/service.go
Normal file
54
infra/service.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/print"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
Service service
|
||||
RootPrefix string = "/services/"
|
||||
)
|
||||
|
||||
type service struct{}
|
||||
|
||||
func (s *service) Register(cli *clientv3.Client, serviceName string, port int) error {
|
||||
lease := clientv3.NewLease(cli)
|
||||
grantResp, err := lease.Grant(context.TODO(), 5)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serviceAddr := utils.GetLocationIP() + ":" + utils.Int2String(port)
|
||||
|
||||
key := RootPrefix + serviceName + "/" + utils.Int642String(time.Now().UnixNano())
|
||||
_, err = cli.KV.Put(context.TODO(), key, serviceAddr, clientv3.WithLease(grantResp.ID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
keepAliveChan, err := lease.KeepAlive(context.TODO(), grantResp.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
print.Info("[Traingo Register] Service Key: %s", key)
|
||||
print.Info("[Traingo Register] Service Val: %s", serviceAddr)
|
||||
|
||||
print.Success("[Traingo Register] Service Register Complete.")
|
||||
|
||||
go func() {
|
||||
for keepAliveResp := range keepAliveChan {
|
||||
if keepAliveResp == nil {
|
||||
log.Println("LeaseKeepAlive", "Failed")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
296
licence/licence.go
Normal file
296
licence/licence.go
Normal file
@@ -0,0 +1,296 @@
|
||||
// 校验授权文件
|
||||
|
||||
package licence
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/crypto/aes"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
)
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// CPU 信息
|
||||
type CpuInfo struct {
|
||||
Cpu int32 `json:"cpu"`
|
||||
Cores int32 `json:"cores"`
|
||||
ModelName string `json:"modelName"`
|
||||
VendorId string `json:"vendorId"`
|
||||
PhysicalId string `json:"physicalId"`
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
type MachineInfo struct {
|
||||
MacAddrs []string `json:"macAddrs"`
|
||||
Cpus []*CpuInfo `json:"cpus"`
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 授权信息
|
||||
type Licence struct {
|
||||
CompanyName string `json:"licence_to"` // 授权公司
|
||||
CreateDate int `json:"create"` // 生效日期
|
||||
ExpireDate int `json:"expire"` // 有效期
|
||||
MachineCodes []string `json:"machine_codes"` // 机器码列表
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
var (
|
||||
des_key string
|
||||
des_iv string
|
||||
|
||||
Check_Licence_File bool = true // 是否检查部署授权文件
|
||||
)
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
const (
|
||||
signKey = "8E853B589944FF7A56BEF02AAA51D6F4"
|
||||
LICENCE_KEY = "TRAIN_LICENCE_KEY"
|
||||
)
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
func init() {
|
||||
des_key = base64.StdEncoding.EncodeToString([]byte(signKey))
|
||||
des_iv = base64.StdEncoding.EncodeToString([]byte(signKey[:16]))
|
||||
}
|
||||
|
||||
func WatchCheckLicence(licPath, licName string) {
|
||||
for {
|
||||
if CheckLicence(licPath, licName) == false {
|
||||
log.Println("授权文件失效,请重新部署授权文件:", licPath)
|
||||
os.Exit(99)
|
||||
}
|
||||
time.Sleep(time.Hour * 1)
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
func CheckLicence(licPath, licName string) bool {
|
||||
// 加载授权文件
|
||||
content, err := LoadLicenceFromFile(licPath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// 解密解码授权文件
|
||||
var l Licence
|
||||
if err := DecodeLicence(content, &l); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return l.VerifyLicence(licName)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// Licence 校验
|
||||
func (l *Licence) VerifyLicence(licName string) bool {
|
||||
// 用于开发环境,为授权公司时跳过验证
|
||||
if l.CompanyName == licName {
|
||||
return true
|
||||
}
|
||||
|
||||
today := StrToInt(time.Now().Format("20060102"))
|
||||
// 机器日期不在授权文件有限期之内 (早于生效日期,或超过有效期)
|
||||
if (today < l.CreateDate) || (today > l.ExpireDate) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 机器码不在授权列表中
|
||||
machine_code := GetMachineCode()
|
||||
if (len(machine_code) == 0) || !l.ValidMachineCode(machine_code) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 检查机器码是否存在授权列表中
|
||||
func (l *Licence) ValidMachineCode(code string) bool {
|
||||
result := false
|
||||
for _, c := range l.MachineCodes {
|
||||
if c == code {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 加载授权文件
|
||||
func LoadLicenceFromFile(licPath string) (string, error) {
|
||||
key_path := path.Join(licPath, "licence.key")
|
||||
if utils.PathExists(key_path) {
|
||||
file, err := os.Open(key_path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
content, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(content), nil
|
||||
}
|
||||
return "", errors.New("授权文件不存在")
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 解密授权文件内容,并析构至 Licence
|
||||
func DecodeLicence(content string, licence *Licence) error {
|
||||
if len(content) == 0 {
|
||||
return errors.New("授权文件无效")
|
||||
}
|
||||
|
||||
if txt := DecryptStr(content); len(txt) > 0 {
|
||||
if err := json.Unmarshal([]byte(txt), licence); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 字符串解密
|
||||
func DecryptStr(txt string) string {
|
||||
result := ""
|
||||
|
||||
if len(txt) > 0 {
|
||||
result = aes.Decrypt(des_key, des_iv, txt)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 加密字符串
|
||||
func EncryptStr(txt string) string {
|
||||
result := ""
|
||||
|
||||
if len(txt) > 0 {
|
||||
result = aes.Encrypt(des_key, des_iv, txt)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 生成授权文件
|
||||
func BuildLicence(company_name string, Create_date int, expire_date int, machine_codes []string) (string, error) {
|
||||
// 构造licence
|
||||
licence := &Licence{
|
||||
CompanyName: company_name,
|
||||
CreateDate: Create_date,
|
||||
ExpireDate: expire_date,
|
||||
MachineCodes: machine_codes,
|
||||
}
|
||||
|
||||
content, err := json.Marshal(licence)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 对json加密
|
||||
result := EncryptStr(string(content))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 生成激活码
|
||||
func GetLicenceCode(machinceCode string) string {
|
||||
return hash256(machinceCode + "&" + signKey)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 获取CPU信息
|
||||
func getCpuInfo() []*CpuInfo {
|
||||
cpus, _ := cpu.Info()
|
||||
cinfos := make([]*CpuInfo, len(cpus))
|
||||
for k, v := range cpus {
|
||||
cinfos[k] = &CpuInfo{
|
||||
Cpu: v.CPU,
|
||||
ModelName: v.ModelName,
|
||||
Cores: v.Cores,
|
||||
VendorId: v.VendorID,
|
||||
PhysicalId: v.PhysicalID,
|
||||
}
|
||||
}
|
||||
sort.SliceStable(cinfos, func(i, j int) bool {
|
||||
if cinfos[i].Cores != cinfos[j].Cores {
|
||||
return cinfos[i].Cores > cinfos[j].Cores
|
||||
}
|
||||
return cinfos[i].Cpu < cinfos[j].Cpu
|
||||
})
|
||||
return cinfos
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 获取MAC地址
|
||||
func getMacAddrs() []string {
|
||||
var macs []string
|
||||
netfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return macs
|
||||
}
|
||||
for i := 0; i < len(netfaces); i++ {
|
||||
if (netfaces[i].Flags&net.FlagUp) != 0 && (netfaces[i].Flags&net.FlagLoopback) == 0 {
|
||||
addrs, _ := netfaces[i].Addrs()
|
||||
for _, address := range addrs {
|
||||
ipnet, ok := address.(*net.IPNet)
|
||||
if ok && ipnet.IP.IsGlobalUnicast() {
|
||||
macs = append(macs, netfaces[i].HardwareAddr.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Slice(macs, func(i, j int) bool {
|
||||
return macs[i] > macs[j]
|
||||
})
|
||||
return macs
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 获取机器码
|
||||
func GetMachineCode() string {
|
||||
info := MachineInfo{MacAddrs: getMacAddrs(), Cpus: getCpuInfo()}
|
||||
json, _ := json.Marshal(info)
|
||||
machineCode := hash256(string(json))
|
||||
licenceCode := GetLicenceCode(machineCode)
|
||||
return licenceCode
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
// 加密
|
||||
func hash256(d string) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(d))
|
||||
bs := fmt.Sprintf("%x", h.Sum(nil))
|
||||
return strings.ToUpper(bs)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
func StrToInt(valstr string) int {
|
||||
val, err := strconv.Atoi(valstr)
|
||||
if err != nil {
|
||||
val = 0
|
||||
}
|
||||
return val
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package nats
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-sdk/engine/vars"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
natsgo "github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user