mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2025-12-06 06:58:54 +08:00
增加了一些测试用例和检查升级的一部分代码
This commit is contained in:
@@ -21,6 +21,7 @@ type APIGroup struct {
|
||||
WafWebSocketApi
|
||||
WafSysInfoApi
|
||||
WafSystemConfigApi
|
||||
WafCommonApi
|
||||
}
|
||||
|
||||
var APIGroupAPP = new(APIGroup)
|
||||
|
||||
85
api/waf_common.go
Normal file
85
api/waf_common.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"SamWaf/model"
|
||||
"SamWaf/model/common/response"
|
||||
"SamWaf/model/request"
|
||||
"fmt"
|
||||
"github.com/360EntSecGroup-Skylar/excelize"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WafCommonApi struct {
|
||||
}
|
||||
|
||||
// 导出excel
|
||||
func (w *WafCommonApi) ExportExcelApi(c *gin.Context) {
|
||||
var req request.WafCommonReq
|
||||
err := c.ShouldBind(&req)
|
||||
if err == nil {
|
||||
// 生成文件名
|
||||
fileName := time.Now().Format("20060102150405") + ".xlsx"
|
||||
|
||||
// 创建 Excel 文件
|
||||
f := excelize.NewFile()
|
||||
sheetName := "Sheet1"
|
||||
// 获取数据的类型和值
|
||||
dataType, dataValue := getStructTypeValueByName(req.TableName)
|
||||
|
||||
/*dataType := reflect.TypeOf(data)
|
||||
dataValue := reflect.ValueOf(data)*/
|
||||
|
||||
// 设置表头
|
||||
for i := 0; i < dataType.NumField(); i++ {
|
||||
field := dataType.Field(i)
|
||||
colName := field.Tag.Get("json") // 获取 excel 标签的值,即表头名称
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%c%d", 'A'+i, 1), colName)
|
||||
}
|
||||
|
||||
// 填充数据
|
||||
for i := 0; i < dataValue.Len(); i++ {
|
||||
rowNum := i + 2
|
||||
rowValue := dataValue.Index(i)
|
||||
for j := 0; j < dataType.NumField(); j++ {
|
||||
colValue := rowValue.Field(j).Interface()
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%c%d", 'A'+j, rowNum), colValue)
|
||||
}
|
||||
}
|
||||
|
||||
// 保存 Excel 文件
|
||||
if err := f.SaveAs(fileName); err != nil {
|
||||
log.Fatal("无法保存 Excel 文件:", err)
|
||||
}
|
||||
|
||||
// 设置响应头
|
||||
c.Header("Content-Type", "application/octet-stream")
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
|
||||
|
||||
// 将文件内容输出给客户端
|
||||
c.File(fileName)
|
||||
// 删除临时文件
|
||||
/*if err := os.Remove(fileName); err != nil {
|
||||
log.Println("无法删除临时文件:", err)
|
||||
}*/
|
||||
} else {
|
||||
response.FailWithMessage("解析失败", c)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取结构体类型通过名称
|
||||
func getStructTypeValueByName(name string) (reflect.Type, reflect.Value) {
|
||||
switch name {
|
||||
case "hosts":
|
||||
// 模拟获取数据
|
||||
webHosts := []model.Hosts{
|
||||
{Host: "www.baidu1.com"},
|
||||
{Host: "www.baidu2.com"},
|
||||
}
|
||||
return reflect.TypeOf(model.Hosts{}), reflect.ValueOf(webHosts)
|
||||
default:
|
||||
return nil, reflect.ValueOf(nil)
|
||||
}
|
||||
}
|
||||
39
api/waf_common_test.go
Normal file
39
api/waf_common_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"SamWaf/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 测试用例 ExportExcelApi
|
||||
func TestExportExcelApi(t *testing.T) {
|
||||
// 创建一个基于 Gin 的引擎
|
||||
r := gin.Default()
|
||||
|
||||
global.GWAF_RELEASE_VERSION = "111"
|
||||
r.GET("/samwaf/export", new(WafCommonApi).ExportExcelApi)
|
||||
// 创建一个模拟的 HTTP 请求 WafCommonReq 对象
|
||||
// 创建一个模拟的 HTTP 请求
|
||||
queryParams := url.Values{}
|
||||
queryParams.Set("table_name", "hosts")
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/samwaf/export?"+queryParams.Encode(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("无法创建请求:%v", err)
|
||||
}
|
||||
|
||||
// 创建一个响应记录器
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
// 将模拟的请求发送到测试的 API 路由
|
||||
r.ServeHTTP(rec, req)
|
||||
|
||||
// 检查响应状态码是否为 200
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("期望的状态码:%d,实际状态码:%d", http.StatusOK, rec.Code)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,11 @@ import (
|
||||
"SamWaf/global"
|
||||
"SamWaf/model"
|
||||
"SamWaf/model/common/response"
|
||||
"SamWaf/utils/zlog"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -18,17 +23,42 @@ func (w *WafSysInfoApi) SysVersionApi(c *gin.Context) {
|
||||
}, "获取成功", c)
|
||||
}
|
||||
|
||||
// TODO 检测版本
|
||||
func (w *WafSysInfoApi) CheckVersionApi(c *gin.Context) {
|
||||
response.OkWithDetailed(model.VersionInfo{
|
||||
Version: global.GWAF_RELEASE_VERSION,
|
||||
VersionName: global.GWAF_RELEASE_VERSION_NAME,
|
||||
VersionRelease: global.GWAF_RELEASE,
|
||||
}, "获取成功", c)
|
||||
resp, err := http.Get(global.GUPDATE_VERSION_URL)
|
||||
if err != nil {
|
||||
zlog.Error("读取版本信息失败", err.Error())
|
||||
response.FailWithMessage("读取版本信息网络失败", c)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
zlog.Error("读取版本信息失败", err.Error())
|
||||
response.FailWithMessage("读取版本信息内容失败", c)
|
||||
return
|
||||
}
|
||||
var updateInfo = model.UpdateVersion{}
|
||||
json.Unmarshal(body, &updateInfo)
|
||||
if updateInfo.VersionCode > global.GetCurrentVersionInt() {
|
||||
response.OkWithDetailed(model.VersionInfo{
|
||||
Version: global.GWAF_RELEASE_VERSION,
|
||||
VersionName: global.GWAF_RELEASE_VERSION_NAME,
|
||||
VersionRelease: global.GWAF_RELEASE,
|
||||
NeedUpdate: true,
|
||||
}, "有新版本", c)
|
||||
} else {
|
||||
response.OkWithDetailed(model.VersionInfo{
|
||||
Version: global.GWAF_RELEASE_VERSION,
|
||||
VersionName: global.GWAF_RELEASE_VERSION_NAME,
|
||||
VersionRelease: global.GWAF_RELEASE,
|
||||
NeedUpdate: false,
|
||||
}, "已经是最新版本", c)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TODO 去升级
|
||||
func (w *WafSysInfoApi) UpdateApi(c *gin.Context) {
|
||||
|
||||
response.OkWithDetailed(model.VersionInfo{
|
||||
Version: global.GWAF_RELEASE_VERSION,
|
||||
VersionName: global.GWAF_RELEASE_VERSION_NAME,
|
||||
|
||||
57
api/waf_sys_info_test.go
Normal file
57
api/waf_sys_info_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"SamWaf/global"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 响应体:{"message":"H ello, World!"},实际响应体:{"code":0,"data":{"need_update":false,"versi on":"555","version_name":"1.0","version_release":"false"},"msg":" 已经是最新版本"}
|
||||
type WafCheckVersionResponse struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
NeedUpdate bool `json:"need_update"`
|
||||
Version string `json:"version"`
|
||||
VersionName string `json:"version_name"`
|
||||
VersionRelease string `json:"version_release"`
|
||||
} `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
// 测试用例 CheckVersionApi
|
||||
func TestCheckVersionApi(t *testing.T) {
|
||||
// 创建一个基于 Gin 的引擎
|
||||
r := gin.Default()
|
||||
|
||||
global.GWAF_RELEASE_VERSION = "111"
|
||||
r.GET("/samwaf/sysinfo/checkversion", new(WafSysInfoApi).CheckVersionApi)
|
||||
// 创建一个模拟的 HTTP 请求
|
||||
req, err := http.NewRequest(http.MethodGet, "/samwaf/sysinfo/checkversion", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("无法创建请求:%v", err)
|
||||
}
|
||||
|
||||
// 创建一个响应记录器
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
// 将模拟的请求发送到测试的 API 路由
|
||||
r.ServeHTTP(rec, req)
|
||||
|
||||
// 检查响应状态码是否为 200
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("期望的状态码:%d,实际状态码:%d", http.StatusOK, rec.Code)
|
||||
}
|
||||
//解析响应体
|
||||
var response WafCheckVersionResponse
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &response)
|
||||
if err != nil {
|
||||
t.Errorf("响应体解析失败:%v", err)
|
||||
}
|
||||
expectedNeedUpdate := false
|
||||
if response.Data.NeedUpdate != expectedNeedUpdate {
|
||||
t.Errorf("期望的响应体:%t,实际响应体:%t", expectedNeedUpdate, response.Data.NeedUpdate)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
Dequelib "github.com/edwingeng/deque"
|
||||
Wssocket "github.com/gorilla/websocket"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -53,3 +54,8 @@ var (
|
||||
//升级相关
|
||||
GUPDATE_VERSION_URL string = "http://update.binaite.net/version.json"
|
||||
)
|
||||
|
||||
func GetCurrentVersionInt() int {
|
||||
version, _ := strconv.Atoi(GWAF_RELEASE_VERSION)
|
||||
return version
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -3,6 +3,7 @@ module SamWaf
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/360EntSecGroup-Skylar/excelize v1.4.1
|
||||
github.com/bytedance/godlp v1.2.15
|
||||
github.com/edwingeng/deque v1.0.3
|
||||
github.com/elastic/go-elasticsearch/v7 v7.17.1
|
||||
@@ -51,6 +52,7 @@ require (
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
|
||||
5
go.sum
5
go.sum
@@ -36,6 +36,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
|
||||
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
|
||||
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
github.com/360EntSecGroup-Skylar/excelize v1.4.1 h1:l55mJb6rkkaUzOpSsgEeKYtS6/0gHwBYyfo5Jcjv/Ks=
|
||||
github.com/360EntSecGroup-Skylar/excelize v1.4.1/go.mod h1:vnax29X2usfl7HHkBrX5EvSCJcmH3dT9luvxzu8iGAE=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
@@ -220,6 +222,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
|
||||
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||
@@ -261,6 +265,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
|
||||
5
model/request/waf_common_req.go
Normal file
5
model/request/waf_common_req.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package request
|
||||
|
||||
type WafCommonReq struct {
|
||||
TableName string `json:"table_name" form:"table_name"` //表名字
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package model
|
||||
|
||||
type VersionInfo struct {
|
||||
NeedUpdate bool `json:"need_update"`
|
||||
Version string `json:"version"`
|
||||
VersionName string `json:"version_name"`
|
||||
VersionRelease string `json:"version_release"`
|
||||
|
||||
10
model/update_version.go
Normal file
10
model/update_version.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package model
|
||||
|
||||
// 生成结构体{"versioncode":100,"versiondesp":"最新内容","isforce":0,"versionurl":"http://update.binaite.net/bin/","versionsign":"DSDFDFFFFF"}
|
||||
type UpdateVersion struct {
|
||||
VersionCode int `json:"versioncode"`
|
||||
VersionDesp string `json:"versiondesp"`
|
||||
IsForce int `json:"isforce"`
|
||||
VersionUrl string `json:"versionurl"`
|
||||
VersionSign string `json:"versionsign"`
|
||||
}
|
||||
15
router/waf_common.go
Normal file
15
router/waf_common.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"SamWaf/api"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type WafCommonRouter struct {
|
||||
}
|
||||
|
||||
func (receiver *WafCommonRouter) InitWafCommonRouter(group *gin.RouterGroup) {
|
||||
api := api.APIGroupAPP.WafCommonApi
|
||||
router := group.Group("")
|
||||
router.GET("/samwaf/export", api.ExportExcelApi)
|
||||
}
|
||||
Reference in New Issue
Block a user