mirror of
https://github.com/fatedier/frp.git
synced 2026-04-21 00:19:09 +08:00
First available commit
This commit is contained in:
67
cmd/frpc/control.go
Normal file
67
cmd/frpc/control.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
"encoding/json"
|
||||
|
||||
"frp/pkg/models"
|
||||
"frp/pkg/utils/conn"
|
||||
"frp/pkg/utils/log"
|
||||
)
|
||||
|
||||
func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) {
|
||||
defer wait.Done()
|
||||
|
||||
c := &conn.Conn{}
|
||||
err := c.ConnectServer(ServerAddr, ServerPort)
|
||||
if err != nil {
|
||||
log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", cli.Name, ServerAddr, ServerPort, err)
|
||||
return
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
req := &models.ClientCtlReq{
|
||||
Type: models.ControlConn,
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
clientCtlRes := &models.ClientCtlRes{}
|
||||
if err = json.Unmarshal([]byte(res), &clientCtlRes); err != nil {
|
||||
log.Error("ProxyName [%s], format server response error, %v", cli.Name, err)
|
||||
return
|
||||
}
|
||||
|
||||
if clientCtlRes.Code != 0 {
|
||||
log.Error("ProxyName [%s], start proxy error, %s", cli.Name, clientCtlRes.Msg)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
// ignore response content now
|
||||
_, err := c.ReadLine()
|
||||
if err == io.EOF {
|
||||
log.Debug("ProxyName [%s], server close this control conn", cli.Name)
|
||||
break
|
||||
} else if err != nil {
|
||||
log.Warn("ProxyName [%s], read from server error, %v", cli.Name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
cli.StartTunnel(ServerAddr, ServerPort)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user