From 34bd21efc4d1a60c29f4c41f6701e52240bf97de Mon Sep 17 00:00:00 2001 From: Star Date: Fri, 20 Dec 2024 18:35:59 +0800 Subject: [PATCH] 1 --- .gitignore | 8 +++++++ COS.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 24 +++++++++++++++++++ main.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main_test.go | 23 ++++++++++++++++++ main_test.js | 11 +++++++++ 6 files changed, 196 insertions(+) create mode 100644 .gitignore create mode 100644 COS.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 main_test.go create mode 100644 main_test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ab6cee --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.* +!.gitignore +go.sum +env.yml +build +release +node_modules +package.json diff --git a/COS.go b/COS.go new file mode 100644 index 0000000..d373695 --- /dev/null +++ b/COS.go @@ -0,0 +1,62 @@ +package tencent + +import ( + "strings" + + sts "github.com/tencentyun/qcloud-cos-sts-sdk/go" +) + +func getCosClient(bucket string) *sts.Client { + return sts.NewClient(conf.Cos[bucket].SecretId, conf.Cos[bucket].SecretKey, nil) +} + +type Cos struct { + client *sts.Client + bucket string + appId string + region string +} + +func GetCos(bucket string) *Cos { + bConf := conf.Cos[bucket] + a := strings.Split(bConf.Bucket, "-") + return &Cos{ + client: getCosClient(bucket), + bucket: bConf.Bucket, + appId: a[len(a)-1], + region: bConf.Region, + } +} + +type CosToken struct { + sts.Credentials + Bucket string + Region string +} + +var defaultAllowPath = "/" + +func (c *Cos) GetToken(allowPath *string) (*CosToken, error) { + if allowPath == nil { + allowPath = &defaultAllowPath + } + crt, err := c.client.GetCredential(&sts.CredentialOptions{ + DurationSeconds: 3600, + Region: c.region, + Policy: &sts.CredentialPolicy{ + Statement: []sts.CredentialPolicyStatement{{ + Action: []string{"name/cos:PostObject", "name/cos:PutObject", "name/cos:InitiateMultipartUpload", "name/cos:ListMultipartUploads", "name/cos:ListParts", "name/cos:UploadPart", "name/cos:CompleteMultipartUpload"}, + Effect: "allow", + Resource: []string{"qcs::cos:" + c.region + ":uid/" + c.appId + ":" + c.bucket + *allowPath + "*"}, + }}, + }, + }) + if err == nil { + return &CosToken{ + Credentials: *crt.Credentials, + Bucket: c.bucket, + Region: c.region, + }, nil + } + return nil, err +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b7f10fa --- /dev/null +++ b/go.mod @@ -0,0 +1,24 @@ +module apigo.cc/cloud/tencent + +go 1.18 + +require ( + apigo.cc/gojs v0.0.10 + apigo.cc/gojs/console v0.0.2 + github.com/ssgo/config v1.7.9 + github.com/ssgo/u v1.7.13 + github.com/tencentyun/qcloud-cos-sts-sdk v0.0.0-20241118064430-63a76784514f +) + +require ( + github.com/dlclark/regexp2 v1.11.4 // indirect + github.com/fsnotify/fsnotify v1.8.0 // indirect + github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect + github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd // indirect + github.com/ssgo/log v1.7.7 // indirect + github.com/ssgo/standard v1.7.7 // indirect + github.com/ssgo/tool v0.4.27 // indirect + golang.org/x/sys v0.28.0 // indirect + golang.org/x/text v0.21.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..5397eef --- /dev/null +++ b/main.go @@ -0,0 +1,68 @@ +package tencent + +import ( + _ "embed" + + "apigo.cc/gojs" + "apigo.cc/gojs/goja" + "github.com/ssgo/config" + "github.com/ssgo/u" +) + +var confAes = u.NewAes([]byte("?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV"), []byte("VFs7@sK61cj^f?HZ")) +var keysIsSet = false + +func SetSSKey(key, iv []byte) { + if !keysIsSet { + confAes = u.NewAes(key, iv) + keysIsSet = true + } +} + +var conf = struct { + Common *struct { + SecretId string + SecretKey string + } + Cos map[string]*struct { + Region string + Bucket string + SecretId string + SecretKey string + } +}{} + +func init() { + obj := gojs.Map{ + "cos": GetCos, + } + + gojs.Register("apigo.cc/cloud/tencent", gojs.Module{ + ObjectMaker: func(vm *goja.Runtime) gojs.Map { + config.LoadConfig("tencent", &conf) + conf.Common.SecretId = confAes.DecryptUrlBase64ToString(conf.Common.SecretId) + conf.Common.SecretKey = confAes.DecryptUrlBase64ToString(conf.Common.SecretKey) + for _, v := range conf.Cos { + v.SecretId, v.SecretKey = makeSecretIdAndKey(v.SecretId, v.SecretKey) + } + + return gojs.ToMap(obj) + }, + TsCode: gojs.MakeTSCode(obj), + SetSSKey: SetSSKey, + }) +} + +func makeSecretIdAndKey(secretId, SecretKey string) (string, string) { + if secretId != "" { + secretId = confAes.DecryptUrlBase64ToString(secretId) + } else { + secretId = conf.Common.SecretId + } + if SecretKey != "" { + SecretKey = confAes.DecryptUrlBase64ToString(SecretKey) + } else { + SecretKey = conf.Common.SecretKey + } + return secretId, SecretKey +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..fb3e01b --- /dev/null +++ b/main_test.go @@ -0,0 +1,23 @@ +package tencent_test + +import ( + "fmt" + "testing" + + _ "apigo.cc/cloud/tencent" + "apigo.cc/gojs" + _ "apigo.cc/gojs/console" + "github.com/ssgo/u" +) + +func TestHash(t *testing.T) { + gojs.ExportForDev() + r, err := gojs.RunFile("main_test.js") + if err != nil { + t.Fatal(err) + } else if r != true { + t.Fatal(r) + } else { + fmt.Println(u.BGreen("test succeess")) + } +} diff --git a/main_test.js b/main_test.js new file mode 100644 index 0000000..bec9097 --- /dev/null +++ b/main_test.js @@ -0,0 +1,11 @@ +import tencent from 'apigo.cc/cloud/tencent' +import co from 'apigo.cc/gojs/console' + +try { + let lao = tencent.cos('lao') + co.info(JSON.stringify(lao.getToken())) +} catch (ex) { + co.error(ex) +} + +return true