From 20afe25ef12713fbedef5c904a885f75f8235fcc Mon Sep 17 00:00:00 2001 From: Gogs Date: Tue, 22 Mar 2016 17:18:05 +0800 Subject: [PATCH] modified: src/frp/models/client/client.go modified: src/frp/models/server/server.go modified: src/frp/utils/conn/conn.go modified: src/frp/utils/pcrypto/pcrypto.go modified: src/frp/utils/pcrypto/pcrypto_test.go --- conf/frpc.ini | 2 +- src/frp/models/client/client.go | 5 +- src/frp/models/server/server.go | 4 +- src/frp/utils/conn/conn.go | 73 +++++++++++++++++++++++++++ src/frp/utils/pcrypto/pcrypto.go | 10 ++-- src/frp/utils/pcrypto/pcrypto_test.go | 14 ----- 6 files changed, 86 insertions(+), 22 deletions(-) diff --git a/conf/frpc.ini b/conf/frpc.ini index 09dfeb7..c5dbb49 100644 --- a/conf/frpc.ini +++ b/conf/frpc.ini @@ -11,4 +11,4 @@ log_level = debug [test1] passwd = 123 local_ip = 127.0.0.1 -local_port = 22 +local_port = 8000 diff --git a/src/frp/models/client/client.go b/src/frp/models/client/client.go index ce99f09..8ee36ca 100644 --- a/src/frp/models/client/client.go +++ b/src/frp/models/client/client.go @@ -81,6 +81,9 @@ func (p *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err erro // l means local, r means remote log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(), remoteConn.GetLocalAddr(), remoteConn.GetRemoteAddr()) - go conn.Join(localConn, remoteConn) + // go conn.Join(localConn, remoteConn) + go conn.PipeEncryptoWriter(localConn.TcpConn, remoteConn.TcpConn, p.Passwd) + go conn.PipeDecryptoReader(remoteConn.TcpConn, localConn.TcpConn, p.Passwd) + return nil } diff --git a/src/frp/models/server/server.go b/src/frp/models/server/server.go index 0b2c3d4..eb39529 100644 --- a/src/frp/models/server/server.go +++ b/src/frp/models/server/server.go @@ -132,7 +132,9 @@ func (p *ProxyServer) Start() (err error) { // l means local, r means remote log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", cliConn.GetLocalAddr(), cliConn.GetRemoteAddr(), userConn.GetLocalAddr(), userConn.GetRemoteAddr()) - go conn.Join(cliConn, userConn) + // go conn.Join(cliConn, userConn) + go conn.PipeEncryptoWriter(userConn.TcpConn, cliConn.TcpConn, p.Passwd) + go conn.PipeDecryptoReader(cliConn.TcpConn, userConn.TcpConn, p.Passwd) } }() diff --git a/src/frp/utils/conn/conn.go b/src/frp/utils/conn/conn.go index fbf4be9..68cf653 100644 --- a/src/frp/utils/conn/conn.go +++ b/src/frp/utils/conn/conn.go @@ -22,6 +22,7 @@ import ( "sync" "frp/utils/log" + "frp/utils/pcrypto" ) type Listener struct { @@ -127,6 +128,7 @@ func (c *Conn) ReadLine() (buff string, err error) { func (c *Conn) Write(content string) (err error) { _, err = c.TcpConn.Write([]byte(content)) return err + } func (c *Conn) Close() { @@ -161,3 +163,74 @@ func Join(c1 *Conn, c2 *Conn) { wait.Wait() return } + +// decrypto msg from reader, then write into writer +func PipeDecryptoReader(r net.Conn, w net.Conn, key string) { + defer r.Close() + defer w.Close() + + laes := new(pcrypto.Pcrypto) + if err := laes.Init([]byte(key)); err != nil { + log.Error("Pcrypto Init error, [%v]", err) + return + } + + log.Debug("PipeDecryptoReader") + + buf := make([]byte, 10*1024) + + for { + n, err := r.Read(buf) + if err != nil { + log.Error("Conn ReadLine error, [%v]", err) + return + } + + res, err := laes.Decrypto(buf[:n]) + if err != nil { + log.Error("Decrypto error, [%s] [%s]", err, string(buf[:n])) + return + } + + _, err = w.Write(res) + if err != nil { + log.Error("net.Conn Write error, [%v]", err) + return + } + } +} + +// recvive msg from reader, then encrypto msg into write +func PipeEncryptoWriter(r net.Conn, w net.Conn, key string) { + defer r.Close() + defer w.Close() + + laes := new(pcrypto.Pcrypto) + if err := laes.Init([]byte(key)); err != nil { + log.Error("Pcrypto Init error, [%v]", err) + return + } + + log.Debug("PipeEncryptoWriter") + + buf := make([]byte, 10*1024) + + for { + n, err := r.Read(buf) + if err != nil { + log.Error("Conn ReadLine error, [%v]", err) + return + } + res, err := laes.Encrypto(buf[:n]) + if err != nil { + log.Error("Encrypto error, [%v]", err) + return + } + + _, err = w.Write(res) + if err != nil { + log.Error("net.Conn Write error, [%v]", err) + return + } + } +} diff --git a/src/frp/utils/pcrypto/pcrypto.go b/src/frp/utils/pcrypto/pcrypto.go index e260a3e..8a4435e 100644 --- a/src/frp/utils/pcrypto/pcrypto.go +++ b/src/frp/utils/pcrypto/pcrypto.go @@ -33,7 +33,7 @@ type Pcrypto struct { func (pc *Pcrypto) Init(key []byte) error { var err error - pc.pkey = PKCS7Padding(key, aes.BlockSize) + pc.pkey = pKCS7Padding(key, aes.BlockSize) pc.paes, err = aes.NewCipher(pc.pkey) return err @@ -41,7 +41,7 @@ func (pc *Pcrypto) Init(key []byte) error { func (pc *Pcrypto) Encrypto(src []byte) ([]byte, error) { // aes - src = PKCS7Padding(src, aes.BlockSize) + src = pKCS7Padding(src, aes.BlockSize) blockMode := cipher.NewCBCEncrypter(pc.paes, pc.pkey) crypted := make([]byte, len(src)) blockMode.CryptBlocks(crypted, src) @@ -83,18 +83,18 @@ func (pc *Pcrypto) Decrypto(str []byte) ([]byte, error) { blockMode := cipher.NewCBCDecrypter(pc.paes, pc.pkey) blockMode.CryptBlocks(decryptText, decryptText) - decryptText = PKCS7UnPadding(decryptText) + decryptText = pKCS7UnPadding(decryptText) return decryptText, nil } -func PKCS7Padding(ciphertext []byte, blockSize int) []byte { +func pKCS7Padding(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 pKCS7UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] diff --git a/src/frp/utils/pcrypto/pcrypto_test.go b/src/frp/utils/pcrypto/pcrypto_test.go index 43e38f0..016dd52 100644 --- a/src/frp/utils/pcrypto/pcrypto_test.go +++ b/src/frp/utils/pcrypto/pcrypto_test.go @@ -15,7 +15,6 @@ package pcrypto import ( - "crypto/aes" "fmt" "testing" ) @@ -46,16 +45,3 @@ func TestDecrypto(t *testing.T) { fmt.Printf("[%s]\n", string(res)) } - -func TestPKCS7Padding(t *testing.T) { - ltt := []byte("Test_PKCS7Padding") - ltt = PKCS7Padding(ltt, aes.BlockSize) - // fmt.Printf("[%x]\n", (ltt)) -} - -func TestPKCS7UnPadding(t *testing.T) { - ltt := []byte("Test_PKCS7Padding") - ltt = PKCS7Padding(ltt, aes.BlockSize) - ltt = PKCS7UnPadding(ltt) - // fmt.Printf("[%x]\n", ltt) -}