Move config.go to models/xxx

This commit is contained in:
fatedier 2016-02-18 18:24:48 +08:00
parent 50165053f8
commit 84f8addd6a
7 changed files with 41 additions and 43 deletions

View File

@ -33,7 +33,7 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
log.Debug("ProxyName [%s], server close this control conn", cli.Name) log.Debug("ProxyName [%s], server close this control conn", cli.Name)
var sleepTime time.Duration = 1 var sleepTime time.Duration = 1
for { 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) tmpConn := loginToServer(cli)
if tmpConn != nil { if tmpConn != nil {
c.Close() c.Close()
@ -52,7 +52,7 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
continue 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 connection = nil
for i := 0; i < 1; i++ { for i := 0; i < 1; i++ {
err := c.ConnectServer(ServerAddr, ServerPort) err := c.ConnectServer(client.ServerAddr, client.ServerPort)
if err != nil { 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 break
} }
@ -99,7 +99,7 @@ func loginToServer(cli *client.ProxyClient) (connection *conn.Conn) {
connection = c connection = c
go startHeartBeat(connection) 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 { if connection == nil {
@ -113,7 +113,7 @@ func startHeartBeat(con *conn.Conn) {
isHeartBeatContinue = true isHeartBeatContinue = true
log.Debug("Start to send heartbeat") log.Debug("Start to send heartbeat")
for { for {
time.Sleep(time.Duration(HeartBeatInterval) * time.Second) time.Sleep(time.Duration(client.HeartBeatInterval) * time.Second)
if isHeartBeatContinue { if isHeartBeatContinue {
err := con.Write("\n") err := con.Write("\n")
if err != nil { if err != nil {

View File

@ -4,22 +4,23 @@ import (
"os" "os"
"sync" "sync"
"github.com/fatedier/frp/models/client"
"github.com/fatedier/frp/utils/log" "github.com/fatedier/frp/utils/log"
) )
func main() { func main() {
err := LoadConf("./frpc.ini") err := client.LoadConf("./frpc.ini")
if err != nil { if err != nil {
os.Exit(-1) os.Exit(-1)
} }
log.InitLog(LogWay, LogFile, LogLevel) log.InitLog(client.LogWay, client.LogFile, client.LogLevel)
// wait until all control goroutine exit // wait until all control goroutine exit
var wait sync.WaitGroup 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) go ControlProcess(client, &wait)
} }

View File

@ -63,34 +63,34 @@ func controlWorker(c *conn.Conn) {
} }
// others is from server to client // others is from server to client
server, ok := ProxyServers[clientCtlReq.ProxyName] s, ok := server.ProxyServers[clientCtlReq.ProxyName]
if !ok { if !ok {
log.Warn("ProxyName [%s] is not exist", clientCtlReq.ProxyName) log.Warn("ProxyName [%s] is not exist", clientCtlReq.ProxyName)
return return
} }
// read control msg from client // read control msg from client
go readControlMsgFromClient(server, c) go readControlMsgFromClient(s, c)
serverCtlReq := &msg.ClientCtlReq{} serverCtlReq := &msg.ClientCtlReq{}
serverCtlReq.Type = consts.WorkConn serverCtlReq.Type = consts.WorkConn
for { for {
_, isStop := server.WaitUserConn() _, isStop := s.WaitUserConn()
if isStop { if isStop {
break break
} }
buf, _ := json.Marshal(serverCtlReq) buf, _ := json.Marshal(serverCtlReq)
err = c.Write(string(buf) + "\n") err = c.Write(string(buf) + "\n")
if err != nil { if err != nil {
log.Warn("ProxyName [%s], write to client error, proxy exit", server.Name) log.Warn("ProxyName [%s], write to client error, proxy exit", s.Name)
server.Close() s.Close()
return 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 return
} }
@ -98,7 +98,7 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne
succ = false succ = false
needRes = true needRes = true
// check if proxy name exist // check if proxy name exist
server, ok := ProxyServers[req.ProxyName] s, ok := server.ProxyServers[req.ProxyName]
if !ok { if !ok {
info = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName) info = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName)
log.Warn(info) log.Warn(info)
@ -106,7 +106,7 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne
} }
// check password // check password
if req.Passwd != server.Passwd { if req.Passwd != s.Passwd {
info = fmt.Sprintf("ProxyName [%s], password is not correct", req.ProxyName) info = fmt.Sprintf("ProxyName [%s], password is not correct", req.ProxyName)
log.Warn(info) log.Warn(info)
return return
@ -114,14 +114,14 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne
// control conn // control conn
if req.Type == consts.CtlConn { 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) info = fmt.Sprintf("ProxyName [%s], already in use", req.ProxyName)
log.Warn(info) log.Warn(info)
return return
} }
// start proxy and listen for user conn, no block // start proxy and listen for user conn, no block
err := server.Start() err := s.Start()
if err != nil { if err != nil {
info = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err.Error()) info = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err.Error())
log.Warn(info) 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 { } else if req.Type == consts.WorkConn {
// work conn // work conn
needRes = false 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) log.Warn("ProxyName [%s], is not working when it gets one new work conn", req.ProxyName)
return return
} }
server.CliConnChan <- c s.CliConnChan <- c
} else { } else {
info = fmt.Sprintf("ProxyName [%s], type [%d] unsupport", req.ProxyName, req.Type) info = fmt.Sprintf("ProxyName [%s], type [%d] unsupport", req.ProxyName, req.Type)
log.Warn(info) log.Warn(info)
@ -148,13 +148,13 @@ func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, ne
return return
} }
func readControlMsgFromClient(server *server.ProxyServer, c *conn.Conn) { func readControlMsgFromClient(s *server.ProxyServer, c *conn.Conn) {
isContinueRead := true isContinueRead := true
f := func() { f := func() {
isContinueRead = false 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() defer timer.Stop()
for isContinueRead { for isContinueRead {
@ -162,16 +162,16 @@ func readControlMsgFromClient(server *server.ProxyServer, c *conn.Conn) {
//log.Debug("Receive msg from client! content:%s", content) //log.Debug("Receive msg from client! content:%s", content)
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
log.Warn("Server detect client[%s] is dead!", server.Name) log.Warn("Server detect client[%s] is dead!", s.Name)
server.StopWaitUserConn() s.StopWaitUserConn()
break break
} }
log.Error("ProxyName [%s], read error:%s", server.Name, err.Error()) log.Error("ProxyName [%s], read error:%s", s.Name, err.Error())
continue continue
} }
if content == "\n" { if content == "\n" {
timer.Reset(time.Duration(HeartBeatTimeout) * time.Second) timer.Reset(time.Duration(server.HeartBeatTimeout) * time.Second)
} }
} }
} }

View File

@ -3,19 +3,20 @@ package main
import ( import (
"os" "os"
"github.com/fatedier/frp/models/server"
"github.com/fatedier/frp/utils/conn" "github.com/fatedier/frp/utils/conn"
"github.com/fatedier/frp/utils/log" "github.com/fatedier/frp/utils/log"
) )
func main() { func main() {
err := LoadConf("./frps.ini") err := server.LoadConf("./frps.ini")
if err != nil { if err != nil {
os.Exit(-1) 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 { if err != nil {
log.Error("Create listener error, %v", err) log.Error("Create listener error, %v", err)
os.Exit(-1) os.Exit(-1)

View File

@ -1,10 +1,9 @@
package main package client
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/fatedier/frp/models/client"
ini "github.com/vaughan0/go-ini" ini "github.com/vaughan0/go-ini"
) )
@ -18,7 +17,7 @@ var (
HeartBeatInterval int64 = 5 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) { func LoadConf(confFile string) (err error) {
var tmpStr string var tmpStr string
@ -58,7 +57,7 @@ func LoadConf(confFile string) (err error) {
// servers // servers
for name, section := range conf { for name, section := range conf {
if name != "common" { if name != "common" {
proxyClient := &client.ProxyClient{} proxyClient := &ProxyClient{}
proxyClient.Name = name proxyClient.Name = name
proxyClient.Passwd, ok = section["passwd"] proxyClient.Passwd, ok = section["passwd"]

View File

@ -1,11 +1,9 @@
package main package server
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/fatedier/frp/models/server"
ini "github.com/vaughan0/go-ini" ini "github.com/vaughan0/go-ini"
) )
@ -19,7 +17,7 @@ var (
HeartBeatTimeout int64 = 30 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) { func LoadConf(confFile string) (err error) {
var tmpStr string var tmpStr string
@ -59,7 +57,7 @@ func LoadConf(confFile string) (err error) {
// servers // servers
for name, section := range conf { for name, section := range conf {
if name != "common" { if name != "common" {
proxyServer := &server.ProxyServer{} proxyServer := &ProxyServer{}
proxyServer.Name = name proxyServer.Name = name
proxyServer.Passwd, ok = section["passwd"] proxyServer.Passwd, ok = section["passwd"]

View File

@ -80,7 +80,6 @@ func Listen(bindAddr string, bindPort int64) (l *Listener, err error) {
for { for {
conn, err := listener.AcceptTCP() conn, err := listener.AcceptTCP()
if err != nil { if err != nil {
log.Error("Accept new tcp connection error, %v", err)
continue continue
} }