2016-03-14 03:18:24 +00: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 10:24:48 +00:00
|
|
|
package client
|
2016-01-27 13:24:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
ini "github.com/vaughan0/go-ini"
|
|
|
|
)
|
|
|
|
|
|
|
|
// common config
|
|
|
|
var (
|
2016-02-05 06:36:04 +00:00
|
|
|
ServerAddr string = "0.0.0.0"
|
|
|
|
ServerPort int64 = 7000
|
2016-03-13 16:48:22 +00:00
|
|
|
LogFile string = "console"
|
|
|
|
LogWay string = "console"
|
|
|
|
LogLevel string = "info"
|
|
|
|
HeartBeatInterval int64 = 20
|
2016-03-17 02:25:23 +00:00
|
|
|
HeartBeatTimeout int64 = 90
|
2016-01-27 13:24:36 +00:00
|
|
|
)
|
|
|
|
|
2016-02-18 10:24:48 +00:00
|
|
|
var ProxyClients map[string]*ProxyClient = make(map[string]*ProxyClient)
|
2016-01-27 13:24:36 +00: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", "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
|
2016-03-13 16:48:22 +00:00
|
|
|
if LogFile == "console" {
|
|
|
|
LogWay = "console"
|
|
|
|
} else {
|
|
|
|
LogWay = "file"
|
|
|
|
}
|
2016-01-27 13:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpStr, ok = conf.Get("common", "log_level")
|
|
|
|
if ok {
|
|
|
|
LogLevel = tmpStr
|
|
|
|
}
|
|
|
|
|
2016-04-05 09:18:21 +00:00
|
|
|
var authToken string
|
|
|
|
tmpStr, ok = conf.Get("common", "auth_token")
|
|
|
|
if ok {
|
|
|
|
authToken = tmpStr
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("auth_token not found")
|
|
|
|
}
|
|
|
|
|
2016-02-29 06:56:47 +00:00
|
|
|
// proxies
|
2016-01-27 13:24:36 +00:00
|
|
|
for name, section := range conf {
|
|
|
|
if name != "common" {
|
2016-02-18 10:24:48 +00:00
|
|
|
proxyClient := &ProxyClient{}
|
2016-03-31 10:48:18 +00:00
|
|
|
// name
|
2016-01-27 13:24:36 +00:00
|
|
|
proxyClient.Name = name
|
|
|
|
|
2016-04-05 09:18:21 +00:00
|
|
|
// auth_token
|
|
|
|
proxyClient.AuthToken = authToken
|
2016-01-27 13:24:36 +00:00
|
|
|
|
2016-03-31 10:48:18 +00:00
|
|
|
// local_ip
|
2016-02-29 04:38:45 +00:00
|
|
|
proxyClient.LocalIp, ok = section["local_ip"]
|
|
|
|
if !ok {
|
2016-02-29 06:56:47 +00:00
|
|
|
// use 127.0.0.1 as default
|
|
|
|
proxyClient.LocalIp = "127.0.0.1"
|
2016-02-29 04:38:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 10:48:18 +00:00
|
|
|
// local_port
|
2016-01-27 13:24:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-03-31 10:48:18 +00:00
|
|
|
// use_encryption
|
|
|
|
proxyClient.UseEncryption = false
|
|
|
|
useEncryptionStr, ok := section["use_encryption"]
|
|
|
|
if ok && useEncryptionStr == "true" {
|
|
|
|
proxyClient.UseEncryption = true
|
|
|
|
}
|
|
|
|
|
2016-01-27 13:24:36 +00:00
|
|
|
ProxyClients[proxyClient.Name] = proxyClient
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ProxyClients) == 0 {
|
|
|
|
return fmt.Errorf("Parse ini file error: no proxy config found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|