forked from Mxmilu666/frp
all: now messages between frps and frpc can be encryped when use_encryption set true
This commit is contained in:
@@ -164,36 +164,38 @@ func Join(c1 *Conn, c2 *Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
func JoinMore(local *Conn, remote *Conn, cryptoKey string) {
|
||||
// messages from c1 to c2 will be encrypted
|
||||
// and from c2 to c1 will be decrypted
|
||||
func JoinMore(c1 *Conn, c2 *Conn, cryptKey string) {
|
||||
var wait sync.WaitGroup
|
||||
encrypPipe := func(from *Conn, to *Conn, key string) {
|
||||
encryptPipe := func(from *Conn, to *Conn, key string) {
|
||||
defer from.Close()
|
||||
defer to.Close()
|
||||
defer wait.Done()
|
||||
|
||||
// we don't care about errors here
|
||||
PipeEncryptoWriter(from.TcpConn, to.TcpConn, key)
|
||||
PipeEncrypt(from.TcpConn, to.TcpConn, key)
|
||||
}
|
||||
|
||||
decryptoPipe := func(to *Conn, from *Conn, key string) {
|
||||
decryptPipe := func(to *Conn, from *Conn, key string) {
|
||||
defer from.Close()
|
||||
defer to.Close()
|
||||
defer wait.Done()
|
||||
|
||||
// we don't care about errors here
|
||||
PipeDecryptoReader(to.TcpConn, from.TcpConn, key)
|
||||
PipeDecrypt(to.TcpConn, from.TcpConn, key)
|
||||
}
|
||||
|
||||
wait.Add(2)
|
||||
go encrypPipe(local, remote, cryptoKey)
|
||||
go decryptoPipe(remote, local, cryptoKey)
|
||||
go encryptPipe(c1, c2, cryptKey)
|
||||
go decryptPipe(c2, c1, cryptKey)
|
||||
wait.Wait()
|
||||
log.Debug("One tunnel stopped")
|
||||
return
|
||||
}
|
||||
|
||||
// decrypto msg from reader, then write into writer
|
||||
func PipeDecryptoReader(r net.Conn, w net.Conn, key string) error {
|
||||
// decrypt msg from reader, then write into writer
|
||||
func PipeDecrypt(r net.Conn, w net.Conn, key string) error {
|
||||
laes := new(pcrypto.Pcrypto)
|
||||
if err := laes.Init([]byte(key)); err != nil {
|
||||
log.Error("Pcrypto Init error: %v", err)
|
||||
@@ -207,10 +209,10 @@ func PipeDecryptoReader(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("Decrypto [%s] error, %v", string(buf), err)
|
||||
return fmt.Errorf("Decrypto [%s] error: %v", string(buf), err)
|
||||
log.Error("Decrypt [%s] error, %v", string(buf), err)
|
||||
return fmt.Errorf("Decrypt [%s] error: %v", string(buf), err)
|
||||
}
|
||||
|
||||
_, err = w.Write(res)
|
||||
@@ -221,8 +223,8 @@ func PipeDecryptoReader(r net.Conn, w net.Conn, key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// recvive msg from reader, then encrypto msg into write
|
||||
func PipeEncryptoWriter(r net.Conn, w net.Conn, key string) error {
|
||||
// recvive msg from reader, then encrypt msg into write
|
||||
func PipeEncrypt(r net.Conn, w net.Conn, key string) error {
|
||||
laes := new(pcrypto.Pcrypto)
|
||||
if err := laes.Init([]byte(key)); err != nil {
|
||||
log.Error("Pcrypto Init error: %v", err)
|
||||
@@ -237,10 +239,10 @@ func PipeEncryptoWriter(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("Encrypto error: %v", err)
|
||||
return fmt.Errorf("Encrypto error: %v", err)
|
||||
log.Error("Encrypt error: %v", err)
|
||||
return fmt.Errorf("Encrypt error: %v", err)
|
||||
}
|
||||
|
||||
res = append(res, '\n')
|
||||
|
||||
@@ -39,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) {
|
||||
// aes
|
||||
src = pKCS7Padding(src, aes.BlockSize)
|
||||
blockMode := cipher.NewCBCEncrypter(pc.paes, pc.pkey)
|
||||
@@ -57,7 +57,7 @@ func (pc *Pcrypto) Encrypto(src []byte) ([]byte, error) {
|
||||
return []byte(base64.StdEncoding.EncodeToString(zbuf.Bytes())), 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 {
|
||||
|
||||
@@ -19,10 +19,10 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncrypto(t *testing.T) {
|
||||
func TestEncrypt(t *testing.T) {
|
||||
pp := new(Pcrypto)
|
||||
pp.Init([]byte("Hana"))
|
||||
res, err := pp.Encrypto([]byte("Just One Test!"))
|
||||
res, err := pp.Encrypt([]byte("Just One Test!"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -30,15 +30,15 @@ func TestEncrypto(t *testing.T) {
|
||||
fmt.Printf("[%x]\n", res)
|
||||
}
|
||||
|
||||
func TestDecrypto(t *testing.T) {
|
||||
func TestDecrypt(t *testing.T) {
|
||||
pp := new(Pcrypto)
|
||||
pp.Init([]byte("Hana"))
|
||||
res, err := pp.Encrypto([]byte("Just One Test!"))
|
||||
res, err := pp.Encrypt([]byte("Just One Test!"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err = pp.Decrypto(res)
|
||||
res, err = pp.Decrypt(res)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user