提交 1.3.0-beta4

This commit is contained in:
dushixiang
2022-10-29 17:36:24 +08:00
parent f87d44d38b
commit b6150c77f8
15 changed files with 104 additions and 135 deletions

View File

@ -5,12 +5,11 @@ import (
"context"
"encoding/csv"
"errors"
"next-terminal/server/common"
"next-terminal/server/common/maps"
"next-terminal/server/common/nt"
"strconv"
"strings"
"next-terminal/server/common/maps"
"next-terminal/server/common/nt"
"next-terminal/server/model"
"next-terminal/server/repository"
"next-terminal/server/service"
@ -22,7 +21,7 @@ import (
type AssetApi struct{}
func (assetApi AssetApi) AssetCreateEndpoint(c echo.Context) error {
m := echo.Map{}
m := maps.Map{}
if err := c.Bind(&m); err != nil {
return err
}
@ -71,29 +70,31 @@ func (assetApi AssetApi) AssetImportEndpoint(c echo.Context) error {
record := records[i]
if len(record) >= 9 {
port, _ := strconv.Atoi(record[3])
asset := model.Asset{
ID: utils.UUID(),
Name: record[0],
Protocol: record[1],
IP: record[2],
Port: port,
AccountType: nt.Custom,
Username: record[4],
Password: record[5],
PrivateKey: record[6],
Passphrase: record[7],
Description: record[8],
Created: common.NowJsonTime(),
Owner: account.ID,
Active: true,
asset := maps.Map{
"id": utils.UUID(),
"name": record[0],
"protocol": record[1],
"ip": record[2],
"port": port,
"accountType": nt.Custom,
"username": record[4],
"password": record[5],
"privateKey": record[6],
"passphrase": record[7],
"Description": record[8],
"owner": account.ID,
}
if record[6] != "" {
asset["accountType"] = nt.PrivateKey
}
if len(record) >= 10 {
tags := strings.ReplaceAll(record[9], "|", ",")
asset.Tags = tags
asset["tags"] = tags
}
err := repository.AssetRepository.Create(context.TODO(), &asset)
_, err := service.AssetService.Create(context.Background(), asset)
if err != nil {
errorCount++
m[strconv.Itoa(i)] = err.Error()
@ -151,7 +152,7 @@ func (assetApi AssetApi) AssetAllEndpoint(c echo.Context) error {
func (assetApi AssetApi) AssetUpdateEndpoint(c echo.Context) error {
id := c.Param("id")
m := echo.Map{}
m := maps.Map{}
if err := c.Bind(&m); err != nil {
return err
}

View File

@ -50,7 +50,7 @@ func (api WebTerminalApi) SshEndpoint(c echo.Context) error {
s, err := service.SessionService.FindByIdAndDecrypt(ctx, sessionId)
if err != nil {
return WriteMessage(ws, dto.NewMessage(Closed, "获取会话失败"))
return WriteMessage(ws, dto.NewMessage(Closed, "获取会话或解密数据失败"))
}
if err := api.permissionCheck(c, s.AssetId); err != nil {

View File

@ -9,5 +9,5 @@ var Banner = ` ___ ___
\/|::/ / /:/\/__/
|:/ / \/__/
\/__/ `
var Version = `v1.3.0-beta2`
var Version = `v1.3.0-beta4`
var Hi = Banner + Version

View File

@ -3,15 +3,14 @@ package repository
import (
"context"
"fmt"
"next-terminal/server/common/nt"
"strconv"
"strings"
"next-terminal/server/common/maps"
"next-terminal/server/common/nt"
"next-terminal/server/config"
"next-terminal/server/model"
"next-terminal/server/utils"
"github.com/labstack/echo/v4"
)
var AssetRepository = new(assetRepository)
@ -172,7 +171,7 @@ func (r assetRepository) FindTags(c context.Context) (o []string, err error) {
return utils.Distinct(o), nil
}
func (r assetRepository) UpdateAttributes(c context.Context, assetId, protocol string, m echo.Map) error {
func (r assetRepository) UpdateAttributes(c context.Context, assetId, protocol string, m maps.Map) error {
var data []model.AssetAttribute
var parameterNames []string
switch protocol {
@ -202,7 +201,7 @@ func (r assetRepository) UpdateAttributes(c context.Context, assetId, protocol s
return r.GetDB(c).CreateInBatches(&data, len(data)).Error
}
func genAttribute(assetId, name string, m echo.Map) model.AssetAttribute {
func genAttribute(assetId, name string, m maps.Map) model.AssetAttribute {
value := fmt.Sprintf("%v", m[name])
attribute := model.AssetAttribute{
Id: utils.Sign([]string{assetId, name}),

View File

@ -7,6 +7,7 @@ import (
"fmt"
"golang.org/x/net/proxy"
"net"
"next-terminal/server/common/maps"
"next-terminal/server/common/nt"
"strconv"
"time"
@ -18,7 +19,6 @@ import (
"next-terminal/server/repository"
"next-terminal/server/utils"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
@ -173,22 +173,22 @@ func (s assetService) CheckStatus(asset *model.Asset, ip string, port int) (bool
}
}
func (s assetService) Create(ctx context.Context, m echo.Map) (model.Asset, error) {
func (s assetService) Create(ctx context.Context, m maps.Map) (*model.Asset, error) {
data, err := json.Marshal(m)
if err != nil {
return model.Asset{}, err
return nil, err
}
var item model.Asset
if err := json.Unmarshal(data, &item); err != nil {
return model.Asset{}, err
return nil, err
}
item.ID = utils.UUID()
item.Created = common.NowJsonTime()
item.Active = true
return item, s.Transaction(ctx, func(ctx context.Context) error {
return &item, s.Transaction(ctx, func(ctx context.Context) error {
if err := s.Encrypt(&item, config.GlobalCfg.EncryptionPassword); err != nil {
return err
}
@ -222,7 +222,7 @@ func (s assetService) DeleteById(id string) error {
})
}
func (s assetService) UpdateById(id string, m echo.Map) error {
func (s assetService) UpdateById(id string, m maps.Map) error {
data, err := json.Marshal(m)
if err != nil {
return err

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"next-terminal/server/common/maps"
"strings"
"next-terminal/server/common"
@ -15,7 +16,6 @@ import (
"next-terminal/server/repository"
"next-terminal/server/utils"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
@ -265,7 +265,7 @@ func (service backupService) Import(backup *dto.Backup) error {
if err != nil {
return err
}
m := echo.Map{}
m := maps.Map{}
if err := json.Unmarshal(data, &m); err != nil {
return err
}

View File

@ -1,54 +0,0 @@
package utils
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/pem"
"github.com/denisbrodbeck/machineid"
)
// SignatureRSA rsa私钥签名
func SignatureRSA(plainText []byte, rsaPrivateKey string) (signed []byte, err error) {
// 使用pem对读取的内容解码得到block
block, _ := pem.Decode([]byte(rsaPrivateKey))
//x509将数据解析得到私钥结构体
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
// 创建一个hash对象
h := sha512.New()
_, _ = h.Write(plainText)
// 计算hash值
hashText := h.Sum(nil)
// 使用rsa函数对散列值签名
signed, err = rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA512, hashText)
if err != nil {
return
}
return signed, nil
}
// VerifyRSA rsa签名认证
func VerifyRSA(plainText, signText []byte, rsaPublicKey string) bool {
// pem解码得到block
block, _ := pem.Decode([]byte(rsaPublicKey))
// x509解析得到接口
publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return false
}
// 对原始明文进行hash运算得到散列值
hashText := sha512.Sum512(plainText)
// 签名认证
err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA512, hashText[:], signText)
return err == nil
}
func GetMachineId() (string, error) {
return machineid.ID()
}