123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- package config
- import (
- "fmt"
- "strconv"
- "strings"
- "github.com/fatedier/frp/pkg/auth"
- plugin "github.com/fatedier/frp/pkg/plugin/server"
- "github.com/fatedier/frp/pkg/util/util"
- ini "github.com/vaughan0/go-ini"
- )
- type ServerCommonConf struct {
- auth.ServerConfig
-
-
- BindAddr string `json:"bind_addr"`
-
-
- BindPort int `json:"bind_port"`
-
-
-
- BindUDPPort int `json:"bind_udp_port"`
-
-
-
- KCPBindPort int `json:"kcp_bind_port"`
-
-
- ProxyBindAddr string `json:"proxy_bind_addr"`
-
-
-
- VhostHTTPPort int `json:"vhost_http_port"`
-
-
-
- VhostHTTPSPort int `json:"vhost_https_port"`
-
-
-
-
- TCPMuxHTTPConnectPort int `json:"tcpmux_httpconnect_port"`
-
-
- VhostHTTPTimeout int64 `json:"vhost_http_timeout"`
-
-
- DashboardAddr string `json:"dashboard_addr"`
-
-
-
- DashboardPort int `json:"dashboard_port"`
-
-
- DashboardUser string `json:"dashboard_user"`
-
-
- DashboardPwd string `json:"dashboard_pwd"`
-
-
- EnablePrometheus bool `json:"enable_prometheus"`
-
-
-
- AssetsDir string `json:"asserts_dir"`
-
-
-
- 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"`
-
-
- DetailedErrorsToClient bool `json:"detailed_errors_to_client"`
-
-
-
-
-
- SubDomainHost string `json:"subdomain_host"`
-
-
-
- TCPMux bool `json:"tcp_mux"`
-
-
-
- Custom404Page string `json:"custom_404_page"`
-
-
-
- AllowPorts map[int]struct{}
-
-
- MaxPoolCount int64 `json:"max_pool_count"`
-
-
-
- MaxPortsPerClient int64 `json:"max_ports_per_client"`
-
-
- TLSOnly bool `json:"tls_only"`
-
-
-
-
- TLSCertFile string `json:"tls_cert_file"`
-
-
-
-
- TLSKeyFile string `json:"tls_key_file"`
-
-
-
-
- TLSTrustedCaFile string `json:"tls_trusted_ca_file"`
-
-
-
- HeartBeatTimeout int64 `json:"heart_beat_timeout"`
-
-
- UserConnTimeout int64 `json:"user_conn_timeout"`
-
- HTTPPlugins map[string]plugin.HTTPPluginOptions `json:"http_plugins"`
-
-
- UDPPacketSize int64 `json:"udp_packet_size"`
- }
- func GetDefaultServerConf() ServerCommonConf {
- return ServerCommonConf{
- BindAddr: "0.0.0.0",
- BindPort: 7000,
- BindUDPPort: 0,
- KCPBindPort: 0,
- ProxyBindAddr: "0.0.0.0",
- VhostHTTPPort: 0,
- VhostHTTPSPort: 0,
- TCPMuxHTTPConnectPort: 0,
- VhostHTTPTimeout: 60,
- DashboardAddr: "0.0.0.0",
- DashboardPort: 0,
- DashboardUser: "admin",
- DashboardPwd: "admin",
- EnablePrometheus: false,
- AssetsDir: "",
- LogFile: "console",
- LogWay: "console",
- LogLevel: "info",
- LogMaxDays: 3,
- DisableLogColor: false,
- DetailedErrorsToClient: true,
- SubDomainHost: "",
- TCPMux: true,
- AllowPorts: make(map[int]struct{}),
- MaxPoolCount: 5,
- MaxPortsPerClient: 0,
- TLSOnly: false,
- TLSCertFile: "",
- TLSKeyFile: "",
- TLSTrustedCaFile: "",
- HeartBeatTimeout: 90,
- UserConnTimeout: 10,
- Custom404Page: "",
- HTTPPlugins: make(map[string]plugin.HTTPPluginOptions),
- UDPPacketSize: 1500,
- }
- }
- func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error) {
- cfg = GetDefaultServerConf()
- conf, err := ini.Load(strings.NewReader(content))
- if err != nil {
- err = fmt.Errorf("parse ini conf file error: %v", err)
- return ServerCommonConf{}, err
- }
- UnmarshalPluginsFromIni(conf, &cfg)
- cfg.ServerConfig = auth.UnmarshalServerConfFromIni(conf)
- var (
- tmpStr string
- ok bool
- v int64
- )
- if tmpStr, ok = conf.Get("common", "bind_addr"); ok {
- cfg.BindAddr = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "bind_port"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid bind_port")
- return
- }
- cfg.BindPort = int(v)
- }
- if tmpStr, ok = conf.Get("common", "bind_udp_port"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid bind_udp_port")
- return
- }
- cfg.BindUDPPort = int(v)
- }
- if tmpStr, ok = conf.Get("common", "kcp_bind_port"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid kcp_bind_port")
- return
- }
- cfg.KCPBindPort = int(v)
- }
- if tmpStr, ok = conf.Get("common", "proxy_bind_addr"); ok {
- cfg.ProxyBindAddr = tmpStr
- } else {
- cfg.ProxyBindAddr = cfg.BindAddr
- }
- if tmpStr, ok = conf.Get("common", "vhost_http_port"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid vhost_http_port")
- return
- }
- cfg.VhostHTTPPort = int(v)
- } else {
- cfg.VhostHTTPPort = 0
- }
- if tmpStr, ok = conf.Get("common", "vhost_https_port"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid vhost_https_port")
- return
- }
- cfg.VhostHTTPSPort = int(v)
- } else {
- cfg.VhostHTTPSPort = 0
- }
- if tmpStr, ok = conf.Get("common", "tcpmux_httpconnect_port"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid tcpmux_httpconnect_port")
- return
- }
- cfg.TCPMuxHTTPConnectPort = int(v)
- } else {
- cfg.TCPMuxHTTPConnectPort = 0
- }
- if tmpStr, ok = conf.Get("common", "vhost_http_timeout"); ok {
- v, errRet := strconv.ParseInt(tmpStr, 10, 64)
- if errRet != nil || v < 0 {
- err = fmt.Errorf("Parse conf error: invalid vhost_http_timeout")
- return
- }
- cfg.VhostHTTPTimeout = v
- }
- if tmpStr, ok = conf.Get("common", "dashboard_addr"); ok {
- cfg.DashboardAddr = tmpStr
- } else {
- cfg.DashboardAddr = cfg.BindAddr
- }
- if tmpStr, ok = conf.Get("common", "dashboard_port"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid dashboard_port")
- return
- }
- cfg.DashboardPort = int(v)
- } else {
- cfg.DashboardPort = 0
- }
- if tmpStr, ok = conf.Get("common", "dashboard_user"); ok {
- cfg.DashboardUser = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "dashboard_pwd"); ok {
- cfg.DashboardPwd = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "enable_prometheus"); ok && tmpStr == "true" {
- cfg.EnablePrometheus = true
- }
- if tmpStr, ok = conf.Get("common", "assets_dir"); ok {
- cfg.AssetsDir = 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 {
- v, err = strconv.ParseInt(tmpStr, 10, 64)
- if err == nil {
- cfg.LogMaxDays = v
- }
- }
- if tmpStr, ok = conf.Get("common", "disable_log_color"); ok && tmpStr == "true" {
- cfg.DisableLogColor = true
- }
- if tmpStr, ok = conf.Get("common", "detailed_errors_to_client"); ok && tmpStr == "false" {
- cfg.DetailedErrorsToClient = false
- } else {
- cfg.DetailedErrorsToClient = true
- }
- if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
-
- ports, errRet := util.ParseRangeNumbers(allowPortsStr)
- if errRet != nil {
- err = fmt.Errorf("Parse conf error: allow_ports: %v", errRet)
- return
- }
- for _, port := range ports {
- cfg.AllowPorts[int(port)] = struct{}{}
- }
- }
- if tmpStr, ok = conf.Get("common", "max_pool_count"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid max_pool_count")
- return
- }
- if v < 0 {
- err = fmt.Errorf("Parse conf error: invalid max_pool_count")
- return
- }
- cfg.MaxPoolCount = v
- }
- if tmpStr, ok = conf.Get("common", "max_ports_per_client"); ok {
- if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
- err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
- return
- }
- if v < 0 {
- err = fmt.Errorf("Parse conf error: invalid max_ports_per_client")
- return
- }
- cfg.MaxPortsPerClient = v
- }
- if tmpStr, ok = conf.Get("common", "subdomain_host"); ok {
- cfg.SubDomainHost = strings.ToLower(strings.TrimSpace(tmpStr))
- }
- if tmpStr, ok = conf.Get("common", "tcp_mux"); ok && tmpStr == "false" {
- cfg.TCPMux = false
- } else {
- cfg.TCPMux = true
- }
- if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
- cfg.Custom404Page = tmpStr
- }
- if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
- v, errRet := strconv.ParseInt(tmpStr, 10, 64)
- if errRet != nil {
- err = fmt.Errorf("Parse conf error: heartbeat_timeout is incorrect")
- return
- }
- cfg.HeartBeatTimeout = v
- }
- if tmpStr, ok = conf.Get("common", "tls_only"); ok && tmpStr == "true" {
- cfg.TLSOnly = true
- } else {
- cfg.TLSOnly = false
- }
- 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
- }
- 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
- cfg.TLSOnly = true
- }
- return
- }
- func UnmarshalPluginsFromIni(sections ini.File, cfg *ServerCommonConf) {
- for name, section := range sections {
- if strings.HasPrefix(name, "plugin.") {
- name = strings.TrimSpace(strings.TrimPrefix(name, "plugin."))
- options := plugin.HTTPPluginOptions{
- Name: name,
- Addr: section["addr"],
- Path: section["path"],
- Ops: strings.Split(section["ops"], ","),
- }
- for i := range options.Ops {
- options.Ops[i] = strings.TrimSpace(options.Ops[i])
- }
- cfg.HTTPPlugins[name] = options
- }
- }
- }
- func (cfg *ServerCommonConf) Check() error {
- return nil
- }
|