package server import ( "cfTest/cloudflareApi/zone" "errors" "github.com/cloudflare/cloudflare-go" "github.com/samber/lo" "log" ) func Go2ProtoZone(goZone cloudflare.Zone) *zone.ZoneCloudflareEntity { return &zone.ZoneCloudflareEntity{ Id: goZone.ID, Name: goZone.Name, Plan: &zone.ZoneCloudflareEntity_Plan{ Id: goZone.Plan.ID, Name: goZone.Plan.Name, Price: int64(goZone.Plan.Price), }, PlanPending: &zone.ZoneCloudflareEntity_Plan{ Id: goZone.PlanPending.ID, Name: goZone.PlanPending.Name, Price: int64(goZone.PlanPending.Price), }, Status: goZone.Status, Paused: goZone.Paused, Type: goZone.Type, VerificationKey: goZone.VerificationKey, } } func Proto2GoRulesetRuleByPhase(rules *zone.Rule, phase zone.Phase) (*cloudflare.RulesetRule, error) { var ( Expression = rules.GetExpression() //TRUE = true //ttl = uint(*rules.Ttl) //origin *cloudflare.RulesetRuleActionParametersOrigin //query cloudflare.RulesetRuleActionParametersCustomKeyQuery Headers map[string]cloudflare.RulesetRuleActionParametersHTTPHeader ) switch phase { case zone.Phase_http_request_transform: var path *cloudflare.RulesetRuleActionParametersURIPath if rules.OriginPath == nil { return nil, nil } path = &cloudflare.RulesetRuleActionParametersURIPath{ //Value: rules.GetOriginPath(), Expression: "concat(\"" + rules.GetOriginPath() + "\",http.request.uri.path)", } return &cloudflare.RulesetRule{ Expression: Expression, Enabled: true, Action: "rewrite", ActionParameters: &cloudflare.RulesetRuleActionParameters{ URI: &cloudflare.RulesetRuleActionParametersURI{ Path: path, }, }, }, nil //case zone.Phase_http_request_origin: // port := uint16(rules.GetOriginProtocol()) // if port == 0 && rules.GetOriginHost() == "" { // return nil, nil // } // if port != 0 { // origin = &cloudflare.RulesetRuleActionParametersOrigin{ // Port: port, // } // } // return &cloudflare.RulesetRule{ // Expression: Expression, // Enabled: true, // Action: "route", // ActionParameters: &cloudflare.RulesetRuleActionParameters{ // Origin: origin, // HostHeader: rules.GetOriginHost(), // }, // }, nil //case zone.Phase_http_request_cache_settings: // switch *rules.QueryArgsType { // case "Exclude": // query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{ // Include: nil, // Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{ // List: rules.QueryArgs, // All: false, // }, // } // case "ExcludeAll": // query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{ // Include: nil, // Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{ // List: nil, // All: true, // }, // } // case "Include": // query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{ // Include: &cloudflare.RulesetRuleActionParametersCustomKeyList{ // List: rules.QueryArgs, // All: false, // }, // Exclude: nil, // } // case "IncludeALL": // query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{ // Include: &cloudflare.RulesetRuleActionParametersCustomKeyList{ // List: nil, // All: true, // }, // Exclude: nil, // } // } // return &cloudflare.RulesetRule{ // Expression: Expression, // Enabled: true, // Action: "set_cache_settings", // ActionParameters: &cloudflare.RulesetRuleActionParameters{ // Cache: &TRUE, // CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{ // CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{ // Query: &query, // }, // }, // EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{ // Mode: "override_origin", // Default: &ttl, // }, // }, // }, nil case zone.Phase_http_request_late_transform: if len(rules.GetRequestHeader()) == 0 { return nil, nil } Headers = make(map[string]cloudflare.RulesetRuleActionParametersHTTPHeader) for key, value := range rules.GetRequestHeader() { Headers[key] = cloudflare.RulesetRuleActionParametersHTTPHeader{ Operation: "set", Value: value, } } actionParameters := &cloudflare.RulesetRuleActionParameters{ Headers: Headers, } return &cloudflare.RulesetRule{ Expression: Expression, Enabled: true, Action: "rewrite", ActionParameters: actionParameters, }, nil case zone.Phase_http_response_headers_transform: if len(rules.GetResponseHeader()) == 0 { return nil, nil } Headers = make(map[string]cloudflare.RulesetRuleActionParametersHTTPHeader) for key, value := range rules.GetResponseHeader() { Headers[key] = cloudflare.RulesetRuleActionParametersHTTPHeader{ Operation: "set", Value: value, } } actionParameters := &cloudflare.RulesetRuleActionParameters{ Headers: Headers, } return &cloudflare.RulesetRule{ Expression: Expression, Enabled: true, Action: "rewrite", ActionParameters: actionParameters, }, nil default: return nil, errors.New("未定义规则阶段") } } func Go2ProtoRuleRuleByPhase(rule *cloudflare.RulesetRule, phase string) (*zone.Rule, error) { switch phase { case "http_request_transform": exp := rule.ActionParameters.URI.Path.Expression u := exp[8 : len(exp)-24] return &zone.Rule{ Expression: rule.Expression, OriginPath: &u, RequestHeader: nil, ResponseHeader: nil, }, nil case "http_request_late_transform": Headers := make(map[string]string) for key, val := range rule.ActionParameters.Headers { Headers[key] = val.Value } return &zone.Rule{ Expression: rule.Expression, OriginPath: nil, RequestHeader: Headers, ResponseHeader: nil, }, nil case "http_response_headers_transform": Headers := make(map[string]string) for key, val := range rule.ActionParameters.Headers { Headers[key] = val.Value } return &zone.Rule{ Expression: rule.Expression, OriginPath: nil, RequestHeader: Headers, ResponseHeader: nil, }, nil default: return nil, errors.New("未定义规则阶段") } } func Proto2GoPageRule(rule *zone.PageRule) (*cloudflare.PageRule, error) { var ( expression = rule.Expression action []cloudflare.PageRuleAction ) //TTL设置 if rule.Ttl == nil { //缓存级别 action = append(action, cloudflare.PageRuleAction{ ID: "cache_level", Value: "bypass", }) } else { //缓存级别 action = append(action, cloudflare.PageRuleAction{ ID: "cache_level", Value: "cache_everything", }) action = append(action, cloudflare.PageRuleAction{ ID: "edge_cache_ttl", Value: rule.Ttl, }) } //缓存键设置 if rule.QueryArgsType != nil { var queryString PageRuleActionCacheKeyFieldsQueryString var nilStrings = make([]string, 0) switch *rule.QueryArgsType { case "Exclude": queryString = PageRuleActionCacheKeyFieldsQueryString{ Exclude: rule.QueryArgs, Include: nilStrings, } case "ExcludeAll": queryString = PageRuleActionCacheKeyFieldsQueryString{ Exclude: "*", Include: nilStrings, } case "Include": queryString = PageRuleActionCacheKeyFieldsQueryString{ Exclude: nilStrings, Include: rule.QueryArgs, } case "IncludeAll": queryString = PageRuleActionCacheKeyFieldsQueryString{ Exclude: nilStrings, Include: "*", } } action = append(action, cloudflare.PageRuleAction{ ID: "cache_key_fields", Value: PageRuleActionCacheKeyFields{ QueryString: queryString, }, }) } //Host设置 if rule.OriginHost != nil { action = append(action, cloudflare.PageRuleAction{ ID: "host_header_override", Value: rule.OriginHost, }) } //协议跟随设置 if rule.OriginProtocol != nil { var value string if *rule.OriginProtocol == 80 { value = "flexible" } else { value = "full" } action = append(action, cloudflare.PageRuleAction{ ID: "ssl", Value: value, }) } return &cloudflare.PageRule{ Targets: []cloudflare.PageRuleTarget{ { Target: "url", Constraint: struct { Operator string `json:"operator"` Value string `json:"value"` }{ Operator: "matches", Value: expression, }, }, }, ID: lo.FromPtr(rule.Id), Actions: action, //Priority: int(rule.Priority), Status: "active", }, nil } func Go2ProtoPageRule(rule *cloudflare.PageRule) *zone.PageRule { var ( ExcludeAll = "ExcludeAll" Exclude = "Exclude" IncludeAll = "IncludeAll" Include = "Include" Ttl80 uint32 = 80 ) pageRule := &zone.PageRule{ Id: &rule.ID, Expression: rule.Targets[0].Constraint.Value, Ttl: nil, QueryArgsType: nil, QueryArgs: nil, OriginHost: nil, OriginProtocol: nil, //Priority: int64(rule.Priority), } for _, action := range rule.Actions { switch action.ID { case "edge_cache_ttl": pageRule.Ttl = lo.ToPtr(uint32(action.Value.(float64))) case "cache_key_fields": //pageRuleActionCacheKeyFields := action.Value.(PageRuleActionCacheKeyFields) valueMap := action.Value.(map[string]interface{}) queryString := valueMap["query_string"].(map[string]interface{}) ////switch queryString.(type) { ////case string: //// if queryString =="*" { //// pageRule.QueryArgsType = &ExcludeAll //// } ////case []string: //// //// ////} //var s = queryString.(string) //if queryString.(type) //s. //if pageRuleActionCacheKeyFields.QueryString.Exclude == "*" { // pageRule.QueryArgsType = &ExcludeAll //} else if pageRuleActionCacheKeyFields.QueryString.Include == "*" { // pageRule.QueryArgsType = &IncludeAll //} else { // includeStrings := pageRuleActionCacheKeyFields.QueryString.Include.([]string) // excludeStrings := pageRuleActionCacheKeyFields.QueryString.Exclude.([]string) // if len(includeStrings) > 0 && len(excludeStrings) == 0 { // pageRule.QueryArgsType = &Include // pageRule.QueryArgs = includeStrings // } // if len(includeStrings) == 0 && len(excludeStrings) > 0 { // pageRule.QueryArgsType = &Exclude // pageRule.QueryArgs = excludeStrings // } // if len(includeStrings) == 0 && len(excludeStrings) == 0 { // pageRule.QueryArgsType = &Include // pageRule.QueryArgs = includeStrings // } // if len(includeStrings) > 0 && len(excludeStrings) > 0 { // log.Print(*rule) // } //} if queryString["exclude"] == "*" { pageRule.QueryArgsType = &ExcludeAll } else if queryString["include"] == "*" { pageRule.QueryArgsType = &IncludeAll } else { includes := queryString["include"].([]interface{}) excludes := queryString["exclude"].([]interface{}) if len(includes) > 0 && len(excludes) == 0 { pageRule.QueryArgsType = &Include includeStrings := make([]string, len(includes)) for i, includeString := range includes { includeStrings[i] = includeString.(string) } pageRule.QueryArgs = includeStrings } if len(includes) == 0 && len(excludes) > 0 { pageRule.QueryArgsType = &Exclude excludeStrings := make([]string, len(excludes)) for i, includeString := range excludes { excludeStrings[i] = includeString.(string) } pageRule.QueryArgs = excludeStrings } if len(includes) == 0 && len(excludes) == 0 { pageRule.QueryArgsType = &Include includeStrings := make([]string, len(includes)) for i, includeString := range includes { includeStrings[i] = includeString.(string) } pageRule.QueryArgs = includeStrings } if len(includes) > 0 && len(excludes) > 0 { log.Print(*rule) } } case "host_header_override": pageRule.OriginHost = lo.ToPtr(action.Value.(string)) case "ssl": s := action.Value.(string) if s == "flexible" { pageRule.Ttl = &Ttl80 } } } return pageRule } func Proto2GoArgoSetting(setting *zone.ArgoSetting) string { if setting.Enabled { return "on" } else { return "off" } } func Go2ProtoArgoSetting(setting string) *zone.ArgoSetting { if setting == "on" { return &zone.ArgoSetting{Enabled: true} } else { return &zone.ArgoSetting{Enabled: false} } }