管理端全部功能跑通
This commit is contained in:
7
EdgeHttpDNS/internal/encrypt/method.go
Normal file
7
EdgeHttpDNS/internal/encrypt/method.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package encrypt
|
||||
|
||||
type MethodInterface interface {
|
||||
Init(key []byte, iv []byte) error
|
||||
Encrypt(src []byte) (dst []byte, err error)
|
||||
Decrypt(dst []byte) (src []byte, err error)
|
||||
}
|
||||
64
EdgeHttpDNS/internal/encrypt/method_aes_256_cfb.go
Normal file
64
EdgeHttpDNS/internal/encrypt/method_aes_256_cfb.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package encrypt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
)
|
||||
|
||||
type AES256CFBMethod struct {
|
||||
block cipher.Block
|
||||
iv []byte
|
||||
}
|
||||
|
||||
func (m *AES256CFBMethod) Init(key, iv []byte) error {
|
||||
keyLen := len(key)
|
||||
if keyLen > 32 {
|
||||
key = key[:32]
|
||||
} else if keyLen < 32 {
|
||||
key = append(key, bytes.Repeat([]byte{' '}, 32-keyLen)...)
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.block = block
|
||||
|
||||
ivLen := len(iv)
|
||||
if ivLen > aes.BlockSize {
|
||||
iv = iv[:aes.BlockSize]
|
||||
} else if ivLen < aes.BlockSize {
|
||||
iv = append(iv, bytes.Repeat([]byte{' '}, aes.BlockSize-ivLen)...)
|
||||
}
|
||||
m.iv = iv
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *AES256CFBMethod) Encrypt(src []byte) (dst []byte, err error) {
|
||||
if len(src) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = RecoverMethodPanic(recover())
|
||||
}()
|
||||
|
||||
dst = make([]byte, len(src))
|
||||
cipher.NewCFBEncrypter(m.block, m.iv).XORKeyStream(dst, src)
|
||||
return
|
||||
}
|
||||
|
||||
func (m *AES256CFBMethod) Decrypt(dst []byte) (src []byte, err error) {
|
||||
if len(dst) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = RecoverMethodPanic(recover())
|
||||
}()
|
||||
|
||||
src = make([]byte, len(dst))
|
||||
cipher.NewCFBDecrypter(m.block, m.iv).XORKeyStream(src, dst)
|
||||
return
|
||||
}
|
||||
15
EdgeHttpDNS/internal/encrypt/method_raw.go
Normal file
15
EdgeHttpDNS/internal/encrypt/method_raw.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package encrypt
|
||||
|
||||
type RawMethod struct{}
|
||||
|
||||
func (m *RawMethod) Init(key []byte, iv []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *RawMethod) Encrypt(src []byte) (dst []byte, err error) {
|
||||
return src, nil
|
||||
}
|
||||
|
||||
func (m *RawMethod) Decrypt(dst []byte) (src []byte, err error) {
|
||||
return dst, nil
|
||||
}
|
||||
40
EdgeHttpDNS/internal/encrypt/method_utils.go
Normal file
40
EdgeHttpDNS/internal/encrypt/method_utils.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package encrypt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var methods = map[string]reflect.Type{
|
||||
"raw": reflect.TypeOf(new(RawMethod)).Elem(),
|
||||
"aes-256-cfb": reflect.TypeOf(new(AES256CFBMethod)).Elem(),
|
||||
}
|
||||
|
||||
func NewMethodInstance(method string, key string, iv string) (MethodInterface, error) {
|
||||
valueType, ok := methods[method]
|
||||
if !ok {
|
||||
return nil, errors.New("method '" + method + "' not found")
|
||||
}
|
||||
|
||||
instance, ok := reflect.New(valueType).Interface().(MethodInterface)
|
||||
if !ok {
|
||||
return nil, errors.New("method '" + method + "' must implement MethodInterface")
|
||||
}
|
||||
|
||||
err := instance.Init([]byte(key), []byte(iv))
|
||||
return instance, err
|
||||
}
|
||||
|
||||
func RecoverMethodPanic(err interface{}) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if s, ok := err.(string); ok {
|
||||
return errors.New(s)
|
||||
}
|
||||
if e, ok := err.(error); ok {
|
||||
return e
|
||||
}
|
||||
return errors.New("unknown error")
|
||||
}
|
||||
Reference in New Issue
Block a user