Skip to content

Commit

Permalink
Move config.go to models/xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed Feb 18, 2016
1 parent 5016505 commit 84f8add
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
12 changes: 6 additions & 6 deletions cmd/frpc/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -52,7 +52,7 @@ func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
continue
}

cli.StartTunnel(ServerAddr, ServerPort)
cli.StartTunnel(client.ServerAddr, client.ServerPort)
}
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions cmd/frpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
40 changes: 20 additions & 20 deletions cmd/frps/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,65 +63,65 @@ 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
}

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]
s, ok := server.ProxyServers[req.ProxyName]
if !ok {
info = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName)
log.Warn(info)
return
}

// 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
}

// 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)
Expand All @@ -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)
Expand All @@ -148,30 +148,30 @@ 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 {
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()
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)
}
}
}
7 changes: 4 additions & 3 deletions cmd/frps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions cmd/frpc/config.go → models/client/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package main
package client

import (
"fmt"
"strconv"

"github.com/fatedier/frp/models/client"
ini "github.com/vaughan0/go-ini"
)

Expand All @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
8 changes: 3 additions & 5 deletions cmd/frps/config.go → models/server/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package main
package server

import (
"fmt"
"strconv"

"github.com/fatedier/frp/models/server"

ini "github.com/vaughan0/go-ini"
)

Expand All @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
1 change: 0 additions & 1 deletion utils/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 84f8add

Please sign in to comment.