From b3ee746be8370895a664d03a1ba46c5d9b118995 Mon Sep 17 00:00:00 2001 From: fatedier Date: Tue, 5 Apr 2016 15:25:55 +0800 Subject: [PATCH 1/2] optimize compress --- src/frp/utils/conn/conn.go | 4 +-- src/frp/utils/pcrypto/pcrypto.go | 54 ++++++++++++++------------------ 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/frp/utils/conn/conn.go b/src/frp/utils/conn/conn.go index eb064c4..a7df146 100644 --- a/src/frp/utils/conn/conn.go +++ b/src/frp/utils/conn/conn.go @@ -209,7 +209,7 @@ func PipeDecrypt(r net.Conn, w net.Conn, key string) error { return err } - res, err := laes.Decrypt(buf) + res, err := laes.Decrypto(buf) if err != nil { log.Error("Decrypt [%s] error, %v", string(buf), err) return fmt.Errorf("Decrypt [%s] error: %v", string(buf), err) @@ -239,7 +239,7 @@ func PipeEncrypt(r net.Conn, w net.Conn, key string) error { if err != nil { return err } - res, err := laes.Encrypt(buf[:n]) + res, err := laes.Encrypto(buf[:n]) if err != nil { log.Error("Encrypt error: %v", err) return fmt.Errorf("Encrypt error: %v", err) diff --git a/src/frp/utils/pcrypto/pcrypto.go b/src/frp/utils/pcrypto/pcrypto.go index c2a2517..308a41f 100644 --- a/src/frp/utils/pcrypto/pcrypto.go +++ b/src/frp/utils/pcrypto/pcrypto.go @@ -1,17 +1,3 @@ -// Copyright 2016 fatedier, fatedier@gmail.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package pcrypto import ( @@ -39,37 +25,37 @@ func (pc *Pcrypto) Init(key []byte) error { return err } -func (pc *Pcrypto) Encrypt(src []byte) ([]byte, error) { +func (pc *Pcrypto) Encrypto(src []byte) ([]byte, error) { + // gzip + var zbuf bytes.Buffer + zwr, err := gzip.NewWriterLevel(&zbuf, -1) + if err != nil { + return nil, err + } + defer zwr.Close() + zwr.Write(src) + zwr.Flush() + fmt.Println("com,src,en", len(src), len(zbuf.Bytes())) + // aes - src = pKCS7Padding(src, aes.BlockSize) + src = pKCS7Padding(zbuf.Bytes(), aes.BlockSize) blockMode := cipher.NewCBCEncrypter(pc.paes, pc.pkey) crypted := make([]byte, len(src)) blockMode.CryptBlocks(crypted, src) - // gzip - var zbuf bytes.Buffer - zwr := gzip.NewWriter(&zbuf) - defer zwr.Close() - zwr.Write(crypted) - zwr.Flush() + fmt.Println("aes,src,en", len(src), len(crypted)) // base64 - return []byte(base64.StdEncoding.EncodeToString(zbuf.Bytes())), nil + return []byte(base64.StdEncoding.EncodeToString(crypted)), nil } -func (pc *Pcrypto) Decrypt(str []byte) ([]byte, error) { +func (pc *Pcrypto) Decrypto(str []byte) ([]byte, error) { // base64 data, err := base64.StdEncoding.DecodeString(string(str)) if err != nil { return nil, err } - // gunzip - zbuf := bytes.NewBuffer(data) - zrd, _ := gzip.NewReader(zbuf) - defer zrd.Close() - data, _ = ioutil.ReadAll(zrd) - // aes decryptText, err := hex.DecodeString(fmt.Sprintf("%x", data)) if err != nil { @@ -85,7 +71,13 @@ func (pc *Pcrypto) Decrypt(str []byte) ([]byte, error) { blockMode.CryptBlocks(decryptText, decryptText) decryptText = pKCS7UnPadding(decryptText) - return decryptText, nil + // gunzip + zbuf := bytes.NewBuffer(decryptText) + zrd, _ := gzip.NewReader(zbuf) + defer zrd.Close() + data, _ = ioutil.ReadAll(zrd) + + return data, nil } func pKCS7Padding(ciphertext []byte, blockSize int) []byte { From 718e707b77f97af6cc7bf8ad3c5e70630ff17458 Mon Sep 17 00:00:00 2001 From: fatedier Date: Tue, 5 Apr 2016 15:40:32 +0800 Subject: [PATCH 2/2] pcrypto: fix --- src/frp/utils/conn/conn.go | 4 ++-- src/frp/utils/pcrypto/pcrypto.go | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/frp/utils/conn/conn.go b/src/frp/utils/conn/conn.go index a7df146..eb064c4 100644 --- a/src/frp/utils/conn/conn.go +++ b/src/frp/utils/conn/conn.go @@ -209,7 +209,7 @@ func PipeDecrypt(r net.Conn, w net.Conn, key string) error { return err } - res, err := laes.Decrypto(buf) + res, err := laes.Decrypt(buf) if err != nil { log.Error("Decrypt [%s] error, %v", string(buf), err) return fmt.Errorf("Decrypt [%s] error: %v", string(buf), err) @@ -239,7 +239,7 @@ func PipeEncrypt(r net.Conn, w net.Conn, key string) error { if err != nil { return err } - res, err := laes.Encrypto(buf[:n]) + res, err := laes.Encrypt(buf[:n]) if err != nil { log.Error("Encrypt error: %v", err) return fmt.Errorf("Encrypt error: %v", err) diff --git a/src/frp/utils/pcrypto/pcrypto.go b/src/frp/utils/pcrypto/pcrypto.go index 308a41f..ebddfac 100644 --- a/src/frp/utils/pcrypto/pcrypto.go +++ b/src/frp/utils/pcrypto/pcrypto.go @@ -1,3 +1,17 @@ +// Copyright 2016 fatedier, fatedier@gmail.com +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package pcrypto import ( @@ -25,7 +39,7 @@ func (pc *Pcrypto) Init(key []byte) error { return err } -func (pc *Pcrypto) Encrypto(src []byte) ([]byte, error) { +func (pc *Pcrypto) Encrypt(src []byte) ([]byte, error) { // gzip var zbuf bytes.Buffer zwr, err := gzip.NewWriterLevel(&zbuf, -1) @@ -49,7 +63,7 @@ func (pc *Pcrypto) Encrypto(src []byte) ([]byte, error) { return []byte(base64.StdEncoding.EncodeToString(crypted)), nil } -func (pc *Pcrypto) Decrypto(str []byte) ([]byte, error) { +func (pc *Pcrypto) Decrypt(str []byte) ([]byte, error) { // base64 data, err := base64.StdEncoding.DecodeString(string(str)) if err != nil {