From f065562ec3e570dda5f560840653ae761a962f67 Mon Sep 17 00:00:00 2001 From: Hurricanezwf <1094646850@qq.com> Date: Thu, 4 Feb 2016 10:55:10 +0800 Subject: [PATCH 1/5] =?UTF-8?q?(1)=E5=88=86=E7=A6=BB=E5=87=BA=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=9A=84=E9=80=BB=E8=BE=91?= =?UTF-8?q?=20=20(2)=E6=96=B0=E5=A2=9Eclient=E6=96=AD=E7=BA=BF=E9=87=8D?= =?UTF-8?q?=E8=BF=9E=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/frpc/control.go | 115 ++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 36 deletions(-) diff --git a/cmd/frpc/control.go b/cmd/frpc/control.go index e917a93..78facb2 100644 --- a/cmd/frpc/control.go +++ b/cmd/frpc/control.go @@ -1,62 +1,54 @@ package main import ( + "encoding/json" "io" "sync" - "encoding/json" + "time" "frp/pkg/models" "frp/pkg/utils/conn" "frp/pkg/utils/log" ) +// 重连时的间隔时间区间 +const ( + sleepMinDuration = 1 + sleepMaxDuration = 60 +) + 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) + c := loginToServer(cli) + if c == nil { + log.Error("ProxyName [%s], connect to server failed!", cli.Name) 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 { + // reconnect when disconnect log.Debug("ProxyName [%s], server close this control conn", cli.Name) - break + 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 @@ -65,3 +57,54 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { cli.StartTunnel(ServerAddr, ServerPort) } } + +func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { + c := &conn.Conn{} + + connection = nil + for i := 0; i < 1; i++ { // ZWF: 此处的for作为控制流使用 + 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{ + 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) + break + } + + res, err := c.ReadLine() + if err != nil { + log.Error("ProxyName [%s], read from server error, %v", cli.Name, err) + break + } + 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) + break + } + + if clientCtlRes.Code != 0 { + log.Error("ProxyName [%s], start proxy error, %s", cli.Name, clientCtlRes.Msg) + break + } + + connection = c + } + + if connection == nil { + c.Close() + } + + return +} From af6fc61537eaa8012c0091a0eb18353de9604c2c Mon Sep 17 00:00:00 2001 From: Hurricanezwf <1094646850@qq.com> Date: Thu, 4 Feb 2016 11:29:04 +0800 Subject: [PATCH 2/5] =?UTF-8?q?(1)=E6=96=B0=E5=A2=9Eclient=E5=90=91server?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=BF=83=E8=B7=B3=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/frpc/control.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/cmd/frpc/control.go b/cmd/frpc/control.go index 78facb2..332a06c 100644 --- a/cmd/frpc/control.go +++ b/cmd/frpc/control.go @@ -11,12 +11,13 @@ import ( "frp/pkg/utils/log" ) -// 重连时的间隔时间区间 const ( - sleepMinDuration = 1 - sleepMaxDuration = 60 + heartbeatDuration = 2 //心跳检测时间间隔,单位秒 ) +// client与server之间连接的保护锁 +var connProtect sync.Mutex + func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { defer wait.Done() @@ -27,11 +28,13 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { } defer c.Close() + go startHeartBeat(c) + for { // ignore response content now _, err := c.ReadLine() if err == io.EOF { - // reconnect when disconnect + connProtect.Lock() // 除了这里,其他地方禁止对连接进行任何操作 log.Debug("ProxyName [%s], server close this control conn", cli.Name) var sleepTime time.Duration = 1 for { @@ -48,6 +51,7 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { } time.Sleep(sleepTime * time.Second) } + connProtect.Unlock() continue } else if err != nil { log.Warn("ProxyName [%s], read from server error, %v", cli.Name, err) @@ -108,3 +112,16 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { return } + +func startHeartBeat(con *conn.Conn) { + for { + time.Sleep(heartbeatDuration * time.Second) + + connProtect.Lock() + err := con.Write("\r\n") + connProtect.Unlock() + if err != nil { + log.Error("Send hearbeat to server failed! Err:%s", err.Error()) + } + } +} From 5d6f37aa82b8e0a5e3cbcb78b6febcb8a44299e0 Mon Sep 17 00:00:00 2001 From: Hurricanezwf <1094646850@qq.com> Date: Fri, 5 Feb 2016 14:18:26 +0800 Subject: [PATCH 3/5] =?UTF-8?q?(1)=E4=BC=98=E5=8C=96=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E5=92=8C=E5=BF=83=E8=B7=B3=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ cmd/frpc/control.go | 25 +++++++++--------- cmd/frps/control.go | 59 +++++++++++++++++++++++++++++++++++------- pkg/models/server.go | 42 ++++++++++++++++++------------ pkg/utils/conn/conn.go | 24 +++++++++-------- 5 files changed, 104 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index fab4548..72c9fd3 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ _testmain.go # Self bin/ +# Cache +*.swp diff --git a/cmd/frpc/control.go b/cmd/frpc/control.go index 332a06c..3bca709 100644 --- a/cmd/frpc/control.go +++ b/cmd/frpc/control.go @@ -15,8 +15,7 @@ const ( heartbeatDuration = 2 //心跳检测时间间隔,单位秒 ) -// client与server之间连接的保护锁 -var connProtect sync.Mutex +var isHeartBeatContinue bool = true func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { defer wait.Done() @@ -28,13 +27,11 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { } defer c.Close() - go startHeartBeat(c) - for { // ignore response content now _, err := c.ReadLine() if err == io.EOF { - connProtect.Lock() // 除了这里,其他地方禁止对连接进行任何操作 + isHeartBeatContinue = false log.Debug("ProxyName [%s], server close this control conn", cli.Name) var sleepTime time.Duration = 1 for { @@ -51,7 +48,6 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { } time.Sleep(sleepTime * time.Second) } - connProtect.Unlock() continue } else if err != nil { log.Warn("ProxyName [%s], read from server error, %v", cli.Name, err) @@ -104,6 +100,8 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { } connection = c + go startHeartBeat(connection) + log.Debug("ProxyName [%s], connect to server[%s:%d] success!", cli.Name, ServerAddr, ServerPort) } if connection == nil { @@ -114,14 +112,17 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { } func startHeartBeat(con *conn.Conn) { + isHeartBeatContinue = true for { time.Sleep(heartbeatDuration * time.Second) - - connProtect.Lock() - err := con.Write("\r\n") - connProtect.Unlock() - if err != nil { - log.Error("Send hearbeat to server failed! Err:%s", err.Error()) + if isHeartBeatContinue { // 把isHeartBeatContinue放在这里是为了防止SIGPIPE + err := con.Write("\r\n") + //log.Debug("send heart beat to server!") + if err != nil { + log.Error("Send hearbeat to server failed! Err:%s", err.Error()) + } + } else { + break } } } diff --git a/cmd/frps/control.go b/cmd/frps/control.go index 62d141e..d6cfaa3 100644 --- a/cmd/frps/control.go +++ b/cmd/frps/control.go @@ -1,12 +1,14 @@ package main import ( - "fmt" "encoding/json" + "fmt" + "io" + "time" - "frp/pkg/utils/log" - "frp/pkg/utils/conn" "frp/pkg/models" + "frp/pkg/utils/conn" + "frp/pkg/utils/log" ) func ProcessControlConn(l *conn.Listener) { @@ -19,6 +21,8 @@ func ProcessControlConn(l *conn.Listener) { // control connection from every client and server func controlWorker(c *conn.Conn) { + defer c.Close() + // the first message is from client to server // if error, close connection res, err := c.ReadLine() @@ -41,19 +45,20 @@ func controlWorker(c *conn.Conn) { clientCtlRes.Code = 1 clientCtlRes.Msg = msg } - + if needRes { buf, _ := json.Marshal(clientCtlRes) err = c.Write(string(buf) + "\n") if err != nil { log.Warn("Write error, %v", err) + time.Sleep(1 * time.Second) + return } } else { - // work conn, just return + // work conn, just return return } - defer c.Close() // others is from server to client server, ok := ProxyServers[clientCtlReq.ProxyName] if !ok { @@ -61,10 +66,16 @@ func controlWorker(c *conn.Conn) { return } + // read control msg from client + go readControlMsgFromClient(server, c) + serverCtlReq := &models.ClientCtlReq{} serverCtlReq.Type = models.WorkConn for { - server.WaitUserConn() + _, isStop := server.WaitUserConn() + if isStop { + break + } buf, _ := json.Marshal(serverCtlReq) err = c.Write(string(buf) + "\n") if err != nil { @@ -76,6 +87,7 @@ func controlWorker(c *conn.Conn) { log.Debug("ProxyName [%s], write to client to add work conn success", server.Name) } + log.Error("ProxyName [%s], I'm dead!", server.Name) return } @@ -96,7 +108,7 @@ func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string, log.Warn(msg) return } - + // control conn if req.Type == models.ControlConn { if server.Status != models.Idle { @@ -115,7 +127,7 @@ func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string, log.Info("ProxyName [%s], start proxy success", req.ProxyName) } else if req.Type == models.WorkConn { - // work conn + // work conn needRes = false if server.Status != models.Working { log.Warn("ProxyName [%s], is not working when it gets one new work conn", req.ProxyName) @@ -132,3 +144,32 @@ func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string, succ = true return } + +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) + } + } +} diff --git a/pkg/models/server.go b/pkg/models/server.go index bd6baa8..2c503dd 100644 --- a/pkg/models/server.go +++ b/pkg/models/server.go @@ -1,8 +1,8 @@ package models import ( - "sync" "container/list" + "sync" "frp/pkg/utils/conn" "frp/pkg/utils/log" @@ -14,22 +14,24 @@ const ( ) type ProxyServer struct { - Name string - Passwd string - BindAddr string - ListenPort int64 + Name string + Passwd string + BindAddr string + ListenPort int64 - Status int64 - Listener *conn.Listener // accept new connection from remote users - CtlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel - CliConnChan chan *conn.Conn // get client conns from control goroutine - UserConnList *list.List // store user conns - Mutex sync.Mutex + Status int64 + Listener *conn.Listener // accept new connection from remote users + 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 + UserConnList *list.List // store user conns + Mutex sync.Mutex } func (p *ProxyServer) Init() { p.Status = Idle p.CtlMsgChan = make(chan int64) + p.StopBlockChan = make(chan int64) p.CliConnChan = make(chan *conn.Conn) p.UserConnList = list.New() } @@ -55,7 +57,7 @@ func (p *ProxyServer) Start() (err error) { go func() { for { // block - c := p.Listener.GetConn() + c := p.Listener.GetConn() log.Debug("ProxyName [%s], get one new user conn [%s]", p.Name, c.GetRemoteAddr()) // put to list @@ -93,7 +95,7 @@ func (p *ProxyServer) Start() (err error) { // msg will transfer to another without modifying 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) } }() @@ -110,7 +112,15 @@ func (p *ProxyServer) Close() { p.Unlock() } -func (p *ProxyServer) WaitUserConn() (res int64) { - res = <-p.CtlMsgChan - return +func (p *ProxyServer) WaitUserConn() (res int64, isStop bool) { + select { + case res = <-p.CtlMsgChan: + return res, false + case <-p.StopBlockChan: + return 0, true + } +} + +func (p *ProxyServer) StopWaitUserConn() { + p.StopBlockChan <- 1 } diff --git a/pkg/utils/conn/conn.go b/pkg/utils/conn/conn.go index 60929ac..42c9468 100644 --- a/pkg/utils/conn/conn.go +++ b/pkg/utils/conn/conn.go @@ -1,18 +1,18 @@ package conn import ( - "fmt" - "net" "bufio" - "sync" + "fmt" "io" + "net" + "sync" "frp/pkg/utils/log" ) type Listener struct { - Addr net.Addr - Conns chan *Conn + Addr net.Addr + Conns chan *Conn } // wait util get one @@ -22,8 +22,8 @@ func (l *Listener) GetConn() (conn *Conn) { } type Conn struct { - TcpConn *net.TCPConn - Reader *bufio.Reader + TcpConn *net.TCPConn + Reader *bufio.Reader } func (c *Conn) ConnectServer(host string, port int64) (err error) { @@ -59,7 +59,9 @@ func (c *Conn) Write(content string) (err error) { } func (c *Conn) Close() { - c.TcpConn.Close() + if c.TcpConn != nil { // ZWF:我觉得应该加一个非空保护 + c.TcpConn.Close() + } } func Listen(bindAddr string, bindPort int64) (l *Listener, err error) { @@ -70,8 +72,8 @@ func Listen(bindAddr string, bindPort int64) (l *Listener, err error) { } l = &Listener{ - Addr: listener.Addr(), - Conns: make(chan *Conn), + Addr: listener.Addr(), + Conns: make(chan *Conn), } go func() { @@ -83,7 +85,7 @@ func Listen(bindAddr string, bindPort int64) (l *Listener, err error) { } c := &Conn{ - TcpConn: conn, + TcpConn: conn, } c.Reader = bufio.NewReader(c.TcpConn) l.Conns <- c From 04c26d1c31c6994d8c77c3982b9f2b38a5776abe Mon Sep 17 00:00:00 2001 From: Hurricanezwf <1094646850@qq.com> Date: Fri, 5 Feb 2016 14:36:04 +0800 Subject: [PATCH 4/5] =?UTF-8?q?(1)=E6=96=B0=E5=A2=9E=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=8F=91=E9=80=81=E9=97=B4=E9=9A=94=E5=92=8C?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/frpc/config.go | 17 +++++++++++------ cmd/frpc/control.go | 6 +----- cmd/frps/config.go | 12 ++++++------ cmd/frps/control.go | 4 ++-- conf/frpc.ini | 4 +++- conf/frps.ini | 4 +++- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/cmd/frpc/config.go b/cmd/frpc/config.go index a26fe6a..c6b232f 100644 --- a/cmd/frpc/config.go +++ b/cmd/frpc/config.go @@ -11,16 +11,16 @@ import ( // common config var ( - ServerAddr string = "0.0.0.0" - ServerPort int64 = 7000 - LogFile string = "./frpc.log" - LogLevel string = "warn" - LogWay string = "file" + ServerAddr string = "0.0.0.0" + ServerPort int64 = 7000 + LogFile string = "./frpc.log" + LogLevel string = "warn" + LogWay string = "file" + HeartBeatInterval int64 = 5 ) var ProxyClients map[string]*models.ProxyClient = make(map[string]*models.ProxyClient) - func LoadConf(confFile string) (err error) { var tmpStr string var ok bool @@ -56,6 +56,11 @@ func LoadConf(confFile string) (err error) { LogWay = tmpStr } + tmpStr, ok = conf.Get("common", "heartbeat_interval") + if ok { + HeartBeatInterval, _ = strconv.ParseInt(tmpStr, 10, 64) + } + // servers for name, section := range conf { if name != "common" { diff --git a/cmd/frpc/control.go b/cmd/frpc/control.go index 3bca709..6bf417a 100644 --- a/cmd/frpc/control.go +++ b/cmd/frpc/control.go @@ -11,10 +11,6 @@ import ( "frp/pkg/utils/log" ) -const ( - heartbeatDuration = 2 //心跳检测时间间隔,单位秒 -) - var isHeartBeatContinue bool = true func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { @@ -114,7 +110,7 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { func startHeartBeat(con *conn.Conn) { isHeartBeatContinue = true for { - time.Sleep(heartbeatDuration * time.Second) + time.Sleep(time.Duration(HeartBeatInterval) * time.Second) if isHeartBeatContinue { // 把isHeartBeatContinue放在这里是为了防止SIGPIPE err := con.Write("\r\n") //log.Debug("send heart beat to server!") diff --git a/cmd/frps/config.go b/cmd/frps/config.go index feb07d5..995557e 100644 --- a/cmd/frps/config.go +++ b/cmd/frps/config.go @@ -11,16 +11,16 @@ import ( // common config var ( - BindAddr string = "0.0.0.0" - BindPort int64 = 9527 - LogFile string = "./frps.log" - LogLevel string = "warn" - LogWay string = "file" + BindAddr string = "0.0.0.0" + BindPort int64 = 9527 + LogFile string = "./frps.log" + LogLevel string = "warn" + LogWay string = "file" + HeartBeatTimeout int64 = 30 ) var ProxyServers map[string]*models.ProxyServer = make(map[string]*models.ProxyServer) - func LoadConf(confFile string) (err error) { var tmpStr string var ok bool diff --git a/cmd/frps/control.go b/cmd/frps/control.go index d6cfaa3..04f7a96 100644 --- a/cmd/frps/control.go +++ b/cmd/frps/control.go @@ -151,7 +151,7 @@ func readControlMsgFromClient(server *models.ProxyServer, c *conn.Conn) { isContinueRead = false server.StopWaitUserConn() } - timer := time.AfterFunc(10*time.Second, f) + timer := time.AfterFunc(time.Duration(HeartBeatTimeout)*time.Second, f) defer timer.Stop() for isContinueRead { @@ -169,7 +169,7 @@ func readControlMsgFromClient(server *models.ProxyServer, c *conn.Conn) { if content == "\r\n" { log.Debug("receive hearbeat:%s", content) - timer.Reset(10 * time.Second) + timer.Reset(time.Duration(HeartBeatTimeout) * time.Second) } } } diff --git a/conf/frpc.ini b/conf/frpc.ini index d2ba710..f9c7c11 100644 --- a/conf/frpc.ini +++ b/conf/frpc.ini @@ -6,7 +6,9 @@ log_file = ./frpc.log # debug, info, warn, error log_level = info # file, console -log_way = file +log_way = console +# 心跳检测时间间隔,单位秒,默认为2 +heartbeat_interval = 2 # test1即为name [test1] diff --git a/conf/frps.ini b/conf/frps.ini index f6a6995..4f1cfdb 100644 --- a/conf/frps.ini +++ b/conf/frps.ini @@ -6,7 +6,9 @@ log_file = ./frps.log # debug, info, warn, error log_level = info # file, console -log_way = file +log_way = console +# 心跳检测超时时间,单位秒,默认为30 +heartbeat_timeout = 30 # test1即为name [test1] From 60c9804776ffb7455b80d2110733805c93636a3c Mon Sep 17 00:00:00 2001 From: Hurricanezwf <1094646850@qq.com> Date: Fri, 5 Feb 2016 16:49:52 +0800 Subject: [PATCH 5/5] =?UTF-8?q?format=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + cmd/frpc/main.go | 2 +- cmd/frps/main.go | 2 +- pkg/models/client.go | 16 ++++++++-------- pkg/models/msg.go | 13 ++++++------- pkg/models/server.go | 1 + pkg/utils/log/log.go | 2 +- 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 72c9fd3..e237cc4 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ bin/ # Cache *.swp +*.swo diff --git a/cmd/frpc/main.go b/cmd/frpc/main.go index 7f07282..8f5b08b 100644 --- a/cmd/frpc/main.go +++ b/cmd/frpc/main.go @@ -3,7 +3,7 @@ package main import ( "os" "sync" - + "frp/pkg/utils/log" ) diff --git a/cmd/frps/main.go b/cmd/frps/main.go index 1288622..d2c98be 100644 --- a/cmd/frps/main.go +++ b/cmd/frps/main.go @@ -3,8 +3,8 @@ package main import ( "os" - "frp/pkg/utils/log" "frp/pkg/utils/conn" + "frp/pkg/utils/log" ) func main() { diff --git a/pkg/models/client.go b/pkg/models/client.go index 1f01d50..0ebac20 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -8,9 +8,9 @@ import ( ) type ProxyClient struct { - Name string - Passwd string - LocalPort int64 + Name string + Passwd string + LocalPort int64 } func (p *ProxyClient) GetLocalConn() (c *conn.Conn, err error) { @@ -24,7 +24,7 @@ func (p *ProxyClient) GetLocalConn() (c *conn.Conn, err error) { func (p *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err error) { c = &conn.Conn{} - defer func(){ + defer func() { if err != nil { c.Close() } @@ -37,9 +37,9 @@ func (p *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err } req := &ClientCtlReq{ - Type: WorkConn, - ProxyName: p.Name, - Passwd: p.Passwd, + Type: WorkConn, + ProxyName: p.Name, + Passwd: p.Passwd, } buf, _ := json.Marshal(req) @@ -64,7 +64,7 @@ func (p *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err erro } 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) return nil } diff --git a/pkg/models/msg.go b/pkg/models/msg.go index 0062556..a3018e7 100644 --- a/pkg/models/msg.go +++ b/pkg/models/msg.go @@ -1,8 +1,8 @@ package models type GeneralRes struct { - Code int64 `json:"code"` - Msg string `json:"msg"` + Code int64 `json:"code"` + Msg string `json:"msg"` } // type @@ -12,16 +12,15 @@ const ( ) type ClientCtlReq struct { - Type int64 `json:"type"` - ProxyName string `json:"proxy_name"` - Passwd string `json:"passwd"` + Type int64 `json:"type"` + ProxyName string `json:"proxy_name"` + Passwd string `json:"passwd"` } type ClientCtlRes struct { GeneralRes } - type ServerCtlReq struct { - Type int64 `json:"type"` + Type int64 `json:"type"` } diff --git a/pkg/models/server.go b/pkg/models/server.go index 2c503dd..f8c9451 100644 --- a/pkg/models/server.go +++ b/pkg/models/server.go @@ -89,6 +89,7 @@ func (p *ProxyServer) Start() (err error) { p.UserConnList.Remove(element) } else { cliConn.Close() + p.Unlock() continue } p.Unlock() diff --git a/pkg/utils/log/log.go b/pkg/utils/log/log.go index 1a55c3c..f6587cd 100644 --- a/pkg/utils/log/log.go +++ b/pkg/utils/log/log.go @@ -22,7 +22,7 @@ func SetLogFile(logWay string, logFile string) { if logWay == "console" { Log.SetLogger("console", "") } else { - Log.SetLogger("file", `{"filename": "` + logFile + `"}`) + Log.SetLogger("file", `{"filename": "`+logFile+`"}`) } }