// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . //go:build plus package oss import ( "crypto/tls" "errors" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ossconfigs" teaconst "github.com/TeaOSLab/EdgeNode/internal/const" "github.com/qiniu/go-sdk/v7/auth/qbox" "github.com/qiniu/go-sdk/v7/storage" "net/http" "time" ) type QiniuKodoProvider struct { isPublic bool domain string mac *qbox.Mac httpClient *http.Client } func NewQiniuKodoProvider() *QiniuKodoProvider { return &QiniuKodoProvider{} } func (this *QiniuKodoProvider) Init(options ossconfigs.OSSOptions, bucketName string) error { realOptions, ok := options.(*ossconfigs.QiniuKodoProviderOptions) if !ok { return errors.New("invalid options for 'QiniuKodoProvider'") } this.isPublic = realOptions.IsPublic this.domain = realOptions.Protocol + "://" + realOptions.Domain this.mac = qbox.NewMac(realOptions.AccessKey, realOptions.SecretKey) this.httpClient = &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, }, } return nil } func (this *QiniuKodoProvider) Head(key string) (httpResponse *http.Response, nativeErrCode string, err error) { var url string if this.isPublic { url = storage.MakePublicURLv2(this.domain, key) } else { var deadline = time.Now().Add(1 * time.Hour).Unix() // 1小时有效期 url = storage.MakePrivateURLv2(this.mac, this.domain, key, deadline) } req, reqErr := http.NewRequest(http.MethodHead, url, nil) if reqErr != nil { err = reqErr return } req.Header.Set("User-Agent", teaconst.GlobalProductName+"-Node/"+teaconst.Version) httpResponse, err = this.httpClient.Do(req) return } func (this *QiniuKodoProvider) Get(key string) (httpResponse *http.Response, nativeErrCode string, err error) { var url string if this.isPublic { url = storage.MakePublicURLv2(this.domain, key) } else { var deadline = time.Now().Add(1 * time.Hour).Unix() // 1小时有效期 url = storage.MakePrivateURLv2(this.mac, this.domain, key, deadline) } req, reqErr := http.NewRequest(http.MethodGet, url, nil) if reqErr != nil { err = reqErr return } req.Header.Set("User-Agent", teaconst.GlobalProductName+"-Node/"+teaconst.Version) httpResponse, err = this.httpClient.Do(req) return } func (this *QiniuKodoProvider) GetRange(key string, bytesRange string) (httpResponse *http.Response, nativeErrCode string, err error) { var url string if this.isPublic { url = storage.MakePublicURLv2(this.domain, key) } else { var deadline = time.Now().Add(1 * time.Hour).Unix() // 1小时有效期 url = storage.MakePrivateURLv2(this.mac, this.domain, key, deadline) } req, reqErr := http.NewRequest(http.MethodGet, url, nil) if reqErr != nil { err = reqErr return } req.Header.Set("User-Agent", teaconst.GlobalProductName+"-Node/"+teaconst.Version) req.Header.Set("Range", bytesRange) httpResponse, err = this.httpClient.Do(req) return }