ZoneCloudflareServer.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. "strings"
  16. "time"
  17. )
  18. type ZoneServer struct {
  19. zone.UnimplementedZoneCloudflareServer
  20. }
  21. type Subscription struct {
  22. ID string `json:"id"`
  23. RatePlan struct {
  24. ID string `json:"id"`
  25. PublicName string `json:"public_name"`
  26. Currency string `json:"currency"`
  27. Scope string `json:"scope"`
  28. ExternallyManaged bool `json:"externally_managed"`
  29. Sets []string `json:"sets"`
  30. IsContract bool `json:"is_contract"`
  31. } `json:"rate_plan"`
  32. ComponentValues []struct {
  33. Name string `json:"name"`
  34. Value int `json:"value"`
  35. Default int `json:"default,omitempty"`
  36. } `json:"component_values"`
  37. Zone struct {
  38. ID string `json:"id"`
  39. Name string `json:"name"`
  40. } `json:"zone"`
  41. Frequency string `json:"frequency"`
  42. Currency string `json:"currency"`
  43. Price int `json:"price"`
  44. Product struct {
  45. Name string `json:"name"`
  46. Period string `json:"period"`
  47. Billing string `json:"billing"`
  48. PublicName string `json:"public_name"`
  49. Duration int `json:"duration"`
  50. } `json:"product"`
  51. CancelAtPeriodEnd bool `json:"cancel_at_period_end"`
  52. CreatedDate time.Time `json:"created_date"`
  53. }
  54. var ArgoSubscriptionId = "argo_zone_ent_tryout"
  55. const TOKEN_PREFIX = "#pt"
  56. const TOKEN_PREFIX_LENGTH = 3
  57. var (
  58. api *cloudflare.API
  59. err error
  60. )
  61. func (s *ZoneServer) GetArgoSettingSubscription(ctx context.Context, request *zone.IdRequest) (*zone.ArgoSetting, error) {
  62. //设置HTTP请求
  63. client := &http.Client{}
  64. req, err := http.NewRequest("GET", "https://api.cloudflare.com/client/v4/zones/"+request.ZoneId+"/subscriptions", nil)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  69. req.Header.Add("Authorization", ApiToken2Authorization(request.GetApiKey()[TOKEN_PREFIX_LENGTH:]))
  70. } else {
  71. req.Header.Add("X-Auth-Key", request.GetApiKey())
  72. req.Header.Add("X-Auth-Email", request.GetApiEmail())
  73. }
  74. //执行HTTP请求
  75. resp, err := client.Do(req)
  76. log.Printf("Cloudflare GetArgoSettingSubscription API response: %v err:%s", resp, err)
  77. if err != nil {
  78. return nil, err
  79. }
  80. defer func(Body io.ReadCloser) {
  81. _ = Body.Close()
  82. }(resp.Body)
  83. body, _ := ioutil.ReadAll(resp.Body)
  84. type ResponseType struct {
  85. Errors []interface{} `json:"errors"`
  86. Messages []interface{} `json:"messages"`
  87. Result []Subscription `json:"result"`
  88. ResultInfo struct {
  89. NextPage bool `json:"next_page"`
  90. } `json:"result_info"`
  91. Success bool `json:"success"`
  92. }
  93. var r ResponseType
  94. err = json.Unmarshal(body, &r)
  95. //处理返回逻辑
  96. if err != nil {
  97. return nil, fmt.Errorf("%s: %w", "error unmarshalling the JSON response", err)
  98. }
  99. if r.Success != true {
  100. return nil, fmt.Errorf("%s", r.Errors)
  101. }
  102. log.Printf("Response: %v", r)
  103. for _, v := range r.Result {
  104. if v.RatePlan.ID == ArgoSubscriptionId {
  105. return &zone.ArgoSetting{Enabled: true}, nil
  106. }
  107. }
  108. return &zone.ArgoSetting{Enabled: false}, nil
  109. }
  110. func (s *ZoneServer) CreateArgoSettingSubscription(ctx context.Context, request *zone.IdRequest) (*zone.ArgoSetting, error) {
  111. type RequestType struct {
  112. RatePlan struct {
  113. ID string `json:"id"`
  114. } `json:"rate_plan"`
  115. Zone struct {
  116. ID string `json:"id"`
  117. Name string `json:"name"`
  118. } `json:"zone"`
  119. }
  120. type ResponseType struct {
  121. Errors []interface{} `json:"errors"`
  122. Messages []interface{} `json:"messages"`
  123. Result Subscription `json:"result"`
  124. Success bool `json:"success"`
  125. }
  126. bytesData, err := json.Marshal(RequestType{
  127. RatePlan: struct {
  128. ID string `json:"id"`
  129. }{
  130. ID: ArgoSubscriptionId,
  131. },
  132. Zone: struct {
  133. ID string `json:"id"`
  134. Name string `json:"name"`
  135. }{
  136. ID: request.ZoneId,
  137. },
  138. })
  139. if err != nil {
  140. return nil, err
  141. }
  142. //设置HTTP请求
  143. client := &http.Client{}
  144. req, err := http.NewRequest("POST", "https://api.cloudflare.com/client/v4/zones/"+request.ZoneId+"/subscription", bytes.NewReader(bytesData))
  145. if err != nil {
  146. return nil, err
  147. }
  148. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  149. req.Header.Add("Authorization", ApiToken2Authorization(request.GetApiKey()[TOKEN_PREFIX_LENGTH:]))
  150. } else {
  151. req.Header.Add("X-Auth-Key", request.GetApiKey())
  152. req.Header.Add("X-Auth-Email", request.GetApiEmail())
  153. }
  154. //执行HTTP请求
  155. resp, err := client.Do(req)
  156. log.Printf("Cloudflare CreateArgoSettingSubscription API response: %v err:%s", resp, err)
  157. if err != nil {
  158. return nil, err
  159. }
  160. defer func(Body io.ReadCloser) {
  161. _ = Body.Close()
  162. }(resp.Body)
  163. body, _ := ioutil.ReadAll(resp.Body)
  164. var r ResponseType
  165. err = json.Unmarshal(body, &r)
  166. //处理返回逻辑
  167. if err != nil {
  168. return nil, fmt.Errorf("%s: %w", "error unmarshalling the JSON response", err)
  169. }
  170. if r.Success != true {
  171. return nil, fmt.Errorf("%s", r.Errors)
  172. }
  173. log.Printf("Response: %v", r)
  174. if r.Result.RatePlan.ID == ArgoSubscriptionId {
  175. return &zone.ArgoSetting{Enabled: true}, nil
  176. }
  177. return &zone.ArgoSetting{Enabled: false}, nil
  178. }
  179. func (s *ZoneServer) UpdateArgoSetting(ctx context.Context, requestCloudflare *zone.UpdateArgoRequestCloudflare) (*zone.ArgoSetting, error) {
  180. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  181. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  182. } else {
  183. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  184. }
  185. if err != nil {
  186. return nil, err
  187. }
  188. settingValue := Proto2GoArgoSetting(requestCloudflare.GetArgoSetting())
  189. setting, err := api.UpdateArgoSmartRouting(ctx, requestCloudflare.ZoneId, settingValue)
  190. log.Printf("Cloudflare UpdateArgoSmartRouting API response: %v err:%s", setting, err)
  191. if err != nil {
  192. return nil, err
  193. }
  194. return Go2ProtoArgoSetting(setting.Value), nil
  195. }
  196. func (s *ZoneServer) GetArgoSetting(ctx context.Context, requestCloudflare *zone.IdRequest) (*zone.ArgoSetting, error) {
  197. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  198. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  199. } else {
  200. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  201. }
  202. if err != nil {
  203. return nil, err
  204. }
  205. setting, err := api.ArgoSmartRouting(ctx, requestCloudflare.ZoneId)
  206. log.Printf("Cloudflare ArgoSmartRouting API response: %v err:%s", setting, err)
  207. if err != nil {
  208. return nil, err
  209. }
  210. return Go2ProtoArgoSetting(setting.Value), nil
  211. }
  212. func (s *ZoneServer) ZoneIDByNameCloudflare(ctx context.Context, requestCloudflare *zone.ZoneIDByNameRequestCloudflare) (*zone.ZoneIDByNameReplyCloudflare, error) {
  213. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  214. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  215. } else {
  216. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  217. }
  218. if err != nil {
  219. return nil, err
  220. }
  221. id, err := api.ZoneIDByName(requestCloudflare.ZoneName)
  222. log.Printf("Cloudflare ZoneIDByName API response: %v err:%s", id, err)
  223. if err != nil {
  224. return nil, err
  225. }
  226. return &zone.ZoneIDByNameReplyCloudflare{ZoneId: id}, err
  227. }
  228. func (s *ZoneServer) ZoneDetails(ctx context.Context, requestCloudflare *zone.ZoneDetailsRequestCloudflare) (*zone.ZoneDetailsReplyCloudflare, error) {
  229. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  230. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  231. } else {
  232. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  233. }
  234. if err != nil {
  235. return nil, err
  236. }
  237. ctxAPI := context.Background()
  238. details, err := api.ZoneDetails(ctxAPI, requestCloudflare.GetZoneId())
  239. log.Printf("Cloudflare ZoneDetails API response: %v err:%s", details, err)
  240. if err != nil {
  241. return nil, err
  242. }
  243. return &zone.ZoneDetailsReplyCloudflare{
  244. Result: Go2ProtoZone(details),
  245. }, err
  246. }
  247. func (s *ZoneServer) GetZones(ctx context.Context, requestCloudflare *zone.GetZonesRequestCloudflare) (*zone.GetZonesReplyCloudflare, error) {
  248. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  249. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  250. } else {
  251. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  252. }
  253. if err != nil {
  254. return nil, err
  255. }
  256. details, err := api.ListZones(ctx, "name", requestCloudflare.GetDomain())
  257. log.Printf("Cloudflare GetZones API response: %v err:%s", details, err)
  258. if err != nil {
  259. return nil, err
  260. }
  261. list := make([]*zone.ZoneCloudflareEntity, len(details))
  262. for i, detail := range details {
  263. list[i] = Go2ProtoZone(detail)
  264. }
  265. return &zone.GetZonesReplyCloudflare{
  266. Result: list,
  267. Success: true,
  268. }, err
  269. }
  270. func (s *ZoneServer) ListZones(ctx context.Context, requestCloudflare *zone.ListZonesRequestCloudflare) (*zone.ZonesList, error) {
  271. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  272. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  273. } else {
  274. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  275. }
  276. if err != nil {
  277. return nil, fmt.Errorf("failed to initialize Cloudflare API: %w", err)
  278. }
  279. ctxAPI := context.Background()
  280. details, err := api.ListZones(ctxAPI)
  281. log.Printf("Cloudflare ListZones API response: %v err:%s", details, err)
  282. if err != nil {
  283. return nil, err
  284. }
  285. list := make([]*zone.ZoneCloudflareEntity, len(details))
  286. for i, detail := range details {
  287. list[i] = Go2ProtoZone(detail)
  288. }
  289. return &zone.ZonesList{
  290. Result: list,
  291. }, err
  292. }
  293. func (s *ZoneServer) ListZonesByAccountId(ctx context.Context, requestCloudflare *zone.ListZonesByAccountIdRequestCloudflare) (*zone.ZonesList, error) {
  294. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  295. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  296. } else {
  297. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  298. }
  299. if err != nil {
  300. return nil, err
  301. }
  302. ctxAPI := context.Background()
  303. opt := cloudflare.WithZoneFilters("", requestCloudflare.AccountId, "")
  304. details, err := api.ListZonesContext(ctxAPI, opt)
  305. log.Printf("Cloudflare ListZonesByAccountId API response: %v err:%s", details, err)
  306. if err != nil {
  307. return nil, err
  308. }
  309. list := make([]*zone.ZoneCloudflareEntity, len(details.Result))
  310. for i, detail := range details.Result {
  311. list[i] = Go2ProtoZone(detail)
  312. }
  313. return &zone.ZonesList{
  314. Result: list,
  315. }, err
  316. }
  317. func (s *ZoneServer) EditZone(ctx context.Context, requestCloudflare *zone.EditZoneRequestCloudflare) (*zone.EditZoneReplyCloudflare, error) {
  318. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  319. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  320. } else {
  321. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  322. }
  323. if err != nil {
  324. return nil, err
  325. }
  326. ctxAPI := context.Background()
  327. opts := cloudflare.ZoneOptions{}
  328. opts.Paused = requestCloudflare.Paused
  329. if requestCloudflare.PlanId != nil {
  330. opts.Plan = &cloudflare.ZonePlan{
  331. ZonePlanCommon: cloudflare.ZonePlanCommon{
  332. ID: requestCloudflare.GetPlanId(),
  333. },
  334. }
  335. }
  336. if requestCloudflare.Type != nil {
  337. opts.Type = requestCloudflare.GetType()
  338. }
  339. editZone, err := api.EditZone(ctxAPI, requestCloudflare.GetZoneId(), opts)
  340. log.Printf("Cloudflare EditZone API response: %v err:%s", editZone, err)
  341. if err != nil {
  342. return nil, err
  343. }
  344. return &zone.EditZoneReplyCloudflare{
  345. Result: Go2ProtoZone(editZone),
  346. Success: true,
  347. }, nil
  348. }
  349. func (s *ZoneServer) DeleteZone(ctx context.Context, requestCloudflare *zone.DeleteZoneRequestCloudflare) (*zone.DeleteZoneReplyCloudflare, error) {
  350. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  351. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  352. } else {
  353. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  354. }
  355. if err != nil {
  356. return nil, err
  357. }
  358. ctxAPI := context.Background()
  359. deleteZone, err := api.DeleteZone(ctxAPI, requestCloudflare.GetZoneId())
  360. log.Printf("Cloudflare DeleteZone API response: %v err:%s", deleteZone, err)
  361. if err != nil {
  362. return nil, err
  363. }
  364. return &zone.DeleteZoneReplyCloudflare{
  365. Success: true,
  366. Id: deleteZone.ID,
  367. }, nil
  368. }
  369. func (s *ZoneServer) CreateZone(ctx context.Context, requestCloudflare *zone.CreateZoneRequestCloudflare) (*zone.CreateZoneReplyCloudflare, error) {
  370. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  371. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  372. } else {
  373. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  374. }
  375. if err != nil {
  376. return nil, err
  377. }
  378. ctxAPI := context.Background()
  379. account, _, err := api.Account(ctxAPI, requestCloudflare.GetAccountId())
  380. log.Printf("Cloudflare Account API response: %v err:%s", account, err)
  381. if err != nil {
  382. return nil, err
  383. }
  384. createZone, err := api.CreateZone(ctxAPI, requestCloudflare.GetDomain(), false, account, "full")
  385. log.Printf("Cloudflare CreateZone API response: %v err:%s", createZone, err)
  386. if err != nil {
  387. return nil, err
  388. }
  389. return &zone.CreateZoneReplyCloudflare{
  390. Success: true,
  391. Id: createZone.ID,
  392. }, nil
  393. }
  394. func (s *ZoneServer) SetZonePlan(ctx context.Context, requestCloudflare *zone.SetZonePlanRequestCloudflare) (*zone.SetZonePlanReplyCloudflare, error) {
  395. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  396. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  397. } else {
  398. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  399. }
  400. if err != nil {
  401. return nil, err
  402. }
  403. ctxAPI := context.Background()
  404. err = api.ZoneSetPlan(ctxAPI, requestCloudflare.GetZoneId(), requestCloudflare.GetZonePlan())
  405. log.Printf("Cloudflare SetZonePlan API err:%s", err)
  406. if err != nil {
  407. return nil, err
  408. }
  409. return &zone.SetZonePlanReplyCloudflare{
  410. Success: true,
  411. }, nil
  412. }
  413. func (s *ZoneServer) ActivationCheck(ctx context.Context, requestCloudflare *zone.ActivationCheckRequestCloudflare) (*zone.ActivationCheckReplyCloudflare, error) {
  414. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  415. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  416. } else {
  417. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  418. }
  419. if err != nil {
  420. return nil, err
  421. }
  422. ctxAPI := context.Background()
  423. activationCheck, err := api.ZoneActivationCheck(ctxAPI, requestCloudflare.GetZoneId())
  424. log.Printf("Cloudflare ZoneActivationCheck API response: %v err:%s", activationCheck, err)
  425. if err != nil {
  426. return nil, err
  427. }
  428. return &zone.ActivationCheckReplyCloudflare{
  429. Success: activationCheck.Success,
  430. }, nil
  431. }
  432. var phaseMap = map[zone.Phase]string{
  433. zone.Phase_http_request_sanitize: "http_request_sanitize",
  434. zone.Phase_http_request_transform: "http_request_transform",
  435. zone.Phase_http_request_origin: "http_request_origin",
  436. zone.Phase_http_request_cache_settings: "http_request_cache_settings",
  437. zone.Phase_http_config_settings: "http_config_settings",
  438. //zone.Phase_http_request_dynamic_redirect: "http_request_dynamic_redirect",
  439. //zone.Phase_ddos_l7: "ddos_l7",
  440. //zone.Phase_http_request_firewall_custom: "http_request_firewall_custom",
  441. //zone.Phase_http_ratelimit: "http_ratelimit",
  442. //zone.Phase_http_request_firewall_managed: "http_request_firewall_managed",
  443. //zone.Phase_http_request_sbfm: "http_request_sbfm",
  444. //zone.Phase_http_request_redirect: "http_request_redirect",
  445. zone.Phase_http_request_late_transform: "http_request_late_transform",
  446. //zone.Phase_http_custom_errors: "http_custom_errors",
  447. zone.Phase_http_response_headers_transform: "http_response_headers_transform",
  448. //zone.Phase_http_response_firewall_managed: "http_response_firewall_managed",
  449. //zone.Phase_http_log_custom_fields: "http_log_custom_fields",
  450. }
  451. func (s *ZoneServer) ListZoneRuleSets(ctx context.Context, requestCloudflare *zone.ListZoneRuleSetsRequestCloudflare) (*zone.RuleSetList, error) {
  452. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  453. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  454. } else {
  455. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  456. }
  457. if err != nil {
  458. return nil, err
  459. }
  460. ctxAPI := context.Background()
  461. zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
  462. ListRulesetsParams := cloudflare.ListRulesetsParams{}
  463. goRuleSets, err := api.ListRulesets(ctxAPI, zoneIdentifier, ListRulesetsParams)
  464. log.Printf("Cloudflare ListZoneRuleSets API response: %v err:%s", goRuleSets, err)
  465. if err != nil {
  466. return nil, err
  467. }
  468. filterMap := lo.FilterMap[cloudflare.Ruleset, *zone.RuleSet](goRuleSets, func(goRuleSet cloudflare.Ruleset, _ int) (*zone.RuleSet, bool) {
  469. protoRuleSet := Go2ProtoRuleSet(&goRuleSet)
  470. return protoRuleSet, true
  471. })
  472. return &zone.RuleSetList{RuleSets: filterMap}, nil
  473. }
  474. func (s *ZoneServer) CreateZoneRuleSet(ctx context.Context, requestCloudflare *zone.CreateZoneRuleSetRequestCloudflare) (*zone.CreateZoneRuleSetReplyCloudflare, error) {
  475. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  476. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  477. } else {
  478. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  479. }
  480. if err != nil {
  481. return nil, err
  482. }
  483. ctxAPI := context.Background()
  484. filterMap := lo.FilterMap[*zone.Rule, cloudflare.RulesetRule](requestCloudflare.Rules, func(rule *zone.Rule, index int) (cloudflare.RulesetRule, bool) {
  485. ruleResult, err := Proto2GoRulesetRuleByPhase(rule, requestCloudflare.RuleSet.Phase)
  486. if err != nil || ruleResult == nil {
  487. return cloudflare.RulesetRule{}, false
  488. }
  489. return *ruleResult, true
  490. })
  491. zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
  492. createRulesetParams := cloudflare.CreateRulesetParams{
  493. Name: requestCloudflare.RuleSet.Name,
  494. Kind: requestCloudflare.RuleSet.Kind,
  495. Phase: phaseMap[requestCloudflare.RuleSet.Phase],
  496. Rules: filterMap,
  497. }
  498. _, err = api.CreateRuleset(ctxAPI, zoneIdentifier, createRulesetParams)
  499. log.Printf("Cloudflare CreateRuleset API err:%s", err)
  500. if err != nil {
  501. return &zone.CreateZoneRuleSetReplyCloudflare{Success: false}, err
  502. }
  503. //protoRuleSet := Go2ProtoRuleSet(&goRuleSet)
  504. return &zone.CreateZoneRuleSetReplyCloudflare{Success: true}, nil
  505. }
  506. func (s *ZoneServer) DeleteZoneRuleSet(ctx context.Context, requestCloudflare *zone.DeleteZoneRuleSetRequestCloudflare) (*zone.DeleteZoneRuleSetReplyCloudflare, error) {
  507. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  508. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  509. } else {
  510. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  511. }
  512. if err != nil {
  513. return nil, err
  514. }
  515. ctxAPI := context.Background()
  516. zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
  517. err = api.DeleteRuleset(ctxAPI, zoneIdentifier, requestCloudflare.GetRuleSetId())
  518. log.Printf("Cloudflare DeleteRuleset API err:%s", err)
  519. if err != nil {
  520. return &zone.DeleteZoneRuleSetReplyCloudflare{Success: false}, err
  521. }
  522. return &zone.DeleteZoneRuleSetReplyCloudflare{Success: true}, nil
  523. }
  524. func (s *ZoneServer) UpdateRuleSetByRuleSetId(ctx context.Context, requestCloudflare *zone.UpdateRuleSetByRuleSetIdRequestCloudflare) (*zone.RuleSet, error) {
  525. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  526. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  527. } else {
  528. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  529. }
  530. if err != nil {
  531. return nil, err
  532. }
  533. ctxAPI := context.Background()
  534. filterMap := lo.FilterMap[*zone.Rule, cloudflare.RulesetRule](requestCloudflare.Rules, func(rule *zone.Rule, index int) (cloudflare.RulesetRule, bool) {
  535. ruleResult, err := Proto2GoRulesetRuleByPhase(rule, requestCloudflare.Phase)
  536. if err != nil || ruleResult == nil {
  537. return cloudflare.RulesetRule{}, false
  538. }
  539. return *ruleResult, true
  540. })
  541. zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
  542. updateRulesetParams := cloudflare.UpdateRulesetParams{
  543. ID: requestCloudflare.RuleSetId,
  544. Rules: filterMap,
  545. }
  546. goRuleSet, err := api.UpdateRuleset(ctxAPI, zoneIdentifier, updateRulesetParams)
  547. log.Printf("Cloudflare UpdateRuleset API response: %v err:%s", goRuleSet, err)
  548. if err != nil {
  549. return nil, err
  550. }
  551. protoRuleSet := Go2ProtoRuleSet(&goRuleSet)
  552. return protoRuleSet, nil
  553. }
  554. func (s *ZoneServer) GetRuleSet(ctx context.Context, requestCloudflare *zone.GetRuleSetRequestCloudflare) (*zone.RuleList, error) {
  555. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  556. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  557. } else {
  558. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  559. }
  560. if err != nil {
  561. return nil, err
  562. }
  563. ctxAPI := context.Background()
  564. switch phaseMap[requestCloudflare.Phase] {
  565. case "http_request_origin", "http_request_cache_settings", "http_config_settings":
  566. case "http_request_transform", "http_request_late_transform", "http_response_headers_transform":
  567. default:
  568. return nil, errors.New("未定义规则阶段")
  569. }
  570. zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
  571. ruleset, err := api.GetEntrypointRuleset(ctxAPI, zoneIdentifier, phaseMap[requestCloudflare.Phase])
  572. log.Printf("Cloudflare GetEntrypointRuleset API response: %v err:%s", ruleset, err)
  573. if err != nil {
  574. _, ok := err.(*cloudflare.NotFoundError)
  575. if ok {
  576. ruleset.Rules = make([]cloudflare.RulesetRule, 0)
  577. err = nil
  578. } else {
  579. return nil, err
  580. }
  581. }
  582. filterMap := lo.FilterMap[cloudflare.RulesetRule, *zone.Rule](ruleset.Rules, func(rule cloudflare.RulesetRule, _ int) (*zone.Rule, bool) {
  583. phase, err := Go2ProtoRuleRuleByPhase(&rule, ruleset.Phase)
  584. if err != nil {
  585. return nil, false
  586. }
  587. return phase, true
  588. })
  589. return &zone.RuleList{Rules: filterMap}, nil
  590. }
  591. func (s *ZoneServer) UpdateRuleSet(ctx context.Context, requestCloudflare *zone.UpdateRuleSetRequestCloudflare) (*zone.RuleList, error) {
  592. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  593. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  594. } else {
  595. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  596. }
  597. if err != nil {
  598. return nil, err
  599. }
  600. ctxAPI := context.Background()
  601. phase := phaseMap[requestCloudflare.Phase]
  602. filterMap := lo.FilterMap[*zone.Rule, cloudflare.RulesetRule](requestCloudflare.Rules, func(rule *zone.Rule, index int) (cloudflare.RulesetRule, bool) {
  603. ruleResult, err := Proto2GoRulesetRuleByPhase(rule, requestCloudflare.Phase)
  604. if err != nil || ruleResult == nil {
  605. return cloudflare.RulesetRule{}, false
  606. }
  607. return *ruleResult, true
  608. })
  609. //for _, rule := range requestCloudflare.Rules {
  610. // ruleResult, err := Proto2GoRulesetRuleByPhase(rule, requestCloudflare.Phase)
  611. // if err != nil {
  612. // return nil, err
  613. // }
  614. // if ruleResult != nil {
  615. // ruleSet.Rules = append(ruleSet.Rules, *ruleResult)
  616. // }
  617. //}
  618. zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
  619. updateEntrypointRulesetParams := cloudflare.UpdateEntrypointRulesetParams{
  620. Phase: phase,
  621. Rules: filterMap,
  622. }
  623. ruleset, err := api.UpdateEntrypointRuleset(ctxAPI, zoneIdentifier, updateEntrypointRulesetParams)
  624. log.Printf("Cloudflare UpdateEntrypointRuleset API response: %v err:%s", ruleset, err)
  625. if err != nil {
  626. return nil, err
  627. }
  628. rulesReturn := lo.FilterMap[cloudflare.RulesetRule, *zone.Rule](ruleset.Rules, func(rule cloudflare.RulesetRule, _ int) (*zone.Rule, bool) {
  629. ruleReturn, err := Go2ProtoRuleRuleByPhase(&rule, ruleset.Phase)
  630. if err != nil {
  631. return nil, false
  632. }
  633. return ruleReturn, true
  634. })
  635. //var rulesReturn []*zone.Rule
  636. //for _, rule := range rulesetResult.Rules {
  637. // ruleReturn, err := Go2ProtoRuleRuleByPhase(&rule, rulesetResult.Phase)
  638. // if err != nil {
  639. // return nil, err
  640. // }
  641. // if ruleReturn != nil {
  642. // rulesReturn = append(rulesReturn, ruleReturn)
  643. // }
  644. //}
  645. return &zone.RuleList{Rules: rulesReturn}, nil
  646. }
  647. //func UpdateRuleSet(apiKey, apiEmail, zoneID, rulesetPhase string, ruleset cloudflare.Ruleset) (*cloudflare.Ruleset, error) {
  648. // api, err := cloudflare.New(apiKey, apiEmail)
  649. // if err != nil {
  650. // return nil, err
  651. // }
  652. // ctxAPI := context.Background()
  653. // phase, err := api.UpdateZoneRulesetPhase(ctxAPI, zoneID, rulesetPhase, ruleset)
  654. // if err != nil {
  655. // return nil, err
  656. // }
  657. // return &phase, nil
  658. //}
  659. //PageRule CURD
  660. func (s *ZoneServer) ListPageRule(ctx context.Context, requestCloudflare *zone.ListPageRuleRequestCloudflare) (*zone.PageRuleList, error) {
  661. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  662. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  663. } else {
  664. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  665. }
  666. if err != nil {
  667. return nil, err
  668. }
  669. ctxAPI := context.Background()
  670. rules, err := api.ListPageRules(ctxAPI, requestCloudflare.GetZoneId())
  671. log.Printf("Cloudflare ListPageRules API response: %v err:%s", rules, err)
  672. if err != nil {
  673. return nil, err
  674. }
  675. filterMap := lo.FilterMap[cloudflare.PageRule, *zone.PageRule](rules, func(rule cloudflare.PageRule, _ int) (*zone.PageRule, bool) {
  676. pageRule := Go2ProtoPageRule(&rule)
  677. return pageRule, true
  678. })
  679. return &zone.PageRuleList{PageRules: filterMap}, nil
  680. }
  681. func (s *ZoneServer) CreatePageRule(ctx context.Context, requestCloudflare *zone.CreatePageRuleRequestCloudflare) (*zone.PageRule, error) {
  682. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  683. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  684. } else {
  685. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  686. }
  687. if err != nil {
  688. return nil, err
  689. }
  690. ctxAPI := context.Background()
  691. rule, err := Proto2GoPageRule(requestCloudflare.PageRule)
  692. if err != nil {
  693. return nil, err
  694. }
  695. pageRule, err := api.CreatePageRule(ctxAPI, requestCloudflare.GetZoneId(), *rule)
  696. log.Printf("Cloudflare CreatePageRule API response: %v err:%s", pageRule, err)
  697. if err != nil {
  698. return nil, err
  699. }
  700. return Go2ProtoPageRule(pageRule), nil
  701. }
  702. func (s *ZoneServer) UpdatePageRule(ctx context.Context, requestCloudflare *zone.UpdatePageRuleRequestCloudflare) (*zone.PageRule, error) {
  703. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  704. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  705. } else {
  706. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  707. }
  708. if err != nil {
  709. return nil, err
  710. }
  711. ctxAPI := context.Background()
  712. rule, err := Proto2GoPageRule(requestCloudflare.PageRule)
  713. if err != nil {
  714. return nil, err
  715. }
  716. err = api.UpdatePageRule(ctxAPI, requestCloudflare.GetZoneId(), rule.ID, *rule)
  717. log.Printf("Cloudflare UpdatePageRule API err:%s", err)
  718. if err != nil {
  719. return nil, err
  720. }
  721. return requestCloudflare.PageRule, nil
  722. }
  723. func (s *ZoneServer) DeletePageRule(ctx context.Context, requestCloudflare *zone.DeletePageRuleRequestCloudflare) (*zone.PageRule, error) {
  724. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  725. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  726. } else {
  727. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  728. }
  729. if err != nil {
  730. return nil, err
  731. }
  732. ctxAPI := context.Background()
  733. err = api.DeletePageRule(ctxAPI, requestCloudflare.ZoneId, requestCloudflare.PageRuleId)
  734. log.Printf("Cloudflare DeletePageRule API err:%s", err)
  735. if err != nil {
  736. return nil, err
  737. }
  738. return &zone.PageRule{}, nil
  739. }
  740. //func UpdatePageRule(apiKey, apiEmail, zoneID string, rule *zone.PageRule) (*zone.PageRule, error) {
  741. // api, err := cloudflare.New(apiKey, apiEmail)
  742. // if err != nil {
  743. // return nil, err
  744. // }
  745. //
  746. // ctxAPI := context.Background()
  747. // goPageRule, err := Proto2GoPageRule(rule)
  748. // if err != nil {
  749. // return nil, err
  750. // }
  751. // pageRule, err := api.CreatePageRule(ctxAPI, zoneID, *goPageRule)
  752. // if err != nil {
  753. // return nil, err
  754. // }
  755. //
  756. // protoPageRule := Go2ProtoPageRule(pageRule)
  757. //
  758. // return protoPageRule, nil
  759. //}
  760. type PageRuleActionCacheKeyFieldsQueryString struct {
  761. Exclude interface{} `json:"exclude"`
  762. Include interface{} `json:"include"`
  763. }
  764. type PageRuleActionCacheKeyFields struct {
  765. QueryString PageRuleActionCacheKeyFieldsQueryString `json:"query_string"`
  766. }
  767. type UpdatePageRulesPrioritiesStruct struct {
  768. Id string `json:"id"`
  769. Priority int `json:"priority"`
  770. }
  771. func (s *ZoneServer) UpdatePageRulesPriorities(ctx context.Context, requestCloudflare *zone.UpdatePageRulesPrioritiesRequestCloudflare) (*zone.PageRuleList, error) {
  772. //TODO 参数检查
  773. //设置HTTP请求
  774. client := &http.Client{}
  775. bytesData, err := json.Marshal(requestCloudflare.Data)
  776. if err != nil {
  777. return nil, err
  778. }
  779. req, err := http.NewRequest("PUT", "https://api.cloudflare.com/client/v4/zones/"+requestCloudflare.ZoneId+"/pagerules/priorities", bytes.NewReader(bytesData))
  780. if err != nil {
  781. return nil, err
  782. }
  783. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  784. req.Header.Add("Authorization", ApiToken2Authorization(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:]))
  785. } else {
  786. req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
  787. req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
  788. }
  789. //执行HTTP请求
  790. resp, err := client.Do(req)
  791. log.Printf("Cloudflare UpdatePageRulesPriorities API response: %v err:%s", resp, err)
  792. if err != nil {
  793. return nil, err
  794. }
  795. defer func(Body io.ReadCloser) {
  796. _ = Body.Close()
  797. }(resp.Body)
  798. body, _ := ioutil.ReadAll(resp.Body)
  799. //解析HTTP返回数据
  800. var r cloudflare.PageRulesResponse
  801. err = json.Unmarshal(body, &r)
  802. //处理返回逻辑
  803. if err != nil {
  804. return nil, fmt.Errorf("%s: %w", "error unmarshalling the JSON response", err)
  805. }
  806. if r.Success != true {
  807. return nil, fmt.Errorf("%s", r.Errors)
  808. }
  809. rules := r.Result
  810. filterMap := lo.FilterMap[cloudflare.PageRule, *zone.PageRule](rules, func(rule cloudflare.PageRule, _ int) (*zone.PageRule, bool) {
  811. pageRule := Go2ProtoPageRule(&rule)
  812. return pageRule, true
  813. })
  814. return &zone.PageRuleList{PageRules: filterMap}, nil
  815. }
  816. // 默认开启Smarttiered cache
  817. func (s *ZoneServer) SetSmartTieredCache(ctx context.Context, requestCloudflare *zone.SetSmartTieredCacheRequestCloudflare) (*zone.SetSmartTieredCacheReplyCloudflare, error) {
  818. type ResponseType struct {
  819. Errors []interface{} `json:"errors"`
  820. Messages []interface{} `json:"messages"`
  821. Result interface{} `json:"result"`
  822. Success bool `json:"success"`
  823. }
  824. //设置HTTP请求
  825. client := &http.Client{}
  826. url := "https://api.cloudflare.com/client/v4/zones/" + requestCloudflare.ZoneId + "/cache/tiered_cache_smart_topology_enable"
  827. payload := strings.NewReader("{\n \"value\": \"on\"\n}")
  828. req, err := http.NewRequest("PATCH", url, payload)
  829. if err != nil {
  830. return nil, err
  831. }
  832. req.Header.Add("Content-Type", "application/json")
  833. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  834. req.Header.Add("Authorization", ApiToken2Authorization(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:]))
  835. } else {
  836. req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
  837. req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
  838. }
  839. //执行HTTP请求
  840. resp, err := client.Do(req)
  841. log.Printf("Cloudflare SetSmartTieredCache API response: %v err:%s", resp, err)
  842. if err != nil {
  843. return nil, err
  844. }
  845. defer func(Body io.ReadCloser) {
  846. _ = Body.Close()
  847. }(resp.Body)
  848. body, _ := ioutil.ReadAll(resp.Body)
  849. print(body)
  850. //解析HTTP返回数据
  851. var r ResponseType
  852. err = json.Unmarshal(body, &r)
  853. if err != nil {
  854. return nil, fmt.Errorf("%s: %w", "error unmarshalling the JSON response", err)
  855. }
  856. if r.Success != true {
  857. print(fmt.Errorf("%s", r.Errors))
  858. }
  859. return &zone.SetSmartTieredCacheReplyCloudflare{Enabled: r.Success}, nil
  860. }
  861. // 开启强制使用https
  862. func (s *ZoneServer) UpdateAlwaysUseHttps(ctx context.Context, requestCloudflare *zone.UpdateAlwaysUseHttpsRequestCloudflare) (*zone.UpdateAlwaysUseHttpsReplyCloudflare, error) {
  863. type ResponseType struct {
  864. Errors []interface{} `json:"errors"`
  865. Messages []interface{} `json:"messages"`
  866. Result interface{} `json:"result"`
  867. Success bool `json:"success"`
  868. }
  869. //设置HTTP请求
  870. client := &http.Client{}
  871. url := "https://api.cloudflare.com/client/v4/zones/" + requestCloudflare.ZoneId + "/settings/always_use_https"
  872. var payload io.Reader
  873. if requestCloudflare.Enabled {
  874. payload = strings.NewReader("{\n \"value\": \"on\"\n}")
  875. } else {
  876. payload = strings.NewReader("{\n \"value\": \"off\"\n}")
  877. }
  878. req, err := http.NewRequest("PATCH", url, payload)
  879. if err != nil {
  880. return nil, err
  881. }
  882. req.Header.Add("Content-Type", "application/json")
  883. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  884. req.Header.Add("Authorization", ApiToken2Authorization(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:]))
  885. } else {
  886. req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
  887. req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
  888. }
  889. //执行HTTP请求
  890. resp, err := client.Do(req)
  891. log.Printf("Cloudflare UpdateAlwaysUseHttps API response: %v err:%s", resp, err)
  892. if err != nil {
  893. return nil, err
  894. }
  895. defer func(Body io.ReadCloser) {
  896. _ = Body.Close()
  897. }(resp.Body)
  898. body, _ := ioutil.ReadAll(resp.Body)
  899. print(body)
  900. //解析HTTP返回数据
  901. var r ResponseType
  902. err = json.Unmarshal(body, &r)
  903. if err != nil {
  904. return nil, fmt.Errorf("%s: %w", "error unmarshalling the JSON response", err)
  905. }
  906. if r.Success != true {
  907. print(fmt.Errorf("%s", r.Errors))
  908. }
  909. return &zone.UpdateAlwaysUseHttpsReplyCloudflare{Success: r.Success}, nil
  910. }