ZoneCloudflareServer.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. package server
  2. import (
  3. "bytes"
  4. "cfTest/cloudflareApi/zone"
  5. "context"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "github.com/cloudflare/cloudflare-go"
  10. "github.com/samber/lo"
  11. "io"
  12. "io/ioutil"
  13. "log"
  14. "net/http"
  15. )
  16. type ZoneServer struct {
  17. zone.UnimplementedZoneCloudflareServer
  18. }
  19. func (s *ZoneServer) ZoneIDByNameCloudflare(ctx context.Context, requestCloudflare *zone.ZoneIDByNameRequestCloudflare) (*zone.ZoneIDByNameReplyCloudflare, error) {
  20. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  21. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  22. log.Printf("zoneName Received: %v", requestCloudflare.GetZoneName())
  23. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  24. if err != nil {
  25. return nil, err
  26. }
  27. id, err := api.ZoneIDByName(requestCloudflare.ZoneName)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &zone.ZoneIDByNameReplyCloudflare{ZoneId: id}, err
  32. }
  33. func (s *ZoneServer) ZoneDetails(ctx context.Context, requestCloudflare *zone.ZoneDetailsRequestCloudflare) (*zone.ZoneDetailsReplyCloudflare, error) {
  34. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  35. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  36. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  37. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  38. if err != nil {
  39. return nil, err
  40. }
  41. ctxAPI := context.Background()
  42. details, err := api.ZoneDetails(ctxAPI, requestCloudflare.GetZoneId())
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &zone.ZoneDetailsReplyCloudflare{
  47. Result: Go2ProtoZone(details),
  48. }, err
  49. }
  50. func (s *ZoneServer) GetZones(ctx context.Context, requestCloudflare *zone.GetZonesRequestCloudflare) (*zone.GetZonesReplyCloudflare, error) {
  51. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  52. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  53. log.Printf("domain Received: %v", requestCloudflare.GetDomain())
  54. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  55. if err != nil {
  56. return nil, err
  57. }
  58. ctxAPI := context.Background()
  59. details, err := api.ListZones(ctxAPI, "name", requestCloudflare.GetDomain())
  60. if err != nil {
  61. return nil, err
  62. }
  63. list := make([]*zone.ZoneCloudflareEntity, len(details))
  64. for i, detail := range details {
  65. list[i] = Go2ProtoZone(detail)
  66. }
  67. return &zone.GetZonesReplyCloudflare{
  68. Result: list,
  69. Success: true,
  70. }, err
  71. }
  72. func (s *ZoneServer) ListZones(ctx context.Context, requestCloudflare *zone.ListZonesRequestCloudflare) (*zone.ZonesList, error) {
  73. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  74. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  75. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  76. if err != nil {
  77. return nil, err
  78. }
  79. ctxAPI := context.Background()
  80. details, err := api.ListZones(ctxAPI)
  81. if err != nil {
  82. return nil, err
  83. }
  84. list := make([]*zone.ZoneCloudflareEntity, len(details))
  85. for i, detail := range details {
  86. list[i] = Go2ProtoZone(detail)
  87. }
  88. return &zone.ZonesList{
  89. Result: list,
  90. }, err
  91. }
  92. func (s *ZoneServer) ListZonesByAccountId(ctx context.Context, requestCloudflare *zone.ListZonesByAccountIdRequestCloudflare) (*zone.ZonesList, error) {
  93. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  94. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  95. log.Printf("accountId Received: %v", requestCloudflare.GetAccountId())
  96. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  97. if err != nil {
  98. return nil, err
  99. }
  100. ctxAPI := context.Background()
  101. opt := cloudflare.WithZoneFilters("", requestCloudflare.AccountId, "")
  102. details, err := api.ListZonesContext(ctxAPI, opt)
  103. if err != nil {
  104. return nil, err
  105. }
  106. list := make([]*zone.ZoneCloudflareEntity, len(details.Result))
  107. for i, detail := range details.Result {
  108. list[i] = Go2ProtoZone(detail)
  109. }
  110. return &zone.ZonesList{
  111. Result: list,
  112. }, err
  113. }
  114. func (s *ZoneServer) EditZone(ctx context.Context, requestCloudflare *zone.EditZoneRequestCloudflare) (*zone.EditZoneReplyCloudflare, error) {
  115. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  116. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  117. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  118. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  119. if err != nil {
  120. return nil, err
  121. }
  122. ctxAPI := context.Background()
  123. opts := cloudflare.ZoneOptions{}
  124. opts.Paused = requestCloudflare.Paused
  125. if requestCloudflare.PlanId != nil {
  126. opts.Plan = &cloudflare.ZonePlan{
  127. ZonePlanCommon: cloudflare.ZonePlanCommon{
  128. ID: requestCloudflare.GetPlanId(),
  129. },
  130. }
  131. }
  132. if requestCloudflare.Type != nil {
  133. opts.Type = requestCloudflare.GetType()
  134. }
  135. editZone, err := api.EditZone(ctxAPI, requestCloudflare.GetZoneId(), opts)
  136. if err != nil {
  137. return nil, err
  138. }
  139. return &zone.EditZoneReplyCloudflare{
  140. Result: Go2ProtoZone(editZone),
  141. Success: true,
  142. }, nil
  143. }
  144. func (s *ZoneServer) DeleteZone(ctx context.Context, requestCloudflare *zone.DeleteZoneRequestCloudflare) (*zone.DeleteZoneReplyCloudflare, error) {
  145. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  146. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  147. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  148. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  149. if err != nil {
  150. return nil, err
  151. }
  152. ctxAPI := context.Background()
  153. deleteZone, err := api.DeleteZone(ctxAPI, requestCloudflare.GetZoneId())
  154. if err != nil {
  155. return nil, err
  156. }
  157. return &zone.DeleteZoneReplyCloudflare{
  158. Success: true,
  159. Id: deleteZone.ID,
  160. }, nil
  161. }
  162. func (s *ZoneServer) ActivationCheck(ctx context.Context, requestCloudflare *zone.ActivationCheckRequestCloudflare) (*zone.ActivationCheckReplyCloudflare, error) {
  163. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  164. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  165. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  166. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  167. if err != nil {
  168. return nil, err
  169. }
  170. ctxAPI := context.Background()
  171. activationCheck, err := api.ZoneActivationCheck(ctxAPI, requestCloudflare.GetZoneId())
  172. if err != nil {
  173. return nil, err
  174. }
  175. return &zone.ActivationCheckReplyCloudflare{
  176. Success: activationCheck.Success,
  177. }, nil
  178. }
  179. var phaseMap = map[zone.Phase]string{
  180. zone.Phase_http_request_sanitize: "http_request_sanitize",
  181. zone.Phase_http_request_transform: "http_request_transform",
  182. //zone.Phase_http_request_origin: "http_request_origin",
  183. //zone.Phase_http_request_cache_settings: "http_request_cache_settings",
  184. //zone.Phase_http_config_settings: "http_config_settings",
  185. //zone.Phase_http_request_dynamic_redirect: "http_request_dynamic_redirect",
  186. //zone.Phase_ddos_l7: "ddos_l7",
  187. //zone.Phase_http_request_firewall_custom: "http_request_firewall_custom",
  188. //zone.Phase_http_ratelimit: "http_ratelimit",
  189. //zone.Phase_http_request_firewall_managed: "http_request_firewall_managed",
  190. //zone.Phase_http_request_sbfm: "http_request_sbfm",
  191. //zone.Phase_http_request_redirect: "http_request_redirect",
  192. zone.Phase_http_request_late_transform: "http_request_late_transform",
  193. //zone.Phase_http_custom_errors: "http_custom_errors",
  194. zone.Phase_http_response_headers_transform: "http_response_headers_transform",
  195. //zone.Phase_http_response_firewall_managed: "http_response_firewall_managed",
  196. //zone.Phase_http_log_custom_fields: "http_log_custom_fields",
  197. }
  198. func (s *ZoneServer) GetRuleSet(ctx context.Context, requestCloudflare *zone.GetRuleSetRequestCloudflare) (*zone.RuleList, error) {
  199. api, err := cloudflare.New(requestCloudflare.ApiKey, requestCloudflare.ApiEmail)
  200. if err != nil {
  201. return nil, err
  202. }
  203. ctxAPI := context.Background()
  204. switch phaseMap[requestCloudflare.Phase] {
  205. case "http_request_transform", "http_request_late_transform", "http_response_headers_transform":
  206. default:
  207. return nil, errors.New("未定义规则阶段")
  208. }
  209. ruleset, err := api.GetZoneRulesetPhase(ctxAPI, requestCloudflare.ZoneId, phaseMap[requestCloudflare.Phase])
  210. if err != nil {
  211. _, ok := err.(*cloudflare.NotFoundError)
  212. if ok {
  213. ruleset.Rules = make([]cloudflare.RulesetRule, 0)
  214. err = nil
  215. } else {
  216. return nil, err
  217. }
  218. }
  219. filterMap := lo.FilterMap[cloudflare.RulesetRule, *zone.Rule](ruleset.Rules, func(rule cloudflare.RulesetRule, _ int) (*zone.Rule, bool) {
  220. phase, err := Go2ProtoRuleRuleByPhase(&rule, ruleset.Phase)
  221. if err != nil {
  222. return nil, false
  223. }
  224. return phase, true
  225. })
  226. return &zone.RuleList{Rules: filterMap}, nil
  227. }
  228. func (s *ZoneServer) UpdateRuleSet(ctx context.Context, requestCloudflare *zone.UpdateRuleSetRequestCloudflare) (*zone.RuleList, error) {
  229. api, err := cloudflare.New(requestCloudflare.ApiKey, requestCloudflare.ApiEmail)
  230. if err != nil {
  231. return nil, err
  232. }
  233. ctxAPI := context.Background()
  234. phase := phaseMap[requestCloudflare.Phase]
  235. filterMap := lo.FilterMap[*zone.Rule, cloudflare.RulesetRule](requestCloudflare.Rules, func(rule *zone.Rule, index int) (cloudflare.RulesetRule, bool) {
  236. ruleResult, err := Proto2GoRulesetRuleByPhase(rule, requestCloudflare.Phase)
  237. if err != nil || ruleResult == nil {
  238. return cloudflare.RulesetRule{}, false
  239. }
  240. return *ruleResult, true
  241. })
  242. //for _, rule := range requestCloudflare.Rules {
  243. // ruleResult, err := Proto2GoRulesetRuleByPhase(rule, requestCloudflare.Phase)
  244. // if err != nil {
  245. // return nil, err
  246. // }
  247. // if ruleResult != nil {
  248. // ruleSet.Rules = append(ruleSet.Rules, *ruleResult)
  249. // }
  250. //}
  251. ruleset, err := api.UpdateZoneRulesetPhase(ctxAPI, requestCloudflare.ZoneId, phase, cloudflare.Ruleset{
  252. Rules: filterMap,
  253. })
  254. if err != nil {
  255. return nil, err
  256. }
  257. rulesReturn := lo.FilterMap[cloudflare.RulesetRule, *zone.Rule](ruleset.Rules, func(rule cloudflare.RulesetRule, _ int) (*zone.Rule, bool) {
  258. ruleReturn, err := Go2ProtoRuleRuleByPhase(&rule, ruleset.Phase)
  259. if err != nil {
  260. return nil, false
  261. }
  262. return ruleReturn, true
  263. })
  264. //var rulesReturn []*zone.Rule
  265. //for _, rule := range rulesetResult.Rules {
  266. // ruleReturn, err := Go2ProtoRuleRuleByPhase(&rule, rulesetResult.Phase)
  267. // if err != nil {
  268. // return nil, err
  269. // }
  270. // if ruleReturn != nil {
  271. // rulesReturn = append(rulesReturn, ruleReturn)
  272. // }
  273. //}
  274. return &zone.RuleList{Rules: rulesReturn}, nil
  275. }
  276. //func UpdateRuleSet(apiKey, apiEmail, zoneID, rulesetPhase string, ruleset cloudflare.Ruleset) (*cloudflare.Ruleset, error) {
  277. // api, err := cloudflare.New(apiKey, apiEmail)
  278. // if err != nil {
  279. // return nil, err
  280. // }
  281. // ctxAPI := context.Background()
  282. // phase, err := api.UpdateZoneRulesetPhase(ctxAPI, zoneID, rulesetPhase, ruleset)
  283. // if err != nil {
  284. // return nil, err
  285. // }
  286. // return &phase, nil
  287. //}
  288. //PageRule CURD
  289. func (s *ZoneServer) ListPageRule(ctx context.Context, requestCloudflare *zone.ListPageRuleRequestCloudflare) (*zone.PageRuleList, error) {
  290. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  291. if err != nil {
  292. return nil, err
  293. }
  294. ctxAPI := context.Background()
  295. rules, err := api.ListPageRules(ctxAPI, requestCloudflare.GetZoneId())
  296. if err != nil {
  297. return nil, err
  298. }
  299. filterMap := lo.FilterMap[cloudflare.PageRule, *zone.PageRule](rules, func(rule cloudflare.PageRule, _ int) (*zone.PageRule, bool) {
  300. pageRule := Go2ProtoPageRule(&rule)
  301. return pageRule, true
  302. })
  303. return &zone.PageRuleList{PageRules: filterMap}, nil
  304. }
  305. func (s *ZoneServer) CreatePageRule(ctx context.Context, requestCloudflare *zone.CreatePageRuleRequestCloudflare) (*zone.PageRule, error) {
  306. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  307. if err != nil {
  308. return nil, err
  309. }
  310. ctxAPI := context.Background()
  311. rule, err := Proto2GoPageRule(requestCloudflare.PageRule)
  312. if err != nil {
  313. return nil, err
  314. }
  315. pageRule, err := api.CreatePageRule(ctxAPI, requestCloudflare.GetZoneId(), *rule)
  316. if err != nil {
  317. return nil, err
  318. }
  319. return Go2ProtoPageRule(pageRule), nil
  320. }
  321. func (s *ZoneServer) UpdatePageRule(ctx context.Context, requestCloudflare *zone.UpdatePageRuleRequestCloudflare) (*zone.PageRule, error) {
  322. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  323. if err != nil {
  324. return nil, err
  325. }
  326. ctxAPI := context.Background()
  327. rule, err := Proto2GoPageRule(requestCloudflare.PageRule)
  328. if err != nil {
  329. return nil, err
  330. }
  331. err = api.UpdatePageRule(ctxAPI, requestCloudflare.GetZoneId(), rule.ID, *rule)
  332. if err != nil {
  333. return nil, err
  334. }
  335. return requestCloudflare.PageRule, nil
  336. }
  337. func (s *ZoneServer) DeletePageRule(ctx context.Context, requestCloudflare *zone.DeletePageRuleRequestCloudflare) (*zone.PageRule, error) {
  338. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  339. if err != nil {
  340. return nil, err
  341. }
  342. ctxAPI := context.Background()
  343. err = api.DeletePageRule(ctxAPI, requestCloudflare.ZoneId, requestCloudflare.PageRuleId)
  344. if err != nil {
  345. return nil, err
  346. }
  347. return &zone.PageRule{}, nil
  348. }
  349. //func UpdatePageRule(apiKey, apiEmail, zoneID string, rule *zone.PageRule) (*zone.PageRule, error) {
  350. // api, err := cloudflare.New(apiKey, apiEmail)
  351. // if err != nil {
  352. // return nil, err
  353. // }
  354. //
  355. // ctxAPI := context.Background()
  356. // goPageRule, err := Proto2GoPageRule(rule)
  357. // if err != nil {
  358. // return nil, err
  359. // }
  360. // pageRule, err := api.CreatePageRule(ctxAPI, zoneID, *goPageRule)
  361. // if err != nil {
  362. // return nil, err
  363. // }
  364. //
  365. // protoPageRule := Go2ProtoPageRule(pageRule)
  366. //
  367. // return protoPageRule, nil
  368. //}
  369. type PageRuleActionCacheKeyFieldsQueryString struct {
  370. Exclude interface{} `json:"exclude"`
  371. Include interface{} `json:"include"`
  372. }
  373. type PageRuleActionCacheKeyFields struct {
  374. QueryString PageRuleActionCacheKeyFieldsQueryString `json:"query_string"`
  375. }
  376. type UpdatePageRulesPrioritiesStruct struct {
  377. Id string `json:"id"`
  378. Priority int `json:"priority"`
  379. }
  380. func (s *ZoneServer) UpdatePageRulesPriorities(ctx context.Context, requestCloudflare *zone.UpdatePageRulesPrioritiesRequestCloudflare) (*zone.PageRuleList, error) {
  381. //TODO 参数检查
  382. //设置HTTP请求
  383. client := &http.Client{}
  384. bytesData, err := json.Marshal(requestCloudflare.Data)
  385. if err != nil {
  386. return nil, err
  387. }
  388. req, err := http.NewRequest("PUT", "https://api.cloudflare.com/client/v4/zones/"+requestCloudflare.ZoneId+"/pagerules/priorities", bytes.NewReader(bytesData))
  389. if err != nil {
  390. return nil, err
  391. }
  392. req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
  393. req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
  394. //执行HTTP请求
  395. resp, err := client.Do(req)
  396. if err != nil {
  397. return nil, err
  398. }
  399. defer func(Body io.ReadCloser) {
  400. _ = Body.Close()
  401. }(resp.Body)
  402. body, _ := ioutil.ReadAll(resp.Body)
  403. //解析HTTP返回数据
  404. var r cloudflare.PageRulesResponse
  405. err = json.Unmarshal(body, &r)
  406. //处理返回逻辑
  407. if err != nil {
  408. return nil, fmt.Errorf("%s: %w", "error unmarshalling the JSON response", err)
  409. }
  410. if r.Success != true {
  411. return nil, fmt.Errorf("%s", r.Errors)
  412. }
  413. rules := r.Result
  414. filterMap := lo.FilterMap[cloudflare.PageRule, *zone.PageRule](rules, func(rule cloudflare.PageRule, _ int) (*zone.PageRule, bool) {
  415. pageRule := Go2ProtoPageRule(&rule)
  416. return pageRule, true
  417. })
  418. return &zone.PageRuleList{PageRules: filterMap}, nil
  419. }