mirror of
https://gitee.com/IrisVega/frp.git
synced 2024-11-01 22:31:29 +08:00
Merge branch 'Hurricanezwf-fix_some_bugs' into dev
This commit is contained in:
commit
30da2a2d15
3
.gitignore
vendored
3
.gitignore
vendored
@ -26,3 +26,6 @@ _testmain.go
|
|||||||
# Self
|
# Self
|
||||||
bin/
|
bin/
|
||||||
|
|
||||||
|
# Cache
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
@ -16,6 +16,7 @@ var (
|
|||||||
LogFile string = "./frpc.log"
|
LogFile string = "./frpc.log"
|
||||||
LogLevel string = "warn"
|
LogLevel string = "warn"
|
||||||
LogWay string = "file"
|
LogWay string = "file"
|
||||||
|
HeartBeatInterval int64 = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
var ProxyClients map[string]*models.ProxyClient = make(map[string]*models.ProxyClient)
|
var ProxyClients map[string]*models.ProxyClient = make(map[string]*models.ProxyClient)
|
||||||
|
@ -4,23 +4,67 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fatedier/frp/pkg/models"
|
"github.com/fatedier/frp/pkg/models"
|
||||||
"github.com/fatedier/frp/pkg/utils/conn"
|
"github.com/fatedier/frp/pkg/utils/conn"
|
||||||
"github.com/fatedier/frp/pkg/utils/log"
|
"github.com/fatedier/frp/pkg/utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var isHeartBeatContinue bool = true
|
||||||
|
|
||||||
func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) {
|
func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) {
|
||||||
defer wait.Done()
|
defer wait.Done()
|
||||||
|
|
||||||
c := &conn.Conn{}
|
c := loginToServer(cli)
|
||||||
err := c.ConnectServer(ServerAddr, ServerPort)
|
if c == nil {
|
||||||
if err != nil {
|
log.Error("ProxyName [%s], connect to server failed!", cli.Name)
|
||||||
log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", cli.Name, ServerAddr, ServerPort, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
|
for {
|
||||||
|
// ignore response content now
|
||||||
|
_, err := c.ReadLine()
|
||||||
|
if err == io.EOF {
|
||||||
|
isHeartBeatContinue = false
|
||||||
|
log.Debug("ProxyName [%s], server close this control conn", cli.Name)
|
||||||
|
var sleepTime time.Duration = 1
|
||||||
|
for {
|
||||||
|
log.Debug("ProxyName [%s], try to reconnect to server[%s:%d]...", cli.Name, ServerAddr, ServerPort)
|
||||||
|
tmpConn := loginToServer(cli)
|
||||||
|
if tmpConn != nil {
|
||||||
|
c.Close()
|
||||||
|
c = tmpConn
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if sleepTime < 60 {
|
||||||
|
sleepTime++
|
||||||
|
}
|
||||||
|
time.Sleep(sleepTime * time.Second)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
} else if err != nil {
|
||||||
|
log.Warn("ProxyName [%s], read from server error, %v", cli.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
cli.StartTunnel(ServerAddr, ServerPort)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) {
|
||||||
|
c := &conn.Conn{}
|
||||||
|
|
||||||
|
connection = nil
|
||||||
|
for i := 0; i < 1; i++ {
|
||||||
|
err := c.ConnectServer(ServerAddr, ServerPort)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", cli.Name, ServerAddr, ServerPort, err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
req := &models.ClientCtlReq{
|
req := &models.ClientCtlReq{
|
||||||
Type: models.ControlConn,
|
Type: models.ControlConn,
|
||||||
ProxyName: cli.Name,
|
ProxyName: cli.Name,
|
||||||
@ -30,38 +74,51 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) {
|
|||||||
err = c.Write(string(buf) + "\n")
|
err = c.Write(string(buf) + "\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("ProxyName [%s], write to server error, %v", cli.Name, err)
|
log.Error("ProxyName [%s], write to server error, %v", cli.Name, err)
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := c.ReadLine()
|
res, err := c.ReadLine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("ProxyName [%s], read from server error, %v", cli.Name, err)
|
log.Error("ProxyName [%s], read from server error, %v", cli.Name, err)
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
log.Debug("ProxyName [%s], read [%s]", cli.Name, res)
|
log.Debug("ProxyName [%s], read [%s]", cli.Name, res)
|
||||||
|
|
||||||
clientCtlRes := &models.ClientCtlRes{}
|
clientCtlRes := &models.ClientCtlRes{}
|
||||||
if err = json.Unmarshal([]byte(res), &clientCtlRes); err != nil {
|
if err = json.Unmarshal([]byte(res), &clientCtlRes); err != nil {
|
||||||
log.Error("ProxyName [%s], format server response error, %v", cli.Name, err)
|
log.Error("ProxyName [%s], format server response error, %v", cli.Name, err)
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if clientCtlRes.Code != 0 {
|
if clientCtlRes.Code != 0 {
|
||||||
log.Error("ProxyName [%s], start proxy error, %s", cli.Name, clientCtlRes.Msg)
|
log.Error("ProxyName [%s], start proxy error, %s", cli.Name, clientCtlRes.Msg)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
connection = c
|
||||||
|
go startHeartBeat(connection)
|
||||||
|
log.Debug("ProxyName [%s], connect to server[%s:%d] success!", cli.Name, ServerAddr, ServerPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
if connection == nil {
|
||||||
|
c.Close()
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func startHeartBeat(con *conn.Conn) {
|
||||||
|
isHeartBeatContinue = true
|
||||||
|
log.Debug("Start to send heartbeat")
|
||||||
for {
|
for {
|
||||||
// ignore response content now
|
time.Sleep(time.Duration(HeartBeatInterval) * time.Second)
|
||||||
_, err := c.ReadLine()
|
if isHeartBeatContinue {
|
||||||
if err == io.EOF {
|
err := con.Write("\n")
|
||||||
log.Debug("ProxyName [%s], server close this control conn", cli.Name)
|
if err != nil {
|
||||||
|
log.Error("Send hearbeat to server failed! Err:%s", err.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
}
|
||||||
log.Warn("ProxyName [%s], read from server error, %v", cli.Name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
cli.StartTunnel(ServerAddr, ServerPort)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ var (
|
|||||||
LogFile string = "./frps.log"
|
LogFile string = "./frps.log"
|
||||||
LogLevel string = "warn"
|
LogLevel string = "warn"
|
||||||
LogWay string = "file"
|
LogWay string = "file"
|
||||||
|
HeartBeatTimeout int64 = 30
|
||||||
)
|
)
|
||||||
|
|
||||||
var ProxyServers map[string]*models.ProxyServer = make(map[string]*models.ProxyServer)
|
var ProxyServers map[string]*models.ProxyServer = make(map[string]*models.ProxyServer)
|
||||||
|
@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fatedier/frp/pkg/models"
|
"github.com/fatedier/frp/pkg/models"
|
||||||
"github.com/fatedier/frp/pkg/utils/conn"
|
"github.com/fatedier/frp/pkg/utils/conn"
|
||||||
@ -17,7 +19,7 @@ func ProcessControlConn(l *conn.Listener) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// control connection from every client and server
|
// connection from every client and server
|
||||||
func controlWorker(c *conn.Conn) {
|
func controlWorker(c *conn.Conn) {
|
||||||
// the first message is from client to server
|
// the first message is from client to server
|
||||||
// if error, close connection
|
// if error, close connection
|
||||||
@ -43,17 +45,21 @@ func controlWorker(c *conn.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if needRes {
|
if needRes {
|
||||||
|
// control conn
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
buf, _ := json.Marshal(clientCtlRes)
|
buf, _ := json.Marshal(clientCtlRes)
|
||||||
err = c.Write(string(buf) + "\n")
|
err = c.Write(string(buf) + "\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Write error, %v", err)
|
log.Warn("Write error, %v", err)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// work conn, just return
|
// work conn, just return
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer c.Close()
|
|
||||||
// others is from server to client
|
// others is from server to client
|
||||||
server, ok := ProxyServers[clientCtlReq.ProxyName]
|
server, ok := ProxyServers[clientCtlReq.ProxyName]
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -61,10 +67,16 @@ func controlWorker(c *conn.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read control msg from client
|
||||||
|
go readControlMsgFromClient(server, c)
|
||||||
|
|
||||||
serverCtlReq := &models.ClientCtlReq{}
|
serverCtlReq := &models.ClientCtlReq{}
|
||||||
serverCtlReq.Type = models.WorkConn
|
serverCtlReq.Type = models.WorkConn
|
||||||
for {
|
for {
|
||||||
server.WaitUserConn()
|
_, isStop := server.WaitUserConn()
|
||||||
|
if isStop {
|
||||||
|
break
|
||||||
|
}
|
||||||
buf, _ := json.Marshal(serverCtlReq)
|
buf, _ := json.Marshal(serverCtlReq)
|
||||||
err = c.Write(string(buf) + "\n")
|
err = c.Write(string(buf) + "\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -76,6 +88,7 @@ func controlWorker(c *conn.Conn) {
|
|||||||
log.Debug("ProxyName [%s], write to client to add work conn success", server.Name)
|
log.Debug("ProxyName [%s], write to client to add work conn success", server.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Error("ProxyName [%s], I'm dead!", server.Name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,11 +137,38 @@ func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string,
|
|||||||
|
|
||||||
server.CliConnChan <- c
|
server.CliConnChan <- c
|
||||||
} else {
|
} else {
|
||||||
msg = fmt.Sprintf("ProxyName [%s], type [%d] unsupport", req.ProxyName)
|
log.Warn("ProxyName [%s], type [%d] unsupport", req.ProxyName, req.Type)
|
||||||
log.Warn(msg)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
succ = true
|
succ = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readControlMsgFromClient(server *models.ProxyServer, c *conn.Conn) {
|
||||||
|
isContinueRead := true
|
||||||
|
f := func() {
|
||||||
|
isContinueRead = false
|
||||||
|
server.StopWaitUserConn()
|
||||||
|
}
|
||||||
|
timer := time.AfterFunc(time.Duration(HeartBeatTimeout)*time.Second, f)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
for isContinueRead {
|
||||||
|
content, err := c.ReadLine()
|
||||||
|
//log.Debug("Receive msg from client! content:%s", content)
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
log.Warn("Server detect client[%s] is dead!", server.Name)
|
||||||
|
server.StopWaitUserConn()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Error("ProxyName [%s], read error:%s", server.Name, err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if content == "\n" {
|
||||||
|
timer.Reset(time.Duration(HeartBeatTimeout) * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,9 +4,9 @@ server_addr = 127.0.0.1
|
|||||||
bind_port = 7000
|
bind_port = 7000
|
||||||
log_file = ./frpc.log
|
log_file = ./frpc.log
|
||||||
# debug, info, warn, error
|
# debug, info, warn, error
|
||||||
log_level = info
|
log_level = debug
|
||||||
# file, console
|
# file, console
|
||||||
log_way = file
|
log_way = console
|
||||||
|
|
||||||
# test1即为name
|
# test1即为name
|
||||||
[test1]
|
[test1]
|
||||||
|
@ -4,9 +4,9 @@ bind_addr = 0.0.0.0
|
|||||||
bind_port = 7000
|
bind_port = 7000
|
||||||
log_file = ./frps.log
|
log_file = ./frps.log
|
||||||
# debug, info, warn, error
|
# debug, info, warn, error
|
||||||
log_level = info
|
log_level = debug
|
||||||
# file, console
|
# file, console
|
||||||
log_way = file
|
log_way = console
|
||||||
|
|
||||||
# test1即为name
|
# test1即为name
|
||||||
[test1]
|
[test1]
|
||||||
|
@ -63,6 +63,7 @@ func (p *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err erro
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// l means local, r means remote
|
||||||
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(),
|
log.Debug("Join two conns, (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)
|
go conn.Join(localConn, remoteConn)
|
||||||
|
@ -22,6 +22,7 @@ type ProxyServer struct {
|
|||||||
Status 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
|
||||||
|
StopBlockChan chan int64 // put any number to the channel, if you want to stop wait user conn
|
||||||
CliConnChan chan *conn.Conn // get client conns from control goroutine
|
CliConnChan chan *conn.Conn // get client conns from control goroutine
|
||||||
UserConnList *list.List // store user conns
|
UserConnList *list.List // store user conns
|
||||||
Mutex sync.Mutex
|
Mutex sync.Mutex
|
||||||
@ -30,6 +31,7 @@ type ProxyServer struct {
|
|||||||
func (p *ProxyServer) Init() {
|
func (p *ProxyServer) Init() {
|
||||||
p.Status = Idle
|
p.Status = Idle
|
||||||
p.CtlMsgChan = make(chan int64)
|
p.CtlMsgChan = make(chan int64)
|
||||||
|
p.StopBlockChan = make(chan int64)
|
||||||
p.CliConnChan = make(chan *conn.Conn)
|
p.CliConnChan = make(chan *conn.Conn)
|
||||||
p.UserConnList = list.New()
|
p.UserConnList = list.New()
|
||||||
}
|
}
|
||||||
@ -87,11 +89,13 @@ func (p *ProxyServer) Start() (err error) {
|
|||||||
p.UserConnList.Remove(element)
|
p.UserConnList.Remove(element)
|
||||||
} else {
|
} else {
|
||||||
cliConn.Close()
|
cliConn.Close()
|
||||||
|
p.Unlock()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
p.Unlock()
|
p.Unlock()
|
||||||
|
|
||||||
// msg will transfer to another without modifying
|
// msg will transfer to another without modifying
|
||||||
|
// l means local, r means remote
|
||||||
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", cliConn.GetLocalAddr(), cliConn.GetRemoteAddr(),
|
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", cliConn.GetLocalAddr(), cliConn.GetRemoteAddr(),
|
||||||
userConn.GetLocalAddr(), userConn.GetRemoteAddr())
|
userConn.GetLocalAddr(), userConn.GetRemoteAddr())
|
||||||
go conn.Join(cliConn, userConn)
|
go conn.Join(cliConn, userConn)
|
||||||
@ -110,7 +114,15 @@ func (p *ProxyServer) Close() {
|
|||||||
p.Unlock()
|
p.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProxyServer) WaitUserConn() (res int64) {
|
func (p *ProxyServer) WaitUserConn() (res int64, isStop bool) {
|
||||||
res = <-p.CtlMsgChan
|
select {
|
||||||
return
|
case res = <-p.CtlMsgChan:
|
||||||
|
return res, false
|
||||||
|
case <-p.StopBlockChan:
|
||||||
|
return 0, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProxyServer) StopWaitUserConn() {
|
||||||
|
p.StopBlockChan <- 1
|
||||||
}
|
}
|
||||||
|
@ -59,8 +59,10 @@ func (c *Conn) Write(content string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Close() {
|
func (c *Conn) Close() {
|
||||||
|
if c.TcpConn != nil {
|
||||||
c.TcpConn.Close()
|
c.TcpConn.Close()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Listen(bindAddr string, bindPort int64) (l *Listener, err error) {
|
func Listen(bindAddr string, bindPort int64) (l *Listener, err error) {
|
||||||
tcpAddr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%d", bindAddr, bindPort))
|
tcpAddr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%d", bindAddr, bindPort))
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Encrypto(t *testing.T) {
|
func TestEncrypto(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.Encrypto([]byte("Just One Test!"))
|
||||||
@ -17,7 +17,7 @@ func Test_Encrypto(t *testing.T) {
|
|||||||
fmt.Printf("[%x]\n", res)
|
fmt.Printf("[%x]\n", res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Decrypto(t *testing.T) {
|
func TestDecrypto(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.Encrypto([]byte("Just One Test!"))
|
||||||
@ -33,13 +33,13 @@ func Test_Decrypto(t *testing.T) {
|
|||||||
fmt.Printf("[%s]\n", string(res))
|
fmt.Printf("[%s]\n", string(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_PKCS7Padding(t *testing.T) {
|
func TestPKCS7Padding(t *testing.T) {
|
||||||
ltt := []byte("Test_PKCS7Padding")
|
ltt := []byte("Test_PKCS7Padding")
|
||||||
ltt = PKCS7Padding(ltt, aes.BlockSize)
|
ltt = PKCS7Padding(ltt, aes.BlockSize)
|
||||||
fmt.Printf("[%x]\n", (ltt))
|
fmt.Printf("[%x]\n", (ltt))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_PKCS7UnPadding(t *testing.T) {
|
func TestPKCS7UnPadding(t *testing.T) {
|
||||||
ltt := []byte("Test_PKCS7Padding")
|
ltt := []byte("Test_PKCS7Padding")
|
||||||
ltt = PKCS7Padding(ltt, aes.BlockSize)
|
ltt = PKCS7Padding(ltt, aes.BlockSize)
|
||||||
ltt = PKCS7UnPadding(ltt)
|
ltt = PKCS7UnPadding(ltt)
|
||||||
|
Loading…
Reference in New Issue
Block a user