CertificateCloudflareServer.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package server
  2. import (
  3. cert "cfTest/cloudflareApi/certificate"
  4. "context"
  5. "github.com/cloudflare/cloudflare-go"
  6. )
  7. type CertificateServer struct {
  8. cert.UnimplementedCertificateCloudflareServer
  9. }
  10. func (s *CertificateServer) UniversalCertificate(ctx context.Context, requestCloudflare *cert.UniversalCertificateRequestCloudflare) (*cert.UniversalCertificateReplyCloudflare, error) {
  11. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  12. if err != nil {
  13. return nil, err
  14. }
  15. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  16. if err != nil {
  17. return nil, err
  18. }
  19. ctxAPI := context.Background()
  20. editUniversalSSLSettings := cloudflare.UniversalSSLSetting{Enabled: requestCloudflare.GetEnable()}
  21. universalCertificate, err := api.EditUniversalSSLSetting(ctxAPI, id, editUniversalSSLSettings)
  22. if err != nil {
  23. return &cert.UniversalCertificateReplyCloudflare{
  24. Success: false, Enabled: false,
  25. }, err
  26. }
  27. return &cert.UniversalCertificateReplyCloudflare{
  28. Success: true,
  29. Enabled: universalCertificate.Enabled,
  30. }, err
  31. }
  32. func (s *CertificateServer) CreateCertificate(ctx context.Context, requestCloudflare *cert.CreateCertificateRequestCloudflare) (*cert.CreateCertificateReplyCloudflare, error) {
  33. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  34. if err != nil {
  35. return nil, err
  36. }
  37. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  38. if err != nil {
  39. return nil, err
  40. }
  41. ctxAPI := context.Background()
  42. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  43. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  44. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  45. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  46. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  47. zoneCustomSSL.BundleMethod = "force"
  48. zoneCustomSSL.Type = "sni_custom"
  49. createCertificate, err := api.CreateSSL(ctxAPI, id, zoneCustomSSL)
  50. if err != nil {
  51. return &cert.CreateCertificateReplyCloudflare{
  52. Success: false,
  53. }, err
  54. }
  55. return &cert.CreateCertificateReplyCloudflare{
  56. Success: true,
  57. Id: createCertificate.ID,
  58. }, err
  59. }
  60. func (s *CertificateServer) DeleteCertificate(ctx context.Context, requestCloudflare *cert.DeleteCertificateRequestCloudflare) (*cert.DeleteCertificateReplyCloudflare, error) {
  61. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  62. if err != nil {
  63. return nil, err
  64. }
  65. ctxAPI := context.Background()
  66. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  67. if err != nil {
  68. return nil, err
  69. }
  70. err = api.DeleteSSL(ctxAPI, id, requestCloudflare.GetCertificateId())
  71. if err != nil {
  72. return &cert.DeleteCertificateReplyCloudflare{
  73. Success: false,
  74. }, err
  75. }
  76. return &cert.DeleteCertificateReplyCloudflare{
  77. Success: true,
  78. Id: requestCloudflare.GetCertificateId(),
  79. }, err
  80. }
  81. func (s *CertificateServer) EditCertificate(ctx context.Context, requestCloudflare *cert.EditCertificateRequestCloudflare) (*cert.EditCertificateReplyCloudflare, error) {
  82. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  83. if err != nil {
  84. return nil, err
  85. }
  86. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  87. if err != nil {
  88. return nil, err
  89. }
  90. ctxAPI := context.Background()
  91. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  92. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  93. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  94. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  95. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  96. zoneCustomSSL.BundleMethod = "force"
  97. zoneCustomSSL.Type = "sni_custom"
  98. editCertificate, err := api.UpdateSSL(ctxAPI, id, requestCloudflare.GetCertificateId(), zoneCustomSSL)
  99. if err != nil {
  100. return &cert.EditCertificateReplyCloudflare{
  101. Success: false,
  102. }, err
  103. }
  104. return &cert.EditCertificateReplyCloudflare{
  105. Success: true,
  106. Id: editCertificate.ID,
  107. }, err
  108. }