github.com/gophish/gophish@v0.12.2-0.20230915144530-8e7929441393/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  
     7  	log "github.com/gophish/gophish/logger"
     8  )
     9  
    10  // AdminServer represents the Admin server configuration details
    11  type AdminServer struct {
    12  	ListenURL            string   `json:"listen_url"`
    13  	UseTLS               bool     `json:"use_tls"`
    14  	CertPath             string   `json:"cert_path"`
    15  	KeyPath              string   `json:"key_path"`
    16  	CSRFKey              string   `json:"csrf_key"`
    17  	AllowedInternalHosts []string `json:"allowed_internal_hosts"`
    18  	TrustedOrigins       []string `json:"trusted_origins"`
    19  }
    20  
    21  // PhishServer represents the Phish server configuration details
    22  type PhishServer struct {
    23  	ListenURL string `json:"listen_url"`
    24  	UseTLS    bool   `json:"use_tls"`
    25  	CertPath  string `json:"cert_path"`
    26  	KeyPath   string `json:"key_path"`
    27  }
    28  
    29  // Config represents the configuration information.
    30  type Config struct {
    31  	AdminConf      AdminServer `json:"admin_server"`
    32  	PhishConf      PhishServer `json:"phish_server"`
    33  	DBName         string      `json:"db_name"`
    34  	DBPath         string      `json:"db_path"`
    35  	DBSSLCaPath    string      `json:"db_sslca_path"`
    36  	MigrationsPath string      `json:"migrations_prefix"`
    37  	TestFlag       bool        `json:"test_flag"`
    38  	ContactAddress string      `json:"contact_address"`
    39  	Logging        *log.Config `json:"logging"`
    40  }
    41  
    42  // Version contains the current gophish version
    43  var Version = ""
    44  
    45  // ServerName is the server type that is returned in the transparency response.
    46  const ServerName = "gophish"
    47  
    48  // LoadConfig loads the configuration from the specified filepath
    49  func LoadConfig(filepath string) (*Config, error) {
    50  	// Get the config file
    51  	configFile, err := ioutil.ReadFile(filepath)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	config := &Config{}
    56  	err = json.Unmarshal(configFile, config)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	if config.Logging == nil {
    61  		config.Logging = &log.Config{}
    62  	}
    63  	// Choosing the migrations directory based on the database used.
    64  	config.MigrationsPath = config.MigrationsPath + config.DBName
    65  	// Explicitly set the TestFlag to false to prevent config.json overrides
    66  	config.TestFlag = false
    67  	return config, nil
    68  }