From 84f8addd6a6119dec46f41d23f3a79a919632837 Mon Sep 17 00:00:00 2001 From: fatedier Date: Thu, 18 Feb 2016 18:24:48 +0800 Subject: [PATCH] Move config.go to models/xxx --- cmd/frpc/control.go | 12 ++++---- cmd/frpc/main.go | 9 +++--- cmd/frps/control.go | 40 +++++++++++++-------------- cmd/frps/main.go | 7 +++-- {cmd/frpc => models/client}/config.go | 7 ++--- {cmd/frps => models/server}/config.go | 8 ++---- utils/conn/conn.go | 1 - 7 files changed, 41 insertions(+), 43 deletions(-) rename {cmd/frpc => models/client}/config.go (90%) rename {cmd/frps => models/server}/config.go (90%) diff --git a/cmd/frpc/control.go b/cmd/frpc/control.go index 2b0c59bb..971c5957 100644 --- a/cmd/frpc/control.go +++ b/cmd/frpc/control.go @@ -33,7 +33,7 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) { log.Debug("ProxyName [%s], server close this control conn", cli.Name) var sleepTime time.Duration = 1 for { - log.Debug("ProxyName [%s], try to reconnect to server[%s:%d]...", cli.Name, ServerAddr, ServerPort) + log.Debug("ProxyName [%s], try to reconnect to server[%s:%d]...", cli.Name, client.ServerAddr, client.ServerPort) tmpConn := loginToServer(cli) if tmpConn != nil { c.Close() @@ -52,7 +52,7 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) { continue } - cli.StartTunnel(ServerAddr, ServerPort) + cli.StartTunnel(client.ServerAddr, client.ServerPort) } } @@ -61,9 +61,9 @@ func loginToServer(cli *client.ProxyClient) (connection *conn.Conn) { connection = nil for i := 0; i < 1; i++ { - err := c.ConnectServer(ServerAddr, ServerPort) + err := c.ConnectServer(client.ServerAddr, client.ServerPort) if err != nil { - log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", cli.Name, ServerAddr, ServerPort, err) + log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", cli.Name, client.ServerAddr, client.ServerPort, err) break } @@ -99,7 +99,7 @@ func loginToServer(cli *client.ProxyClient) (connection *conn.Conn) { connection = c go startHeartBeat(connection) - log.Debug("ProxyName [%s], connect to server[%s:%d] success!", cli.Name, ServerAddr, ServerPort) + log.Debug("ProxyName [%s], connect to server[%s:%d] success!", cli.Name, client.ServerAddr, client.ServerPort) } if connection == nil { @@ -113,7 +113,7 @@ func startHeartBeat(con *conn.Conn) { isHeartBeatContinue = true log.Debug("Start to send heartbeat") for { - time.Sleep(time.Duration(HeartBeatInterval) * time.Second) + time.Sleep(time.Duration(client.HeartBeatInterval) * time.Second) if isHeartBeatContinue { err := con.Write("\n") if err != nil { diff --git a/cmd/frpc/main.go b/cmd/frpc/main.go index 7e7fe4ae..c17f3e76 100644 --- a/cmd/frpc/main.go +++ b/cmd/frpc/main.go @@ -4,22 +4,23 @@ import ( "os" "sync" + "github.com/fatedier/frp/models/client" "github.com/fatedier/frp/utils/log" ) func main() { - err := LoadConf("./frpc.ini") + err := client.LoadConf("./frpc.ini") if err != nil { os.Exit(-1) } - log.InitLog(LogWay, LogFile, LogLevel) + log.InitLog(client.LogWay, client.LogFile, client.LogLevel) // wait until all control goroutine exit var wait sync.WaitGroup - wait.Add(len(ProxyClients)) + wait.Add(len(client.ProxyClients)) - for _, client := range ProxyClients { + for _, client := range client.ProxyClients { go ControlProcess(client, &wait) } diff --git a/cmd/frps/control.go b/cmd/frps/control.go index 17973850..5f2eab99 100644 --- a/cmd/frps/control.go +++ b/cmd/frps/control.go @@ -63,34 +63,34 @@ func controlWorker(c *conn.Conn) { } // others is from server to client - server, ok := ProxyServers[clientCtlReq.ProxyName] + s, ok := server.ProxyServers[clientCtlReq.ProxyName] if !ok { log.Warn("ProxyName [%s] is not exist", clientCtlReq.ProxyName) return } // read control msg from client - go readControlMsgFromClient(server, c) + go readControlMsgFromClient(s, c) serverCtlReq := &msg.ClientCtlReq{} serverCtlReq.Type = consts.WorkConn for { - _, isStop := server.WaitUserConn() + _, isStop := s.WaitUserConn() if isStop { break } 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() + log.Warn("ProxyName [%s], write to client error, proxy exit", s.Name) + s.Close() return } - log.Debug("ProxyName [%s], write to client to add work conn success", server.Name) + log.Debug("ProxyName [%s], write to client to add work conn success", s.Name) } - log.Error("ProxyName [%s], I'm dead!", server.Name) + log.Error("ProxyName [%s], I'm dead!", s.Name) return } @@ -98,7 +98,7 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne succ = false needRes = true // check if proxy name exist - server, ok := ProxyServers[req.ProxyName] + s, ok := server.ProxyServers[req.ProxyName] if !ok { info = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName) log.Warn(info) @@ -106,7 +106,7 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne } // check password - if req.Passwd != server.Passwd { + if req.Passwd != s.Passwd { info = fmt.Sprintf("ProxyName [%s], password is not correct", req.ProxyName) log.Warn(info) return @@ -114,14 +114,14 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne // control conn if req.Type == consts.CtlConn { - if server.Status != consts.Idle { + if s.Status != consts.Idle { info = fmt.Sprintf("ProxyName [%s], already in use", req.ProxyName) log.Warn(info) return } // start proxy and listen for user conn, no block - err := server.Start() + err := s.Start() if err != nil { info = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err.Error()) log.Warn(info) @@ -132,12 +132,12 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne } else if req.Type == consts.WorkConn { // work conn needRes = false - if server.Status != consts.Working { + if s.Status != consts.Working { log.Warn("ProxyName [%s], is not working when it gets one new work conn", req.ProxyName) return } - server.CliConnChan <- c + s.CliConnChan <- c } else { info = fmt.Sprintf("ProxyName [%s], type [%d] unsupport", req.ProxyName, req.Type) log.Warn(info) @@ -148,13 +148,13 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne return } -func readControlMsgFromClient(server *server.ProxyServer, c *conn.Conn) { +func readControlMsgFromClient(s *server.ProxyServer, c *conn.Conn) { isContinueRead := true f := func() { isContinueRead = false - server.StopWaitUserConn() + s.StopWaitUserConn() } - timer := time.AfterFunc(time.Duration(HeartBeatTimeout)*time.Second, f) + timer := time.AfterFunc(time.Duration(server.HeartBeatTimeout)*time.Second, f) defer timer.Stop() for isContinueRead { @@ -162,16 +162,16 @@ func readControlMsgFromClient(server *server.ProxyServer, c *conn.Conn) { //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() + log.Warn("Server detect client[%s] is dead!", s.Name) + s.StopWaitUserConn() break } - log.Error("ProxyName [%s], read error:%s", server.Name, err.Error()) + log.Error("ProxyName [%s], read error:%s", s.Name, err.Error()) continue } if content == "\n" { - timer.Reset(time.Duration(HeartBeatTimeout) * time.Second) + timer.Reset(time.Duration(server.HeartBeatTimeout) * time.Second) } } } diff --git a/cmd/frps/main.go b/cmd/frps/main.go index 4aef2789..e21f927e 100644 --- a/cmd/frps/main.go +++ b/cmd/frps/main.go @@ -3,19 +3,20 @@ package main import ( "os" + "github.com/fatedier/frp/models/server" "github.com/fatedier/frp/utils/conn" "github.com/fatedier/frp/utils/log" ) func main() { - err := LoadConf("./frps.ini") + err := server.LoadConf("./frps.ini") if err != nil { os.Exit(-1) } - log.InitLog(LogWay, LogFile, LogLevel) + log.InitLog(server.LogWay, server.LogFile, server.LogLevel) - l, err := conn.Listen(BindAddr, BindPort) + l, err := conn.Listen(server.BindAddr, server.BindPort) if err != nil { log.Error("Create listener error, %v", err) os.Exit(-1) diff --git a/cmd/frpc/config.go b/models/client/config.go similarity index 90% rename from cmd/frpc/config.go rename to models/client/config.go index b500e474..063b216d 100644 --- a/cmd/frpc/config.go +++ b/models/client/config.go @@ -1,10 +1,9 @@ -package main +package client import ( "fmt" "strconv" - "github.com/fatedier/frp/models/client" ini "github.com/vaughan0/go-ini" ) @@ -18,7 +17,7 @@ var ( HeartBeatInterval int64 = 5 ) -var ProxyClients map[string]*client.ProxyClient = make(map[string]*client.ProxyClient) +var ProxyClients map[string]*ProxyClient = make(map[string]*ProxyClient) func LoadConf(confFile string) (err error) { var tmpStr string @@ -58,7 +57,7 @@ func LoadConf(confFile string) (err error) { // servers for name, section := range conf { if name != "common" { - proxyClient := &client.ProxyClient{} + proxyClient := &ProxyClient{} proxyClient.Name = name proxyClient.Passwd, ok = section["passwd"] diff --git a/cmd/frps/config.go b/models/server/config.go similarity index 90% rename from cmd/frps/config.go rename to models/server/config.go index d3b829fb..f9e974ed 100644 --- a/cmd/frps/config.go +++ b/models/server/config.go @@ -1,11 +1,9 @@ -package main +package server import ( "fmt" "strconv" - "github.com/fatedier/frp/models/server" - ini "github.com/vaughan0/go-ini" ) @@ -19,7 +17,7 @@ var ( HeartBeatTimeout int64 = 30 ) -var ProxyServers map[string]*server.ProxyServer = make(map[string]*server.ProxyServer) +var ProxyServers map[string]*ProxyServer = make(map[string]*ProxyServer) func LoadConf(confFile string) (err error) { var tmpStr string @@ -59,7 +57,7 @@ func LoadConf(confFile string) (err error) { // servers for name, section := range conf { if name != "common" { - proxyServer := &server.ProxyServer{} + proxyServer := &ProxyServer{} proxyServer.Name = name proxyServer.Passwd, ok = section["passwd"] diff --git a/utils/conn/conn.go b/utils/conn/conn.go index 1fa0cb34..7607ff48 100644 --- a/utils/conn/conn.go +++ b/utils/conn/conn.go @@ -80,7 +80,6 @@ func Listen(bindAddr string, bindPort int64) (l *Listener, err error) { for { conn, err := listener.AcceptTCP() if err != nil { - log.Error("Accept new tcp connection error, %v", err) continue }