2016-01-27 21:24:36 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-02-05 14:18:26 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
2016-01-27 21:24:36 +08:00
|
|
|
|
|
|
|
"frp/pkg/models"
|
2016-02-05 14:18:26 +08:00
|
|
|
"frp/pkg/utils/conn"
|
|
|
|
"frp/pkg/utils/log"
|
2016-01-27 21:24:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func ProcessControlConn(l *conn.Listener) {
|
|
|
|
for {
|
|
|
|
c := l.GetConn()
|
|
|
|
log.Debug("Get one new conn, %v", c.GetRemoteAddr())
|
|
|
|
go controlWorker(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// control connection from every client and server
|
|
|
|
func controlWorker(c *conn.Conn) {
|
2016-02-05 14:18:26 +08:00
|
|
|
defer c.Close()
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
// the first message is from client to server
|
|
|
|
// if error, close connection
|
|
|
|
res, err := c.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Read error, %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("get: %s", res)
|
|
|
|
|
|
|
|
clientCtlReq := &models.ClientCtlReq{}
|
|
|
|
clientCtlRes := &models.ClientCtlRes{}
|
|
|
|
if err := json.Unmarshal([]byte(res), &clientCtlReq); err != nil {
|
|
|
|
log.Warn("Parse err: %v : %s", err, res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check
|
|
|
|
succ, msg, needRes := checkProxy(clientCtlReq, c)
|
|
|
|
if !succ {
|
|
|
|
clientCtlRes.Code = 1
|
|
|
|
clientCtlRes.Msg = msg
|
|
|
|
}
|
2016-02-05 14:18:26 +08:00
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
if needRes {
|
|
|
|
buf, _ := json.Marshal(clientCtlRes)
|
|
|
|
err = c.Write(string(buf) + "\n")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Write error, %v", err)
|
2016-02-05 14:18:26 +08:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-05 14:18:26 +08:00
|
|
|
// work conn, just return
|
2016-01-27 21:24:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// others is from server to client
|
|
|
|
server, ok := ProxyServers[clientCtlReq.ProxyName]
|
|
|
|
if !ok {
|
|
|
|
log.Warn("ProxyName [%s] is not exist", clientCtlReq.ProxyName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:18:26 +08:00
|
|
|
// read control msg from client
|
|
|
|
go readControlMsgFromClient(server, c)
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
serverCtlReq := &models.ClientCtlReq{}
|
|
|
|
serverCtlReq.Type = models.WorkConn
|
|
|
|
for {
|
2016-02-05 14:18:26 +08:00
|
|
|
_, isStop := server.WaitUserConn()
|
|
|
|
if isStop {
|
|
|
|
break
|
|
|
|
}
|
2016-01-27 21:24:36 +08:00
|
|
|
buf, _ := json.Marshal(serverCtlReq)
|
|
|
|
err = c.Write(string(buf) + "\n")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("ProxyName [%s], write to client error, proxy exit", server.Name)
|
|
|
|
server.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("ProxyName [%s], write to client to add work conn success", server.Name)
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:18:26 +08:00
|
|
|
log.Error("ProxyName [%s], I'm dead!", server.Name)
|
2016-01-27 21:24:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string, needRes bool) {
|
|
|
|
succ = false
|
|
|
|
needRes = true
|
|
|
|
// check if proxy name exist
|
|
|
|
server, ok := ProxyServers[req.ProxyName]
|
|
|
|
if !ok {
|
|
|
|
msg = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName)
|
|
|
|
log.Warn(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check password
|
|
|
|
if req.Passwd != server.Passwd {
|
|
|
|
msg = fmt.Sprintf("ProxyName [%s], password is not correct", req.ProxyName)
|
|
|
|
log.Warn(msg)
|
|
|
|
return
|
|
|
|
}
|
2016-02-05 14:18:26 +08:00
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
// control conn
|
|
|
|
if req.Type == models.ControlConn {
|
|
|
|
if server.Status != models.Idle {
|
|
|
|
msg = fmt.Sprintf("ProxyName [%s], already in use", req.ProxyName)
|
|
|
|
log.Warn(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// start proxy and listen for user conn, no block
|
|
|
|
err := server.Start()
|
|
|
|
if err != nil {
|
|
|
|
msg = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err.Error())
|
|
|
|
log.Warn(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("ProxyName [%s], start proxy success", req.ProxyName)
|
|
|
|
} else if req.Type == models.WorkConn {
|
2016-02-05 14:18:26 +08:00
|
|
|
// work conn
|
2016-01-27 21:24:36 +08:00
|
|
|
needRes = false
|
|
|
|
if server.Status != models.Working {
|
|
|
|
log.Warn("ProxyName [%s], is not working when it gets one new work conn", req.ProxyName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
server.CliConnChan <- c
|
|
|
|
} else {
|
|
|
|
msg = fmt.Sprintf("ProxyName [%s], type [%d] unsupport", req.ProxyName)
|
|
|
|
log.Warn(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
succ = true
|
|
|
|
return
|
|
|
|
}
|
2016-02-05 14:18:26 +08:00
|
|
|
|
|
|
|
func readControlMsgFromClient(server *models.ProxyServer, c *conn.Conn) {
|
|
|
|
isContinueRead := true
|
|
|
|
f := func() {
|
|
|
|
isContinueRead = false
|
|
|
|
server.StopWaitUserConn()
|
|
|
|
}
|
|
|
|
timer := time.AfterFunc(10*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 == "\r\n" {
|
|
|
|
log.Debug("receive hearbeat:%s", content)
|
|
|
|
timer.Reset(10 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|