证书调整直接从数据库读取

This commit is contained in:
samwaf
2022-10-08 14:05:41 +08:00
parent 4fda55103c
commit cbbb3b554b
7 changed files with 54 additions and 63 deletions
+6 -1
View File
@@ -150,6 +150,8 @@ func StartLocalServer() {
REMOTE_APP: waf_host_add_req.REMOTE_APP,
Remote_host: waf_host_add_req.Remote_host,
Remote_port: waf_host_add_req.Remote_port,
Certfile: waf_host_add_req.Certfile,
Keyfile: waf_host_add_req.Keyfile,
REMARKS: waf_host_add_req.REMARKS,
CREATE_TIME: time.Now(),
UPDATE_TIME: time.Now(),
@@ -242,7 +244,10 @@ func StartLocalServer() {
"Remote_host": waf_host_edit_req.Remote_host,
"Remote_port": waf_host_edit_req.Remote_port,
"REMARKS": waf_host_edit_req.REMARKS,
"UPDATE_TIME": time.Now(),
"Certfile": waf_host_edit_req.Certfile,
"Keyfile": waf_host_edit_req.Keyfile,
"UPDATE_TIME": time.Now(),
}
//var edit_waf_host model.Hosts
//global.GWAF_LOCAL_DB.Debug().Where("CODE=?", waf_host_edit_req.CODE).Find(edit_waf_host)
+23 -6
View File
@@ -79,6 +79,14 @@
<t-radio value="1">加密证书(需上传证书)</t-radio>
</t-radio-group>
</t-form-item>
<t-form-item label="证书串" name="certfile" v-if="formData.ssl=='1'">
<t-textarea :style="{ width: '480px' }" v-model="formData.certfile" placeholder="请输入内容" name="certfile">
</t-textarea>
</t-form-item>
<t-form-item label="密钥串" name="keyfile" v-if="formData.ssl=='1'">
<t-textarea :style="{ width: '480px' }" v-model="formData.keyfile" placeholder="请输入内容" name="keyfile">
</t-textarea>
</t-form-item>
<t-form-item label="后端系统类型" name="remote_system">
<t-select v-model="formData.remote_system" clearable :style="{ width: '480px' }">
<t-option v-for="(item, index) in remote_system_options" :value="item.value" :label="item.label" :key="index">
@@ -100,8 +108,8 @@
<t-input-number :style="{ width: '150px' }" v-model="formData.remote_port" placeholder="请输入网站的端口一般是80/443"></t-input-number>
</t-form-item>
<t-form-item label="备注" name="mark">
<t-textarea :style="{ width: '480px' }" v-model="textareaValue" placeholder="请输入内容" name="description">
<t-form-item label="备注" name="remarks">
<t-textarea :style="{ width: '480px' }" v-model="textareaValue" placeholder="请输入内容" name="remarks">
</t-textarea>
</t-form-item>
<t-form-item style="float: right">
@@ -126,9 +134,17 @@
<t-form-item label="加密证书" name="ssl">
<t-radio-group v-model="formEditData.ssl">
<t-radio value="0">非加密</t-radio>
<t-radio value="1">加密证书(需上传证书)</t-radio>
<t-radio value="1">加密证书(需填写证书)</t-radio>
</t-radio-group>
</t-form-item>
<t-form-item label="证书串" name="certfile" v-if="formEditData.ssl=='1'">
<t-textarea :style="{ width: '480px' }" v-model="formEditData.certfile" placeholder="请输入内容" name="certfile">
</t-textarea>
</t-form-item>
<t-form-item label="密钥串" name="keyfile" v-if="formEditData.ssl=='1'">
<t-textarea :style="{ width: '480px' }" v-model="formEditData.keyfile" placeholder="请输入内容" name="keyfile">
</t-textarea>
</t-form-item>
<t-form-item label="后端系统类型" name="remote_system">
<t-select v-model="formEditData.remote_system" clearable :style="{ width: '480px' }">
<t-option v-for="(item, index) in remote_system_options" :value="item.value" :label="item.label" :key="index">
@@ -150,8 +166,8 @@
<t-input-number :style="{ width: '150px' }" v-model="formEditData.remote_port" placeholder="请输入网站的端口一般是80/443"></t-input-number>
</t-form-item>
<t-form-item label="备注" name="mark">
<t-textarea :style="{ width: '480px' }" v-model="textareaValue" placeholder="请输入内容" name="description">
<t-form-item label="备注" name="remarks">
<t-textarea :style="{ width: '480px' }" v-model="textareaValue" placeholder="请输入内容" name="remarks">
</t-textarea>
</t-form-item>
<t-form-item style="float: right">
@@ -186,7 +202,7 @@ const INITIAL_DATA = {
port: 80,
remote_host: 'baidu2.com',
remote_port: 80,
ssl:0,
ssl:'0',
remote_system:"宝塔",
remote_app:"API业务系统",
guard_status: '',
@@ -506,6 +522,7 @@ export default Vue.extend({
console.log(resdata)
if (resdata.code === 200) {
that.detail_data = resdata.data;
that.detail_data.ssl = that.detail_data.ssl.toString()
that.formEditData = {...that.detail_data}
}
})
+4 -37
View File
@@ -13,16 +13,12 @@ import (
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
"github.com/satori/go.uuid"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"io"
"log"
"net/http"
"net/http/httputil"
_ "net/http/pprof"
"net/url"
"os"
"strconv"
"strings"
"time"
@@ -230,37 +226,12 @@ func Start_WAF() {
global.GWAF_LOCAL_SERVER_PORT = config.GetInt("local_port") //读取本地端口
fmt.Println(" load ini: ", global.GWAF_USER_CODE)
//数据库连接
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer(日志输出的目标,前缀和日志包含的内容——译者注)
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Silent, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误
Colorful: false, // 禁用彩色打印
},
)
//初始化本地数据库
InitDb()
dsn := "samwaf:v&GP5e0BRpkm^RA@tcp(bj-cynosdbmysql-grp-37amo1rw.sql.tencentcdb.com:26762)/samwaf?charset=utf8mb4&parseTime=True"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: newLogger})
if err != nil {
fmt.Printf("error `%s`: %s\n", "db initial error", err)
return
}
var base_config model.Base_config
db.Debug().Where("name = ?", "esurl").Find(&base_config)
//esHelper.Init(base_config.Value) todo 暂时屏蔽es
fmt.Printf("current_version %s \r\n", global.Version_name)
var hosts []model.Hosts
db.Where("user_code = ?", global.GWAF_USER_CODE).Find(&hosts)
global.GWAF_LOCAL_DB.Where("user_code = ?", global.GWAF_USER_CODE).Find(&hosts)
//初始化步骤[加载ip数据库]
var dbPath = "data/ip2region.xdb"
@@ -278,11 +249,7 @@ func Start_WAF() {
for i := 0; i < len(hosts); i++ {
//检测https
if hosts[i].Ssl == 1 {
//查询ssl证书
var sslconfig model.Sslconfig
db.Debug().Where("code = ? and user_code=? ", hosts[i].Code, global.GWAF_USER_CODE).Find(&sslconfig)
// 第一个域名:example.com
cert, err := tls.LoadX509KeyPair(sslconfig.Certfile, sslconfig.Keyfile)
cert, err := tls.X509KeyPair([]byte(hosts[i].Certfile), []byte(hosts[i].Keyfile))
if err != nil {
log.Fatal("Cannot find %s cert & key file. Error is: %s\n", hosts[i].Host, err)
continue
@@ -311,7 +278,7 @@ func Start_WAF() {
//查询规则
var ruleconfig model.Rules
db.Debug().Where("code = ? and user_code=? ", hosts[i].Code, global.GWAF_USER_CODE).Find(&ruleconfig)
global.GWAF_LOCAL_DB.Debug().Where("code = ? and user_code=? ", hosts[i].Code, global.GWAF_USER_CODE).Find(&ruleconfig)
ruleHelper.LoadRule(ruleconfig)
hostsafe := &HostSafe{
@@ -393,7 +360,7 @@ func Start_WAF() {
for code, host := range hostCode {
//hostTarget[host].Rule
var ruleconfig model.Rules
db.Debug().Where("code = ? and user_code=? ", code, global.GWAF_USER_CODE).Find(&ruleconfig)
global.GWAF_LOCAL_DB.Debug().Where("code = ? and user_code=? ", code, global.GWAF_USER_CODE).Find(&ruleconfig)
if ruleconfig.Ruleversion > hostTarget[host].RuleData.Ruleversion {
//说明该code有更新
hostRuleChan <- ruleconfig
+17 -16
View File
@@ -5,20 +5,21 @@ import (
)
type Hosts struct {
Id int `gorm:"primary_key" json:" - "` //
USER_CODE string `json:"user_code"`
Tenant_id string `json:"tenant_id"` //租户ID
Code string `json:"code"` //唯一码
Host string `json:"host"` //域名
Port int `json:"port"` //端口
Ssl int `json:"ssl"` //是否是ssl
GUARD_STATUS int `json:"guard_status"` //防御状态
REMOTE_SYSTEM string `json:"remote_system"` //是宝塔 phpstudy等
REMOTE_APP string `json:"remote_app"` //是什么类型的应用
Remote_host string `json:"remote_host"` //远端域名
Remote_port int `json:"remote_port"` //远端端口
REMARKS string `json:"remarks"` //备注
CREATE_TIME time.Time `json:"create_time"` //创建时间
UPDATE_TIME time.Time `json:"update_time"` //更新时间
Id int `gorm:"primary_key" json:" - "` //
USER_CODE string `json:"user_code"`
Tenant_id string `json:"tenant_id"` //租户ID
Code string `json:"code"` //唯一码
Host string `json:"host"` //域名
Port int `json:"port"` //端口
Ssl int `json:"ssl"` //是否是ssl
GUARD_STATUS int `json:"guard_status"` //防御状态
REMOTE_SYSTEM string `json:"remote_system"` //是宝塔 phpstudy等
REMOTE_APP string `json:"remote_app"` //是什么类型的应用
Remote_host string `json:"remote_host"` //远端域名
Remote_port int `json:"remote_port"` //远端端口
Certfile string `json:"certfile"` // 证书文件
Keyfile string `json:"keyfile"` // 密钥文件
REMARKS string `json:"remarks"` //备注
CREATE_TIME time.Time `json:"create_time"` //创建时间
UPDATE_TIME time.Time `json:"update_time"` //更新时间
}
+2
View File
@@ -9,4 +9,6 @@ type WafHostAddReq struct {
Remote_host string `json:"remote_host"` //远端域名
Remote_port int `json:"remote_port"` //远端端口
REMARKS string `json:"remarks"` //备注
Certfile string `json:"certfile"` // 证书文件
Keyfile string `json:"keyfile"` // 密钥文件
}
@@ -10,4 +10,6 @@ type WafHostEditReq struct {
Remote_host string `json:"remote_host"` //远端域名
Remote_port int `json:"remote_port"` //远端端口
REMARKS string `json:"remarks"` //备注
Certfile string `json:"certfile"` // 证书文件
Keyfile string `json:"keyfile"` // 密钥文件
}
-3
View File
@@ -1,4 +1 @@
package main
type WafEngine struct {
}