123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package server
- import (
- cert "cfTest/cloudflareApi/certificate"
- "context"
- "github.com/cloudflare/cloudflare-go"
- "log"
- )
- type CertificateServer struct {
- cert.UnimplementedCertificateCloudflareServer
- }
- func (s *CertificateServer) UniversalCertificate(ctx context.Context, requestCloudflare *cert.UniversalCertificateRequestCloudflare) (*cert.UniversalCertificateReplyCloudflare, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneName Received: %v", requestCloudflare.GetZoneName())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- return nil, err
- }
- id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- editUniversalSSLSettings := cloudflare.UniversalSSLSetting{Enabled: requestCloudflare.GetEnable()}
- universalCertificate, err := api.EditUniversalSSLSetting(ctxAPI, id, editUniversalSSLSettings)
- if err != nil {
- return &cert.UniversalCertificateReplyCloudflare{
- Success: false, Enabled: false,
- }, err
- }
- return &cert.UniversalCertificateReplyCloudflare{
- Success: true,
- Enabled: universalCertificate.Enabled,
- }, err
- }
- func (s *CertificateServer) CreateCertificate(ctx context.Context, requestCloudflare *cert.CreateCertificateRequestCloudflare) (*cert.CreateCertificateReplyCloudflare, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneName Received: %v", requestCloudflare.GetZoneName())
- log.Printf("certificate Received: %v", requestCloudflare.GetCertificate())
- log.Printf("privateKey Received: %v", requestCloudflare.GetPrivateKey())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- return nil, err
- }
- id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
- zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
- zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
- zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
- zoneCustomSSL.GeoRestrictions = &geoRestrictions
- zoneCustomSSL.BundleMethod = "force"
- zoneCustomSSL.Type = "sni_custom"
- createCertificate, err := api.CreateSSL(ctxAPI, id, zoneCustomSSL)
- if err != nil {
- return &cert.CreateCertificateReplyCloudflare{
- Success: false,
- }, err
- }
- return &cert.CreateCertificateReplyCloudflare{
- Success: true,
- Id: createCertificate.ID,
- }, err
- }
- func (s *CertificateServer) DeleteCertificate(ctx context.Context, requestCloudflare *cert.DeleteCertificateRequestCloudflare) (*cert.DeleteCertificateReplyCloudflare, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneName Received: %v", requestCloudflare.GetZoneName())
- log.Printf("certificateId Received: %v", requestCloudflare.GetCertificateId())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
- if err != nil {
- return nil, err
- }
- err = api.DeleteSSL(ctxAPI, id, requestCloudflare.GetCertificateId())
- if err != nil {
- return &cert.DeleteCertificateReplyCloudflare{
- Success: false,
- }, err
- }
- return &cert.DeleteCertificateReplyCloudflare{
- Success: true,
- Id: requestCloudflare.GetCertificateId(),
- }, err
- }
- func (s *CertificateServer) EditCertificate(ctx context.Context, requestCloudflare *cert.EditCertificateRequestCloudflare) (*cert.EditCertificateReplyCloudflare, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneName Received: %v", requestCloudflare.GetZoneName())
- log.Printf("certificateId Received: %v", requestCloudflare.GetCertificateId())
- log.Printf("certificate Received: %v", requestCloudflare.GetCertificate())
- log.Printf("privateKey Received: %v", requestCloudflare.GetPrivateKey())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- return nil, err
- }
- id, err := api.ZoneIDByName(requestCloudflare.GetZoneName())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- geoRestrictions := cloudflare.ZoneCustomSSLGeoRestrictions{Label: "us"}
- zoneCustomSSL := cloudflare.ZoneCustomSSLOptions{}
- zoneCustomSSL.Certificate = requestCloudflare.GetCertificate()
- zoneCustomSSL.PrivateKey = requestCloudflare.GetPrivateKey()
- zoneCustomSSL.GeoRestrictions = &geoRestrictions
- zoneCustomSSL.BundleMethod = "force"
- zoneCustomSSL.Type = "sni_custom"
- editCertificate, err := api.UpdateSSL(ctxAPI, id, requestCloudflare.GetCertificateId(), zoneCustomSSL)
- if err != nil {
- return &cert.EditCertificateReplyCloudflare{
- Success: false,
- }, err
- }
- return &cert.EditCertificateReplyCloudflare{
- Success: true,
- Id: editCertificate.ID,
- }, err
- }
|