123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- 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: "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,
- })
- }
- //缓存级别 恒为缓存所有
- action = append(action, cloudflare.PageRuleAction{
- ID: "cache_level",
- Value: "cache_everything",
- })
- 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
- }
|