diff --git a/internal/db/models/authority/authority_key_dao.go b/internal/db/models/authority/authority_key_dao.go index d966a6ad..c0c050be 100644 --- a/internal/db/models/authority/authority_key_dao.go +++ b/internal/db/models/authority/authority_key_dao.go @@ -5,6 +5,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/dbs" + timeutil "github.com/iwind/TeaGo/utils/time" "time" ) @@ -81,3 +82,15 @@ func (this *AuthorityKeyDAO) ResetKey(tx *dbs.Tx) error { Delete() return err } + +// IsPlus 判断是否为企业版 +func (this *AuthorityKeyDAO) IsPlus(tx *dbs.Tx) (bool, error) { + key, err := this.ReadKey(tx) + if err != nil { + return false, err + } + if key == nil { + return false, nil + } + return key.DayTo >= timeutil.Format("Y-m-d"), nil +} diff --git a/internal/db/models/db_node_initializer.go b/internal/db/models/db_node_initializer.go index aeb62562..0cdf2a7d 100644 --- a/internal/db/models/db_node_initializer.go +++ b/internal/db/models/db_node_initializer.go @@ -16,16 +16,29 @@ import ( "time" ) -var accessLogDBMapping = map[int64]*dbs.DB{} // dbNodeId => DB -var accessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO +var accessLogDBMapping = map[int64]*dbs.DB{} // dbNodeId => DB var accessLogLocker = &sync.RWMutex{} -var accessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true +// HTTP服务访问 +var httpAccessLogDAOMapping = map[int64]*HTTPAccessLogDAOWrapper{} // dbNodeId => DAO +var httpAccessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true + +// DNS服务访问 +var nsAccessLogDAOMapping = map[int64]*NSAccessLogDAOWrapper{} // dbNodeId => DAO +var nsAccessLogTableMapping = map[string]bool{} // tableName_crc(dsn) => true + +// HTTPAccessLogDAOWrapper HTTP访问日志DAO type HTTPAccessLogDAOWrapper struct { DAO *HTTPAccessLogDAO NodeId int64 } +// NSAccessLogDAOWrapper NS访问日志DAO +type NSAccessLogDAOWrapper struct { + DAO *NSAccessLogDAO + NodeId int64 +} + func init() { initializer := NewDBNodeInitializer() dbs.OnReadyDone(func() { @@ -34,12 +47,26 @@ func init() { } // 获取获取DAO -func randomAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) { +func randomHTTPAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) { accessLogLocker.RLock() - if len(accessLogDAOMapping) == 0 { + if len(httpAccessLogDAOMapping) == 0 { dao = nil } else { - for _, d := range accessLogDAOMapping { + for _, d := range httpAccessLogDAOMapping { + dao = d + break + } + } + accessLogLocker.RUnlock() + return +} + +func randomNSAccessLogDAO() (dao *NSAccessLogDAOWrapper) { + accessLogLocker.RLock() + if len(nsAccessLogDAOMapping) == 0 { + dao = nil + } else { + for _, d := range nsAccessLogDAOMapping { dao = d break } @@ -49,7 +76,7 @@ func randomAccessLogDAO() (dao *HTTPAccessLogDAOWrapper) { } // 检查表格是否存在 -func findAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) { +func findHTTPAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) { if !regexp.MustCompile(`^\d{8}$`).MatchString(day) { err = errors.New("invalid day '" + day + "', should be YYYYMMDD") return @@ -64,7 +91,36 @@ func findAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn))) accessLogLocker.RLock() - _, ok = accessLogTableMapping[cacheKey] + _, ok = httpAccessLogTableMapping[cacheKey] + accessLogLocker.RUnlock() + if ok { + return tableName, true, nil + } + + tableNames, err := db.TableNames() + if err != nil { + return tableName, false, err + } + + return tableName, lists.ContainsString(tableNames, tableName), nil +} + +func findNSAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, err error) { + if !regexp.MustCompile(`^\d{8}$`).MatchString(day) { + err = errors.New("invalid day '" + day + "', should be YYYYMMDD") + return + } + + config, err := db.Config() + if err != nil { + return "", false, err + } + + tableName = "edgeNSAccessLogs_" + day + cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn))) + + accessLogLocker.RLock() + _, ok = nsAccessLogTableMapping[cacheKey] accessLogLocker.RUnlock() if ok { return tableName, true, nil @@ -79,7 +135,7 @@ func findAccessLogTableName(db *dbs.DB, day string) (tableName string, ok bool, } // 根据日期获取表名 -func findAccessLogTable(db *dbs.DB, day string, force bool) (string, error) { +func findHTTPAccessLogTable(db *dbs.DB, day string, force bool) (string, error) { config, err := db.Config() if err != nil { return "", err @@ -90,7 +146,7 @@ func findAccessLogTable(db *dbs.DB, day string, force bool) (string, error) { if !force { accessLogLocker.RLock() - _, ok := accessLogTableMapping[cacheKey] + _, ok := httpAccessLogTableMapping[cacheKey] accessLogLocker.RUnlock() if ok { return tableName, nil @@ -104,7 +160,7 @@ func findAccessLogTable(db *dbs.DB, day string, force bool) (string, error) { if lists.ContainsString(tableNames, tableName) { accessLogLocker.Lock() - accessLogTableMapping[cacheKey] = true + httpAccessLogTableMapping[cacheKey] = true accessLogLocker.Unlock() return tableName, nil } @@ -116,13 +172,56 @@ func findAccessLogTable(db *dbs.DB, day string, force bool) (string, error) { } accessLogLocker.Lock() - accessLogTableMapping[cacheKey] = true + httpAccessLogTableMapping[cacheKey] = true accessLogLocker.Unlock() return tableName, nil } -// 初始化数据库连接 +func findNSAccessLogTable(db *dbs.DB, day string, force bool) (string, error) { + config, err := db.Config() + if err != nil { + return "", err + } + + tableName := "edgeNSAccessLogs_" + day + cacheKey := tableName + "_" + fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte(config.Dsn))) + + if !force { + accessLogLocker.RLock() + _, ok := nsAccessLogTableMapping[cacheKey] + accessLogLocker.RUnlock() + if ok { + return tableName, nil + } + } + + tableNames, err := db.TableNames() + if err != nil { + return tableName, err + } + + if lists.ContainsString(tableNames, tableName) { + accessLogLocker.Lock() + nsAccessLogTableMapping[cacheKey] = true + accessLogLocker.Unlock() + return tableName, nil + } + + // 创建表格 + _, err = db.Exec("CREATE TABLE `" + tableName + "` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordId` int(11) unsigned DEFAULT '0' COMMENT '记录ID',\n `content` json DEFAULT NULL COMMENT '访问数据',\n `requestId` varchar(128) DEFAULT NULL COMMENT '请求ID',\n `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n PRIMARY KEY (`id`),\n KEY `nodeId` (`nodeId`),\n KEY `domainId` (`domainId`),\n KEY `recordId` (`recordId`),\n KEY `requestId` (`requestId`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='域名服务访问日志';") + if err != nil { + return tableName, err + } + + accessLogLocker.Lock() + nsAccessLogTableMapping[cacheKey] = true + accessLogLocker.Unlock() + + return tableName, nil +} + +// DBNodeInitializer 初始化数据库连接 type DBNodeInitializer struct { } @@ -130,7 +229,7 @@ func NewDBNodeInitializer() *DBNodeInitializer { return &DBNodeInitializer{} } -// 启动 +// Start 启动 func (this *DBNodeInitializer) Start() { // 初始运行 err := this.loop() @@ -167,7 +266,8 @@ func (this *DBNodeInitializer) loop() error { if !lists.ContainsInt64(nodeIds, nodeId) { closingDbs = append(closingDbs, db) delete(accessLogDBMapping, nodeId) - delete(accessLogDAOMapping, nodeId) + delete(httpAccessLogDAOMapping, nodeId) + delete(nsAccessLogDAOMapping, nodeId) logs.Println("[DB_NODE]close db node '" + strconv.FormatInt(nodeId, 10) + "'") } } @@ -213,46 +313,94 @@ func (this *DBNodeInitializer) loop() error { } // 检查表是否存在 - tableName, err := findAccessLogTable(db, timeutil.Format("Ymd"), false) - if err != nil { - if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误 - logs.Println("[DB_NODE]create first table in database node failed: " + err.Error()) + // httpAccessLog + { + tableName, err := findHTTPAccessLogTable(db, timeutil.Format("Ymd"), false) + if err != nil { + if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误 + logs.Println("[DB_NODE]create first table in database node failed: " + err.Error()) - // 创建节点日志 - createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix()) - if createLogErr != nil { - logs.Println("[NODE_LOG]" + createLogErr.Error()) + // 创建节点日志 + createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix()) + if createLogErr != nil { + logs.Println("[NODE_LOG]" + createLogErr.Error()) + } + + continue + } else { + err = nil } - - continue - } else { - err = nil } + + daoObject := dbs.DAOObject{ + Instance: db, + DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")", + Table: tableName, + PkName: "id", + Model: new(HTTPAccessLog), + } + err = daoObject.Init() + if err != nil { + logs.Println("[DB_NODE]initialize dao failed: " + err.Error()) + continue + } + + accessLogLocker.Lock() + accessLogDBMapping[nodeId] = db + dao := &HTTPAccessLogDAO{ + DAOObject: daoObject, + } + httpAccessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{ + DAO: dao, + NodeId: nodeId, + } + accessLogLocker.Unlock() } - daoObject := dbs.DAOObject{ - Instance: db, - DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")", - Table: tableName, - PkName: "id", - Model: new(HTTPAccessLog), - } - err = daoObject.Init() - if err != nil { - logs.Println("[DB_NODE]initialize dao failed: " + err.Error()) - continue - } - accessLogLocker.Lock() - accessLogDBMapping[nodeId] = db - dao := &HTTPAccessLogDAO{ - DAOObject: daoObject, + // nsAccessLog + { + tableName, err := findNSAccessLogTable(db, timeutil.Format("Ymd"), false) + if err != nil { + if !strings.Contains(err.Error(), "1050") { // 非表格已存在错误 + logs.Println("[DB_NODE]create first table in database node failed: " + err.Error()) + + // 创建节点日志 + createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix()) + if createLogErr != nil { + logs.Println("[NODE_LOG]" + createLogErr.Error()) + } + + continue + } else { + err = nil + } + } + + daoObject := dbs.DAOObject{ + Instance: db, + DB: node.Name + "(id:" + strconv.Itoa(int(node.Id)) + ")", + Table: tableName, + PkName: "id", + Model: new(NSAccessLog), + } + err = daoObject.Init() + if err != nil { + logs.Println("[DB_NODE]initialize dao failed: " + err.Error()) + continue + } + + accessLogLocker.Lock() + accessLogDBMapping[nodeId] = db + dao := &NSAccessLogDAO{ + DAOObject: daoObject, + } + nsAccessLogDAOMapping[nodeId] = &NSAccessLogDAOWrapper{ + DAO: dao, + NodeId: nodeId, + } + accessLogLocker.Unlock() } - accessLogDAOMapping[nodeId] = &HTTPAccessLogDAOWrapper{ - DAO: dao, - NodeId: nodeId, - } - accessLogLocker.Unlock() } } diff --git a/internal/db/models/http_access_log_dao.go b/internal/db/models/http_access_log_dao.go index d050d34f..48c0a24f 100644 --- a/internal/db/models/http_access_log_dao.go +++ b/internal/db/models/http_access_log_dao.go @@ -41,9 +41,9 @@ func NewHTTPAccessLogDAO() *HTTPAccessLogDAO { }).(*HTTPAccessLogDAO) } -// 创建访问日志 +// CreateHTTPAccessLogs 创建访问日志 func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb.HTTPAccessLog) error { - dao := randomAccessLogDAO() + dao := randomHTTPAccessLogDAO() if dao == nil { dao = &HTTPAccessLogDAOWrapper{ DAO: SharedHTTPAccessLogDAO, @@ -53,7 +53,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogs(tx *dbs.Tx, accessLogs []*pb. return this.CreateHTTPAccessLogsWithDAO(tx, dao, accessLogs) } -// 使用特定的DAO创建访问日志 +// CreateHTTPAccessLogsWithDAO 使用特定的DAO创建访问日志 func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *HTTPAccessLogDAOWrapper, accessLogs []*pb.HTTPAccessLog) error { if daoWrapper == nil { return errors.New("dao should not be nil") @@ -68,7 +68,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper for _, accessLog := range accessLogs { day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0)) - table, err := findAccessLogTable(dao.Instance, day, false) + table, err := findHTTPAccessLogTable(dao.Instance, day, false) if err != nil { return err } @@ -97,7 +97,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper if err != nil { // 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试 if strings.Contains(err.Error(), "1146") { - table, err = findAccessLogTable(dao.Instance, day, true) + table, err = findHTTPAccessLogTable(dao.Instance, day, true) if err != nil { return err } @@ -115,7 +115,7 @@ func (this *HTTPAccessLogDAO) CreateHTTPAccessLogsWithDAO(tx *dbs.Tx, daoWrapper return nil } -// 读取往前的 单页访问日志 +// ListAccessLogs 读取往前的 单页访问日志 func (this *HTTPAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, serverId int64, reverse bool, hasError bool, firewallPolicyId int64, firewallRuleGroupId int64, firewallRuleSetId int64, hasFirewallPolicy bool, userId int64) (result []*HTTPAccessLog, nextLastRequestId string, hasMore bool, err error) { if len(day) != 8 { return @@ -155,7 +155,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s accessLogLocker.RLock() daoList := []*HTTPAccessLogDAOWrapper{} - for _, daoWrapper := range accessLogDAOMapping { + for _, daoWrapper := range httpAccessLogDAOMapping { daoList = append(daoList, daoWrapper) } accessLogLocker.RUnlock() @@ -178,7 +178,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s dao := daoWrapper.DAO - tableName, exists, err := findAccessLogTableName(dao.Instance, day) + tableName, exists, err := findHTTPAccessLogTableName(dao.Instance, day) if !exists { // 表格不存在则跳过 return @@ -278,7 +278,7 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, s } } -// 根据请求ID获取访问日志 +// FindAccessLogWithRequestId 根据请求ID获取访问日志 func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*HTTPAccessLog, error) { if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) { return nil, errors.New("invalid requestId") @@ -286,7 +286,7 @@ func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId s accessLogLocker.RLock() daoList := []*HTTPAccessLogDAOWrapper{} - for _, daoWrapper := range accessLogDAOMapping { + for _, daoWrapper := range httpAccessLogDAOMapping { daoList = append(daoList, daoWrapper) } accessLogLocker.RUnlock() @@ -309,7 +309,7 @@ func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId s dao := daoWrapper.DAO - tableName, exists, err := findAccessLogTableName(dao.Instance, day) + tableName, exists, err := findHTTPAccessLogTableName(dao.Instance, day) if err != nil { logs.Println("[DB_NODE]" + err.Error()) return diff --git a/internal/db/models/http_access_log_model_ext.go b/internal/db/models/http_access_log_model_ext.go index 8fe5c052..ec4963df 100644 --- a/internal/db/models/http_access_log_model_ext.go +++ b/internal/db/models/http_access_log_model_ext.go @@ -5,7 +5,7 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" ) -// 转换成PB对象 +// ToPB 转换成PB对象 func (this *HTTPAccessLog) ToPB() (*pb.HTTPAccessLog, error) { p := &pb.HTTPAccessLog{} err := json.Unmarshal([]byte(this.Content), p) diff --git a/internal/db/models/nameservers/ns_cluster_dao.go b/internal/db/models/nameservers/ns_cluster_dao.go index 74812282..1a87810b 100644 --- a/internal/db/models/nameservers/ns_cluster_dao.go +++ b/internal/db/models/nameservers/ns_cluster_dao.go @@ -100,8 +100,8 @@ func (this *NSClusterDAO) CountAllEnabledClusters(tx *dbs.Tx) (int64, error) { Count() } -// ListEnabledNSClusters 列出单页集群 -func (this *NSClusterDAO) ListEnabledNSClusters(tx *dbs.Tx, offset int64, size int64) (result []*NSCluster, err error) { +// ListEnabledClusters 列出单页集群 +func (this *NSClusterDAO) ListEnabledClusters(tx *dbs.Tx, offset int64, size int64) (result []*NSCluster, err error) { _, err = this.Query(tx). State(NSClusterStateEnabled). Offset(offset). @@ -112,8 +112,8 @@ func (this *NSClusterDAO) ListEnabledNSClusters(tx *dbs.Tx, offset int64, size i return } -// FindAllEnabledNSClusters 列出所有集群 -func (this *NSClusterDAO) FindAllEnabledNSClusters(tx *dbs.Tx) (result []*NSCluster, err error) { +// FindAllEnabledClusters 列出所有集群 +func (this *NSClusterDAO) FindAllEnabledClusters(tx *dbs.Tx) (result []*NSCluster, err error) { _, err = this.Query(tx). State(NSClusterStateEnabled). DescPk(). @@ -121,3 +121,20 @@ func (this *NSClusterDAO) FindAllEnabledNSClusters(tx *dbs.Tx) (result []*NSClus FindAll() return } + +// UpdateClusterAccessLog 设置访问日志 +func (this *NSClusterDAO) UpdateClusterAccessLog(tx *dbs.Tx, clusterId int64, accessLogJSON []byte) error { + return this.Query(tx). + Pk(clusterId). + Set("accessLog", accessLogJSON). + UpdateQuickly() +} + +// FindClusterAccessLog 读取访问日志配置 +func (this *NSClusterDAO) FindClusterAccessLog(tx *dbs.Tx, clusterId int64) ([]byte, error) { + accessLog, err := this.Query(tx). + Pk(clusterId). + Result("accessLog"). + FindStringCol("") + return []byte(accessLog), err +} diff --git a/internal/db/models/nameservers/ns_cluster_model.go b/internal/db/models/nameservers/ns_cluster_model.go index bc7ea4cf..f4a3fac2 100644 --- a/internal/db/models/nameservers/ns_cluster_model.go +++ b/internal/db/models/nameservers/ns_cluster_model.go @@ -7,6 +7,7 @@ type NSCluster struct { Name string `field:"name"` // 集群名 InstallDir string `field:"installDir"` // 安装目录 State uint8 `field:"state"` // 状态 + AccessLog string `field:"accessLog"` // 访问日志配置 } type NSClusterOperator struct { @@ -15,6 +16,7 @@ type NSClusterOperator struct { Name interface{} // 集群名 InstallDir interface{} // 安装目录 State interface{} // 状态 + AccessLog interface{} // 访问日志配置 } func NewNSClusterOperator() *NSClusterOperator { diff --git a/internal/db/models/nameservers/ns_node_dao.go b/internal/db/models/nameservers/ns_node_dao.go index d6f87488..568a0ca9 100644 --- a/internal/db/models/nameservers/ns_node_dao.go +++ b/internal/db/models/nameservers/ns_node_dao.go @@ -6,6 +6,7 @@ import ( "github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/utils" "github.com/TeaOSLab/EdgeCommon/pkg/configutils" + "github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" _ "github.com/go-sql-driver/mysql" "github.com/iwind/TeaGo/Tea" @@ -330,6 +331,54 @@ func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []by return err } +// CountAllLowerVersionNodes 计算所有节点中低于某个版本的节点数量 +func (this *NSNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) { + return this.Query(tx). + State(NSNodeStateEnabled). + Where("status IS NOT NULL"). + Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)"). + Param("version", utils.VersionToLong(version)). + Count() +} + +// ComposeNodeConfig 组合节点配置 +func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.NSNodeConfig, error) { + if nodeId <= 0 { + return nil, nil + } + node, err := this.FindEnabledNSNode(tx, nodeId) + if err != nil { + return nil, err + } + if node == nil { + return nil, nil + } + + cluster, err := SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(node.ClusterId)) + if err != nil { + return nil, err + } + if cluster == nil { + return nil, nil + } + + config := &dnsconfigs.NSNodeConfig{ + Id: int64(node.Id), + ClusterId: int64(node.ClusterId), + } + + if len(cluster.AccessLog) > 0 { + ref := &dnsconfigs.AccessLogRef{} + err = json.Unmarshal([]byte(cluster.AccessLog), ref) + if err != nil { + return nil, err + } + config.AccessLogRef = ref + } + + return config, nil +} + // NotifyUpdate 通知更新 func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error { // TODO 先什么都不做 diff --git a/internal/db/models/ns_access_log_dao.go b/internal/db/models/ns_access_log_dao.go new file mode 100644 index 00000000..5325c145 --- /dev/null +++ b/internal/db/models/ns_access_log_dao.go @@ -0,0 +1,309 @@ +package models + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAPI/internal/configs" + "github.com/TeaOSLab/EdgeAPI/internal/errors" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + _ "github.com/go-sql-driver/mysql" + "github.com/iwind/TeaGo/Tea" + "github.com/iwind/TeaGo/dbs" + "github.com/iwind/TeaGo/lists" + "github.com/iwind/TeaGo/logs" + "github.com/iwind/TeaGo/types" + timeutil "github.com/iwind/TeaGo/utils/time" + "regexp" + "sort" + "strconv" + "strings" + "sync" + "time" +) + +type NSAccessLogDAO dbs.DAO + +func NewNSAccessLogDAO() *NSAccessLogDAO { + return dbs.NewDAO(&NSAccessLogDAO{ + DAOObject: dbs.DAOObject{ + DB: Tea.Env, + Table: "edgeNSAccessLogs", + Model: new(NSAccessLog), + PkName: "id", + }, + }).(*NSAccessLogDAO) +} + +var SharedNSAccessLogDAO *NSAccessLogDAO + +func init() { + dbs.OnReady(func() { + SharedNSAccessLogDAO = NewNSAccessLogDAO() + }) +} + +// CreateNSAccessLogs 创建访问日志 +func (this *NSAccessLogDAO) CreateNSAccessLogs(tx *dbs.Tx, accessLogs []*pb.NSAccessLog) error { + dao := randomNSAccessLogDAO() + if dao == nil { + dao = &NSAccessLogDAOWrapper{ + DAO: SharedNSAccessLogDAO, + NodeId: 0, + } + } + return this.CreateNSAccessLogsWithDAO(tx, dao, accessLogs) +} + +// CreateNSAccessLogsWithDAO 使用特定的DAO创建访问日志 +func (this *NSAccessLogDAO) CreateNSAccessLogsWithDAO(tx *dbs.Tx, daoWrapper *NSAccessLogDAOWrapper, accessLogs []*pb.NSAccessLog) error { + if daoWrapper == nil { + return errors.New("dao should not be nil") + } + if len(accessLogs) == 0 { + return nil + } + + dao := daoWrapper.DAO + + // TODO 改成事务批量提交,以加快速度 + + for _, accessLog := range accessLogs { + day := timeutil.Format("Ymd", time.Unix(accessLog.Timestamp, 0)) + table, err := findNSAccessLogTable(dao.Instance, day, false) + if err != nil { + return err + } + + fields := map[string]interface{}{} + fields["nodeId"] = accessLog.NsNodeId + fields["domainId"] = accessLog.NsDomainId + fields["recordId"] = accessLog.NsRecordId + fields["createdAt"] = accessLog.Timestamp + fields["requestId"] = accessLog.RequestId + strconv.FormatInt(time.Now().UnixNano(), 10) + configs.PaddingId + + content, err := json.Marshal(accessLog) + if err != nil { + return err + } + fields["content"] = content + + _, err = dao.Query(tx). + Table(table). + Sets(fields). + Insert() + if err != nil { + // 是否为 Error 1146: Table 'xxx.xxx' doesn't exist 如果是,则创建表之后重试 + if strings.Contains(err.Error(), "1146") { + table, err = findNSAccessLogTable(dao.Instance, day, true) + if err != nil { + return err + } + _, err = dao.Query(tx). + Table(table). + Sets(fields). + Insert() + if err != nil { + return err + } + } + } + } + + return nil +} + +// ListAccessLogs 读取往前的 单页访问日志 +func (this *NSAccessLogDAO) ListAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, nodeId int64, domainId int64, recordId int64, reverse bool) (result []*NSAccessLog, nextLastRequestId string, hasMore bool, err error) { + if len(day) != 8 { + return + } + + // 限制能查询的最大条数,防止占用内存过多 + if size > 1000 { + size = 1000 + } + + result, nextLastRequestId, err = this.listAccessLogs(tx, lastRequestId, size, day, nodeId, domainId, recordId, reverse) + if err != nil || int64(len(result)) < size { + return + } + + moreResult, _, _ := this.listAccessLogs(tx, nextLastRequestId, 1, day, nodeId, domainId, recordId, reverse) + hasMore = len(moreResult) > 0 + return +} + +// 读取往前的单页访问日志 +func (this *NSAccessLogDAO) listAccessLogs(tx *dbs.Tx, lastRequestId string, size int64, day string, nodeId int64, domainId int64, recordId int64, reverse bool) (result []*NSAccessLog, nextLastRequestId string, err error) { + if size <= 0 { + return nil, lastRequestId, nil + } + + accessLogLocker.RLock() + daoList := []*NSAccessLogDAOWrapper{} + for _, daoWrapper := range nsAccessLogDAOMapping { + daoList = append(daoList, daoWrapper) + } + accessLogLocker.RUnlock() + + if len(daoList) == 0 { + daoList = []*NSAccessLogDAOWrapper{{ + DAO: SharedNSAccessLogDAO, + NodeId: 0, + }} + } + + locker := sync.Mutex{} + + count := len(daoList) + wg := &sync.WaitGroup{} + wg.Add(count) + for _, daoWrapper := range daoList { + go func(daoWrapper *NSAccessLogDAOWrapper) { + defer wg.Done() + + dao := daoWrapper.DAO + + tableName, exists, err := findNSAccessLogTableName(dao.Instance, day) + if !exists { + // 表格不存在则跳过 + return + } + if err != nil { + logs.Println("[DB_NODE]" + err.Error()) + return + } + + query := dao.Query(tx) + + // 条件 + if nodeId > 0 { + query.Attr("nodeId", nodeId) + } + if domainId > 0 { + query.Attr("domainId", domainId) + } + if recordId > 0 { + query.Attr("recordId", recordId) + } + + // offset + if len(lastRequestId) > 0 { + if !reverse { + query.Where("requestId<:requestId"). + Param("requestId", lastRequestId) + } else { + query.Where("requestId>:requestId"). + Param("requestId", lastRequestId) + } + } + + if !reverse { + query.Desc("requestId") + } else { + query.Asc("requestId") + } + + // 开始查询 + ones, err := query. + Table(tableName). + Limit(size). + FindAll() + if err != nil { + logs.Println("[DB_NODE]" + err.Error()) + return + } + locker.Lock() + for _, one := range ones { + accessLog := one.(*NSAccessLog) + result = append(result, accessLog) + } + locker.Unlock() + }(daoWrapper) + } + wg.Wait() + + if len(result) == 0 { + return nil, lastRequestId, nil + } + + // 按照requestId排序 + sort.Slice(result, func(i, j int) bool { + if !reverse { + return result[i].RequestId > result[j].RequestId + } else { + return result[i].RequestId < result[j].RequestId + } + }) + + if int64(len(result)) > size { + result = result[:size] + } + + requestId := result[len(result)-1].RequestId + if reverse { + lists.Reverse(result) + } + + if !reverse { + return result, requestId, nil + } else { + return result, requestId, nil + } +} + +// FindAccessLogWithRequestId 根据请求ID获取访问日志 +func (this *NSAccessLogDAO) FindAccessLogWithRequestId(tx *dbs.Tx, requestId string) (*NSAccessLog, error) { + if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) { + return nil, errors.New("invalid requestId") + } + + accessLogLocker.RLock() + daoList := []*NSAccessLogDAOWrapper{} + for _, daoWrapper := range nsAccessLogDAOMapping { + daoList = append(daoList, daoWrapper) + } + accessLogLocker.RUnlock() + + if len(daoList) == 0 { + daoList = []*NSAccessLogDAOWrapper{{ + DAO: SharedNSAccessLogDAO, + NodeId: 0, + }} + } + + count := len(daoList) + wg := &sync.WaitGroup{} + wg.Add(count) + var result *NSAccessLog = nil + day := timeutil.FormatTime("Ymd", types.Int64(requestId[:10])) + for _, daoWrapper := range daoList { + go func(daoWrapper *NSAccessLogDAOWrapper) { + defer wg.Done() + + dao := daoWrapper.DAO + + tableName, exists, err := findNSAccessLogTableName(dao.Instance, day) + if err != nil { + logs.Println("[DB_NODE]" + err.Error()) + return + } + if !exists { + return + } + + one, err := dao.Query(tx). + Table(tableName). + Attr("requestId", requestId). + Find() + if err != nil { + logs.Println("[DB_NODE]" + err.Error()) + return + } + if one != nil { + result = one.(*NSAccessLog) + } + }(daoWrapper) + } + wg.Wait() + return result, nil +} diff --git a/internal/db/models/ns_access_log_dao_test.go b/internal/db/models/ns_access_log_dao_test.go new file mode 100644 index 00000000..224e9db7 --- /dev/null +++ b/internal/db/models/ns_access_log_dao_test.go @@ -0,0 +1,6 @@ +package models + +import ( + _ "github.com/go-sql-driver/mysql" + _ "github.com/iwind/TeaGo/bootstrap" +) diff --git a/internal/db/models/ns_access_log_model.go b/internal/db/models/ns_access_log_model.go new file mode 100644 index 00000000..4d46173e --- /dev/null +++ b/internal/db/models/ns_access_log_model.go @@ -0,0 +1,26 @@ +package models + +// NSAccessLog 域名服务访问日志 +type NSAccessLog struct { + Id uint64 `field:"id"` // ID + NodeId uint32 `field:"nodeId"` // 节点ID + DomainId uint32 `field:"domainId"` // 域名ID + RecordId uint32 `field:"recordId"` // 记录ID + Content string `field:"content"` // 访问数据 + RequestId string `field:"requestId"` // 请求ID + CreatedAt uint64 `field:"createdAt"` // 创建时间 +} + +type NSAccessLogOperator struct { + Id interface{} // ID + NodeId interface{} // 节点ID + DomainId interface{} // 域名ID + RecordId interface{} // 记录ID + Content interface{} // 访问数据 + RequestId interface{} // 请求ID + CreatedAt interface{} // 创建时间 +} + +func NewNSAccessLogOperator() *NSAccessLogOperator { + return &NSAccessLogOperator{} +} diff --git a/internal/db/models/ns_access_log_model_ext.go b/internal/db/models/ns_access_log_model_ext.go new file mode 100644 index 00000000..090d6ad5 --- /dev/null +++ b/internal/db/models/ns_access_log_model_ext.go @@ -0,0 +1,17 @@ +package models + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" +) + +// ToPB 转换成PB对象 +func (this *NSAccessLog) ToPB() (*pb.NSAccessLog, error) { + p := &pb.NSAccessLog{} + err := json.Unmarshal([]byte(this.Content), p) + if err != nil { + return nil, err + } + p.RequestId = this.RequestId + return p, nil +} diff --git a/internal/nodes/api_node_services.go b/internal/nodes/api_node_services.go index 2f65cf41..dfc658be 100644 --- a/internal/nodes/api_node_services.go +++ b/internal/nodes/api_node_services.go @@ -95,4 +95,5 @@ func (this *APINode) registerServices(server *grpc.Server) { pb.RegisterNSDomainServiceServer(server, &nameservers.NSDomainService{}) pb.RegisterNSRecordServiceServer(server, &nameservers.NSRecordService{}) pb.RegisterNSRouteServiceServer(server, &nameservers.NSRouteService{}) + pb.RegisterNSAccessLogServiceServer(server, &nameservers.NSAccessLogService{}) } diff --git a/internal/rpc/services/nameservers/service_ns_access_log.go b/internal/rpc/services/nameservers/service_ns_access_log.go new file mode 100644 index 00000000..4b455554 --- /dev/null +++ b/internal/rpc/services/nameservers/service_ns_access_log.go @@ -0,0 +1,102 @@ +package nameservers + +import ( + "context" + "github.com/TeaOSLab/EdgeAPI/internal/db/models" + "github.com/TeaOSLab/EdgeAPI/internal/rpc/services" + rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" +) + +// NSAccessLogService 访问日志相关服务 +type NSAccessLogService struct { + services.BaseService +} + +// CreateNSAccessLogs 创建访问日志 +func (this *NSAccessLogService) CreateNSAccessLogs(ctx context.Context, req *pb.CreateNSAccessLogsRequest) (*pb.CreateNSAccessLogsResponse, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeDNS) + if err != nil { + return nil, err + } + + if len(req.NsAccessLogs) == 0 { + return &pb.CreateNSAccessLogsResponse{}, nil + } + + tx := this.NullTx() + + err = models.SharedNSAccessLogDAO.CreateNSAccessLogs(tx, req.NsAccessLogs) + if err != nil { + return nil, err + } + + return &pb.CreateNSAccessLogsResponse{}, nil +} + +// ListNSAccessLogs 列出单页访问日志 +func (this *NSAccessLogService) ListNSAccessLogs(ctx context.Context, req *pb.ListNSAccessLogsRequest) (*pb.ListNSAccessLogsResponse, error) { + // 校验请求 + _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) + if err != nil { + return nil, err + } + + tx := this.NullTx() + + // 检查服务ID + if userId > 0 { + // TODO + } + + accessLogs, requestId, hasMore, err := models.SharedNSAccessLogDAO.ListAccessLogs(tx, req.RequestId, req.Size, req.Day, req.NsNodeId, req.NsDomainId, req.NsRecordId, req.Reverse) + if err != nil { + return nil, err + } + + result := []*pb.NSAccessLog{} + for _, accessLog := range accessLogs { + a, err := accessLog.ToPB() + if err != nil { + return nil, err + } + result = append(result, a) + } + + return &pb.ListNSAccessLogsResponse{ + NsAccessLogs: result, + HasMore: hasMore, + RequestId: requestId, + }, nil +} + +// FindNSAccessLog 查找单个日志 +func (this *NSAccessLogService) FindNSAccessLog(ctx context.Context, req *pb.FindNSAccessLogRequest) (*pb.FindNSAccessLogResponse, error) { + // 校验请求 + _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) + if err != nil { + return nil, err + } + + tx := this.NullTx() + + accessLog, err := models.SharedNSAccessLogDAO.FindAccessLogWithRequestId(tx, req.RequestId) + if err != nil { + return nil, err + } + if accessLog == nil { + return &pb.FindNSAccessLogResponse{NsAccessLog: nil}, nil + } + + // 检查权限 + if userId > 0 { + // TODO + } + + a, err := accessLog.ToPB() + if err != nil { + return nil, err + } + return &pb.FindNSAccessLogResponse{NsAccessLog: a}, nil +} diff --git a/internal/rpc/services/nameservers/service_ns_cluster.go b/internal/rpc/services/nameservers/service_ns_cluster.go index 7b9290ef..ee34818a 100644 --- a/internal/rpc/services/nameservers/service_ns_cluster.go +++ b/internal/rpc/services/nameservers/service_ns_cluster.go @@ -42,6 +42,36 @@ func (this *NSClusterService) UpdateNSCluster(ctx context.Context, req *pb.Updat return this.Success() } +// FindNSClusterAccessLog 查找集群访问日志配置 +func (this *NSClusterService) FindNSClusterAccessLog(ctx context.Context, req *pb.FindNSClusterAccessLogRequest) (*pb.FindNSClusterAccessLogResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + accessLogJSON, err := nameservers.SharedNSClusterDAO.FindClusterAccessLog(tx, req.NsClusterId) + if err != nil { + return nil, err + } + return &pb.FindNSClusterAccessLogResponse{AccessLogJSON: accessLogJSON}, nil +} + +// UpdateNSClusterAccessLog 修改集群访问日志配置 +func (this *NSClusterService) UpdateNSClusterAccessLog(ctx context.Context, req *pb.UpdateNSClusterAccessLogRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + err = nameservers.SharedNSClusterDAO.UpdateClusterAccessLog(tx, req.NsClusterId, req.AccessLogJSON) + if err != nil { + return nil, err + } + return this.Success() +} + // DeleteNSCluster 删除集群 func (this *NSClusterService) DeleteNSCluster(ctx context.Context, req *pb.DeleteNSCluster) (*pb.RPCSuccess, error) { _, err := this.ValidateAdmin(ctx, 0) @@ -99,7 +129,7 @@ func (this *NSClusterService) ListEnabledNSClusters(ctx context.Context, req *pb return nil, err } var tx = this.NullTx() - clusters, err := nameservers.SharedNSClusterDAO.ListEnabledNSClusters(tx, req.Offset, req.Size) + clusters, err := nameservers.SharedNSClusterDAO.ListEnabledClusters(tx, req.Offset, req.Size) if err != nil { return nil, err } @@ -122,7 +152,7 @@ func (this *NSClusterService) FindAllEnabledNSClusters(ctx context.Context, req return nil, err } var tx = this.NullTx() - clusters, err := nameservers.SharedNSClusterDAO.FindAllEnabledNSClusters(tx) + clusters, err := nameservers.SharedNSClusterDAO.FindAllEnabledClusters(tx) if err != nil { return nil, err } diff --git a/internal/rpc/services/nameservers/service_ns_node.go b/internal/rpc/services/nameservers/service_ns_node.go index 21776d7d..ea05e57d 100644 --- a/internal/rpc/services/nameservers/service_ns_node.go +++ b/internal/rpc/services/nameservers/service_ns_node.go @@ -4,6 +4,7 @@ package nameservers import ( "context" + "encoding/json" "github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers" "github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/installers" @@ -341,8 +342,8 @@ func (this *NSNodeService) UpdateNSNodeStatus(ctx context.Context, req *pb.Updat return this.Success() } -// FindCurrentNSNode 获取当前节点信息 -func (this *NSNodeService) FindCurrentNSNode(ctx context.Context, req *pb.FindCurrentNSNodeRequest) (*pb.FindCurrentNSNodeResponse, error) { +// FindCurrentNSNodeConfig 获取当前节点信息 +func (this *NSNodeService) FindCurrentNSNodeConfig(ctx context.Context, req *pb.FindCurrentNSNodeConfigRequest) (*pb.FindCurrentNSNodeConfigResponse, error) { // 校验节点 _, nodeId, err := this.ValidateNodeId(ctx, rpcutils.UserTypeDNS) if err != nil { @@ -350,50 +351,17 @@ func (this *NSNodeService) FindCurrentNSNode(ctx context.Context, req *pb.FindCu } var tx = this.NullTx() - node, err := nameservers.SharedNSNodeDAO.FindEnabledNSNode(tx, nodeId) - if err != nil { - return nil, err - } - if node == nil { - return &pb.FindCurrentNSNodeResponse{NsNode: nil}, nil - } - - // 集群信息 - clusterName, err := nameservers.SharedNSClusterDAO.FindEnabledNSClusterName(tx, int64(node.ClusterId)) + config, err := nameservers.SharedNSNodeDAO.ComposeNodeConfig(tx, nodeId) if err != nil { return nil, err } - // 安装信息 - installStatus, err := node.DecodeInstallStatus() + if config == nil { + return &pb.FindCurrentNSNodeConfigResponse{NsNodeJSON: nil}, nil + } + configJSON, err := json.Marshal(config) if err != nil { return nil, err } - installStatusResult := &pb.NodeInstallStatus{} - if installStatus != nil { - installStatusResult = &pb.NodeInstallStatus{ - IsRunning: installStatus.IsRunning, - IsFinished: installStatus.IsFinished, - IsOk: installStatus.IsOk, - Error: installStatus.Error, - ErrorCode: installStatus.ErrorCode, - UpdatedAt: installStatus.UpdatedAt, - } - } - - return &pb.FindCurrentNSNodeResponse{NsNode: &pb.NSNode{ - Id: int64(node.Id), - Name: node.Name, - StatusJSON: []byte(node.Status), - UniqueId: node.UniqueId, - Secret: node.Secret, - IsInstalled: node.IsInstalled == 1, - InstallDir: node.InstallDir, - NsCluster: &pb.NSCluster{ - Id: int64(node.ClusterId), - Name: clusterName, - }, - InstallStatus: installStatusResult, - IsOn: node.IsOn == 1, - }}, nil + return &pb.FindCurrentNSNodeConfigResponse{NsNodeJSON: configJSON}, nil } diff --git a/internal/rpc/services/service_admin.go b/internal/rpc/services/service_admin.go index 8c5c3939..83712666 100644 --- a/internal/rpc/services/service_admin.go +++ b/internal/rpc/services/service_admin.go @@ -6,6 +6,7 @@ import ( teaconst "github.com/TeaOSLab/EdgeAPI/internal/const" "github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models/authority" + "github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers" "github.com/TeaOSLab/EdgeAPI/internal/db/models/stats" "github.com/TeaOSLab/EdgeAPI/internal/errors" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" @@ -546,6 +547,12 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com }) } + // 是否是企业版 + isPlus, err := authority.SharedAuthorityKeyDAO.IsPlus(tx) + if err != nil { + return nil, err + } + // 边缘节点升级信息 { upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{ @@ -560,7 +567,7 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com } // 监控节点升级信息 - { + if isPlus { upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{ NewVersion: teaconst.MonitorNodeVersion, } @@ -573,7 +580,7 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com } // 认证节点升级信息 - { + if isPlus { upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{ NewVersion: teaconst.AuthorityNodeVersion, } @@ -586,7 +593,7 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com } // 用户节点升级信息 - { + if isPlus { upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{ NewVersion: teaconst.UserNodeVersion, } @@ -611,5 +618,18 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com resp.ApiNodeUpgradeInfo = upgradeInfo } + // DNS节点升级信息 + if isPlus { + upgradeInfo := &pb.ComposeAdminDashboardResponse_UpgradeInfo{ + NewVersion: teaconst.DNSNodeVersion, + } + countNodes, err := nameservers.SharedNSNodeDAO.CountAllLowerVersionNodes(tx, upgradeInfo.NewVersion) + if err != nil { + return nil, err + } + upgradeInfo.CountNodes = countNodes + resp.NsNodeUpgradeInfo = upgradeInfo + } + return resp, nil } diff --git a/internal/rpc/services/service_http_access_log.go b/internal/rpc/services/service_http_access_log.go index d73aa846..f76b0843 100644 --- a/internal/rpc/services/service_http_access_log.go +++ b/internal/rpc/services/service_http_access_log.go @@ -7,12 +7,12 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" ) -// 访问日志相关服务 +// HTTPAccessLogService 访问日志相关服务 type HTTPAccessLogService struct { BaseService } -// 创建访问日志 +// CreateHTTPAccessLogs 创建访问日志 func (this *HTTPAccessLogService) CreateHTTPAccessLogs(ctx context.Context, req *pb.CreateHTTPAccessLogsRequest) (*pb.CreateHTTPAccessLogsResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode) @@ -20,13 +20,13 @@ func (this *HTTPAccessLogService) CreateHTTPAccessLogs(ctx context.Context, req return nil, err } - if len(req.AccessLogs) == 0 { + if len(req.HttpAccessLogs) == 0 { return &pb.CreateHTTPAccessLogsResponse{}, nil } tx := this.NullTx() - err = models.SharedHTTPAccessLogDAO.CreateHTTPAccessLogs(tx, req.AccessLogs) + err = models.SharedHTTPAccessLogDAO.CreateHTTPAccessLogs(tx, req.HttpAccessLogs) if err != nil { return nil, err } @@ -34,7 +34,7 @@ func (this *HTTPAccessLogService) CreateHTTPAccessLogs(ctx context.Context, req return &pb.CreateHTTPAccessLogsResponse{}, nil } -// 列出单页访问日志 +// ListHTTPAccessLogs 列出单页访问日志 func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *pb.ListHTTPAccessLogsRequest) (*pb.ListHTTPAccessLogsResponse, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -74,13 +74,13 @@ func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *p } return &pb.ListHTTPAccessLogsResponse{ - AccessLogs: result, - HasMore: hasMore, - RequestId: requestId, + HttpAccessLogs: result, + HasMore: hasMore, + RequestId: requestId, }, nil } -// 查找单个日志 +// FindHTTPAccessLog 查找单个日志 func (this *HTTPAccessLogService) FindHTTPAccessLog(ctx context.Context, req *pb.FindHTTPAccessLogRequest) (*pb.FindHTTPAccessLogResponse, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -95,7 +95,7 @@ func (this *HTTPAccessLogService) FindHTTPAccessLog(ctx context.Context, req *pb return nil, err } if accessLog == nil { - return &pb.FindHTTPAccessLogResponse{AccessLog: nil}, nil + return &pb.FindHTTPAccessLogResponse{HttpAccessLog: nil}, nil } // 检查权限 @@ -110,5 +110,5 @@ func (this *HTTPAccessLogService) FindHTTPAccessLog(ctx context.Context, req *pb if err != nil { return nil, err } - return &pb.FindHTTPAccessLogResponse{AccessLog: a}, nil + return &pb.FindHTTPAccessLogResponse{HttpAccessLog: a}, nil }