frp/src/frp/models/server/config.go

109 lines
2.5 KiB
Go
Raw Normal View History

2016-03-14 11:18:24 +08:00
// Copyright 2016 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2016-02-18 18:24:48 +08:00
package server
2016-01-27 21:24:36 +08:00
import (
"fmt"
"strconv"
ini "github.com/vaughan0/go-ini"
)
// common config
var (
BindAddr string = "0.0.0.0"
2016-03-14 00:21:40 +08:00
BindPort int64 = 7000
LogFile string = "console"
LogWay string = "console" // console or file
LogLevel string = "info"
2016-03-17 10:25:23 +08:00
HeartBeatTimeout int64 = 90
2016-02-25 10:28:34 +08:00
UserConnTimeout int64 = 10
2016-01-27 21:24:36 +08:00
)
2016-02-18 18:24:48 +08:00
var ProxyServers map[string]*ProxyServer = make(map[string]*ProxyServer)
2016-01-27 21:24:36 +08:00
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", "bind_addr")
if ok {
BindAddr = tmpStr
}
tmpStr, ok = conf.Get("common", "bind_port")
if ok {
BindPort, _ = strconv.ParseInt(tmpStr, 10, 64)
}
tmpStr, ok = conf.Get("common", "log_file")
if ok {
LogFile = tmpStr
2016-03-14 00:21:40 +08:00
if LogFile == "console" {
LogWay = "console"
} else {
LogWay = "file"
}
2016-01-27 21:24:36 +08:00
}
tmpStr, ok = conf.Get("common", "log_level")
if ok {
LogLevel = tmpStr
}
// servers
for name, section := range conf {
if name != "common" {
2016-02-18 18:24:48 +08:00
proxyServer := &ProxyServer{}
2016-01-27 21:24:36 +08:00
proxyServer.Name = name
proxyServer.AuthToken, ok = section["auth_token"]
2016-01-27 21:24:36 +08:00
if !ok {
return fmt.Errorf("Parse ini file error: proxy [%s] no auth_token found", proxyServer.Name)
2016-01-27 21:24:36 +08:00
}
proxyServer.BindAddr, ok = section["bind_addr"]
if !ok {
proxyServer.BindAddr = "0.0.0.0"
}
portStr, ok := section["listen_port"]
if ok {
proxyServer.ListenPort, err = strconv.ParseInt(portStr, 10, 64)
if err != nil {
return fmt.Errorf("Parse ini file error: proxy [%s] listen_port error", proxyServer.Name)
}
} else {
return fmt.Errorf("Parse ini file error: proxy [%s] listen_port not found", proxyServer.Name)
}
proxyServer.Init()
ProxyServers[proxyServer.Name] = proxyServer
}
}
if len(ProxyServers) == 0 {
return fmt.Errorf("Parse ini file error: no proxy config found")
}
return nil
}