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

@@ -1,88 +0,0 @@
package main
import (
"fmt"
"strconv"
"github.com/fatedier/frp/models/client"
ini "github.com/vaughan0/go-ini"
)
// common config
var (
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]*client.ProxyClient = make(map[string]*client.ProxyClient)
func LoadConf(confFile string) (err error) {
var tmpStr string
var ok bool
conf, err := ini.LoadFile(confFile)
if err != nil {
return err
}
// common
tmpStr, ok = conf.Get("common", "server_addr")
if ok {
ServerAddr = tmpStr
}
tmpStr, ok = conf.Get("common", "server_port")
if ok {
ServerPort, _ = strconv.ParseInt(tmpStr, 10, 64)
}
tmpStr, ok = conf.Get("common", "log_file")
if ok {
LogFile = tmpStr
}
tmpStr, ok = conf.Get("common", "log_level")
if ok {
LogLevel = tmpStr
}
tmpStr, ok = conf.Get("common", "log_way")
if ok {
LogWay = tmpStr
}
// servers
for name, section := range conf {
if name != "common" {
proxyClient := &client.ProxyClient{}
proxyClient.Name = name
proxyClient.Passwd, ok = section["passwd"]
if !ok {
return fmt.Errorf("Parse ini file error: proxy [%s] no passwd found", proxyClient.Name)
}
portStr, ok := section["local_port"]
if ok {
proxyClient.LocalPort, err = strconv.ParseInt(portStr, 10, 64)
if err != nil {
return fmt.Errorf("Parse ini file error: proxy [%s] local_port error", proxyClient.Name)
}
} else {
return fmt.Errorf("Parse ini file error: proxy [%s] local_port not found", proxyClient.Name)
}
ProxyClients[proxyClient.Name] = proxyClient
}
}
if len(ProxyClients) == 0 {
return fmt.Errorf("Parse ini file error: no proxy config found")
}
return nil
}

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)
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 {

View File

@@ -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)
}