zone_convert.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. //缓存级别
  218. action = append(action, cloudflare.PageRuleAction{
  219. ID: "cache_level",
  220. Value: "bypass",
  221. })
  222. } else {
  223. //缓存级别
  224. action = append(action, cloudflare.PageRuleAction{
  225. ID: "cache_level",
  226. Value: "cache_everything",
  227. })
  228. action = append(action, cloudflare.PageRuleAction{
  229. ID: "edge_cache_ttl",
  230. Value: rule.Ttl,
  231. })
  232. }
  233. //缓存键设置
  234. if rule.QueryArgsType != nil {
  235. var queryString PageRuleActionCacheKeyFieldsQueryString
  236. var nilStrings = make([]string, 0)
  237. switch *rule.QueryArgsType {
  238. case "Exclude":
  239. queryString = PageRuleActionCacheKeyFieldsQueryString{
  240. Exclude: rule.QueryArgs,
  241. Include: nilStrings,
  242. }
  243. case "ExcludeAll":
  244. queryString = PageRuleActionCacheKeyFieldsQueryString{
  245. Exclude: "*",
  246. Include: nilStrings,
  247. }
  248. case "Include":
  249. queryString = PageRuleActionCacheKeyFieldsQueryString{
  250. Exclude: nilStrings,
  251. Include: rule.QueryArgs,
  252. }
  253. case "IncludeAll":
  254. queryString = PageRuleActionCacheKeyFieldsQueryString{
  255. Exclude: nilStrings,
  256. Include: "*",
  257. }
  258. }
  259. action = append(action, cloudflare.PageRuleAction{
  260. ID: "cache_key_fields",
  261. Value: PageRuleActionCacheKeyFields{
  262. QueryString: queryString,
  263. },
  264. })
  265. }
  266. //Host设置
  267. if rule.OriginHost != nil {
  268. action = append(action, cloudflare.PageRuleAction{
  269. ID: "host_header_override",
  270. Value: rule.OriginHost,
  271. })
  272. }
  273. //协议跟随设置
  274. if rule.OriginProtocol != nil {
  275. var value string
  276. if *rule.OriginProtocol == 80 {
  277. value = "flexible"
  278. } else {
  279. value = "full"
  280. }
  281. action = append(action, cloudflare.PageRuleAction{
  282. ID: "ssl",
  283. Value: value,
  284. })
  285. }
  286. return &cloudflare.PageRule{
  287. Targets: []cloudflare.PageRuleTarget{
  288. {
  289. Target: "url",
  290. Constraint: struct {
  291. Operator string `json:"operator"`
  292. Value string `json:"value"`
  293. }{
  294. Operator: "matches",
  295. Value: expression,
  296. },
  297. },
  298. },
  299. ID: lo.FromPtr(rule.Id),
  300. Actions: action,
  301. //Priority: int(rule.Priority),
  302. Status: "active",
  303. }, nil
  304. }
  305. func Go2ProtoPageRule(rule *cloudflare.PageRule) *zone.PageRule {
  306. var (
  307. ExcludeAll = "ExcludeAll"
  308. Exclude = "Exclude"
  309. IncludeAll = "IncludeAll"
  310. Include = "Include"
  311. Ttl80 uint32 = 80
  312. )
  313. pageRule := &zone.PageRule{
  314. Id: &rule.ID,
  315. Expression: rule.Targets[0].Constraint.Value,
  316. Ttl: nil,
  317. QueryArgsType: nil,
  318. QueryArgs: nil,
  319. OriginHost: nil,
  320. OriginProtocol: nil,
  321. //Priority: int64(rule.Priority),
  322. }
  323. for _, action := range rule.Actions {
  324. switch action.ID {
  325. case "edge_cache_ttl":
  326. pageRule.Ttl = lo.ToPtr(uint32(action.Value.(float64)))
  327. case "cache_key_fields":
  328. //pageRuleActionCacheKeyFields := action.Value.(PageRuleActionCacheKeyFields)
  329. valueMap := action.Value.(map[string]interface{})
  330. queryString := valueMap["query_string"].(map[string]interface{})
  331. ////switch queryString.(type) {
  332. ////case string:
  333. //// if queryString =="*" {
  334. //// pageRule.QueryArgsType = &ExcludeAll
  335. //// }
  336. ////case []string:
  337. ////
  338. ////
  339. ////}
  340. //var s = queryString.(string) //if queryString.(type)
  341. //s.
  342. //if pageRuleActionCacheKeyFields.QueryString.Exclude == "*" {
  343. // pageRule.QueryArgsType = &ExcludeAll
  344. //} else if pageRuleActionCacheKeyFields.QueryString.Include == "*" {
  345. // pageRule.QueryArgsType = &IncludeAll
  346. //} else {
  347. // includeStrings := pageRuleActionCacheKeyFields.QueryString.Include.([]string)
  348. // excludeStrings := pageRuleActionCacheKeyFields.QueryString.Exclude.([]string)
  349. // if len(includeStrings) > 0 && len(excludeStrings) == 0 {
  350. // pageRule.QueryArgsType = &Include
  351. // pageRule.QueryArgs = includeStrings
  352. // }
  353. // if len(includeStrings) == 0 && len(excludeStrings) > 0 {
  354. // pageRule.QueryArgsType = &Exclude
  355. // pageRule.QueryArgs = excludeStrings
  356. // }
  357. // if len(includeStrings) == 0 && len(excludeStrings) == 0 {
  358. // pageRule.QueryArgsType = &Include
  359. // pageRule.QueryArgs = includeStrings
  360. // }
  361. // if len(includeStrings) > 0 && len(excludeStrings) > 0 {
  362. // log.Print(*rule)
  363. // }
  364. //}
  365. if queryString["exclude"] == "*" {
  366. pageRule.QueryArgsType = &ExcludeAll
  367. } else if queryString["include"] == "*" {
  368. pageRule.QueryArgsType = &IncludeAll
  369. } else {
  370. includes := queryString["include"].([]interface{})
  371. excludes := queryString["exclude"].([]interface{})
  372. if len(includes) > 0 && len(excludes) == 0 {
  373. pageRule.QueryArgsType = &Include
  374. includeStrings := make([]string, len(includes))
  375. for i, includeString := range includes {
  376. includeStrings[i] = includeString.(string)
  377. }
  378. pageRule.QueryArgs = includeStrings
  379. }
  380. if len(includes) == 0 && len(excludes) > 0 {
  381. pageRule.QueryArgsType = &Exclude
  382. excludeStrings := make([]string, len(excludes))
  383. for i, includeString := range excludes {
  384. excludeStrings[i] = includeString.(string)
  385. }
  386. pageRule.QueryArgs = excludeStrings
  387. }
  388. if len(includes) == 0 && len(excludes) == 0 {
  389. pageRule.QueryArgsType = &Include
  390. includeStrings := make([]string, len(includes))
  391. for i, includeString := range includes {
  392. includeStrings[i] = includeString.(string)
  393. }
  394. pageRule.QueryArgs = includeStrings
  395. }
  396. if len(includes) > 0 && len(excludes) > 0 {
  397. log.Print(*rule)
  398. }
  399. }
  400. case "host_header_override":
  401. pageRule.OriginHost = lo.ToPtr(action.Value.(string))
  402. case "ssl":
  403. s := action.Value.(string)
  404. if s == "flexible" {
  405. pageRule.Ttl = &Ttl80
  406. }
  407. }
  408. }
  409. return pageRule
  410. }
  411. func Proto2GoArgoSetting(setting *zone.ArgoSetting) string {
  412. if setting.Enabled {
  413. return "on"
  414. } else {
  415. return "off"
  416. }
  417. }
  418. func Go2ProtoArgoSetting(setting string) *zone.ArgoSetting {
  419. if setting == "on" {
  420. return &zone.ArgoSetting{Enabled: true}
  421. } else {
  422. return &zone.ArgoSetting{Enabled: false}
  423. }
  424. }