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
This commit is contained in:
Gogs 2016-03-22 17:18:05 +08:00
parent 90349a48b0
commit 20afe25ef1
6 changed files with 86 additions and 22 deletions

View File

@ -11,4 +11,4 @@ log_level = debug
[test1]
passwd = 123
local_ip = 127.0.0.1
local_port = 22
local_port = 8000

View File

@ -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
}

View File

@ -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)
}
}()

View File

@ -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
}
}
}

View File

@ -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)]

View File

@ -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)
}