-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lilang
committed
Sep 29, 2022
1 parent
ff4eda3
commit 924c051
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
||
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118" | ||
) | ||
|
||
func handler(w http.ResponseWriter, r *http.Request) { | ||
m, err := url.ParseQuery(r.URL.RawQuery) | ||
if err != nil { | ||
fmt.Fprint(w, err.Error()) | ||
return | ||
} | ||
|
||
ciphertext := m.Get("Ciphertext") | ||
kmsRegion := m.Get("KMSRegion") | ||
if len(ciphertext) == 0 || len(kmsRegion) == 0 { | ||
fmt.Fprint(w, "Ciphertext or KMSRegion is empty") | ||
return | ||
} | ||
|
||
c, err := kms.NewClientWithSecretId(os.Getenv("COS_SECRETID"), os.Getenv("COS_SECRETKEY"), kmsRegion) | ||
if err != nil { | ||
fmt.Fprint(w, err.Error()) | ||
return | ||
} | ||
|
||
req := kms.NewDecryptRequest() | ||
req.CiphertextBlob = &ciphertext | ||
rsp, err := c.Decrypt(req) | ||
if err != nil { | ||
fmt.Fprint(w, err.Error()) | ||
return | ||
} | ||
s, err := base64.StdEncoding.DecodeString(*rsp.Response.Plaintext) | ||
if err != nil { | ||
fmt.Fprint(w, err.Error()) | ||
return | ||
} | ||
fmt.Fprint(w, string(s)) | ||
} | ||
|
||
func GetLocalIp() string { | ||
localIP := "127.0.0.1" | ||
addrSlice, err := net.InterfaceAddrs() | ||
if nil == err { | ||
for _, addr := range addrSlice { | ||
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { | ||
if nil != ipnet.IP.To4() { | ||
localIP = ipnet.IP.String() | ||
break | ||
} | ||
} | ||
} | ||
} | ||
return localIP | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", handler) | ||
http.ListenAndServe(GetLocalIp()+":8082", nil) | ||
} |