zone_convert.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package server
  2. import (
  3. "cfTest/cloudflareApi/zone"
  4. "errors"
  5. "github.com/cloudflare/cloudflare-go"
  6. "github.com/samber/lo"
  7. "log"
  8. )
  9. func Go2ProtoZone(goZone cloudflare.Zone) *zone.ZoneCloudflareEntity {
  10. return &zone.ZoneCloudflareEntity{
  11. Id: goZone.ID,
  12. Name: goZone.Name,
  13. Plan: &zone.ZoneCloudflareEntity_Plan{
  14. Id: goZone.Plan.ID,
  15. Name: goZone.Plan.Name,
  16. Price: int64(goZone.Plan.Price),
  17. },
  18. PlanPending: &zone.ZoneCloudflareEntity_Plan{
  19. Id: goZone.PlanPending.ID,
  20. Name: goZone.PlanPending.Name,
  21. Price: int64(goZone.PlanPending.Price),
  22. },
  23. Status: goZone.Status,
  24. Paused: goZone.Paused,
  25. Type: goZone.Type,
  26. VerificationKey: goZone.VerificationKey,
  27. }
  28. }
  29. func Proto2GoRulesetRuleByPhase(rules *zone.Rule, phase zone.Phase) (*cloudflare.RulesetRule, error) {
  30. var (
  31. Expression = rules.GetExpression()
  32. //TRUE = true
  33. //ttl = uint(*rules.Ttl)
  34. //origin *cloudflare.RulesetRuleActionParametersOrigin
  35. //query cloudflare.RulesetRuleActionParametersCustomKeyQuery
  36. Headers map[string]cloudflare.RulesetRuleActionParametersHTTPHeader
  37. )
  38. switch phase {
  39. case zone.Phase_http_request_transform:
  40. var path *cloudflare.RulesetRuleActionParametersURIPath
  41. if rules.OriginPath == nil {
  42. return nil, nil
  43. }
  44. path = &cloudflare.RulesetRuleActionParametersURIPath{
  45. //Value: rules.GetOriginPath(),
  46. Expression: "concat(\"" + rules.GetOriginPath() + "\",http.request.uri.path)",
  47. }
  48. return &cloudflare.RulesetRule{
  49. Expression: Expression,
  50. Enabled: true,
  51. Action: "rewrite",
  52. ActionParameters: &cloudflare.RulesetRuleActionParameters{
  53. URI: &cloudflare.RulesetRuleActionParametersURI{
  54. Path: path,
  55. },
  56. },
  57. }, nil
  58. //case zone.Phase_http_request_origin:
  59. // port := uint16(rules.GetOriginProtocol())
  60. // if port == 0 && rules.GetOriginHost() == "" {
  61. // return nil, nil
  62. // }
  63. // if port != 0 {
  64. // origin = &cloudflare.RulesetRuleActionParametersOrigin{
  65. // Port: port,
  66. // }
  67. // }
  68. // return &cloudflare.RulesetRule{
  69. // Expression: Expression,
  70. // Enabled: true,
  71. // Action: "route",
  72. // ActionParameters: &cloudflare.RulesetRuleActionParameters{
  73. // Origin: origin,
  74. // HostHeader: rules.GetOriginHost(),
  75. // },
  76. // }, nil
  77. //case zone.Phase_http_request_cache_settings:
  78. // switch *rules.QueryArgsType {
  79. // case "Exclude":
  80. // query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  81. // Include: nil,
  82. // Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  83. // List: rules.QueryArgs,
  84. // All: false,
  85. // },
  86. // }
  87. // case "ExcludeAll":
  88. // query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  89. // Include: nil,
  90. // Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  91. // List: nil,
  92. // All: true,
  93. // },
  94. // }
  95. // case "Include":
  96. // query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  97. // Include: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  98. // List: rules.QueryArgs,
  99. // All: false,
  100. // },
  101. // Exclude: nil,
  102. // }
  103. // case "IncludeALL":
  104. // query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  105. // Include: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  106. // List: nil,
  107. // All: true,
  108. // },
  109. // Exclude: nil,
  110. // }
  111. // }
  112. // return &cloudflare.RulesetRule{
  113. // Expression: Expression,
  114. // Enabled: true,
  115. // Action: "set_cache_settings",
  116. // ActionParameters: &cloudflare.RulesetRuleActionParameters{
  117. // Cache: &TRUE,
  118. // CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{
  119. // CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{
  120. // Query: &query,
  121. // },
  122. // },
  123. // EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{
  124. // Mode: "override_origin",
  125. // Default: &ttl,
  126. // },
  127. // },
  128. // }, nil
  129. case zone.Phase_http_request_late_transform:
  130. if len(rules.GetRequestHeader()) == 0 {
  131. return nil, nil
  132. }
  133. Headers = make(map[string]cloudflare.RulesetRuleActionParametersHTTPHeader)
  134. for key, value := range rules.GetRequestHeader() {
  135. Headers[key] = cloudflare.RulesetRuleActionParametersHTTPHeader{
  136. Operation: "set",
  137. Value: value,
  138. }
  139. }
  140. actionParameters := &cloudflare.RulesetRuleActionParameters{
  141. Headers: Headers,
  142. }
  143. return &cloudflare.RulesetRule{
  144. Expression: Expression,
  145. Enabled: true,
  146. Action: "rewrite",
  147. ActionParameters: actionParameters,
  148. }, nil
  149. case zone.Phase_http_response_headers_transform:
  150. if len(rules.GetResponseHeader()) == 0 {
  151. return nil, nil
  152. }
  153. Headers = make(map[string]cloudflare.RulesetRuleActionParametersHTTPHeader)
  154. for key, value := range rules.GetResponseHeader() {
  155. Headers[key] = cloudflare.RulesetRuleActionParametersHTTPHeader{
  156. Operation: "set",
  157. Value: value,
  158. }
  159. }
  160. actionParameters := &cloudflare.RulesetRuleActionParameters{
  161. Headers: Headers,
  162. }
  163. return &cloudflare.RulesetRule{
  164. Expression: Expression,
  165. Enabled: true,
  166. Action: "rewrite",
  167. ActionParameters: actionParameters,
  168. }, nil
  169. default:
  170. return nil, errors.New("未定义规则阶段")
  171. }
  172. }
  173. func Go2ProtoRuleRuleByPhase(rule *cloudflare.RulesetRule, phase string) (*zone.Rule, error) {
  174. switch phase {
  175. case "http_request_transform":
  176. exp := rule.ActionParameters.URI.Path.Expression
  177. u := exp[8 : len(exp)-24]
  178. return &zone.Rule{
  179. Expression: rule.Expression,
  180. OriginPath: &u,
  181. RequestHeader: nil,
  182. ResponseHeader: nil,
  183. }, nil
  184. case "http_request_late_transform":
  185. Headers := make(map[string]string)
  186. for key, val := range rule.ActionParameters.Headers {
  187. Headers[key] = val.Value
  188. }
  189. return &zone.Rule{
  190. Expression: rule.Expression,
  191. OriginPath: nil,
  192. RequestHeader: Headers,
  193. ResponseHeader: nil,
  194. }, nil
  195. case "http_response_headers_transform":
  196. Headers := make(map[string]string)
  197. for key, val := range rule.ActionParameters.Headers {
  198. Headers[key] = val.Value
  199. }
  200. return &zone.Rule{
  201. Expression: rule.Expression,
  202. OriginPath: nil,
  203. RequestHeader: Headers,
  204. ResponseHeader: nil,
  205. }, nil
  206. default:
  207. return nil, errors.New("未定义规则阶段")
  208. }
  209. }
  210. func Proto2GoPageRule(rule *zone.PageRule) (*cloudflare.PageRule, error) {
  211. var (
  212. expression = rule.Expression
  213. action []cloudflare.PageRuleAction
  214. )
  215. //TTL设置
  216. if rule.Ttl != nil {
  217. action = append(action, cloudflare.PageRuleAction{
  218. ID: "edge_cache_ttl",
  219. Value: rule.Ttl,
  220. })
  221. }
  222. //缓存键设置
  223. if rule.QueryArgsType != nil {
  224. var queryString PageRuleActionCacheKeyFieldsQueryString
  225. var nilStrings = make([]string, 0)
  226. switch *rule.QueryArgsType {
  227. case "Exclude":
  228. queryString = PageRuleActionCacheKeyFieldsQueryString{
  229. Exclude: rule.QueryArgs,
  230. Include: nilStrings,
  231. }
  232. case "ExcludeAll":
  233. queryString = PageRuleActionCacheKeyFieldsQueryString{
  234. Exclude: "*",
  235. Include: nilStrings,
  236. }
  237. case "Include":
  238. queryString = PageRuleActionCacheKeyFieldsQueryString{
  239. Exclude: nilStrings,
  240. Include: rule.QueryArgs,
  241. }
  242. case "IncludeAll":
  243. queryString = PageRuleActionCacheKeyFieldsQueryString{
  244. Exclude: nilStrings,
  245. Include: "*",
  246. }
  247. }
  248. action = append(action, cloudflare.PageRuleAction{
  249. ID: "cache_key_fields",
  250. Value: PageRuleActionCacheKeyFields{
  251. QueryString: queryString,
  252. },
  253. })
  254. }
  255. //Host设置
  256. if rule.OriginHost != nil {
  257. action = append(action, cloudflare.PageRuleAction{
  258. ID: "host_header_override",
  259. Value: rule.OriginHost,
  260. })
  261. }
  262. //协议跟随设置
  263. if rule.OriginProtocol != nil {
  264. var value string
  265. if *rule.OriginProtocol == 80 {
  266. value = "flexible"
  267. } else {
  268. value = "full"
  269. }
  270. action = append(action, cloudflare.PageRuleAction{
  271. ID: "ssl",
  272. Value: value,
  273. })
  274. }
  275. //缓存级别 恒为缓存所有
  276. action = append(action, cloudflare.PageRuleAction{
  277. ID: "cache_level",
  278. Value: "cache_everything",
  279. })
  280. return &cloudflare.PageRule{
  281. Targets: []cloudflare.PageRuleTarget{
  282. {
  283. Target: "url",
  284. Constraint: struct {
  285. Operator string `json:"operator"`
  286. Value string `json:"value"`
  287. }{
  288. Operator: "matches",
  289. Value: expression,
  290. },
  291. },
  292. },
  293. ID: lo.FromPtr(rule.Id),
  294. Actions: action,
  295. //Priority: int(rule.Priority),
  296. Status: "active",
  297. }, nil
  298. }
  299. func Go2ProtoPageRule(rule *cloudflare.PageRule) *zone.PageRule {
  300. var (
  301. ExcludeAll = "ExcludeAll"
  302. Exclude = "Exclude"
  303. IncludeAll = "IncludeAll"
  304. Include = "Include"
  305. Ttl80 uint32 = 80
  306. )
  307. pageRule := &zone.PageRule{
  308. Id: &rule.ID,
  309. Expression: rule.Targets[0].Constraint.Value,
  310. Ttl: nil,
  311. QueryArgsType: nil,
  312. QueryArgs: nil,
  313. OriginHost: nil,
  314. OriginProtocol: nil,
  315. //Priority: int64(rule.Priority),
  316. }
  317. for _, action := range rule.Actions {
  318. switch action.ID {
  319. case "edge_cache_ttl":
  320. pageRule.Ttl = lo.ToPtr(uint32(action.Value.(float64)))
  321. case "cache_key_fields":
  322. //pageRuleActionCacheKeyFields := action.Value.(PageRuleActionCacheKeyFields)
  323. valueMap := action.Value.(map[string]interface{})
  324. queryString := valueMap["query_string"].(map[string]interface{})
  325. ////switch queryString.(type) {
  326. ////case string:
  327. //// if queryString =="*" {
  328. //// pageRule.QueryArgsType = &ExcludeAll
  329. //// }
  330. ////case []string:
  331. ////
  332. ////
  333. ////}
  334. //var s = queryString.(string) //if queryString.(type)
  335. //s.
  336. //if pageRuleActionCacheKeyFields.QueryString.Exclude == "*" {
  337. // pageRule.QueryArgsType = &ExcludeAll
  338. //} else if pageRuleActionCacheKeyFields.QueryString.Include == "*" {
  339. // pageRule.QueryArgsType = &IncludeAll
  340. //} else {
  341. // includeStrings := pageRuleActionCacheKeyFields.QueryString.Include.([]string)
  342. // excludeStrings := pageRuleActionCacheKeyFields.QueryString.Exclude.([]string)
  343. // if len(includeStrings) > 0 && len(excludeStrings) == 0 {
  344. // pageRule.QueryArgsType = &Include
  345. // pageRule.QueryArgs = includeStrings
  346. // }
  347. // if len(includeStrings) == 0 && len(excludeStrings) > 0 {
  348. // pageRule.QueryArgsType = &Exclude
  349. // pageRule.QueryArgs = excludeStrings
  350. // }
  351. // if len(includeStrings) == 0 && len(excludeStrings) == 0 {
  352. // pageRule.QueryArgsType = &Include
  353. // pageRule.QueryArgs = includeStrings
  354. // }
  355. // if len(includeStrings) > 0 && len(excludeStrings) > 0 {
  356. // log.Print(*rule)
  357. // }
  358. //}
  359. if queryString["exclude"] == "*" {
  360. pageRule.QueryArgsType = &ExcludeAll
  361. } else if queryString["include"] == "*" {
  362. pageRule.QueryArgsType = &IncludeAll
  363. } else {
  364. includes := queryString["include"].([]interface{})
  365. excludes := queryString["exclude"].([]interface{})
  366. if len(includes) > 0 && len(excludes) == 0 {
  367. pageRule.QueryArgsType = &Include
  368. includeStrings := make([]string, len(includes))
  369. for i, includeString := range includes {
  370. includeStrings[i] = includeString.(string)
  371. }
  372. pageRule.QueryArgs = includeStrings
  373. }
  374. if len(includes) == 0 && len(excludes) > 0 {
  375. pageRule.QueryArgsType = &Exclude
  376. excludeStrings := make([]string, len(excludes))
  377. for i, includeString := range excludes {
  378. excludeStrings[i] = includeString.(string)
  379. }
  380. pageRule.QueryArgs = excludeStrings
  381. }
  382. if len(includes) == 0 && len(excludes) == 0 {
  383. pageRule.QueryArgsType = &Include
  384. includeStrings := make([]string, len(includes))
  385. for i, includeString := range includes {
  386. includeStrings[i] = includeString.(string)
  387. }
  388. pageRule.QueryArgs = includeStrings
  389. }
  390. if len(includes) > 0 && len(excludes) > 0 {
  391. log.Print(*rule)
  392. }
  393. }
  394. case "host_header_override":
  395. pageRule.OriginHost = lo.ToPtr(action.Value.(string))
  396. case "ssl":
  397. s := action.Value.(string)
  398. if s == "flexible" {
  399. pageRule.Ttl = &Ttl80
  400. }
  401. }
  402. }
  403. return pageRule
  404. }