2017-03-09 02:03:47 +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.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2017-05-25 01:45:38 +08:00
|
|
|
"strings"
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
ini "github.com/vaughan0/go-ini"
|
|
|
|
)
|
|
|
|
|
|
|
|
// client common config
|
|
|
|
type ClientCommonConf struct {
|
2018-04-10 17:46:49 +08:00
|
|
|
ServerAddr string `json:"server_addr"`
|
|
|
|
ServerPort int `json:"server_port"`
|
|
|
|
HttpProxy string `json:"http_proxy"`
|
|
|
|
LogFile string `json:"log_file"`
|
|
|
|
LogWay string `json:"log_way"`
|
|
|
|
LogLevel string `json:"log_level"`
|
|
|
|
LogMaxDays int64 `json:"log_max_days"`
|
2019-08-12 00:47:35 +08:00
|
|
|
DisableLogColor bool `json:"disable_log_color"`
|
2018-04-10 17:46:49 +08:00
|
|
|
Token string `json:"token"`
|
|
|
|
AdminAddr string `json:"admin_addr"`
|
|
|
|
AdminPort int `json:"admin_port"`
|
|
|
|
AdminUser string `json:"admin_user"`
|
|
|
|
AdminPwd string `json:"admin_pwd"`
|
|
|
|
PoolCount int `json:"pool_count"`
|
|
|
|
TcpMux bool `json:"tcp_mux"`
|
|
|
|
User string `json:"user"`
|
2018-04-23 02:59:40 +08:00
|
|
|
DnsServer string `json:"dns_server"`
|
2018-04-10 17:46:49 +08:00
|
|
|
LoginFailExit bool `json:"login_fail_exit"`
|
|
|
|
Start map[string]struct{} `json:"start"`
|
|
|
|
Protocol string `json:"protocol"`
|
2019-03-11 14:14:31 +08:00
|
|
|
TLSEnable bool `json:"tls_enable"`
|
2018-04-10 17:46:49 +08:00
|
|
|
HeartBeatInterval int64 `json:"heartbeat_interval"`
|
|
|
|
HeartBeatTimeout int64 `json:"heartbeat_timeout"`
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func GetDefaultClientConf() *ClientCommonConf {
|
2017-03-09 02:03:47 +08:00
|
|
|
return &ClientCommonConf{
|
|
|
|
ServerAddr: "0.0.0.0",
|
|
|
|
ServerPort: 7000,
|
2018-04-10 17:46:49 +08:00
|
|
|
HttpProxy: os.Getenv("http_proxy"),
|
2017-03-09 02:03:47 +08:00
|
|
|
LogFile: "console",
|
|
|
|
LogWay: "console",
|
|
|
|
LogLevel: "info",
|
|
|
|
LogMaxDays: 3,
|
2019-08-12 00:47:35 +08:00
|
|
|
DisableLogColor: false,
|
2018-04-10 17:46:49 +08:00
|
|
|
Token: "",
|
2017-07-13 02:20:49 +08:00
|
|
|
AdminAddr: "127.0.0.1",
|
|
|
|
AdminPort: 0,
|
|
|
|
AdminUser: "",
|
|
|
|
AdminPwd: "",
|
2017-03-09 02:03:47 +08:00
|
|
|
PoolCount: 1,
|
2017-05-17 17:47:20 +08:00
|
|
|
TcpMux: true,
|
2017-03-09 02:03:47 +08:00
|
|
|
User: "",
|
2018-04-23 02:59:40 +08:00
|
|
|
DnsServer: "",
|
2017-05-25 01:10:58 +08:00
|
|
|
LoginFailExit: true,
|
2017-05-25 01:45:38 +08:00
|
|
|
Start: make(map[string]struct{}),
|
2017-06-04 19:56:21 +08:00
|
|
|
Protocol: "tcp",
|
2019-03-11 14:14:31 +08:00
|
|
|
TLSEnable: false,
|
2017-05-15 00:08:21 +08:00
|
|
|
HeartBeatInterval: 30,
|
|
|
|
HeartBeatTimeout: 90,
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func UnmarshalClientConfFromIni(defaultCfg *ClientCommonConf, content string) (cfg *ClientCommonConf, err error) {
|
|
|
|
cfg = defaultCfg
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = GetDefaultClientConf()
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := ini.Load(strings.NewReader(content))
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("parse ini conf file error: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
var (
|
|
|
|
tmpStr string
|
|
|
|
ok bool
|
|
|
|
v int64
|
|
|
|
)
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "server_addr"); ok {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.ServerAddr = tmpStr
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "server_port"); ok {
|
2018-01-17 21:49:37 +08:00
|
|
|
v, err = strconv.ParseInt(tmpStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Parse conf error: invalid server_port")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cfg.ServerPort = int(v)
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2019-08-12 00:47:35 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
|
|
|
|
cfg.DisableLogColor = true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "http_proxy"); ok {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.HttpProxy = tmpStr
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "log_file"); ok {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.LogFile = tmpStr
|
|
|
|
if cfg.LogFile == "console" {
|
|
|
|
cfg.LogWay = "console"
|
|
|
|
} else {
|
|
|
|
cfg.LogWay = "file"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "log_level"); ok {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.LogLevel = tmpStr
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
|
2017-07-13 02:20:49 +08:00
|
|
|
if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
|
|
|
|
cfg.LogMaxDays = v
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "token"); ok {
|
|
|
|
cfg.Token = tmpStr
|
2017-03-09 02:03:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "admin_addr"); ok {
|
2017-07-13 02:20:49 +08:00
|
|
|
cfg.AdminAddr = tmpStr
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "admin_port"); ok {
|
2017-07-13 02:20:49 +08:00
|
|
|
if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
|
2018-01-17 21:49:37 +08:00
|
|
|
cfg.AdminPort = int(v)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Parse conf error: invalid admin_port")
|
|
|
|
return
|
2017-07-13 02:20:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "admin_user"); ok {
|
2017-07-13 02:20:49 +08:00
|
|
|
cfg.AdminUser = tmpStr
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "admin_pwd"); ok {
|
2017-07-13 02:20:49 +08:00
|
|
|
cfg.AdminPwd = tmpStr
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "pool_count"); ok {
|
|
|
|
if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.PoolCount = int(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
|
2017-05-17 17:47:20 +08:00
|
|
|
cfg.TcpMux = false
|
|
|
|
} else {
|
|
|
|
cfg.TcpMux = true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "user"); ok {
|
2017-03-09 02:03:47 +08:00
|
|
|
cfg.User = tmpStr
|
|
|
|
}
|
|
|
|
|
2018-04-23 02:59:40 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "dns_server"); ok {
|
|
|
|
cfg.DnsServer = tmpStr
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "start"); ok {
|
2017-05-25 01:45:38 +08:00
|
|
|
proxyNames := strings.Split(tmpStr, ",")
|
|
|
|
for _, name := range proxyNames {
|
2017-07-13 02:20:49 +08:00
|
|
|
cfg.Start[strings.TrimSpace(name)] = struct{}{}
|
2017-05-25 01:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "login_fail_exit"); ok && tmpStr == "false" {
|
2017-05-25 01:10:58 +08:00
|
|
|
cfg.LoginFailExit = false
|
|
|
|
} else {
|
|
|
|
cfg.LoginFailExit = true
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "protocol"); ok {
|
2018-08-10 11:43:08 +08:00
|
|
|
// Now it only support tcp and kcp and websocket.
|
|
|
|
if tmpStr != "tcp" && tmpStr != "kcp" && tmpStr != "websocket" {
|
|
|
|
err = fmt.Errorf("Parse conf error: invalid protocol")
|
|
|
|
return
|
2017-06-04 19:56:21 +08:00
|
|
|
}
|
|
|
|
cfg.Protocol = tmpStr
|
|
|
|
}
|
|
|
|
|
2019-03-11 14:14:31 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "tls_enable"); ok && tmpStr == "true" {
|
|
|
|
cfg.TLSEnable = true
|
|
|
|
} else {
|
|
|
|
cfg.TLSEnable = false
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
|
|
|
|
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
|
2018-01-17 21:49:37 +08:00
|
|
|
err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout")
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
cfg.HeartBeatTimeout = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
if tmpStr, ok = conf.Get("common", "heartbeat_interval"); ok {
|
|
|
|
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
|
2018-01-17 21:49:37 +08:00
|
|
|
err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
cfg.HeartBeatInterval = v
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
return
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
func (cfg *ClientCommonConf) Check() (err error) {
|
2017-03-09 02:03:47 +08:00
|
|
|
if cfg.HeartBeatInterval <= 0 {
|
2018-01-17 21:49:37 +08:00
|
|
|
err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.HeartBeatTimeout < cfg.HeartBeatInterval {
|
2018-01-17 21:49:37 +08:00
|
|
|
err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout, heartbeat_timeout is less than heartbeat_interval")
|
2017-03-09 02:03:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|