|
|
@ -14,6 +14,9 @@ import ( |
|
|
|
"frp/utils/log" |
|
|
|
"frp/utils/log" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var connection *conn.Conn = nil |
|
|
|
|
|
|
|
var heartBeatTimer *time.Timer = nil |
|
|
|
|
|
|
|
|
|
|
|
func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) { |
|
|
|
func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) { |
|
|
|
defer wait.Done() |
|
|
|
defer wait.Done() |
|
|
|
|
|
|
|
|
|
|
@ -22,12 +25,13 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) { |
|
|
|
log.Error("ProxyName [%s], connect to server failed!", cli.Name) |
|
|
|
log.Error("ProxyName [%s], connect to server failed!", cli.Name) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
defer c.Close() |
|
|
|
connection = c |
|
|
|
|
|
|
|
defer connection.Close() |
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
for { |
|
|
|
// ignore response content now
|
|
|
|
// ignore response content now
|
|
|
|
_, err := c.ReadLine() |
|
|
|
content, err := connection.ReadLine() |
|
|
|
if err == io.EOF { |
|
|
|
if err == io.EOF || nil == connection || connection.IsClosed() { |
|
|
|
log.Debug("ProxyName [%s], server close this control conn", cli.Name) |
|
|
|
log.Debug("ProxyName [%s], server close this control conn", cli.Name) |
|
|
|
var sleepTime time.Duration = 1 |
|
|
|
var sleepTime time.Duration = 1 |
|
|
|
|
|
|
|
|
|
|
@ -36,8 +40,8 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) { |
|
|
|
log.Debug("ProxyName [%s], try to reconnect to server[%s:%d]...", cli.Name, client.ServerAddr, client.ServerPort) |
|
|
|
log.Debug("ProxyName [%s], try to reconnect to server[%s:%d]...", cli.Name, client.ServerAddr, client.ServerPort) |
|
|
|
tmpConn, err := loginToServer(cli) |
|
|
|
tmpConn, err := loginToServer(cli) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
c.Close() |
|
|
|
connection.Close() |
|
|
|
c = tmpConn |
|
|
|
connection = tmpConn |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -52,6 +56,21 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) { |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clientCtlRes := &msg.ClientCtlRes{} |
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(content), clientCtlRes); err != nil { |
|
|
|
|
|
|
|
log.Warn("Parse err: %v : %s", err, content) |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if consts.SCHeartBeatRes == clientCtlRes.GeneralRes.Code { |
|
|
|
|
|
|
|
if heartBeatTimer != nil { |
|
|
|
|
|
|
|
log.Debug("Client rcv heartbeat response") |
|
|
|
|
|
|
|
heartBeatTimer.Reset(time.Duration(client.HeartBeatTimeout) * time.Second) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
log.Error("heartBeatTimer is nil") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cli.StartTunnel(client.ServerAddr, client.ServerPort) |
|
|
|
cli.StartTunnel(client.ServerAddr, client.ServerPort) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -100,18 +119,37 @@ func loginToServer(cli *client.ProxyClient) (c *conn.Conn, err error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func startHeartBeat(c *conn.Conn) { |
|
|
|
func startHeartBeat(c *conn.Conn) { |
|
|
|
|
|
|
|
f := func() { |
|
|
|
|
|
|
|
log.Error("HeartBeat timeout!") |
|
|
|
|
|
|
|
if c != nil { |
|
|
|
|
|
|
|
c.Close() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
heartBeatTimer = time.AfterFunc(time.Duration(client.HeartBeatTimeout)*time.Second, f) |
|
|
|
|
|
|
|
defer heartBeatTimer.Stop() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clientCtlReq := &msg.ClientCtlReq{ |
|
|
|
|
|
|
|
Type: consts.CSHeartBeatReq, |
|
|
|
|
|
|
|
ProxyName: "", |
|
|
|
|
|
|
|
Passwd: "", |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
request, err := json.Marshal(clientCtlReq) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Warn("Serialize clientCtlReq err! Err: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.Debug("Start to send heartbeat") |
|
|
|
log.Debug("Start to send heartbeat") |
|
|
|
for { |
|
|
|
for { |
|
|
|
time.Sleep(time.Duration(client.HeartBeatInterval) * time.Second) |
|
|
|
time.Sleep(time.Duration(client.HeartBeatInterval) * time.Second) |
|
|
|
if !c.IsClosed() { |
|
|
|
if c != nil && !c.IsClosed() { |
|
|
|
err := c.Write("\n") |
|
|
|
err = c.Write(string(request) + "\n") |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error("Send hearbeat to server failed! Err:%s", err.Error()) |
|
|
|
log.Error("Send hearbeat to server failed! Err:%v", err) |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
log.Debug("heartbeat exit") |
|
|
|
log.Debug("Heartbeat exit") |
|
|
|
} |
|
|
|
} |
|
|
|