123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- package config
- import (
- "fmt"
- "strings"
- "github.com/go-playground/validator/v10"
- "gopkg.in/ini.v1"
- "github.com/fatedier/frp/pkg/auth"
- plugin "github.com/fatedier/frp/pkg/plugin/server"
- "github.com/fatedier/frp/pkg/util/util"
- )
- type ServerCommonConf struct {
- auth.ServerConfig `ini:",extends"`
-
-
- BindAddr string `ini:"bind_addr" json:"bind_addr"`
-
-
- BindPort int `ini:"bind_port" json:"bind_port" validate:"gte=0,lte=65535"`
-
-
-
- KCPBindPort int `ini:"kcp_bind_port" json:"kcp_bind_port" validate:"gte=0,lte=65535"`
-
-
-
- QUICBindPort int `ini:"quic_bind_port" json:"quic_bind_port" validate:"gte=0,lte=65535"`
-
- QUICKeepalivePeriod int `ini:"quic_keepalive_period" json:"quic_keepalive_period" validate:"gte=0"`
- QUICMaxIdleTimeout int `ini:"quic_max_idle_timeout" json:"quic_max_idle_timeout" validate:"gte=0"`
- QUICMaxIncomingStreams int `ini:"quic_max_incoming_streams" json:"quic_max_incoming_streams" validate:"gte=0"`
-
-
- ProxyBindAddr string `ini:"proxy_bind_addr" json:"proxy_bind_addr"`
-
-
-
- VhostHTTPPort int `ini:"vhost_http_port" json:"vhost_http_port" validate:"gte=0,lte=65535"`
-
-
-
- VhostHTTPSPort int `ini:"vhost_https_port" json:"vhost_https_port" validate:"gte=0,lte=65535"`
-
-
-
-
- TCPMuxHTTPConnectPort int `ini:"tcpmux_httpconnect_port" json:"tcpmux_httpconnect_port" validate:"gte=0,lte=65535"`
-
- TCPMuxPassthrough bool `ini:"tcpmux_passthrough" json:"tcpmux_passthrough"`
-
-
- VhostHTTPTimeout int64 `ini:"vhost_http_timeout" json:"vhost_http_timeout"`
-
-
- DashboardAddr string `ini:"dashboard_addr" json:"dashboard_addr"`
-
-
-
- DashboardPort int `ini:"dashboard_port" json:"dashboard_port" validate:"gte=0,lte=65535"`
-
-
-
- DashboardTLSCertFile string `ini:"dashboard_tls_cert_file" json:"dashboard_tls_cert_file"`
-
-
-
- DashboardTLSKeyFile string `ini:"dashboard_tls_key_file" json:"dashboard_tls_key_file"`
-
-
- DashboardTLSMode bool `ini:"dashboard_tls_mode" json:"dashboard_tls_mode"`
-
-
- DashboardUser string `ini:"dashboard_user" json:"dashboard_user"`
-
-
- DashboardPwd string `ini:"dashboard_pwd" json:"dashboard_pwd"`
-
-
- EnablePrometheus bool `ini:"enable_prometheus" json:"enable_prometheus"`
-
-
-
- AssetsDir string `ini:"assets_dir" json:"assets_dir"`
-
-
-
- LogFile string `ini:"log_file" json:"log_file"`
-
-
-
-
- LogWay string `ini:"log_way" json:"log_way"`
-
-
- LogLevel string `ini:"log_level" json:"log_level"`
-
-
-
- LogMaxDays int64 `ini:"log_max_days" json:"log_max_days"`
-
-
- DisableLogColor bool `ini:"disable_log_color" json:"disable_log_color"`
-
-
- DetailedErrorsToClient bool `ini:"detailed_errors_to_client" json:"detailed_errors_to_client"`
-
-
-
-
-
- SubDomainHost string `ini:"subdomain_host" json:"subdomain_host"`
-
-
-
- TCPMux bool `ini:"tcp_mux" json:"tcp_mux"`
-
-
- TCPMuxKeepaliveInterval int64 `ini:"tcp_mux_keepalive_interval" json:"tcp_mux_keepalive_interval"`
-
-
- TCPKeepAlive int64 `ini:"tcp_keepalive" json:"tcp_keepalive"`
-
-
-
- Custom404Page string `ini:"custom_404_page" json:"custom_404_page"`
-
-
-
- AllowPorts map[int]struct{} `ini:"-" json:"-"`
-
- AllowPortsStr string `ini:"-" json:"-"`
-
-
- MaxPoolCount int64 `ini:"max_pool_count" json:"max_pool_count"`
-
-
-
- MaxPortsPerClient int64 `ini:"max_ports_per_client" json:"max_ports_per_client"`
-
-
- TLSOnly bool `ini:"tls_only" json:"tls_only"`
-
-
-
-
- TLSCertFile string `ini:"tls_cert_file" json:"tls_cert_file"`
-
-
-
-
- TLSKeyFile string `ini:"tls_key_file" json:"tls_key_file"`
-
-
-
-
- TLSTrustedCaFile string `ini:"tls_trusted_ca_file" json:"tls_trusted_ca_file"`
-
-
-
- HeartbeatTimeout int64 `ini:"heartbeat_timeout" json:"heartbeat_timeout"`
-
-
- UserConnTimeout int64 `ini:"user_conn_timeout" json:"user_conn_timeout"`
-
- HTTPPlugins map[string]plugin.HTTPPluginOptions `ini:"-" json:"http_plugins"`
-
-
- UDPPacketSize int64 `ini:"udp_packet_size" json:"udp_packet_size"`
-
-
- PprofEnable bool `ini:"pprof_enable" json:"pprof_enable"`
-
- NatHoleAnalysisDataReserveHours int64 `ini:"nat_hole_analysis_data_reserve_hours" json:"nat_hole_analysis_data_reserve_hours"`
- }
- func GetDefaultServerConf() ServerCommonConf {
- return ServerCommonConf{
- ServerConfig: auth.GetDefaultServerConf(),
- BindAddr: "0.0.0.0",
- BindPort: 7000,
- QUICKeepalivePeriod: 10,
- QUICMaxIdleTimeout: 30,
- QUICMaxIncomingStreams: 100000,
- VhostHTTPTimeout: 60,
- DashboardAddr: "0.0.0.0",
- LogFile: "console",
- LogWay: "console",
- LogLevel: "info",
- LogMaxDays: 3,
- DetailedErrorsToClient: true,
- TCPMux: true,
- TCPMuxKeepaliveInterval: 60,
- TCPKeepAlive: 7200,
- AllowPorts: make(map[int]struct{}),
- MaxPoolCount: 5,
- MaxPortsPerClient: 0,
- HeartbeatTimeout: 90,
- UserConnTimeout: 10,
- HTTPPlugins: make(map[string]plugin.HTTPPluginOptions),
- UDPPacketSize: 1500,
- NatHoleAnalysisDataReserveHours: 7 * 24,
- }
- }
- func UnmarshalServerConfFromIni(source interface{}) (ServerCommonConf, error) {
- f, err := ini.LoadSources(ini.LoadOptions{
- Insensitive: false,
- InsensitiveSections: false,
- InsensitiveKeys: false,
- IgnoreInlineComment: true,
- AllowBooleanKeys: true,
- }, source)
- if err != nil {
- return ServerCommonConf{}, err
- }
- s, err := f.GetSection("common")
- if err != nil {
- return ServerCommonConf{}, err
- }
- common := GetDefaultServerConf()
- err = s.MapTo(&common)
- if err != nil {
- return ServerCommonConf{}, err
- }
-
- allowPortStr := s.Key("allow_ports").String()
- if allowPortStr != "" {
- allowPorts, err := util.ParseRangeNumbers(allowPortStr)
- if err != nil {
- return ServerCommonConf{}, fmt.Errorf("invalid allow_ports: %v", err)
- }
- for _, port := range allowPorts {
- common.AllowPorts[int(port)] = struct{}{}
- }
- common.AllowPortsStr = allowPortStr
- }
-
- pluginOpts := make(map[string]plugin.HTTPPluginOptions)
- for _, section := range f.Sections() {
- name := section.Name()
- if !strings.HasPrefix(name, "plugin.") {
- continue
- }
- opt, err := loadHTTPPluginOpt(section)
- if err != nil {
- return ServerCommonConf{}, err
- }
- pluginOpts[opt.Name] = *opt
- }
- common.HTTPPlugins = pluginOpts
- return common, nil
- }
- func (cfg *ServerCommonConf) Complete() {
- if cfg.LogFile == "console" {
- cfg.LogWay = "console"
- } else {
- cfg.LogWay = "file"
- }
- if cfg.ProxyBindAddr == "" {
- cfg.ProxyBindAddr = cfg.BindAddr
- }
- if cfg.TLSTrustedCaFile != "" {
- cfg.TLSOnly = true
- }
- }
- func (cfg *ServerCommonConf) Validate() error {
- if !cfg.DashboardTLSMode {
- if cfg.DashboardTLSCertFile != "" {
- fmt.Println("WARNING! dashboard_tls_cert_file is invalid when dashboard_tls_mode is false")
- }
- if cfg.DashboardTLSKeyFile != "" {
- fmt.Println("WARNING! dashboard_tls_key_file is invalid when dashboard_tls_mode is false")
- }
- } else {
- if cfg.DashboardTLSCertFile == "" {
- return fmt.Errorf("ERROR! dashboard_tls_cert_file must be specified when dashboard_tls_mode is true")
- }
- if cfg.DashboardTLSKeyFile == "" {
- return fmt.Errorf("ERROR! dashboard_tls_cert_file must be specified when dashboard_tls_mode is true")
- }
- }
- return validator.New().Struct(cfg)
- }
- func loadHTTPPluginOpt(section *ini.Section) (*plugin.HTTPPluginOptions, error) {
- name := strings.TrimSpace(strings.TrimPrefix(section.Name(), "plugin."))
- opt := new(plugin.HTTPPluginOptions)
- err := section.MapTo(opt)
- if err != nil {
- return nil, err
- }
- opt.Name = name
- return opt, nil
- }
|