123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- package config
- import (
- "fmt"
- "os"
- "strconv"
- "strings"
- "github.com/fatedier/frp/pkg/auth"
- ini "github.com/vaughan0/go-ini"
- )
- type ClientCommonConf struct {
- auth.ClientConfig
-
-
- 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"`
-
-
- DisableLogColor bool `json:"disable_log_color"`
-
-
- AdminAddr string `json:"admin_addr"`
-
-
-
- AdminPort int `json:"admin_port"`
-
-
- AdminUser string `json:"admin_user"`
-
-
- AdminPwd string `json:"admin_pwd"`
-
-
-
- AssetsDir string `json:"assets_dir"`
-
-
- PoolCount int `json:"pool_count"`
-
-
-
-
- TCPMux bool `json:"tcp_mux"`
-
-
-
- User string `json:"user"`
-
-
- DNSServer string `json:"dns_server"`
-
-
-
- LoginFailExit bool `json:"login_fail_exit"`
-
-
-
- Start map[string]struct{} `json:"start"`
-
-
-
- Protocol string `json:"protocol"`
-
-
-
- TLSEnable bool `json:"tls_enable"`
-
-
- TLSCertFile string `json:"tls_cert_file"`
-
-
-
- TLSKeyFile string `json:"tls_key_file"`
-
-
-
- TLSTrustedCaFile string `json:"tls_trusted_ca_file"`
-
-
-
- HeartbeatInterval int64 `json:"heartbeat_interval"`
-
-
-
- HeartbeatTimeout int64 `json:"heartbeat_timeout"`
-
- Metas map[string]string `json:"metas"`
-
-
- UDPPacketSize int64 `json:"udp_packet_size"`
- }
- func GetDefaultClientConf() ClientCommonConf {
- return ClientCommonConf{
- ServerAddr: "0.0.0.0",
- ServerPort: 7000,
- HTTPProxy: os.Getenv("http_proxy"),
- LogFile: "console",
- LogWay: "console",
- LogLevel: "info",
- LogMaxDays: 3,
- DisableLogColor: false,
- AdminAddr: "127.0.0.1",
- AdminPort: 0,
- AdminUser: "",
- AdminPwd: "",
- AssetsDir: "",
- PoolCount: 1,
- TCPMux: true,
- User: "",
- DNSServer: "",
- LoginFailExit: true,
- Start: make(map[string]struct{}),
- Protocol: "tcp",
- TLSEnable: false,
- TLSCertFile: "",
- TLSKeyFile: "",
- TLSTrustedCaFile: "",
- HeartbeatInterval: 30,
- HeartbeatTimeout: 90,
- Metas: make(map[string]string),
- UDPPacketSize: 1500,
- }
- }
- func UnmarshalClientConfFromIni(content string) (cfg ClientCommonConf, err error) {
- cfg = GetDefaultClientConf()
- conf, err := ini.Load(strings.NewReader(content))
- if err != nil {
- return ClientCommonConf{}, fmt.Errorf("parse ini conf file error: %v", err)
- }
- cfg.ClientConfig = auth.UnmarshalClientConfFromIni(conf)
- var (
- tmpStr string
- ok bool
- v int64
- )
- if tmpStr, ok = conf.Get("common", "server_addr"); ok {
- cfg.ServerAddr = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "server_port"); ok {
- v, err = strconv.ParseInt(tmpStr, 10, 64)
- if err != nil {
- err = fmt.Errorf("Parse conf error: invalid server_port")
- return
- }
- cfg.ServerPort = int(v)
- }
- if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
- cfg.DisableLogColor = true
- }
- if tmpStr, ok = conf.Get("common", "http_proxy"); ok {
- cfg.HTTPProxy = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "log_file"); ok {
- cfg.LogFile = tmpStr
- if cfg.LogFile == "console" {
- cfg.LogWay = "console"
- } else {
- cfg.LogWay = "file"
- }
- }
- if tmpStr, ok = conf.Get("common", "log_level"); ok {
- cfg.LogLevel = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "log_max_days"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
- cfg.LogMaxDays = v
- }
- }
- if tmpStr, ok = conf.Get("common", "admin_addr"); ok {
- cfg.AdminAddr = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "admin_port"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
- cfg.AdminPort = int(v)
- } else {
- err = fmt.Errorf("Parse conf error: invalid admin_port")
- return
- }
- }
- if tmpStr, ok = conf.Get("common", "admin_user"); ok {
- cfg.AdminUser = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "admin_pwd"); ok {
- cfg.AdminPwd = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
- cfg.AssetsDir = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "pool_count"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err == nil {
- cfg.PoolCount = int(v)
- }
- }
- if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
- cfg.TCPMux = false
- } else {
- cfg.TCPMux = true
- }
- if tmpStr, ok = conf.Get("common", "user"); ok {
- cfg.User = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "dns_server"); ok {
- cfg.DNSServer = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "start"); ok {
- proxyNames := strings.Split(tmpStr, ",")
- for _, name := range proxyNames {
- cfg.Start[strings.TrimSpace(name)] = struct{}{}
- }
- }
- if tmpStr, ok = conf.Get("common", "login_fail_exit"); ok && tmpStr == "false" {
- cfg.LoginFailExit = false
- } else {
- cfg.LoginFailExit = true
- }
- if tmpStr, ok = conf.Get("common", "protocol"); ok {
-
- if tmpStr != "tcp" && tmpStr != "kcp" && tmpStr != "websocket" {
- err = fmt.Errorf("Parse conf error: invalid protocol")
- return
- }
- cfg.Protocol = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "tls_enable"); ok && tmpStr == "true" {
- cfg.TLSEnable = true
- } else {
- cfg.TLSEnable = false
- }
- if tmpStr, ok = conf.Get("common", "tls_cert_file"); ok {
- cfg.TLSCertFile = tmpStr
- }
- if tmpStr, ok := conf.Get("common", "tls_key_file"); ok {
- cfg.TLSKeyFile = tmpStr
- }
- if tmpStr, ok := conf.Get("common", "tls_trusted_ca_file"); ok {
- cfg.TLSTrustedCaFile = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout")
- return
- }
- cfg.HeartbeatTimeout = v
- }
- if tmpStr, ok = conf.Get("common", "heartbeat_interval"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
- return
- }
- cfg.HeartbeatInterval = v
- }
- for k, v := range conf.Section("common") {
- if strings.HasPrefix(k, "meta_") {
- cfg.Metas[strings.TrimPrefix(k, "meta_")] = v
- }
- }
- if tmpStr, ok = conf.Get("common", "udp_packet_size"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid udp_packet_size")
- return
- }
- cfg.UDPPacketSize = v
- }
- return
- }
- func (cfg *ClientCommonConf) Check() (err error) {
- if cfg.HeartbeatInterval <= 0 {
- err = fmt.Errorf("Parse conf error: invalid heartbeat_interval")
- return
- }
- if cfg.HeartbeatTimeout < cfg.HeartbeatInterval {
- err = fmt.Errorf("Parse conf error: invalid heartbeat_timeout, heartbeat_timeout is less than heartbeat_interval")
- return
- }
- if cfg.TLSEnable == false {
- if cfg.TLSCertFile != "" {
- fmt.Println("WARNING! tls_cert_file is invalid when tls_enable is false")
- }
- if cfg.TLSKeyFile != "" {
- fmt.Println("WARNING! tls_key_file is invalid when tls_enable is false")
- }
- if cfg.TLSTrustedCaFile != "" {
- fmt.Println("WARNING! tls_trusted_ca_file is invalid when tls_enable is false")
- }
- }
- return
- }
|