完善架构

This commit is contained in:
2312708932@qq.com
2024-12-14 14:11:31 +08:00
parent 0417195b66
commit ac0f078167
3 changed files with 48 additions and 13 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ Usage of bin/goodlink-linux-amd64:
-gogo-restart-delay int
自动重启的延迟时间, 单位: 毫秒 (default 1000)
-key string
自定义, 请保证客户端和服务端一致。为避免冲突, 建议16-32个字节长度: {name}_{YYYYMMDDHHMM}, 例如: kony_202412140928
自定义, 客户端和服务端必须一致。16-24个字节长度: {name}_{YYYYMMDDHHMM}, 例如: kony_202412140928
-local string
客户端监听的地址端口
-pprof_addr string
+46 -11
View File
@@ -5,10 +5,30 @@ import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
"goodlink/md5"
)
func getKey(key string) []byte {
if len(key) > 24 {
return []byte(key[:24])
}
// 填充key
for len(key) < 24 {
key = key + "0"
}
return []byte(key)
}
func PKCS7Padding(ciphertext []byte, blocksize int) []byte {
padding := blocksize - len(ciphertext)%blocksize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func Encrypt(origData []byte, key string) string {
k := []byte(key)
k := getKey(key)
block, _ := aes.NewCipher(k)
blockSize := block.BlockSize()
origData = PKCS7Padding(origData, blockSize)
@@ -18,9 +38,15 @@ func Encrypt(origData []byte, key string) string {
return base64.StdEncoding.EncodeToString(cryted)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func Decrypt(cryted []byte, key string) []byte {
crytedByte, _ := base64.StdEncoding.DecodeString(string(cryted))
k := []byte(key)
k := getKey(key)
block, _ := aes.NewCipher(k)
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, k[:blockSize])
@@ -29,14 +55,23 @@ func Decrypt(cryted []byte, key string) []byte {
return PKCS7UnPadding(orig)
}
func PKCS7Padding(ciphertext []byte, blocksize int) []byte {
padding := blocksize - len(ciphertext)%blocksize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func AesTest() {
text := []byte("hello world")
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
temp0 := md5.Encode(string(text))
fmt.Printf("md5后: %s\n", temp0)
key := "1234567812345677777777777781238"
temp1 := Encrypt(text, key)
fmt.Printf("加密后: %s\n", temp1)
temp2 := Decrypt([]byte(temp1), key)
fmt.Printf("解密后: %s\n", temp2)
key = "123456781238"
temp1 = Encrypt(text, key)
fmt.Printf("加密后: %s\n", temp1)
temp2 = Decrypt([]byte(temp1), key)
fmt.Printf("解密后: %s\n", temp2)
}
+1 -1
View File
@@ -32,7 +32,7 @@ func help() {
flag.IntVar(&m_cli_redis_id, "redis_id", 15, "Redis服务可使用的表ID")
flag.StringVar(&m_cli_tun_local_addr, "local", "", "客户端监听的地址端口")
flag.StringVar(&m_cli_tun_remote_addr, "remote", "", "服务端所处网络中, 需要被远程访问的主机地址端口, 例如: 192.168.3.2:9999")
flag.StringVar(&m_cli_tun_key, "key", "", "自定义, 请保证客户端和服务端一致。为避免冲突, 建议16-32个字节长度: {name}_{YYYYMMDDHHMM}, 例如: kony_202412140928")
flag.StringVar(&m_cli_tun_key, "key", "", "自定义, 客户端和服务端必须一致。16-24个字节长度: {name}_{YYYYMMDDHHMM}, 例如: kony_202412140928")
flag.Parse()