CertificateCloudflareServer.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package server
  2. import (
  3. cert "cfTest/cloudflareApi/certificate"
  4. "context"
  5. "github.com/cloudflare/cloudflare-go"
  6. "log"
  7. "strings"
  8. )
  9. type CertificateServer struct {
  10. cert.UnimplementedCertificateCloudflareServer
  11. }
  12. func (s *CertificateServer) UniversalCertificate(ctx context.Context, requestCloudflare *cert.UniversalCertificateRequestCloudflare) (*cert.UniversalCertificateReplyCloudflare, error) {
  13. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  14. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  15. } else {
  16. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  17. }
  18. if err != nil {
  19. return nil, err
  20. }
  21. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  22. if err != nil {
  23. return nil, err
  24. }
  25. ctxAPI := context.Background()
  26. editUniversalSSLSettings := cloudflare.UniversalSSLSetting{Enabled: requestCloudflare.GetEnable()}
  27. universalCertificate, err := api.EditUniversalSSLSetting(ctxAPI, id, editUniversalSSLSettings)
  28. if err != nil {
  29. return &cert.UniversalCertificateReplyCloudflare{
  30. Success: false, Enabled: false,
  31. }, err
  32. }
  33. return &cert.UniversalCertificateReplyCloudflare{
  34. Success: true,
  35. Enabled: universalCertificate.Enabled,
  36. }, err
  37. }
  38. func (s *CertificateServer) ListZoneCertificates(ctx context.Context, requestCloudflare *cert.ListZoneCertificatesRequestCloudflare) (*cert.ListZoneCertificatesReplyCloudflare, error) {
  39. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  40. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  41. } else {
  42. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  43. }
  44. if err != nil {
  45. return nil, err
  46. }
  47. ctxAPI := context.Background()
  48. response, err := api.ListSSL(ctxAPI, requestCloudflare.GetZoneId())
  49. log.Printf("Cloudflare ListZoneCertificates API response: %v err:%s", response, err)
  50. if err != nil {
  51. return &cert.ListZoneCertificatesReplyCloudflare{
  52. Result: nil,
  53. Success: false,
  54. }, err
  55. }
  56. list := make([]*cert.ZoneCustomSSL, len(response))
  57. for i, detail := range response {
  58. list[i] = Go2ProtoCertificate(detail)
  59. }
  60. return &cert.ListZoneCertificatesReplyCloudflare{
  61. Result: list,
  62. Success: true,
  63. }, err
  64. }
  65. func (s *CertificateServer) CreateCertificate(ctx context.Context, requestCloudflare *cert.CreateCertificateRequestCloudflare) (*cert.CreateCertificateReplyCloudflare, error) {
  66. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  67. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  68. } else {
  69. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  70. }
  71. if err != nil {
  72. return nil, err
  73. }
  74. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  75. if err != nil {
  76. return nil, err
  77. }
  78. ctxAPI := context.Background()
  79. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  80. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  81. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  82. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  83. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  84. zoneCustomSSL.BundleMethod = "ubiquitous"
  85. zoneCustomSSL.Type = "sni_custom"
  86. createCertificate, err := api.CreateSSL(ctxAPI, id, zoneCustomSSL)
  87. log.Printf("Cloudflare CreateCertificate API response: %v err:%s", createCertificate, err)
  88. if err != nil {
  89. return &cert.CreateCertificateReplyCloudflare{
  90. Success: false,
  91. }, err
  92. }
  93. return &cert.CreateCertificateReplyCloudflare{
  94. Success: true,
  95. Id: createCertificate.ID,
  96. }, err
  97. }
  98. func (s *CertificateServer) CreateCertificateV2(ctx context.Context, requestCloudflare *cert.CreateCertificateRequestCloudflareV2) (*cert.CreateCertificateReplyCloudflare, error) {
  99. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  100. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  101. } else {
  102. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  103. }
  104. if err != nil {
  105. return nil, err
  106. }
  107. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  108. if err != nil {
  109. return nil, err
  110. }
  111. ctxAPI := context.Background()
  112. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  113. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  114. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  115. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  116. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  117. zoneCustomSSL.BundleMethod = requestCloudflare.GetBundleMethod()
  118. if requestCloudflare.GetNoSni() {
  119. //默认方式
  120. zoneCustomSSL.Type = "legacy_custom"
  121. } else {
  122. zoneCustomSSL.Type = "sni_custom"
  123. }
  124. createCertificate, err := api.CreateSSL(ctxAPI, id, zoneCustomSSL)
  125. log.Printf("Cloudflare CreateCertificateV2 API response: %v err:%s", createCertificate, err)
  126. if err != nil {
  127. return &cert.CreateCertificateReplyCloudflare{
  128. Success: false,
  129. }, err
  130. }
  131. return &cert.CreateCertificateReplyCloudflare{
  132. Success: true,
  133. Id: createCertificate.ID,
  134. }, err
  135. }
  136. func (s *CertificateServer) DeleteCertificate(ctx context.Context, requestCloudflare *cert.DeleteCertificateRequestCloudflare) (*cert.DeleteCertificateReplyCloudflare, error) {
  137. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  138. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  139. } else {
  140. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  141. }
  142. if err != nil {
  143. return nil, err
  144. }
  145. ctxAPI := context.Background()
  146. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  147. if err != nil {
  148. return nil, err
  149. }
  150. err = api.DeleteSSL(ctxAPI, id, requestCloudflare.GetCertificateId())
  151. log.Printf("Cloudflare DeleteCertificate API err:%s", err)
  152. if err != nil {
  153. return &cert.DeleteCertificateReplyCloudflare{
  154. Success: false,
  155. }, err
  156. }
  157. return &cert.DeleteCertificateReplyCloudflare{
  158. Success: true,
  159. Id: requestCloudflare.GetCertificateId(),
  160. }, err
  161. }
  162. func (s *CertificateServer) EditCertificate(ctx context.Context, requestCloudflare *cert.EditCertificateRequestCloudflare) (*cert.EditCertificateReplyCloudflare, error) {
  163. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  164. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  165. } else {
  166. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  167. }
  168. if err != nil {
  169. return nil, err
  170. }
  171. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  172. if err != nil {
  173. return nil, err
  174. }
  175. ctxAPI := context.Background()
  176. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  177. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  178. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  179. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  180. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  181. zoneCustomSSL.BundleMethod = "ubiquitous"
  182. zoneCustomSSL.Type = "sni_custom"
  183. editCertificate, err := api.UpdateSSL(ctxAPI, id, requestCloudflare.GetCertificateId(), zoneCustomSSL)
  184. log.Printf("Cloudflare EditCertificate API response: %v err:%s", editCertificate, err)
  185. if err != nil {
  186. return &cert.EditCertificateReplyCloudflare{
  187. Success: false,
  188. }, err
  189. }
  190. return &cert.EditCertificateReplyCloudflare{
  191. Success: true,
  192. Id: editCertificate.ID,
  193. }, err
  194. }
  195. func (s *CertificateServer) EditCertificateV2(ctx context.Context, requestCloudflare *cert.EditCertificateRequestCloudflareV2) (*cert.EditCertificateReplyCloudflare, error) {
  196. if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
  197. api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  198. } else {
  199. api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  200. }
  201. if err != nil {
  202. return nil, err
  203. }
  204. id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
  205. if err != nil {
  206. return nil, err
  207. }
  208. ctxAPI := context.Background()
  209. geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
  210. zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
  211. zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
  212. zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
  213. zoneCustomSSL.GeoRestrictions = &geoRestrictions
  214. // ubiquitous无处不在 optimal最佳的 force强制的
  215. zoneCustomSSL.BundleMethod = requestCloudflare.GetBundleMethod()
  216. if requestCloudflare.GetNoSni() {
  217. zoneCustomSSL.Type = "legacy_custom"
  218. } else {
  219. zoneCustomSSL.Type = "sni_custom"
  220. }
  221. editCertificate, err := api.UpdateSSL(ctxAPI, id, requestCloudflare.GetCertificateId(), zoneCustomSSL)
  222. log.Printf("Cloudflare EditCertificateV2 API response: %v err:%s", editCertificate, err)
  223. if err != nil {
  224. return &cert.EditCertificateReplyCloudflare{
  225. Success: false,
  226. }, err
  227. }
  228. return &cert.EditCertificateReplyCloudflare{
  229. Success: true,
  230. Id: editCertificate.ID,
  231. }, err
  232. }