Browse Source

opt: go调用api方式改造,使之兼容token与apikey

liufan 1 month ago
parent
commit
92b8df5f09

+ 36 - 7
server/CertificateCloudflareServer.go

@@ -4,6 +4,7 @@ import (
 	cert "cfTest/cloudflareApi/certificate"
 	"context"
 	"github.com/cloudflare/cloudflare-go"
+	"strings"
 )
 
 type CertificateServer struct {
@@ -11,7 +12,11 @@ type CertificateServer struct {
 }
 
 func (s *CertificateServer) UniversalCertificate(ctx context.Context, requestCloudflare *cert.UniversalCertificateRequestCloudflare) (*cert.UniversalCertificateReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -36,7 +41,11 @@ func (s *CertificateServer) UniversalCertificate(ctx context.Context, requestClo
 }
 
 func (s *CertificateServer) ListZoneCertificates(ctx context.Context, requestCloudflare *cert.ListZoneCertificatesRequestCloudflare) (*cert.ListZoneCertificatesReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -59,7 +68,11 @@ func (s *CertificateServer) ListZoneCertificates(ctx context.Context, requestClo
 }
 
 func (s *CertificateServer) CreateCertificate(ctx context.Context, requestCloudflare *cert.CreateCertificateRequestCloudflare) (*cert.CreateCertificateReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -90,7 +103,11 @@ func (s *CertificateServer) CreateCertificate(ctx context.Context, requestCloudf
 }
 
 func (s *CertificateServer) CreateCertificateV2(ctx context.Context, requestCloudflare *cert.CreateCertificateRequestCloudflareV2) (*cert.CreateCertificateReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -126,7 +143,11 @@ func (s *CertificateServer) CreateCertificateV2(ctx context.Context, requestClou
 }
 
 func (s *CertificateServer) DeleteCertificate(ctx context.Context, requestCloudflare *cert.DeleteCertificateRequestCloudflare) (*cert.DeleteCertificateReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -148,7 +169,11 @@ func (s *CertificateServer) DeleteCertificate(ctx context.Context, requestCloudf
 }
 
 func (s *CertificateServer) EditCertificate(ctx context.Context, requestCloudflare *cert.EditCertificateRequestCloudflare) (*cert.EditCertificateReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -178,7 +203,11 @@ func (s *CertificateServer) EditCertificate(ctx context.Context, requestCloudfla
 }
 
 func (s *CertificateServer) EditCertificateV2(ctx context.Context, requestCloudflare *cert.EditCertificateRequestCloudflareV2) (*cert.EditCertificateReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}

+ 21 - 4
server/DnsCloudflareServer.go

@@ -6,6 +6,7 @@ import (
 	"github.com/cloudflare/cloudflare-go"
 	"github.com/jinzhu/copier"
 	"github.com/samber/lo"
+	"strings"
 )
 
 type DnsServer struct {
@@ -55,7 +56,11 @@ func goRecord2UpdateDnsRecordParams(goRecord *cloudflare.DNSRecord) (updateDnsRe
 }
 
 func (d DnsServer) List(ctx context.Context, request *dns.ListRequest) (*dns.DnsRecordList, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -78,7 +83,11 @@ func (d DnsServer) List(ctx context.Context, request *dns.ListRequest) (*dns.Dns
 }
 
 func (d DnsServer) Create(ctx context.Context, request *dns.DnsRecordRequest) (*dns.DnsRecord, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -97,7 +106,11 @@ func (d DnsServer) Create(ctx context.Context, request *dns.DnsRecordRequest) (*
 }
 
 func (d DnsServer) Update(ctx context.Context, request *dns.DnsRecordRequest) (*dns.Empty, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -113,7 +126,11 @@ func (d DnsServer) Update(ctx context.Context, request *dns.DnsRecordRequest) (*
 }
 
 func (d DnsServer) Delete(ctx context.Context, request *dns.DeleteRequest) (*dns.Empty, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}

+ 21 - 4
server/LoadBalancerCloudflareServer.go

@@ -6,6 +6,7 @@ import (
 	"github.com/cloudflare/cloudflare-go"
 	"github.com/jinzhu/copier"
 	"github.com/samber/lo"
+	"strings"
 )
 
 type LoadBalancerServer struct {
@@ -34,7 +35,11 @@ func Proto2GoLoadBalancer(protoBalancer *loadBalancer.LoadBalancer) (goBalancer
 }
 
 func (d LoadBalancerServer) List(ctx context.Context, request *loadBalancer.ListRequest) (*loadBalancer.LoadBalancerList, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -60,7 +65,11 @@ func (d LoadBalancerServer) List(ctx context.Context, request *loadBalancer.List
 }
 
 func (d LoadBalancerServer) Create(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -83,7 +92,11 @@ func (d LoadBalancerServer) Create(ctx context.Context, request *loadBalancer.Lo
 }
 
 func (d LoadBalancerServer) Update(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -106,7 +119,11 @@ func (d LoadBalancerServer) Update(ctx context.Context, request *loadBalancer.Lo
 }
 
 func (d LoadBalancerServer) Delete(ctx context.Context, request *loadBalancer.DeleteRequest) (*loadBalancer.Empty, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}

+ 26 - 5
server/LoadBalancerPoolCloudflareServer.go

@@ -6,6 +6,7 @@ import (
 	"github.com/cloudflare/cloudflare-go"
 	"github.com/jinzhu/copier"
 	"github.com/samber/lo"
+	"strings"
 )
 
 type LoadBalancerPoolServer struct {
@@ -13,7 +14,11 @@ type LoadBalancerPoolServer struct {
 }
 
 func (d LoadBalancerPoolServer) Get(ctx context.Context, request *loadBalancerPool.IdRequest) (*loadBalancerPool.LoadBalancerPool, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -33,7 +38,11 @@ func (d LoadBalancerPoolServer) Get(ctx context.Context, request *loadBalancerPo
 }
 
 func (d LoadBalancerPoolServer) List(ctx context.Context, request *loadBalancerPool.ListRequest) (*loadBalancerPool.LoadBalancerPoolList, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -59,7 +68,11 @@ func (d LoadBalancerPoolServer) List(ctx context.Context, request *loadBalancerP
 }
 
 func (d LoadBalancerPoolServer) Create(ctx context.Context, request *loadBalancerPool.LoadBalancerPoolRequest) (*loadBalancerPool.LoadBalancerPool, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -83,7 +96,11 @@ func (d LoadBalancerPoolServer) Create(ctx context.Context, request *loadBalance
 }
 
 func (d LoadBalancerPoolServer) Update(ctx context.Context, request *loadBalancerPool.LoadBalancerPoolRequest) (*loadBalancerPool.LoadBalancerPool, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -105,7 +122,11 @@ func (d LoadBalancerPoolServer) Update(ctx context.Context, request *loadBalance
 }
 
 func (d LoadBalancerPoolServer) Delete(ctx context.Context, request *loadBalancerPool.IdRequest) (*loadBalancerPool.Empty, error) {
-	api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}

+ 31 - 7
server/LogPushCloudflareServer.go

@@ -6,6 +6,7 @@ import (
 	"errors"
 	"github.com/cloudflare/cloudflare-go"
 	"log"
+	"strings"
 )
 
 type LogPushCloudflareServer struct {
@@ -14,8 +15,11 @@ type LogPushCloudflareServer struct {
 
 func (s *LogPushCloudflareServer) ListLogPushJobsByZoneId(ctx context.Context,
 	requestCloudflare *logPush.ListLogPushJobRequestByZoneId) (*logPush.ListLogPushJobReplay, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		log.Printf("new cloudflare api error : %v", err)
 		return nil, err
@@ -47,7 +51,11 @@ func (s *LogPushCloudflareServer) ListLogPushJobsByZoneId(ctx context.Context,
 func (s *LogPushCloudflareServer) CreateLogPushJobByZoneId(ctx context.Context,
 	requestCloudflare *logPush.CreateLogPushJobRequestByZoneId) (*logPush.CreateLogPushJobReplay, error) {
 
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		log.Printf("new cloudflare api error : %v", err)
 		return nil, err
@@ -93,7 +101,11 @@ func (s *LogPushCloudflareServer) CreateLogPushJobByZoneId(ctx context.Context,
 func (s *LogPushCloudflareServer) DeleteLogPushJobByZoneId(ctx context.Context,
 	requestCloudflare *logPush.DeleteLogPushJobRequestByZoneId) (*logPush.DeleteLogPushJobReplay, error) {
 
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		log.Printf("new cloudflare api error : %v", err)
 		return nil, err
@@ -120,7 +132,11 @@ func (s *LogPushCloudflareServer) DeleteLogPushJobByZoneId(ctx context.Context,
 func (s *LogPushCloudflareServer) GetOwnershipChallengeByZoneId(ctx context.Context,
 	requestCloudflare *logPush.GetOwnershipChallengeRequestByZoneId) (*logPush.GetOwnershipChallengeReply, error) {
 
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		log.Printf("new cloudflare api error : %v", err)
 		return nil, err
@@ -157,7 +173,11 @@ func (s *LogPushCloudflareServer) GetOwnershipChallengeByZoneId(ctx context.Cont
 func (s *LogPushCloudflareServer) CheckDestinationExist(ctx context.Context,
 	requestCloudflare *logPush.CheckDestinationExistRequest) (*logPush.CheckDestinationExistReply, error) {
 
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		log.Printf("new cloudflare api error : %v", err)
 		return nil, err
@@ -181,7 +201,11 @@ func (s *LogPushCloudflareServer) CheckDestinationExist(ctx context.Context,
 func (s *LogPushCloudflareServer) ValidateOwnershipChallenge(ctx context.Context,
 	requestCloudflare *logPush.ValidateOwnershipChallengeRequest) (*logPush.ValidateOwnershipChallengeReply, error) {
 
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		log.Printf("new cloudflare api error : %v", err)
 		return nil, err

+ 16 - 11
server/PurgeCloudflareServer.go

@@ -5,6 +5,7 @@ import (
 	"context"
 	"github.com/cloudflare/cloudflare-go"
 	"log"
+	"strings"
 )
 
 type PurgeServer struct {
@@ -12,9 +13,11 @@ type PurgeServer struct {
 }
 
 func (s *PurgeServer) PurgeCloudflare(ctx context.Context, in *pb.PurgeRequestCloudflare) (*pb.PurgeReplyCloudflare, error) {
-
-	// Construct a new API object using a global API key
-	api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
+	if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail())
+	}
 	// alternatively, you can use a scoped API token
 	// api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
 	if err != nil {
@@ -46,10 +49,11 @@ func (s *PurgeServer) PurgeCloudflare(ctx context.Context, in *pb.PurgeRequestCl
 
 func (s *PurgeServer) PurgeCloudflareByPrefix(ctx context.Context, in *pb.PurgeRequestCloudflarePrefix) (*pb.PurgeReplyCloudflare, error) {
 
-	// Construct a new API object using a global API key
-	api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
-	// alternatively, you can use a scoped API token
-	// api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
+	if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail())
+	}
 	if err != nil {
 		log.Fatal(err)
 		return &pb.PurgeReplyCloudflare{Result: false}, err
@@ -79,10 +83,11 @@ func (s *PurgeServer) PurgeCloudflareByPrefix(ctx context.Context, in *pb.PurgeR
 
 func (s *PurgeServer) PurgeCloudflareEverything(ctx context.Context, in *pb.PurgeRequestCloudflareEverything) (*pb.PurgeReplyCloudflare, error) {
 
-	// Construct a new API object using a global API key
-	api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
-	// alternatively, you can use a scoped API token
-	// api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
+	if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail())
+	}
 	if err != nil {
 		log.Fatal(err)
 		return &pb.PurgeReplyCloudflare{Result: false}, err

+ 148 - 51
server/ZoneCloudflareServer.go

@@ -57,6 +57,13 @@ type Subscription struct {
 
 var ArgoSubscriptionId = "argo_zone_ent_tryout"
 
+const TOKEN_PREFIX = "#pt"
+
+var (
+	api *cloudflare.API
+	err error
+)
+
 func (s *ZoneServer) GetArgoSettingSubscription(ctx context.Context, request *zone.IdRequest) (*zone.ArgoSetting, error) {
 
 	//设置HTTP请求
@@ -65,10 +72,12 @@ func (s *ZoneServer) GetArgoSettingSubscription(ctx context.Context, request *zo
 	if err != nil {
 		return nil, err
 	}
-
-	req.Header.Add("X-Auth-Key", request.GetApiKey())
-	req.Header.Add("X-Auth-Email", request.GetApiEmail())
-
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		req.Header.Add("Authorization", ApiToken2Authorization(request.GetApiKey()[3:]))
+	} else {
+		req.Header.Add("X-Auth-Key", request.GetApiKey())
+		req.Header.Add("X-Auth-Email", request.GetApiEmail())
+	}
 	//执行HTTP请求
 	resp, err := client.Do(req)
 	if err != nil {
@@ -148,10 +157,12 @@ func (s *ZoneServer) CreateArgoSettingSubscription(ctx context.Context, request
 	if err != nil {
 		return nil, err
 	}
-
-	req.Header.Add("X-Auth-Key", request.GetApiKey())
-	req.Header.Add("X-Auth-Email", request.GetApiEmail())
-
+	if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
+		req.Header.Add("Authorization", ApiToken2Authorization(request.GetApiKey()[3:]))
+	} else {
+		req.Header.Add("X-Auth-Key", request.GetApiKey())
+		req.Header.Add("X-Auth-Email", request.GetApiEmail())
+	}
 	//执行HTTP请求
 	resp, err := client.Do(req)
 	if err != nil {
@@ -182,8 +193,11 @@ func (s *ZoneServer) CreateArgoSettingSubscription(ctx context.Context, request
 }
 
 func (s *ZoneServer) UpdateArgoSetting(ctx context.Context, requestCloudflare *zone.UpdateArgoRequestCloudflare) (*zone.ArgoSetting, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -199,8 +213,11 @@ func (s *ZoneServer) UpdateArgoSetting(ctx context.Context, requestCloudflare *z
 }
 
 func (s *ZoneServer) GetArgoSetting(ctx context.Context, requestCloudflare *zone.IdRequest) (*zone.ArgoSetting, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -214,8 +231,11 @@ func (s *ZoneServer) GetArgoSetting(ctx context.Context, requestCloudflare *zone
 }
 
 func (s *ZoneServer) ZoneIDByNameCloudflare(ctx context.Context, requestCloudflare *zone.ZoneIDByNameRequestCloudflare) (*zone.ZoneIDByNameReplyCloudflare, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -229,8 +249,11 @@ func (s *ZoneServer) ZoneIDByNameCloudflare(ctx context.Context, requestCloudfla
 }
 
 func (s *ZoneServer) ZoneDetails(ctx context.Context, requestCloudflare *zone.ZoneDetailsRequestCloudflare) (*zone.ZoneDetailsReplyCloudflare, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -248,8 +271,11 @@ func (s *ZoneServer) ZoneDetails(ctx context.Context, requestCloudflare *zone.Zo
 }
 
 func (s *ZoneServer) GetZones(ctx context.Context, requestCloudflare *zone.GetZonesRequestCloudflare) (*zone.GetZonesReplyCloudflare, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -269,10 +295,13 @@ func (s *ZoneServer) GetZones(ctx context.Context, requestCloudflare *zone.GetZo
 }
 
 func (s *ZoneServer) ListZones(ctx context.Context, requestCloudflare *zone.ListZonesRequestCloudflare) (*zone.ZonesList, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
-		return nil, err
+		return nil, fmt.Errorf("failed to initialize Cloudflare API: %w", err)
 	}
 
 	ctxAPI := context.Background()
@@ -291,8 +320,11 @@ func (s *ZoneServer) ListZones(ctx context.Context, requestCloudflare *zone.List
 }
 
 func (s *ZoneServer) ListZonesByAccountId(ctx context.Context, requestCloudflare *zone.ListZonesByAccountIdRequestCloudflare) (*zone.ZonesList, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -313,8 +345,11 @@ func (s *ZoneServer) ListZonesByAccountId(ctx context.Context, requestCloudflare
 }
 
 func (s *ZoneServer) EditZone(ctx context.Context, requestCloudflare *zone.EditZoneRequestCloudflare) (*zone.EditZoneReplyCloudflare, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -350,8 +385,11 @@ func (s *ZoneServer) EditZone(ctx context.Context, requestCloudflare *zone.EditZ
 }
 
 func (s *ZoneServer) DeleteZone(ctx context.Context, requestCloudflare *zone.DeleteZoneRequestCloudflare) (*zone.DeleteZoneReplyCloudflare, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -370,8 +408,11 @@ func (s *ZoneServer) DeleteZone(ctx context.Context, requestCloudflare *zone.Del
 }
 
 func (s *ZoneServer) CreateZone(ctx context.Context, requestCloudflare *zone.CreateZoneRequestCloudflare) (*zone.CreateZoneReplyCloudflare, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -395,8 +436,11 @@ func (s *ZoneServer) CreateZone(ctx context.Context, requestCloudflare *zone.Cre
 }
 
 func (s *ZoneServer) SetZonePlan(ctx context.Context, requestCloudflare *zone.SetZonePlanRequestCloudflare) (*zone.SetZonePlanReplyCloudflare, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -413,8 +457,11 @@ func (s *ZoneServer) SetZonePlan(ctx context.Context, requestCloudflare *zone.Se
 }
 
 func (s *ZoneServer) ActivationCheck(ctx context.Context, requestCloudflare *zone.ActivationCheckRequestCloudflare) (*zone.ActivationCheckReplyCloudflare, error) {
-
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -452,7 +499,11 @@ var phaseMap = map[zone.Phase]string{
 }
 
 func (s *ZoneServer) ListZoneRuleSets(ctx context.Context, requestCloudflare *zone.ListZoneRuleSetsRequestCloudflare) (*zone.RuleSetList, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -471,7 +522,11 @@ func (s *ZoneServer) ListZoneRuleSets(ctx context.Context, requestCloudflare *zo
 }
 
 func (s *ZoneServer) CreateZoneRuleSet(ctx context.Context, requestCloudflare *zone.CreateZoneRuleSetRequestCloudflare) (*zone.CreateZoneRuleSetReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -499,7 +554,11 @@ func (s *ZoneServer) CreateZoneRuleSet(ctx context.Context, requestCloudflare *z
 }
 
 func (s *ZoneServer) DeleteZoneRuleSet(ctx context.Context, requestCloudflare *zone.DeleteZoneRuleSetRequestCloudflare) (*zone.DeleteZoneRuleSetReplyCloudflare, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -513,7 +572,11 @@ func (s *ZoneServer) DeleteZoneRuleSet(ctx context.Context, requestCloudflare *z
 }
 
 func (s *ZoneServer) UpdateRuleSetByRuleSetId(ctx context.Context, requestCloudflare *zone.UpdateRuleSetByRuleSetIdRequestCloudflare) (*zone.RuleSet, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -539,7 +602,11 @@ func (s *ZoneServer) UpdateRuleSetByRuleSetId(ctx context.Context, requestCloudf
 }
 
 func (s *ZoneServer) GetRuleSet(ctx context.Context, requestCloudflare *zone.GetRuleSetRequestCloudflare) (*zone.RuleList, error) {
-	api, err := cloudflare.New(requestCloudflare.ApiKey, requestCloudflare.ApiEmail)
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -571,7 +638,11 @@ func (s *ZoneServer) GetRuleSet(ctx context.Context, requestCloudflare *zone.Get
 }
 
 func (s *ZoneServer) UpdateRuleSet(ctx context.Context, requestCloudflare *zone.UpdateRuleSetRequestCloudflare) (*zone.RuleList, error) {
-	api, err := cloudflare.New(requestCloudflare.ApiKey, requestCloudflare.ApiEmail)
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -642,7 +713,11 @@ func (s *ZoneServer) UpdateRuleSet(ctx context.Context, requestCloudflare *zone.
 //PageRule CURD
 
 func (s *ZoneServer) ListPageRule(ctx context.Context, requestCloudflare *zone.ListPageRuleRequestCloudflare) (*zone.PageRuleList, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -659,7 +734,11 @@ func (s *ZoneServer) ListPageRule(ctx context.Context, requestCloudflare *zone.L
 }
 
 func (s *ZoneServer) CreatePageRule(ctx context.Context, requestCloudflare *zone.CreatePageRuleRequestCloudflare) (*zone.PageRule, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -676,7 +755,11 @@ func (s *ZoneServer) CreatePageRule(ctx context.Context, requestCloudflare *zone
 }
 
 func (s *ZoneServer) UpdatePageRule(ctx context.Context, requestCloudflare *zone.UpdatePageRuleRequestCloudflare) (*zone.PageRule, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -693,7 +776,11 @@ func (s *ZoneServer) UpdatePageRule(ctx context.Context, requestCloudflare *zone
 }
 
 func (s *ZoneServer) DeletePageRule(ctx context.Context, requestCloudflare *zone.DeletePageRuleRequestCloudflare) (*zone.PageRule, error) {
-	api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		api, err = cloudflare.NewWithAPIToken(requestCloudflare.GetApiKey()[3:])
+	} else {
+		api, err = cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
+	}
 	if err != nil {
 		return nil, err
 	}
@@ -751,9 +838,12 @@ func (s *ZoneServer) UpdatePageRulesPriorities(ctx context.Context, requestCloud
 	if err != nil {
 		return nil, err
 	}
-	req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
-	req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
-
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		req.Header.Add("Authorization", ApiToken2Authorization(requestCloudflare.GetApiKey()[3:]))
+	} else {
+		req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
+		req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
+	}
 	//执行HTTP请求
 	resp, err := client.Do(req)
 	if err != nil {
@@ -802,10 +892,13 @@ func (s *ZoneServer) SetSmartTieredCache(ctx context.Context, requestCloudflare
 	if err != nil {
 		return nil, err
 	}
-	req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
 	req.Header.Add("Content-Type", "application/json")
-	req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
-
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		req.Header.Add("Authorization", ApiToken2Authorization(requestCloudflare.GetApiKey()[3:]))
+	} else {
+		req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
+		req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
+	}
 	//执行HTTP请求
 	resp, err := client.Do(req)
 	if err != nil {
@@ -854,9 +947,13 @@ func (s *ZoneServer) UpdateAlwaysUseHttps(ctx context.Context, requestCloudflare
 	if err != nil {
 		return nil, err
 	}
-	req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
 	req.Header.Add("Content-Type", "application/json")
-	req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
+	if strings.HasPrefix(requestCloudflare.GetApiKey(), TOKEN_PREFIX) {
+		req.Header.Add("Authorization", ApiToken2Authorization(requestCloudflare.GetApiKey()[3:]))
+	} else {
+		req.Header.Add("X-Auth-Key", requestCloudflare.GetApiKey())
+		req.Header.Add("X-Auth-Email", requestCloudflare.GetApiEmail())
+	}
 
 	//执行HTTP请求
 	resp, err := client.Do(req)

+ 4 - 0
server/zone_convert.go

@@ -497,3 +497,7 @@ func Go2ProtoArgoSetting(setting string) *zone.ArgoSetting {
 		return &zone.ArgoSetting{Enabled: false}
 	}
 }
+
+func ApiToken2Authorization(token string) string {
+	return "Bearer " + token
+}