Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change tecent api_gw to http sfc url #92

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 DVK
Copyright (c) 2024 DVK

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
90 changes: 35 additions & 55 deletions pkg/sdk/tencent/tencent_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416"

"github.com/DVKunion/SeaMoon/pkg/api/enum"
"github.com/DVKunion/SeaMoon/pkg/api/models"
"github.com/DVKunion/SeaMoon/pkg/system/errors"
"github.com/DVKunion/SeaMoon/pkg/system/xlog"
Expand All @@ -26,22 +25,15 @@ const (
)

type triggerDesc struct {
Api triggerDescApi `json:"api"`
Service triggerDescService `json:"service"`
Release struct {
EnvironmentName string `json:"environmentName"`
} `json:"release"`
AuthType string `json:"AuthType"`
NetConfig *netConfig `json:"NetConfig"`
}

type triggerDescApi struct {
AuthRequired string `json:"authRequired"`
AuthType string `json:"authType"`
RequestConfig struct {
Method string `json:"method"`
Path string `json:"path"`
} `json:"requestConfig"`
IsIntegratedResponse string `json:"isIntegratedResponse"`
IsBase64Encoded string `json:"isBase64Encoded"`
type netConfig struct {
EnableIntranet bool `json:"EnableIntranet"`
EnableExtranet bool `json:"EnableExtranet"`
IntranetUrl string `json:"IntranetUrl"`
ExtranetUrl string `json:"ExtranetUrl"`
}

type triggerDescService struct {
Expand All @@ -52,9 +44,6 @@ type triggerDescService struct {
}

type triggerResp struct {
Service struct {
SubDomain string `json:"subDomain"`
} `json:"service"`
}

type fcInfo struct {
Expand Down Expand Up @@ -291,36 +280,25 @@ func deploy(ca *models.CloudAuth, tun *models.Tunnel) (string, string, error) {
}

// 尝试创建触发器
// 2024.07.01 腾讯云停止了 API GATEWAY 服务
// 改为了创建函数 URL
// https://cloud.tencent.com/document/product/583/96099
r := scf.NewCreateTriggerRequest()
r.TriggerName = common.StringPtr("apigw")
r.TriggerName = common.StringPtr("http")
r.FunctionName = common.StringPtr(fcName)
r.Type = common.StringPtr("apigw")
r.Type = common.StringPtr("http")

config, err := json.Marshal(&triggerDesc{
Api: triggerDescApi{
AuthRequired: func() string {
if tun.Config.FcAuthType == enum.AuthParis {
return "TRUE"
}
return "FALSE"
}(),
RequestConfig: struct {
Method string `json:"method"`
Path string `json:"path"`
}{Method: "GET", Path: "/"},
IsIntegratedResponse: "FALSE",
IsBase64Encoded: "FALSE",
},
Service: triggerDescService{
ServiceName: "SCF_API_SERVICE",
ServiceType: "BASIC",
AuthType: "NONE", // todo 增加 auth
NetConfig: &netConfig{
true,
true,
"",
"",
},
Release: struct {
EnvironmentName string `json:"environmentName"`
}{EnvironmentName: "release"},
})

// 触发器配置参数叫做 desc...
//
//触发器配置参数叫做 desc...
r.TriggerDesc = common.StringPtr(string(config))
r.Namespace = common.StringPtr(serviceName)

Expand All @@ -329,13 +307,13 @@ func deploy(ca *models.CloudAuth, tun *models.Tunnel) (string, string, error) {
return "", "", err
}

extractor := &triggerResp{}
extractor := &triggerDesc{}
desc := *response.Response.TriggerInfo.TriggerDesc
if err := json.Unmarshal([]byte(desc), extractor); err != nil {
return "", "", err
}

return extractor.Service.SubDomain, uid, nil
return extractor.NetConfig.ExtranetUrl, uid, nil
}

func destroy(ca *models.CloudAuth, tun *models.Tunnel) error {
Expand All @@ -356,15 +334,16 @@ func destroy(ca *models.CloudAuth, tun *models.Tunnel) error {

fcName := *tun.Name

// 2024.07.01 无需触发器了
// 先删除触发器
r := scf.NewDeleteTriggerRequest()
r.TriggerName = common.StringPtr("apigw")
r.Type = common.StringPtr("apigw")
r.FunctionName = common.StringPtr(fcName)
r.Namespace = common.StringPtr(serviceName)
if _, err = client.DeleteTrigger(r); err != nil {
return err
}
//r := scf.NewDeleteTriggerRequest()
//r.TriggerName = common.StringPtr("http")
//r.Type = common.StringPtr("http")
//r.FunctionName = common.StringPtr(fcName)
//r.Namespace = common.StringPtr(serviceName)
//if _, err = client.DeleteTrigger(r); err != nil {
// return err
//}
// 再删除函数
request := scf.NewDeleteFunctionRequest()
request.FunctionName = common.StringPtr(fcName)
Expand Down Expand Up @@ -440,11 +419,12 @@ func sync(ca *models.CloudAuth, regions []string) ([]fcInfo, error) {
} else {
var tri triggerDesc
err := json.Unmarshal([]byte(*trigger[0].TriggerDesc), &tri)
if err != nil {
if err != nil || tri.NetConfig == nil {
xlog.Error(xlog.SDKTriggerUnmarshalError, "name", *fc.FunctionName, "err", err)
}
target.addr = strings.Replace(tri.Service.SubDomain, "https://", "", -1)
target.auth = tri.Api.AuthType
target.addr = strings.Replace(tri.NetConfig.ExtranetUrl, "https://", "", -1)
// todo
target.auth = "NONE"
}
}

Expand Down