CertificateCloudflareServer.go 5.2 KB

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