frp/src/models/client/config.go

229 lines
6.1 KiB
Go
Raw Normal View History

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"
2016-06-26 14:36:07 +00:00
"strings"
2016-01-27 13:24:36 +00:00
ini "github.com/vaughan0/go-ini"
)
// common config
var (
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"
LogMaxDays int64 = 3
2016-06-27 16:21:13 +00:00
PrivilegeToken string = ""
2016-03-13 16:48:22 +00:00
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
}
tmpStr, ok = conf.Get("common", "log_max_days")
if ok {
LogMaxDays, _ = strconv.ParseInt(tmpStr, 10, 64)
}
2016-06-27 16:21:13 +00:00
tmpStr, ok = conf.Get("common", "privilege_token")
2016-06-26 14:36:07 +00:00
if ok {
2016-06-27 16:21:13 +00:00
PrivilegeToken = tmpStr
2016-06-26 14:36:07 +00:00
}
var authToken string
tmpStr, ok = conf.Get("common", "auth_token")
if ok {
authToken = tmpStr
}
// 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{}
// name
2016-01-27 13:24:36 +00:00
proxyClient.Name = name
2016-06-27 16:21:13 +00:00
// auth_token
proxyClient.AuthToken = authToken
// local_ip
proxyClient.LocalIp, ok = section["local_ip"]
if !ok {
// use 127.0.0.1 as default
proxyClient.LocalIp = "127.0.0.1"
}
// local_port
2016-06-26 14:36:07 +00:00
tmpStr, ok = section["local_port"]
2016-01-27 13:24:36 +00:00
if ok {
2016-06-26 14:36:07 +00:00
proxyClient.LocalPort, err = strconv.ParseInt(tmpStr, 10, 64)
2016-01-27 13:24:36 +00:00
if err != nil {
2016-06-26 14:36:07 +00:00
return fmt.Errorf("Parse conf error: proxy [%s] local_port error", proxyClient.Name)
2016-01-27 13:24:36 +00:00
}
} else {
2016-06-26 14:36:07 +00:00
return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", proxyClient.Name)
2016-01-27 13:24:36 +00:00
}
2016-04-18 07:16:40 +00:00
// type
proxyClient.Type = "tcp"
2016-06-26 14:36:07 +00:00
tmpStr, ok = section["type"]
2016-04-18 07:16:40 +00:00
if ok {
2016-06-26 14:36:07 +00:00
if tmpStr != "tcp" && tmpStr != "http" && tmpStr != "https" {
return fmt.Errorf("Parse conf error: proxy [%s] type error", proxyClient.Name)
2016-04-18 07:16:40 +00:00
}
2016-06-26 14:36:07 +00:00
proxyClient.Type = tmpStr
2016-04-18 07:16:40 +00:00
}
// use_encryption
proxyClient.UseEncryption = false
2016-06-26 14:36:07 +00:00
tmpStr, ok = section["use_encryption"]
if ok && tmpStr == "true" {
proxyClient.UseEncryption = true
}
2016-06-14 08:58:53 +00:00
// use_gzip
proxyClient.UseGzip = false
2016-06-26 14:36:07 +00:00
tmpStr, ok = section["use_gzip"]
if ok && tmpStr == "true" {
proxyClient.UseGzip = true
}
if proxyClient.Type == "http" {
// host_header_rewrite
tmpStr, ok = section["host_header_rewrite"]
if ok {
proxyClient.HostHeaderRewrite = tmpStr
}
}
2016-06-26 14:36:07 +00:00
// privilege_mode
proxyClient.PrivilegeMode = false
tmpStr, ok = section["privilege_mode"]
if ok && tmpStr == "true" {
proxyClient.PrivilegeMode = true
}
2016-07-29 15:08:00 +00:00
// pool_count
proxyClient.PoolCount = 0
tmpStr, ok = section["pool_count"]
if ok {
tmpInt, err := strconv.ParseInt(tmpStr, 10, 64)
if err != nil || tmpInt < 0 {
return fmt.Errorf("Parse conf error: proxy [%s] pool_count error", proxyClient.Name)
}
proxyClient.PoolCount = tmpInt
}
2016-06-26 14:36:07 +00:00
// configures used in privilege mode
if proxyClient.PrivilegeMode == true {
2016-06-27 16:21:13 +00:00
if PrivilegeToken == "" {
2016-07-29 15:08:00 +00:00
return fmt.Errorf("Parse conf error: proxy [%s] privilege_token must be set when privilege_mode = true", proxyClient.Name)
2016-06-27 16:21:13 +00:00
} else {
proxyClient.PrivilegeToken = PrivilegeToken
}
2016-06-26 14:36:07 +00:00
if proxyClient.Type == "tcp" {
// remote_port
tmpStr, ok = section["remote_port"]
if ok {
proxyClient.RemotePort, err = strconv.ParseInt(tmpStr, 10, 64)
if err != nil {
return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", proxyClient.Name)
}
} else {
return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", proxyClient.Name)
}
} else if proxyClient.Type == "http" {
// custom_domains
2016-06-26 14:36:07 +00:00
domainStr, ok := section["custom_domains"]
if ok {
proxyClient.CustomDomains = strings.Split(domainStr, ",")
if len(proxyClient.CustomDomains) == 0 {
return fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyClient.Name)
}
for i, domain := range proxyClient.CustomDomains {
proxyClient.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
}
} else {
return fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyClient.Name)
}
} else if proxyClient.Type == "https" {
// custom_domains
2016-06-26 14:36:07 +00:00
domainStr, ok := section["custom_domains"]
if ok {
proxyClient.CustomDomains = strings.Split(domainStr, ",")
if len(proxyClient.CustomDomains) == 0 {
return fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals https", proxyClient.Name)
}
for i, domain := range proxyClient.CustomDomains {
proxyClient.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
}
} else {
return fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyClient.Name)
}
}
}
2016-01-27 13:24:36 +00:00
ProxyClients[proxyClient.Name] = proxyClient
}
}
if len(ProxyClients) == 0 {
2016-06-26 14:36:07 +00:00
return fmt.Errorf("Parse conf error: no proxy config found")
2016-01-27 13:24:36 +00:00
}
return nil
}