diff --git a/cmd/frpc/config.go b/cmd/frpc/config.go index ff6f1ad..b500e47 100644 --- a/cmd/frpc/config.go +++ b/cmd/frpc/config.go @@ -4,8 +4,7 @@ import ( "fmt" "strconv" - "github.com/fatedier/frp/pkg/models" - + "github.com/fatedier/frp/models/client" ini "github.com/vaughan0/go-ini" ) @@ -19,7 +18,7 @@ var ( HeartBeatInterval int64 = 5 ) -var ProxyClients map[string]*models.ProxyClient = make(map[string]*models.ProxyClient) +var ProxyClients map[string]*client.ProxyClient = make(map[string]*client.ProxyClient) func LoadConf(confFile string) (err error) { var tmpStr string @@ -59,7 +58,7 @@ func LoadConf(confFile string) (err error) { // servers for name, section := range conf { if name != "common" { - proxyClient := &models.ProxyClient{} + proxyClient := &client.ProxyClient{} proxyClient.Name = name proxyClient.Passwd, ok = section["passwd"] diff --git a/cmd/frpc/control.go b/cmd/frpc/control.go index 57fce55..2b0c59b 100644 --- a/cmd/frpc/control.go +++ b/cmd/frpc/control.go @@ -6,14 +6,16 @@ import ( "sync" "time" - "github.com/fatedier/frp/pkg/models" - "github.com/fatedier/frp/pkg/utils/conn" - "github.com/fatedier/frp/pkg/utils/log" + "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" ) var isHeartBeatContinue bool = true -func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { +func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) { defer wait.Done() c := loginToServer(cli) @@ -54,7 +56,7 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { } } -func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { +func loginToServer(cli *client.ProxyClient) (connection *conn.Conn) { c := &conn.Conn{} connection = nil @@ -65,8 +67,8 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { break } - req := &models.ClientCtlReq{ - Type: models.ControlConn, + req := &msg.ClientCtlReq{ + Type: consts.CtlConn, ProxyName: cli.Name, Passwd: cli.Passwd, } @@ -84,7 +86,7 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { } log.Debug("ProxyName [%s], read [%s]", cli.Name, res) - clientCtlRes := &models.ClientCtlRes{} + clientCtlRes := &msg.ClientCtlRes{} if err = json.Unmarshal([]byte(res), &clientCtlRes); err != nil { log.Error("ProxyName [%s], format server response error, %v", cli.Name, err) break diff --git a/cmd/frpc/main.go b/cmd/frpc/main.go index 1bb5eb3..7e7fe4a 100644 --- a/cmd/frpc/main.go +++ b/cmd/frpc/main.go @@ -4,7 +4,7 @@ import ( "os" "sync" - "github.com/fatedier/frp/pkg/utils/log" + "github.com/fatedier/frp/utils/log" ) func main() { diff --git a/cmd/frps/config.go b/cmd/frps/config.go index af523e0..d3b829f 100644 --- a/cmd/frps/config.go +++ b/cmd/frps/config.go @@ -4,7 +4,7 @@ import ( "fmt" "strconv" - "github.com/fatedier/frp/pkg/models" + "github.com/fatedier/frp/models/server" ini "github.com/vaughan0/go-ini" ) @@ -19,7 +19,7 @@ var ( HeartBeatTimeout int64 = 30 ) -var ProxyServers map[string]*models.ProxyServer = make(map[string]*models.ProxyServer) +var ProxyServers map[string]*server.ProxyServer = make(map[string]*server.ProxyServer) func LoadConf(confFile string) (err error) { var tmpStr string @@ -59,7 +59,7 @@ func LoadConf(confFile string) (err error) { // servers for name, section := range conf { if name != "common" { - proxyServer := &models.ProxyServer{} + proxyServer := &server.ProxyServer{} proxyServer.Name = name proxyServer.Passwd, ok = section["passwd"] diff --git a/cmd/frps/control.go b/cmd/frps/control.go index 609b25a..1797385 100644 --- a/cmd/frps/control.go +++ b/cmd/frps/control.go @@ -6,9 +6,11 @@ import ( "io" "time" - "github.com/fatedier/frp/pkg/models" - "github.com/fatedier/frp/pkg/utils/conn" - "github.com/fatedier/frp/pkg/utils/log" + "github.com/fatedier/frp/models/consts" + "github.com/fatedier/frp/models/msg" + "github.com/fatedier/frp/models/server" + "github.com/fatedier/frp/utils/conn" + "github.com/fatedier/frp/utils/log" ) func ProcessControlConn(l *conn.Listener) { @@ -30,18 +32,18 @@ func controlWorker(c *conn.Conn) { } log.Debug("get: %s", res) - clientCtlReq := &models.ClientCtlReq{} - clientCtlRes := &models.ClientCtlRes{} + clientCtlReq := &msg.ClientCtlReq{} + clientCtlRes := &msg.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) + succ, info, needRes := checkProxy(clientCtlReq, c) if !succ { clientCtlRes.Code = 1 - clientCtlRes.Msg = msg + clientCtlRes.Msg = info } if needRes { @@ -70,8 +72,8 @@ func controlWorker(c *conn.Conn) { // read control msg from client go readControlMsgFromClient(server, c) - serverCtlReq := &models.ClientCtlReq{} - serverCtlReq.Type = models.WorkConn + serverCtlReq := &msg.ClientCtlReq{} + serverCtlReq.Type = consts.WorkConn for { _, isStop := server.WaitUserConn() if isStop { @@ -92,52 +94,53 @@ func controlWorker(c *conn.Conn) { return } -func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string, needRes bool) { +func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info 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) + info = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName) + log.Warn(info) return } // check password if req.Passwd != server.Passwd { - msg = fmt.Sprintf("ProxyName [%s], password is not correct", req.ProxyName) - log.Warn(msg) + info = fmt.Sprintf("ProxyName [%s], password is not correct", req.ProxyName) + log.Warn(info) return } // 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) + if req.Type == consts.CtlConn { + if server.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() if err != nil { - msg = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err.Error()) - log.Warn(msg) + info = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err.Error()) + log.Warn(info) return } log.Info("ProxyName [%s], start proxy success", req.ProxyName) - } else if req.Type == models.WorkConn { + } else if req.Type == consts.WorkConn { // work conn needRes = false - if server.Status != models.Working { + if server.Status != consts.Working { log.Warn("ProxyName [%s], is not working when it gets one new work conn", req.ProxyName) return } server.CliConnChan <- c } else { - log.Warn("ProxyName [%s], type [%d] unsupport", req.ProxyName, req.Type) + info = fmt.Sprintf("ProxyName [%s], type [%d] unsupport", req.ProxyName, req.Type) + log.Warn(info) return } @@ -145,7 +148,7 @@ func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string, return } -func readControlMsgFromClient(server *models.ProxyServer, c *conn.Conn) { +func readControlMsgFromClient(server *server.ProxyServer, c *conn.Conn) { isContinueRead := true f := func() { isContinueRead = false diff --git a/cmd/frps/main.go b/cmd/frps/main.go index 83fdc48..4aef278 100644 --- a/cmd/frps/main.go +++ b/cmd/frps/main.go @@ -3,8 +3,8 @@ package main import ( "os" - "github.com/fatedier/frp/pkg/utils/conn" - "github.com/fatedier/frp/pkg/utils/log" + "github.com/fatedier/frp/utils/conn" + "github.com/fatedier/frp/utils/log" ) func main() { diff --git a/pkg/models/client.go b/models/client/client.go similarity index 86% rename from pkg/models/client.go rename to models/client/client.go index 3fc5f57..481ba8e 100644 --- a/pkg/models/client.go +++ b/models/client/client.go @@ -1,10 +1,12 @@ -package models +package client import ( "encoding/json" - "github.com/fatedier/frp/pkg/utils/conn" - "github.com/fatedier/frp/pkg/utils/log" + "github.com/fatedier/frp/models/consts" + "github.com/fatedier/frp/models/msg" + "github.com/fatedier/frp/utils/conn" + "github.com/fatedier/frp/utils/log" ) type ProxyClient struct { @@ -36,8 +38,8 @@ func (p *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err return } - req := &ClientCtlReq{ - Type: WorkConn, + req := &msg.ClientCtlReq{ + Type: consts.WorkConn, ProxyName: p.Name, Passwd: p.Passwd, } diff --git a/models/consts/consts.go b/models/consts/consts.go new file mode 100644 index 0000000..51dfe20 --- /dev/null +++ b/models/consts/consts.go @@ -0,0 +1,13 @@ +package consts + +// server status +const ( + Idle = iota + Working +) + +// connection type +const ( + CtlConn = iota + WorkConn +) diff --git a/pkg/models/msg.go b/models/msg/msg.go similarity index 83% rename from pkg/models/msg.go rename to models/msg/msg.go index a3018e7..6555296 100644 --- a/pkg/models/msg.go +++ b/models/msg/msg.go @@ -1,16 +1,10 @@ -package models +package msg type GeneralRes struct { Code int64 `json:"code"` Msg string `json:"msg"` } -// type -const ( - ControlConn = iota - WorkConn -) - type ClientCtlReq struct { Type int64 `json:"type"` ProxyName string `json:"proxy_name"` diff --git a/pkg/models/server.go b/models/server/server.go similarity index 91% rename from pkg/models/server.go rename to models/server/server.go index 7f58e5e..0d11717 100644 --- a/pkg/models/server.go +++ b/models/server/server.go @@ -1,16 +1,12 @@ -package models +package server import ( "container/list" "sync" - "github.com/fatedier/frp/pkg/utils/conn" - "github.com/fatedier/frp/pkg/utils/log" -) - -const ( - Idle = iota - Working + "github.com/fatedier/frp/models/consts" + "github.com/fatedier/frp/utils/conn" + "github.com/fatedier/frp/utils/log" ) type ProxyServer struct { @@ -29,7 +25,7 @@ type ProxyServer struct { } func (p *ProxyServer) Init() { - p.Status = Idle + p.Status = consts.Idle p.CtlMsgChan = make(chan int64) p.StopBlockChan = make(chan int64) p.CliConnChan = make(chan *conn.Conn) @@ -51,7 +47,7 @@ func (p *ProxyServer) Start() (err error) { return err } - p.Status = Working + p.Status = consts.Working // start a goroutine for listener go func() { @@ -62,7 +58,7 @@ func (p *ProxyServer) Start() (err error) { // put to list p.Lock() - if p.Status != Working { + if p.Status != consts.Working { log.Debug("ProxyName [%s] is not working, new user conn close", p.Name) c.Close() p.Unlock() @@ -107,7 +103,7 @@ func (p *ProxyServer) Start() (err error) { func (p *ProxyServer) Close() { p.Lock() - p.Status = Idle + p.Status = consts.Idle p.CtlMsgChan = make(chan int64) p.CliConnChan = make(chan *conn.Conn) p.UserConnList = list.New() diff --git a/pkg/utils/conn/conn.go b/utils/conn/conn.go similarity index 98% rename from pkg/utils/conn/conn.go rename to utils/conn/conn.go index 5f65329..1fa0cb3 100644 --- a/pkg/utils/conn/conn.go +++ b/utils/conn/conn.go @@ -7,7 +7,7 @@ import ( "net" "sync" - "github.com/fatedier/frp/pkg/utils/log" + "github.com/fatedier/frp/utils/log" ) type Listener struct { diff --git a/pkg/utils/log/log.go b/utils/log/log.go similarity index 100% rename from pkg/utils/log/log.go rename to utils/log/log.go diff --git a/pkg/utils/pcrypto/pcrypto.go b/utils/pcrypto/pcrypto.go similarity index 100% rename from pkg/utils/pcrypto/pcrypto.go rename to utils/pcrypto/pcrypto.go diff --git a/pkg/utils/pcrypto/pcrypto_test.go b/utils/pcrypto/pcrypto_test.go similarity index 100% rename from pkg/utils/pcrypto/pcrypto_test.go rename to utils/pcrypto/pcrypto_test.go