all: now messages between frps and frpc can be encryped when use_encryption set true

pull/12/head
fatedier 9 years ago
parent 52f99bbc00
commit 80fc76da52

@ -8,7 +8,7 @@ frp is a fast reverse proxy to help you expose a local server behind a NAT or fi
## Status ## Status
frp is under development and you can try it with available version 0.2.0. frp is under development and you can try it with available version 0.3.0.
## Quick Start ## Quick Start

@ -12,3 +12,5 @@ log_level = debug
passwd = 123 passwd = 123
local_ip = 127.0.0.1 local_ip = 127.0.0.1
local_port = 22 local_port = 22
# true or false, if true, messages between frps and frpc will be encrypted, default is false
use_encryption = true

@ -131,9 +131,10 @@ func loginToServer(cli *client.ProxyClient) (c *conn.Conn, err error) {
} }
req := &msg.ControlReq{ req := &msg.ControlReq{
Type: consts.NewCtlConn, Type: consts.NewCtlConn,
ProxyName: cli.Name, ProxyName: cli.Name,
Passwd: cli.Passwd, Passwd: cli.Passwd,
UseEncryption: cli.UseEncryption,
} }
buf, _ := json.Marshal(req) buf, _ := json.Marshal(req)
err = c.Write(string(buf) + "\n") err = c.Write(string(buf) + "\n")

@ -212,6 +212,9 @@ func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) {
return return
} }
// set infomations from frpc
s.UseEncryption = req.UseEncryption
// start proxy and listen for user connections, no block // start proxy and listen for user connections, no block
err := s.Start() err := s.Start()
if err != nil { if err != nil {

@ -24,10 +24,11 @@ import (
) )
type ProxyClient struct { type ProxyClient struct {
Name string Name string
Passwd string Passwd string
LocalIp string LocalIp string
LocalPort int64 LocalPort int64
UseEncryption bool
} }
func (p *ProxyClient) GetLocalConn() (c *conn.Conn, err error) { func (p *ProxyClient) GetLocalConn() (c *conn.Conn, err error) {
@ -81,8 +82,11 @@ func (p *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err erro
// l means local, r means remote // l means local, r means remote
log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(), log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(),
remoteConn.GetLocalAddr(), remoteConn.GetRemoteAddr()) remoteConn.GetLocalAddr(), remoteConn.GetRemoteAddr())
// go conn.Join(localConn, remoteConn) if p.UseEncryption {
go conn.JoinMore(localConn, remoteConn, p.Passwd) go conn.JoinMore(localConn, remoteConn, p.Passwd)
} else {
go conn.Join(localConn, remoteConn)
}
return nil return nil
} }

