diff --git a/internal/db/models/http_access_log_dao.go b/internal/db/models/http_access_log_dao.go index e076cc4f..be05616e 100644 --- a/internal/db/models/http_access_log_dao.go +++ b/internal/db/models/http_access_log_dao.go @@ -10,7 +10,9 @@ import ( "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" @@ -258,3 +260,60 @@ func (this *HTTPAccessLogDAO) listAccessLogs(lastRequestId string, size int64, d return result, requestId, nil } } + +// 根据请求ID获取访问日志 +func (this *HTTPAccessLogDAO) FindAccessLogWithRequestId(requestId string) (*HTTPAccessLog, error) { + if !regexp.MustCompile(`^\d{30,}`).MatchString(requestId) { + return nil, errors.New("invalid requestId") + } + + accessLogLocker.RLock() + daoList := []*HTTPAccessLogDAOWrapper{} + for _, daoWrapper := range accessLogDAOMapping { + daoList = append(daoList, daoWrapper) + } + accessLogLocker.RUnlock() + + if len(daoList) == 0 { + daoList = []*HTTPAccessLogDAOWrapper{{ + DAO: SharedHTTPAccessLogDAO, + NodeId: 0, + }} + } + + count := len(daoList) + wg := &sync.WaitGroup{} + wg.Add(count) + var result *HTTPAccessLog = nil + day := timeutil.FormatTime("Ymd", types.Int64(requestId[:10])) + for _, daoWrapper := range daoList { + go func(daoWrapper *HTTPAccessLogDAOWrapper) { + defer wg.Done() + + dao := daoWrapper.DAO + + tableName, exists, err := findAccessLogTableName(dao.Instance, day) + if err != nil { + logs.Println("[DB_NODE]" + err.Error()) + return + } + if !exists { + return + } + + one, err := dao.Query(). + Table(tableName). + Attr("requestId", requestId). + Find() + if err != nil { + logs.Println("[DB_NODE]" + err.Error()) + return + } + if one != nil { + result = one.(*HTTPAccessLog) + } + }(daoWrapper) + } + wg.Wait() + return result, nil +} diff --git a/internal/rpc/services/service_http_access_log.go b/internal/rpc/services/service_http_access_log.go index c9e158a0..45295260 100644 --- a/internal/rpc/services/service_http_access_log.go +++ b/internal/rpc/services/service_http_access_log.go @@ -59,3 +59,25 @@ func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *p RequestId: requestId, }, nil } + +// 查找单个日志 +func (this *HTTPAccessLogService) FindHTTPAccessLog(ctx context.Context, req *pb.FindHTTPAccessLogRequest) (*pb.FindHTTPAccessLogResponse, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + accessLog, err := models.SharedHTTPAccessLogDAO.FindAccessLogWithRequestId(req.RequestId) + if err != nil { + return nil, err + } + if accessLog == nil { + return &pb.FindHTTPAccessLogResponse{AccessLog: nil}, nil + } + a, err := accessLog.ToPB() + if err != nil { + return nil, err + } + return &pb.FindHTTPAccessLogResponse{AccessLog: a}, nil +} diff --git a/internal/rpc/services/service_http_firewall_rule_group.go b/internal/rpc/services/service_http_firewall_rule_group.go index 71370f91..46f05328 100644 --- a/internal/rpc/services/service_http_firewall_rule_group.go +++ b/internal/rpc/services/service_http_firewall_rule_group.go @@ -60,7 +60,7 @@ func (this *HTTPFirewallRuleGroupService) UpdateHTTPFirewallRuleGroup(ctx contex } // 获取分组配置 -func (this *HTTPFirewallRuleGroupService) FindHTTPFirewallRuleGroupConfig(ctx context.Context, req *pb.FindHTTPFirewallRuleGroupConfigRequest) (*pb.FindHTTPFirewallRuleGroupConfigResponse, error) { +func (this *HTTPFirewallRuleGroupService) FindEnabledHTTPFirewallRuleGroupConfig(ctx context.Context, req *pb.FindEnabledHTTPFirewallRuleGroupConfigRequest) (*pb.FindEnabledHTTPFirewallRuleGroupConfigResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) if err != nil { @@ -72,13 +72,42 @@ func (this *HTTPFirewallRuleGroupService) FindHTTPFirewallRuleGroupConfig(ctx co return nil, err } if groupConfig == nil { - return &pb.FindHTTPFirewallRuleGroupConfigResponse{FirewallRuleGroupJSON: nil}, nil + return &pb.FindEnabledHTTPFirewallRuleGroupConfigResponse{FirewallRuleGroupJSON: nil}, nil } groupConfigJSON, err := json.Marshal(groupConfig) if err != nil { return nil, err } - return &pb.FindHTTPFirewallRuleGroupConfigResponse{FirewallRuleGroupJSON: groupConfigJSON}, nil + return &pb.FindEnabledHTTPFirewallRuleGroupConfigResponse{FirewallRuleGroupJSON: groupConfigJSON}, nil +} + +// 获取分组信息 +func (this *HTTPFirewallRuleGroupService) FindEnabledHTTPFirewallRuleGroup(ctx context.Context, req *pb.FindEnabledHTTPFirewallRuleGroupRequest) (*pb.FindEnabledHTTPFirewallRuleGroupResponse, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + group, err := models.SharedHTTPFirewallRuleGroupDAO.FindEnabledHTTPFirewallRuleGroup(req.FirewallRuleGroupId) + if err != nil { + return nil, err + } + if group == nil { + return &pb.FindEnabledHTTPFirewallRuleGroupResponse{ + FirewallRuleGroup: nil, + }, nil + } + + return &pb.FindEnabledHTTPFirewallRuleGroupResponse{ + FirewallRuleGroup: &pb.HTTPFirewallRuleGroup{ + Id: int64(group.Id), + Name: group.Name, + IsOn: group.IsOn == 1, + Description: group.Description, + Code: group.Code, + }, + }, nil } // 修改分组的规则集 diff --git a/internal/rpc/services/service_http_firewall_rule_set.go b/internal/rpc/services/service_http_firewall_rule_set.go index a051425e..02084e28 100644 --- a/internal/rpc/services/service_http_firewall_rule_set.go +++ b/internal/rpc/services/service_http_firewall_rule_set.go @@ -52,7 +52,7 @@ func (this *HTTPFirewallRuleSetService) UpdateHTTPFirewallRuleSetIsOn(ctx contex } // 查找规则集配置 -func (this *HTTPFirewallRuleSetService) FindHTTPFirewallRuleSetConfig(ctx context.Context, req *pb.FindHTTPFirewallRuleSetConfigRequest) (*pb.FindHTTPFirewallRuleSetConfigResponse, error) { +func (this *HTTPFirewallRuleSetService) FindEnabledHTTPFirewallRuleSetConfig(ctx context.Context, req *pb.FindEnabledHTTPFirewallRuleSetConfigRequest) (*pb.FindEnabledHTTPFirewallRuleSetConfigResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) if err != nil { @@ -64,11 +64,40 @@ func (this *HTTPFirewallRuleSetService) FindHTTPFirewallRuleSetConfig(ctx contex return nil, err } if config == nil { - return &pb.FindHTTPFirewallRuleSetConfigResponse{FirewallRuleSetJSON: nil}, nil + return &pb.FindEnabledHTTPFirewallRuleSetConfigResponse{FirewallRuleSetJSON: nil}, nil } configJSON, err := json.Marshal(config) if err != nil { return nil, err } - return &pb.FindHTTPFirewallRuleSetConfigResponse{FirewallRuleSetJSON: configJSON}, nil + return &pb.FindEnabledHTTPFirewallRuleSetConfigResponse{FirewallRuleSetJSON: configJSON}, nil +} + +// 查找规则集 +func (this *HTTPFirewallRuleSetService) FindEnabledHTTPFirewallRuleSet(ctx context.Context, req *pb.FindEnabledHTTPFirewallRuleSetRequest) (*pb.FindEnabledHTTPFirewallRuleSetResponse, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + set, err := models.SharedHTTPFirewallRuleSetDAO.FindEnabledHTTPFirewallRuleSet(req.FirewallRuleSetId) + if err != nil { + return nil, err + } + if set == nil { + return &pb.FindEnabledHTTPFirewallRuleSetResponse{ + FirewallRuleSet: nil, + }, nil + } + + return &pb.FindEnabledHTTPFirewallRuleSetResponse{ + FirewallRuleSet: &pb.HTTPFirewallRuleSet{ + Id: int64(set.Id), + Name: set.Name, + IsOn: set.IsOn == 1, + Description: set.Description, + Code: set.Code, + }, + }, nil }