zone_convert.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. var origin *cloudflare.RulesetRuleActionParametersOrigin
  60. port := uint16(rules.GetOriginProtocol())
  61. if port == 0 && rules.GetOriginHost() == "" {
  62. return nil, nil
  63. }
  64. if port != 0 {
  65. origin = &cloudflare.RulesetRuleActionParametersOrigin{
  66. Port: port,
  67. }
  68. }
  69. return &cloudflare.RulesetRule{
  70. Expression: Expression,
  71. Enabled: &TRUE,
  72. Action: "route",
  73. ActionParameters: &cloudflare.RulesetRuleActionParameters{
  74. Origin: origin,
  75. HostHeader: rules.GetOriginHost(),
  76. },
  77. }, nil
  78. case zone.Phase_http_request_cache_settings:
  79. var ttl = uint(*rules.Ttl)
  80. var query cloudflare.RulesetRuleActionParametersCustomKeyQuery
  81. switch *rules.QueryArgsType {
  82. case "Exclude":
  83. query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  84. Include: nil,
  85. Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  86. List: rules.QueryArgs,
  87. All: false,
  88. },
  89. }
  90. case "ExcludeAll":
  91. query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  92. Include: nil,
  93. Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  94. List: nil,
  95. All: true,
  96. },
  97. }
  98. case "Include":
  99. query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  100. Include: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  101. List: rules.QueryArgs,
  102. All: false,
  103. },
  104. Exclude: nil,
  105. }
  106. case "IncludeALL":
  107. query = cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  108. Include: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  109. List: nil,
  110. All: true,
  111. },
  112. Exclude: nil,
  113. }
  114. }
  115. return &cloudflare.RulesetRule{
  116. Expression: Expression,
  117. Enabled: &TRUE,
  118. Action: "set_cache_settings",
  119. ActionParameters: &cloudflare.RulesetRuleActionParameters{
  120. Cache: &TRUE,
  121. CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{
  122. CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{
  123. Query: &query,
  124. },
  125. },
  126. EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{
  127. Mode: "override_origin",
  128. Default: &ttl,
  129. },
  130. },
  131. }, nil
  132. case zone.Phase_http_request_late_transform:
  133. if len(rules.GetRequestHeader()) == 0 {
  134. return nil, nil
  135. }
  136. Headers = make(map[string]cloudflare.RulesetRuleActionParametersHTTPHeader)
  137. for key, value := range rules.GetRequestHeader() {
  138. Headers[key] = cloudflare.RulesetRuleActionParametersHTTPHeader{
  139. Operation: "set",
  140. Value: value,
  141. }
  142. }
  143. actionParameters := &cloudflare.RulesetRuleActionParameters{
  144. Headers: Headers,
  145. }
  146. return &cloudflare.RulesetRule{
  147. Expression: Expression,
  148. Enabled: &TRUE,
  149. Action: "rewrite",
  150. ActionParameters: actionParameters,
  151. }, nil
  152. case zone.Phase_http_response_headers_transform:
  153. if len(rules.GetResponseHeader()) == 0 {
  154. return nil, nil
  155. }
  156. Headers = make(map[string]cloudflare.RulesetRuleActionParametersHTTPHeader)
  157. for key, value := range rules.GetResponseHeader() {
  158. Headers[key] = cloudflare.RulesetRuleActionParametersHTTPHeader{
  159. Operation: "set",
  160. Value: value,
  161. }
  162. }
  163. actionParameters := &cloudflare.RulesetRuleActionParameters{
  164. Headers: Headers,
  165. }
  166. return &cloudflare.RulesetRule{
  167. Expression: Expression,
  168. Enabled: &TRUE,
  169. Action: "rewrite",
  170. ActionParameters: actionParameters,
  171. }, nil
  172. case zone.Phase_http_config_settings:
  173. var value string
  174. if rules.OriginProtocol != nil {
  175. if rules.GetOriginProtocol() == 80 {
  176. value = "flexible"
  177. } else {
  178. value = "full"
  179. }
  180. }
  181. ssl, _ := cloudflare.SSLFromString(value)
  182. return &cloudflare.RulesetRule{
  183. Expression: Expression,
  184. Enabled: &TRUE,
  185. Action: "set_config",
  186. ActionParameters: &cloudflare.RulesetRuleActionParameters{
  187. SSL: ssl,
  188. },
  189. }, nil
  190. default:
  191. return nil, errors.New("未定义规则阶段")
  192. }
  193. }
  194. func Go2ProtoRuleRuleByPhase(rule *cloudflare.RulesetRule, phase string) (*zone.Rule, error) {
  195. switch phase {
  196. case "http_request_transform":
  197. exp := rule.ActionParameters.URI.Path.Expression
  198. u := exp[8 : len(exp)-24]
  199. return &zone.Rule{
  200. Expression: rule.Expression,
  201. OriginPath: &u,
  202. RequestHeader: nil,
  203. ResponseHeader: nil,
  204. }, nil
  205. case "http_request_late_transform":
  206. Headers := make(map[string]string)
  207. for key, val := range rule.ActionParameters.Headers {
  208. Headers[key] = val.Value
  209. }
  210. return &zone.Rule{
  211. Expression: rule.Expression,
  212. OriginPath: nil,
  213. RequestHeader: Headers,
  214. ResponseHeader: nil,
  215. }, nil
  216. case "http_response_headers_transform":
  217. Headers := make(map[string]string)
  218. for key, val := range rule.ActionParameters.Headers {
  219. Headers[key] = val.Value
  220. }
  221. return &zone.Rule{
  222. Expression: rule.Expression,
  223. OriginPath: nil,
  224. RequestHeader: Headers,
  225. ResponseHeader: nil,
  226. }, nil
  227. //case "http_request_cache_settings":
  228. // var ttl = uint32(*rule.ActionParameters.EdgeTTL.Default)
  229. // queryArgsType :=rule.ActionParameters.
  230. // return &zone.Rule{
  231. // Expression: rule.Expression,
  232. // Ttl: &ttl,
  233. // QueryArgsType:
  234. // }, nil
  235. default:
  236. return nil, errors.New("未定义规则阶段")
  237. }
  238. }
  239. func Proto2GoPageRule(rule *zone.PageRule) (*cloudflare.PageRule, error) {
  240. var (
  241. expression = rule.Expression
  242. action []cloudflare.PageRuleAction
  243. )
  244. //TTL设置
  245. if rule.Ttl == nil {
  246. //缓存级别
  247. action = append(action, cloudflare.PageRuleAction{
  248. ID: "cache_level",
  249. Value: "bypass",
  250. })
  251. } else {
  252. //缓存级别
  253. action = append(action, cloudflare.PageRuleAction{
  254. ID: "cache_level",
  255. Value: "cache_everything",
  256. })
  257. action = append(action, cloudflare.PageRuleAction{
  258. ID: "edge_cache_ttl",
  259. Value: rule.Ttl,
  260. })
  261. }
  262. //缓存键设置
  263. if rule.QueryArgsType != nil {
  264. var queryString PageRuleActionCacheKeyFieldsQueryString
  265. var nilStrings = make([]string, 0)
  266. switch *rule.QueryArgsType {
  267. case "Exclude":
  268. queryString = PageRuleActionCacheKeyFieldsQueryString{
  269. Exclude: rule.QueryArgs,
  270. Include: nilStrings,
  271. }
  272. case "ExcludeAll":
  273. queryString = PageRuleActionCacheKeyFieldsQueryString{
  274. Exclude: "*",
  275. Include: nilStrings,
  276. }
  277. case "Include":
  278. queryString = PageRuleActionCacheKeyFieldsQueryString{
  279. Exclude: nilStrings,
  280. Include: rule.QueryArgs,
  281. }
  282. case "IncludeAll":
  283. queryString = PageRuleActionCacheKeyFieldsQueryString{
  284. Exclude: nilStrings,
  285. Include: "*",
  286. }
  287. }
  288. action = append(action, cloudflare.PageRuleAction{
  289. ID: "cache_key_fields",
  290. Value: PageRuleActionCacheKeyFields{
  291. QueryString: queryString,
  292. },
  293. })
  294. }
  295. //Host设置
  296. if rule.OriginHost != nil {
  297. action = append(action, cloudflare.PageRuleAction{
  298. ID: "host_header_override",
  299. Value: rule.OriginHost,
  300. })
  301. }
  302. //协议跟随设置
  303. if rule.OriginProtocol != nil {
  304. var value string
  305. if *rule.OriginProtocol == 80 {
  306. value = "flexible"
  307. } else {
  308. value = "full"
  309. }
  310. action = append(action, cloudflare.PageRuleAction{
  311. ID: "ssl",
  312. Value: value,
  313. })
  314. }
  315. return &cloudflare.PageRule{
  316. Targets: []cloudflare.PageRuleTarget{
  317. {
  318. Target: "url",
  319. Constraint: struct {
  320. Operator string `json:"operator"`
  321. Value string `json:"value"`
  322. }{
  323. Operator: "matches",
  324. Value: expression,
  325. },
  326. },
  327. },
  328. ID: lo.FromPtr(rule.Id),
  329. Actions: action,
  330. //Priority: int(rule.Priority),
  331. Status: "active",
  332. }, nil
  333. }
  334. var stringToPhaseMap = map[string]zone.Phase{
  335. "http_request_sanitize": zone.Phase_http_request_sanitize,
  336. "http_request_transform": zone.Phase_http_request_transform,
  337. "http_request_origin": zone.Phase_http_request_origin,
  338. "http_request_cache_settings": zone.Phase_http_request_cache_settings,
  339. "http_config_settings": zone.Phase_http_config_settings,
  340. //zone.Phase_http_request_dynamic_redirect: "http_request_dynamic_redirect",
  341. //zone.Phase_ddos_l7: "ddos_l7",
  342. //zone.Phase_http_request_firewall_custom: "http_request_firewall_custom",
  343. //zone.Phase_http_ratelimit: "http_ratelimit",
  344. //zone.Phase_http_request_firewall_managed: "http_request_firewall_managed",
  345. //zone.Phase_http_request_sbfm: "http_request_sbfm",
  346. //zone.Phase_http_request_redirect: "http_request_redirect",
  347. "http_request_late_transform": zone.Phase_http_request_late_transform,
  348. //zone.Phase_http_custom_errors: "http_custom_errors",
  349. "http_response_headers_transform": zone.Phase_http_response_headers_transform,
  350. //zone.Phase_http_response_firewall_managed: "http_response_firewall_managed",
  351. //zone.Phase_http_log_custom_fields: "http_log_custom_fields",
  352. }
  353. func Go2ProtoRuleSet(ruleSetFromCloudflare *cloudflare.Ruleset) *zone.RuleSet {
  354. ruleSet := &zone.RuleSet{
  355. Id: &ruleSetFromCloudflare.ID,
  356. Name: ruleSetFromCloudflare.Name,
  357. Kind: ruleSetFromCloudflare.Kind,
  358. Phase: stringToPhaseMap[ruleSetFromCloudflare.Phase],
  359. }
  360. return ruleSet
  361. }
  362. func Go2ProtoPageRule(rule *cloudflare.PageRule) *zone.PageRule {
  363. var (
  364. ExcludeAll = "ExcludeAll"
  365. Exclude = "Exclude"
  366. IncludeAll = "IncludeAll"
  367. Include = "Include"
  368. Ttl80 uint32 = 80
  369. )
  370. pageRule := &zone.PageRule{
  371. Id: &rule.ID,
  372. Expression: rule.Targets[0].Constraint.Value,
  373. Ttl: nil,
  374. QueryArgsType: nil,
  375. QueryArgs: nil,
  376. OriginHost: nil,
  377. OriginProtocol: nil,
  378. //Priority: int64(rule.Priority),
  379. }
  380. for _, action := range rule.Actions {
  381. switch action.ID {
  382. case "edge_cache_ttl":
  383. pageRule.Ttl = lo.ToPtr(uint32(action.Value.(float64)))
  384. case "cache_key_fields":
  385. //pageRuleActionCacheKeyFields := action.Value.(PageRuleActionCacheKeyFields)
  386. valueMap := action.Value.(map[string]interface{})
  387. queryString := valueMap["query_string"].(map[string]interface{})
  388. ////switch queryString.(type) {
  389. ////case string:
  390. //// if queryString =="*" {
  391. //// pageRule.QueryArgsType = &ExcludeAll
  392. //// }
  393. ////case []string:
  394. ////
  395. ////
  396. ////}
  397. //var s = queryString.(string) //if queryString.(type)
  398. //s.
  399. //if pageRuleActionCacheKeyFields.QueryString.Exclude == "*" {
  400. // pageRule.QueryArgsType = &ExcludeAll
  401. //} else if pageRuleActionCacheKeyFields.QueryString.Include == "*" {
  402. // pageRule.QueryArgsType = &IncludeAll
  403. //} else {
  404. // includeStrings := pageRuleActionCacheKeyFields.QueryString.Include.([]string)
  405. // excludeStrings := pageRuleActionCacheKeyFields.QueryString.Exclude.([]string)
  406. // if len(includeStrings) > 0 && len(excludeStrings) == 0 {
  407. // pageRule.QueryArgsType = &Include
  408. // pageRule.QueryArgs = includeStrings
  409. // }
  410. // if len(includeStrings) == 0 && len(excludeStrings) > 0 {
  411. // pageRule.QueryArgsType = &Exclude
  412. // pageRule.QueryArgs = excludeStrings
  413. // }
  414. // if len(includeStrings) == 0 && len(excludeStrings) == 0 {
  415. // pageRule.QueryArgsType = &Include
  416. // pageRule.QueryArgs = includeStrings
  417. // }
  418. // if len(includeStrings) > 0 && len(excludeStrings) > 0 {
  419. // log.Print(*rule)
  420. // }
  421. //}
  422. if queryString["exclude"] == "*" {
  423. pageRule.QueryArgsType = &ExcludeAll
  424. } else if queryString["include"] == "*" {
  425. pageRule.QueryArgsType = &IncludeAll
  426. } else {
  427. includes := queryString["include"].([]interface{})
  428. excludes := queryString["exclude"].([]interface{})
  429. if len(includes) > 0 && len(excludes) == 0 {
  430. pageRule.QueryArgsType = &Include
  431. includeStrings := make([]string, len(includes))
  432. for i, includeString := range includes {
  433. includeStrings[i] = includeString.(string)
  434. }
  435. pageRule.QueryArgs = includeStrings
  436. }
  437. if len(includes) == 0 && len(excludes) > 0 {
  438. pageRule.QueryArgsType = &Exclude
  439. excludeStrings := make([]string, len(excludes))
  440. for i, includeString := range excludes {
  441. excludeStrings[i] = includeString.(string)
  442. }
  443. pageRule.QueryArgs = excludeStrings
  444. }
  445. if len(includes) == 0 && len(excludes) == 0 {
  446. pageRule.QueryArgsType = &Include
  447. includeStrings := make([]string, len(includes))
  448. for i, includeString := range includes {
  449. includeStrings[i] = includeString.(string)
  450. }
  451. pageRule.QueryArgs = includeStrings
  452. }
  453. if len(includes) > 0 && len(excludes) > 0 {
  454. log.Print(*rule)
  455. }
  456. }
  457. case "host_header_override":
  458. pageRule.OriginHost = lo.ToPtr(action.Value.(string))
  459. case "ssl":
  460. s := action.Value.(string)
  461. if s == "flexible" {
  462. pageRule.Ttl = &Ttl80
  463. }
  464. }
  465. }
  466. return pageRule
  467. }
  468. func Proto2GoArgoSetting(setting *zone.ArgoSetting) string {
  469. if setting.Enabled {
  470. return "on"
  471. } else {
  472. return "off"
  473. }
  474. }
  475. func Go2ProtoArgoSetting(setting string) *zone.ArgoSetting {
  476. if setting == "on" {
  477. return &zone.ArgoSetting{Enabled: true}
  478. } else {
  479. return &zone.ArgoSetting{Enabled: false}
  480. }
  481. }