Skip to content

Commit

Permalink
release v0.8.1
Browse files Browse the repository at this point in the history
release v0.8.1
  • Loading branch information
fatedier authored Aug 23, 2016
2 parents e1ed666 + 94c7f57 commit 899d683
Show file tree
Hide file tree
Showing 13 changed files with 1,004 additions and 131 deletions.
4 changes: 2 additions & 2 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/cmd/frps/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/fatedier/frp/src/models/consts"
"github.com/fatedier/frp/src/models/metric"
"github.com/fatedier/frp/src/models/msg"
"github.com/fatedier/frp/src/models/server"
"github.com/fatedier/frp/src/utils/conn"
Expand Down Expand Up @@ -228,6 +229,7 @@ func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
} else if req.PrivilegeKey != privilegeKey {
info = fmt.Sprintf("ProxyName [%s], privilege mode authorization failed", req.ProxyName)
log.Warn(info)
log.Debug("PrivilegeKey [%s] and get [%s]", privilegeKey, req.PrivilegeKey)
return
}
} else {
Expand All @@ -240,6 +242,7 @@ func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
} else if req.AuthKey != authKey {
info = fmt.Sprintf("ProxyName [%s], authorization failed", req.ProxyName)
log.Warn(info)
log.Debug("AuthKey [%s] and get [%s]", authKey, req.AuthKey)
return
}
}
Expand Down Expand Up @@ -298,6 +301,9 @@ func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
return
}

// update metric's proxy status
metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip, s.PrivilegeMode, s.CustomDomains, s.ListenPort)

// start proxy and listen for user connections, no block
err := s.Start(c)
if err != nil {
Expand Down
62 changes: 47 additions & 15 deletions src/utils/pcrypto/pcrypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
)

Expand All @@ -33,35 +34,47 @@ type Pcrypto struct {

func (pc *Pcrypto) Init(key []byte) error {
var err error
pc.pkey = pKCS7Padding(key, aes.BlockSize)
pc.pkey = pkKeyPadding(key)
pc.paes, err = aes.NewCipher(pc.pkey)
return err
}

func (pc *Pcrypto) Encrypt(src []byte) ([]byte, error) {
// aes
src = pKCS7Padding(src, aes.BlockSize)
blockMode := cipher.NewCBCEncrypter(pc.paes, pc.pkey)
crypted := make([]byte, len(src))
blockMode.CryptBlocks(crypted, src)
return crypted, nil
src = pKCS5Padding(src, aes.BlockSize)
ciphertext := make([]byte, aes.BlockSize+len(src))

// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
blockMode := cipher.NewCBCEncrypter(pc.paes, iv)
blockMode.CryptBlocks(ciphertext[aes.BlockSize:], src)
return ciphertext, nil
}

func (pc *Pcrypto) Decrypt(str []byte) ([]byte, error) {
// aes
decryptText, err := hex.DecodeString(fmt.Sprintf("%x", str))
ciphertext, err := hex.DecodeString(fmt.Sprintf("%x", str))
if err != nil {
return nil, err
}

if len(decryptText)%aes.BlockSize != 0 {
return nil, errors.New("crypto/cipher: ciphertext is not a multiple of the block size")
if len(ciphertext) < aes.BlockSize {
return nil, fmt.Errorf("ciphertext too short")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]

blockMode := cipher.NewCBCDecrypter(pc.paes, pc.pkey)
if len(ciphertext)%aes.BlockSize != 0 {
return nil, fmt.Errorf("crypto/cipher: ciphertext is not a multiple of the block size")
}

blockMode.CryptBlocks(decryptText, decryptText)
return pKCS7UnPadding(decryptText), nil
blockMode := cipher.NewCBCDecrypter(pc.paes, iv)
blockMode.CryptBlocks(ciphertext, ciphertext)
return pKCS5UnPadding(ciphertext), nil
}

func (pc *Pcrypto) Compression(src []byte) ([]byte, error) {
Expand All @@ -87,13 +100,32 @@ func (pc *Pcrypto) Decompression(src []byte) ([]byte, error) {
return str, nil
}

func pKCS7Padding(ciphertext []byte, blockSize int) []byte {
func pkKeyPadding(key []byte) []byte {
l := len(key)
if l == 16 || l == 24 || l == 32 {
return key
}
if l < 16 {
return append(key, bytes.Repeat([]byte{byte(0)}, 16-l)...)
} else if l < 24 {
return append(key, bytes.Repeat([]byte{byte(0)}, 24-l)...)
} else if l < 32 {
return append(key, bytes.Repeat([]byte{byte(0)}, 32-l)...)
} else {
md5Ctx := md5.New()
md5Ctx.Write(key)
md5Str := md5Ctx.Sum(nil)
return []byte(hex.EncodeToString(md5Str))
}
}

func pKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}

func pKCS7UnPadding(origData []byte) []byte {
func pKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
Expand Down
17 changes: 16 additions & 1 deletion src/utils/pcrypto/pcrypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (

func init() {
pp = &Pcrypto{}
pp.Init([]byte("Hana"))
pp.Init([]byte("12234567890123451223456789012345321:wq"))
}

func TestEncrypt(t *testing.T) {
Expand Down Expand Up @@ -60,3 +60,18 @@ func TestCompression(t *testing.T) {
t.Fatalf("test compression error, from [%s] to [%s]", testStr, string(res))
}
}

func BenchmarkEncrypt(b *testing.B) {
testStr := "Test Encrypt!"
for i := 0; i < b.N; i++ {
pp.Encrypt([]byte(testStr))
}
}

func BenchmarkDecrypt(b *testing.B) {
testStr := "Test Encrypt!"
res, _ := pp.Encrypt([]byte(testStr))
for i := 0; i < b.N; i++ {
pp.Decrypt([]byte(res))
}
}
28 changes: 28 additions & 0 deletions vendor/github.com/astaxie/beego/logs/color.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 899d683

Please sign in to comment.