2016-01-27 21:24:36 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-02-03 18:46:24 +08:00
|
|
|
"encoding/json"
|
2016-02-19 17:01:47 +08:00
|
|
|
"fmt"
|
2016-01-27 21:24:36 +08:00
|
|
|
"io"
|
|
|
|
"sync"
|
2016-02-04 10:55:10 +08:00
|
|
|
"time"
|
2016-01-27 21:24:36 +08:00
|
|
|
|
2016-02-18 16:56:55 +08:00
|
|
|
"github.com/fatedier/frp/models/client"
|
|
|
|
"github.com/fatedier/frp/models/consts"
|
|
|
|
"github.com/fatedier/frp/models/msg"
|
|
|
|
"github.com/fatedier/frp/utils/conn"
|
|
|
|
"github.com/fatedier/frp/utils/log"
|
2016-01-27 21:24:36 +08:00
|
|
|
)
|
|
|
|
|
2016-02-05 14:18:26 +08:00
|
|
|
var isHeartBeatContinue bool = true
|
2016-02-04 11:29:04 +08:00
|
|
|
|
2016-02-18 16:56:55 +08:00
|
|
|
func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
|
2016-01-27 21:24:36 +08:00
|
|
|
defer wait.Done()
|
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
c, err := loginToServer(cli)
|
|
|
|
if err != nil {
|
2016-02-04 10:55:10 +08:00
|
|
|
log.Error("ProxyName [%s], connect to server failed!", cli.Name)
|
2016-01-27 21:24:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// ignore response content now
|
|
|
|
_, err := c.ReadLine()
|
|
|
|
if err == io.EOF {
|
2016-02-05 14:18:26 +08:00
|
|
|
isHeartBeatContinue = false
|
2016-01-27 21:24:36 +08:00
|
|
|
log.Debug("ProxyName [%s], server close this control conn", cli.Name)
|
2016-02-04 10:55:10 +08:00
|
|
|
var sleepTime time.Duration = 1
|
|
|
|
for {
|
2016-02-18 18:24:48 +08:00
|
|
|
log.Debug("ProxyName [%s], try to reconnect to server[%s:%d]...", cli.Name, client.ServerAddr, client.ServerPort)
|
2016-02-19 17:01:47 +08:00
|
|
|
tmpConn, err := loginToServer(cli)
|
|
|
|
if err == nil {
|
2016-02-04 10:55:10 +08:00
|
|
|
c.Close()
|
|
|
|
c = tmpConn
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if sleepTime < 60 {
|
2016-02-19 17:01:47 +08:00
|
|
|
sleepTime = sleepTime * 2
|
2016-02-04 10:55:10 +08:00
|
|
|
}
|
|
|
|
time.Sleep(sleepTime * time.Second)
|
|
|
|
}
|
|
|
|
continue
|
2016-01-27 21:24:36 +08:00
|
|
|
} else if err != nil {
|
|
|
|
log.Warn("ProxyName [%s], read from server error, %v", cli.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-02-18 18:24:48 +08:00
|
|
|
cli.StartTunnel(client.ServerAddr, client.ServerPort)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 10:55:10 +08:00
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
func loginToServer(cli *client.ProxyClient) (c *conn.Conn, err error) {
|
|
|
|
c, err = conn.ConnectServer(client.ServerAddr, client.ServerPort)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", cli.Name, client.ServerAddr, client.ServerPort, err)
|
|
|
|
return
|
|
|
|
}
|
2016-02-04 10:55:10 +08:00
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
req := &msg.ClientCtlReq{
|
|
|
|
Type: consts.CtlConn,
|
|
|
|
ProxyName: cli.Name,
|
|
|
|
Passwd: cli.Passwd,
|
|
|
|
}
|
|
|
|
buf, _ := json.Marshal(req)
|
|
|
|
err = c.Write(string(buf) + "\n")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("ProxyName [%s], write to server error, %v", cli.Name, err)
|
|
|
|
return
|
|
|
|
}
|
2016-02-04 10:55:10 +08:00
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
res, err := c.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("ProxyName [%s], read from server error, %v", cli.Name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("ProxyName [%s], read [%s]", cli.Name, res)
|
2016-02-04 10:55:10 +08:00
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
clientCtlRes := &msg.ClientCtlRes{}
|
|
|
|
if err = json.Unmarshal([]byte(res), &clientCtlRes); err != nil {
|
|
|
|
log.Error("ProxyName [%s], format server response error, %v", cli.Name, err)
|
|
|
|
return
|
2016-02-04 10:55:10 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
if clientCtlRes.Code != 0 {
|
|
|
|
log.Error("ProxyName [%s], start proxy error, %s", cli.Name, clientCtlRes.Msg)
|
|
|
|
return c, fmt.Errorf("%s", clientCtlRes.Msg)
|
2016-02-04 10:55:10 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
go startHeartBeat(c)
|
|
|
|
log.Debug("ProxyName [%s], connect to server[%s:%d] success!", cli.Name, client.ServerAddr, client.ServerPort)
|
|
|
|
|
2016-02-04 10:55:10 +08:00
|
|
|
return
|
|
|
|
}
|
2016-02-04 11:29:04 +08:00
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
func startHeartBeat(c *conn.Conn) {
|
2016-02-18 11:42:31 +08:00
|
|
|
log.Debug("Start to send heartbeat")
|
2016-02-04 11:29:04 +08:00
|
|
|
for {
|
2016-02-18 18:24:48 +08:00
|
|
|
time.Sleep(time.Duration(client.HeartBeatInterval) * time.Second)
|
2016-02-19 17:01:47 +08:00
|
|
|
if !c.IsClosed() {
|
|
|
|
err := c.Write("\n")
|
2016-02-05 14:18:26 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Send hearbeat to server failed! Err:%s", err.Error())
|
2016-02-19 17:01:47 +08:00
|
|
|
continue
|
2016-02-05 14:18:26 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
2016-02-04 11:29:04 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-19 17:01:47 +08:00
|
|
|
log.Info("heartbeat exit")
|
2016-02-04 11:29:04 +08:00
|
|
|
}
|