@ -73,19 +73,23 @@ func LoadConf(confFile string) (err error) {
for name, section := range conf { for name, section := range conf {
if name != "common" { if name != "common" {
proxyClient := &ProxyClient{} proxyClient := &ProxyClient{}
// name
proxyClient.Name = name proxyClient.Name = name
// passwd
proxyClient.Passwd, ok = section["passwd"] proxyClient.Passwd, ok = section["passwd"]
if !ok { if !ok {
return fmt.Errorf("Parse ini file error: proxy [%s] no passwd found", proxyClient.Name) return fmt.Errorf("Parse ini file error: proxy [%s] no passwd found", proxyClient.Name)
} }
// local_ip
proxyClient.LocalIp, ok = section["local_ip"] proxyClient.LocalIp, ok = section["local_ip"]
if !ok { if !ok {
// use 127.0.0.1 as default // use 127.0.0.1 as default
proxyClient.LocalIp = "127.0.0.1" proxyClient.LocalIp = "127.0.0.1"
} }
// local_port
portStr, ok := section["local_port"] portStr, ok := section["local_port"]
if ok { if ok {
proxyClient.LocalPort, err = strconv.ParseInt(portStr, 10, 64) proxyClient.LocalPort, err = strconv.ParseInt(portStr, 10, 64)
@ -96,6 +100,13 @@ func LoadConf(confFile string) (err error) {
return fmt.Errorf("Parse ini file error: proxy [%s] local_port not found", proxyClient.Name) return fmt.Errorf("Parse ini file error: proxy [%s] local_port not found", proxyClient.Name)
} }
// use_encryption
proxyClient.UseEncryption = false
useEncryptionStr, ok := section["use_encryption"]
if ok && useEncryptionStr == "true" {
proxyClient.UseEncryption = true
}
ProxyClients[proxyClient.Name] = proxyClient ProxyClients[proxyClient.Name] = proxyClient
} }
} }

@ -21,9 +21,10 @@ type GeneralRes struct {
// messages between control connection of frpc and frps // messages between control connection of frpc and frps
type ControlReq struct { type ControlReq struct {
Type int64 `json:"type"` Type int64 `json:"type"`
ProxyName string `json:"proxy_name,omitempty"` ProxyName string `json:"proxy_name,omitempty"`
Passwd string `json:"passwd, omitempty"` Passwd string `json:"passwd, omitempty"`
UseEncryption bool `json:"use_encryption, omitempty"`
} }
type ControlRes struct { type ControlRes struct {

@ -25,11 +25,12 @@ import (
) )
type ProxyServer struct { type ProxyServer struct {
Name string Name string
Passwd string Passwd string
BindAddr string UseEncryption bool
ListenPort int64 BindAddr string
Status int64 ListenPort int64
Status int64
listener *conn.Listener // accept new connection from remote users listener *conn.Listener // accept new connection from remote users
ctlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel ctlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel
@ -132,8 +133,12 @@ func (p *ProxyServer) Start() (err error) {
// l means local, r means remote // l means local, r means remote
log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", workConn.GetLocalAddr(), workConn.GetRemoteAddr(), log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", workConn.GetLocalAddr(), workConn.GetRemoteAddr(),
userConn.GetLocalAddr(), userConn.GetRemoteAddr()) userConn.GetLocalAddr(), userConn.GetRemoteAddr())
// go conn.Join(workConn, userConn)
go conn.JoinMore(userConn, workConn, p.Passwd) if p.UseEncryption {
go conn.JoinMore(userConn, workConn, p.Passwd)
} else {
go conn.Join(userConn, workConn)
}
} }
}() }()

@ -164,36 +164,38 @@ func Join(c1 *Conn, c2 *Conn) {
return 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 var wait sync.WaitGroup
encrypPipe := func(from *Conn, to *Conn, key string) { encryptPipe := func(from *Conn, to *Conn, key string) {
defer from.Close() defer from.Close()
defer to.Close() defer to.Close()
defer wait.Done() defer wait.Done()
// we don't care about errors here // 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 from.Close()
defer to.Close() defer to.Close()
defer wait.Done() defer wait.Done()
// we don't care about errors here // we don't care about errors here
PipeDecryptoReader(to.TcpConn, from.TcpConn, key) PipeDecrypt(to.TcpConn, from.TcpConn, key)
} }
wait.Add(2) wait.Add(2)
go encrypPipe(local, remote, cryptoKey) go encryptPipe(c1, c2, cryptKey)
go decryptoPipe(remote, local, cryptoKey) go decryptPipe(c2, c1, cryptKey)
wait.Wait() wait.Wait()
log.Debug("One tunnel stopped") log.Debug("One tunnel stopped")
return return
} }
// decrypto msg from reader, then write into writer // decrypt msg from reader, then write into writer
func PipeDecryptoReader(r net.Conn, w net.Conn, key string) error { func PipeDecrypt(r net.Conn, w net.Conn, key string) error {
laes := new(pcrypto.Pcrypto) laes := new(pcrypto.Pcrypto)
if err := laes.Init([]byte(key)); err != nil { if err := laes.Init([]byte(key)); err != nil {
log.Error("Pcrypto Init error: %v", err) log.Error("Pcrypto Init error: %v", err)
@ -207,10 +209,10 @@ func PipeDecryptoReader(r net.Conn, w net.Conn, key string) error {
return err return err
} }
res, err := laes.Decrypto(buf) res, err := laes.Decrypt(buf)
if err != nil { if err != nil {
log.Error("Decrypto [%s] error, %v", string(buf), err) log.Error("Decrypt [%s] error, %v", string(buf), err)
return fmt.Errorf("Decrypto [%s] error: %v", string(buf), err) return fmt.Errorf("Decrypt [%s] error: %v", string(buf), err)
} }
_, err = w.Write(res) _, err = w.Write(res)
@ -221,8 +223,8 @@ func PipeDecryptoReader(r net.Conn, w net.Conn, key string) error {
return nil return nil
} }
// recvive msg from reader, then encrypto msg into write // recvive msg from reader, then encrypt msg into write
func PipeEncryptoWriter(r net.Conn, w net.Conn, key string) error { func PipeEncrypt(r net.Conn, w net.Conn, key string) error {
laes := new(pcrypto.Pcrypto) laes := new(pcrypto.Pcrypto)
if err := laes.Init([]byte(key)); err != nil { if err := laes.Init([]byte(key)); err != nil {
log.Error("Pcrypto Init error: %v", err) 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 { if err != nil {
return err return err
} }
res, err := laes.Encrypto(buf[:n]) res, err := laes.Encrypt(buf[:n])
if err != nil { if err != nil {
log.Error("Encrypto error: %v", err) log.Error("Encrypt error: %v", err)
return fmt.Errorf("Encrypto error: %v", err) return fmt.Errorf("Encrypt error: %v", err)
} }
res = append(res, '\n') res = append(res, '\n')

@ -39,7 +39,7 @@ func (pc *Pcrypto) Init(key []byte) error {
return err return err
} }
func (pc *Pcrypto) Encrypto(src []byte) ([]byte, error) { func (pc *Pcrypto) Encrypt(src []byte) ([]byte, error) {
// aes // aes
src = pKCS7Padding(src, aes.BlockSize) src = pKCS7Padding(src, aes.BlockSize)
blockMode := cipher.NewCBCEncrypter(pc.paes, pc.pkey) 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 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 // base64
data, err := base64.StdEncoding.DecodeString(string(str)) data, err := base64.StdEncoding.DecodeString(string(str))
if err != nil { if err != nil {

@ -19,10 +19,10 @@ import (
"testing" "testing"
) )
func TestEncrypto(t *testing.T) { func TestEncrypt(t *testing.T) {
pp := new(Pcrypto) pp := new(Pcrypto)
pp.Init([]byte("Hana")) pp.Init([]byte("Hana"))
res, err := pp.Encrypto([]byte("Just One Test!")) res, err := pp.Encrypt([]byte("Just One Test!"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -30,15 +30,15 @@ func TestEncrypto(t *testing.T) {
fmt.Printf("[%x]\n", res) fmt.Printf("[%x]\n", res)
} }
func TestDecrypto(t *testing.T) { func TestDecrypt(t *testing.T) {
pp := new(Pcrypto) pp := new(Pcrypto)
pp.Init([]byte("Hana")) pp.Init([]byte("Hana"))
res, err := pp.Encrypto([]byte("Just One Test!")) res, err := pp.Encrypt([]byte("Just One Test!"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
res, err = pp.Decrypto(res) res, err = pp.Decrypt(res)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

Loading…
Cancel
Save