CertificateCloudflareServer.go 8.3 KB

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