CertificateCloudflareServer.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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) ListZoneCertificates(ctx context.Context, requestCloudflare *cert.ListZoneCertificatesRequestCloudflare) (*cert.ListZoneCertificatesReplyCloudflare, error) {
  33. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  34. if err != nil {
  35. return nil, err
  36. }
  37. ctxAPI := context.Background()
  38. response, err := api.ListSSL(ctxAPI, requestCloudflare.GetZoneId())
  39. if err != nil {
  40. return &cert.ListZoneCertificatesReplyCloudflare{
  41. Result: nil,
  42. Success: false,
  43. }, err
  44. }
  45. list := make([]*cert.ZoneCustomSSL, len(response))
  46. for i, detail := range response {
  47. list[i] = Go2ProtoCertificate(detail)
  48. }
  49. return &cert.ListZoneCertificatesReplyCloudflare{
  50. Result: list,
  51. Success: true,
  52. }, err
  53. }
  54. func (s *CertificateServer) CreateCertificate(ctx context.Context, requestCloudflare *cert.CreateCertificateRequestCloudflare) (*cert.CreateCertificateReplyCloudflare, error) {
  55. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  56. if err != nil {
  57. return nil, err
  58. }
  59. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  60. if err != nil {
  61. return nil, err
  62. }
  63. ctxAPI := context.Background()
  64. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  65. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  66. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  67. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  68. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  69. zoneCustomSSL.BundleMethod = "ubiquitous"
  70. zoneCustomSSL.Type = "sni_custom"
  71. createCertificate, err := api.CreateSSL(ctxAPI, id, zoneCustomSSL)
  72. if err != nil {
  73. return &cert.CreateCertificateReplyCloudflare{
  74. Success: false,
  75. }, err
  76. }
  77. return &cert.CreateCertificateReplyCloudflare{
  78. Success: true,
  79. Id: createCertificate.ID,
  80. }, err
  81. }
  82. func (s *CertificateServer) CreateCertificateV2(ctx context.Context, requestCloudflare *cert.CreateCertificateRequestCloudflareV2) (*cert.CreateCertificateReplyCloudflare, error) {
  83. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  84. if err != nil {
  85. return nil, err
  86. }
  87. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  88. if err != nil {
  89. return nil, err
  90. }
  91. ctxAPI := context.Background()
  92. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  93. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  94. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  95. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  96. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  97. zoneCustomSSL.BundleMethod = requestCloudflare.GetBundleMethod()
  98. if requestCloudflare.GetNoSni() {
  99. //默认方式
  100. zoneCustomSSL.Type = "legacy_custom"
  101. } else {
  102. zoneCustomSSL.Type = "sni_custom"
  103. }
  104. createCertificate, err := api.CreateSSL(ctxAPI, id, zoneCustomSSL)
  105. if err != nil {
  106. return &cert.CreateCertificateReplyCloudflare{
  107. Success: false,
  108. }, err
  109. }
  110. return &cert.CreateCertificateReplyCloudflare{
  111. Success: true,
  112. Id: createCertificate.ID,
  113. }, err
  114. }
  115. func (s *CertificateServer) DeleteCertificate(ctx context.Context, requestCloudflare *cert.DeleteCertificateRequestCloudflare) (*cert.DeleteCertificateReplyCloudflare, error) {
  116. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  117. if err != nil {
  118. return nil, err
  119. }
  120. ctxAPI := context.Background()
  121. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  122. if err != nil {
  123. return nil, err
  124. }
  125. err = api.DeleteSSL(ctxAPI, id, requestCloudflare.GetCertificateId())
  126. if err != nil {
  127. return &cert.DeleteCertificateReplyCloudflare{
  128. Success: false,
  129. }, err
  130. }
  131. return &cert.DeleteCertificateReplyCloudflare{
  132. Success: true,
  133. Id: requestCloudflare.GetCertificateId(),
  134. }, err
  135. }
  136. func (s *CertificateServer) EditCertificate(ctx context.Context, requestCloudflare *cert.EditCertificateRequestCloudflare) (*cert.EditCertificateReplyCloudflare, error) {
  137. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  138. if err != nil {
  139. return nil, err
  140. }
  141. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  142. if err != nil {
  143. return nil, err
  144. }
  145. ctxAPI := context.Background()
  146. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  147. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  148. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  149. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  150. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  151. zoneCustomSSL.BundleMethod = "ubiquitous"
  152. zoneCustomSSL.Type = "sni_custom"
  153. editCertificate, err := api.UpdateSSL(ctxAPI, id, requestCloudflare.GetCertificateId(), zoneCustomSSL)
  154. if err != nil {
  155. return &cert.EditCertificateReplyCloudflare{
  156. Success: false,
  157. }, err
  158. }
  159. return &cert.EditCertificateReplyCloudflare{
  160. Success: true,
  161. Id: editCertificate.ID,
  162. }, err
  163. }
  164. func (s *CertificateServer) EditCertificateV2(ctx context.Context, requestCloudflare *cert.EditCertificateRequestCloudflareV2) (*cert.EditCertificateReplyCloudflare, error) {
  165. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  166. if err != nil {
  167. return nil, err
  168. }
  169. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  170. if err != nil {
  171. return nil, err
  172. }
  173. ctxAPI := context.Background()
  174. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  175. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  176. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  177. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  178. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  179. // ubiquitous无处不在 optimal最佳的 force强制的
  180. zoneCustomSSL.BundleMethod = requestCloudflare.GetBundleMethod()
  181. if requestCloudflare.GetNoSni() {
  182. zoneCustomSSL.Type = "legacy_custom"
  183. } else {
  184. zoneCustomSSL.Type = "sni_custom"
  185. }
  186. editCertificate, err := api.UpdateSSL(ctxAPI, id, requestCloudflare.GetCertificateId(), zoneCustomSSL)
  187. if err != nil {
  188. return &cert.EditCertificateReplyCloudflare{
  189. Success: false,
  190. }, err
  191. }
  192. return &cert.EditCertificateReplyCloudflare{
  193. Success: true,
  194. Id: editCertificate.ID,
  195. }, err
  196. }