Clean up some code -- reorganize some structs and unexport some consts.

This commit is contained in:
Eugene Bujak 2018-11-27 20:51:12 +03:00
parent ec7efcc9d6
commit 6cb991fe7f
3 changed files with 31 additions and 35 deletions

View File

@ -14,42 +14,37 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
// Current schema version. We compare it with the value from const (
// the configuration file and perform necessary upgrade operations if needed currentSchemaVersion = 1 // used for upgrading from old configs to new config
const CurrentSchemaVersion = 1 dataDir = "data" // data storage
filterDir = "filters" // cache location for downloaded filters, it's under DataDir
// Directory where we'll store all downloaded filters contents userFilterID = 0 // special filter ID, always 0
const FiltersDir = "filters" )
// User filter ID is always 0
const UserFilterId = 0
// Just a counter that we use for incrementing the filter ID // Just a counter that we use for incrementing the filter ID
var NextFilterId = time.Now().Unix() var NextFilterId = time.Now().Unix()
// configuration is loaded from YAML // configuration is loaded from YAML
// field ordering is important -- yaml fields will mirror ordering from here
type configuration struct { type configuration struct {
// Config filename (can be overriden via the command line arguments) ourConfigFilename string // Config filename (can be overriden via the command line arguments)
ourConfigFilename string ourBinaryDir string // Location of our directory, used to protect against CWD being somewhere else
// Basically, this is our working directory
ourBinaryDir string
// Directory to store data (i.e. filters contents)
ourDataDir string
// Schema version of the config file. This value is used when performing the app updates. BindHost string `yaml:"bind_host"`
SchemaVersion int `yaml:"schema_version"` BindPort int `yaml:"bind_port"`
BindHost string `yaml:"bind_host"` AuthName string `yaml:"auth_name"`
BindPort int `yaml:"bind_port"` AuthPass string `yaml:"auth_pass"`
AuthName string `yaml:"auth_name"` Language string `yaml:"language"` // two-letter ISO 639-1 language code
AuthPass string `yaml:"auth_pass"` CoreDNS coreDNSConfig `yaml:"coredns"`
CoreDNS coreDNSConfig `yaml:"coredns"` Filters []filter `yaml:"filters"`
Filters []filter `yaml:"filters"` UserRules []string `yaml:"user_rules,omitempty"`
UserRules []string `yaml:"user_rules"`
Language string `yaml:"language"` // two-letter ISO 639-1 language code
sync.RWMutex `yaml:"-"` sync.RWMutex `yaml:"-"`
SchemaVersion int `yaml:"schema_version"` // keeping last so that users will be less tempted to change it -- used when upgrading between versions
} }
// field ordering is important -- yaml fields will mirror ordering from here
type coreDNSConfig struct { type coreDNSConfig struct {
binaryFile string binaryFile string
coreFile string coreFile string
@ -72,14 +67,16 @@ type coreDNSConfig struct {
UpstreamDNS []string `yaml:"upstream_dns"` UpstreamDNS []string `yaml:"upstream_dns"`
} }
// field ordering is important -- yaml fields will mirror ordering from here
type filter struct { type filter struct {
ID int64 `json:"id" yaml:"id"` // auto-assigned when filter is added (see NextFilterId) Enabled bool `json:"enabled"`
URL string `json:"url"` URL string `json:"url"`
Name string `json:"name" yaml:"name"` Name string `json:"name" yaml:"name"`
Enabled bool `json:"enabled"`
RulesCount int `json:"rulesCount" yaml:"-"` RulesCount int `json:"rulesCount" yaml:"-"`
Contents []byte `json:"-" yaml:"-"` LastUpdated time.Time `json:"lastUpdated,omitempty" yaml:"last_updated,omitempty"`
LastUpdated time.Time `json:"lastUpdated" yaml:"last_updated"` ID int64 // auto-assigned when filter is added (see NextFilterId)
Contents []byte `json:"-" yaml:"-"` // not in yaml or json
} }
var defaultDNS = []string{"tls://1.1.1.1", "tls://1.0.0.1"} var defaultDNS = []string{"tls://1.1.1.1", "tls://1.0.0.1"}
@ -87,7 +84,6 @@ var defaultDNS = []string{"tls://1.1.1.1", "tls://1.0.0.1"}
// initialize to default values, will be changed later when reading config or parsing command line // initialize to default values, will be changed later when reading config or parsing command line
var config = configuration{ var config = configuration{
ourConfigFilename: "AdGuardHome.yaml", ourConfigFilename: "AdGuardHome.yaml",
ourDataDir: "data",
BindPort: 3000, BindPort: 3000,
BindHost: "127.0.0.1", BindHost: "127.0.0.1",
CoreDNS: coreDNSConfig{ CoreDNS: coreDNSConfig{
@ -125,7 +121,7 @@ func userFilter() filter {
userFilter := filter{ userFilter := filter{
// User filter always has constant ID=0 // User filter always has constant ID=0
ID: UserFilterId, ID: userFilterID,
Contents: contents, Contents: contents,
Enabled: true, Enabled: true,
} }

View File

@ -659,7 +659,7 @@ func (filter *filter) update(force bool) (bool, error) {
return true, nil return true, nil
} }
// saves filter contents to the file in config.ourDataDir // saves filter contents to the file in dataDir
func (filter *filter) save() error { func (filter *filter) save() error {
filterFilePath := filter.Path() filterFilePath := filter.Path()
log.Printf("Saving filter %d contents to: %s", filter.ID, filterFilePath) log.Printf("Saving filter %d contents to: %s", filter.ID, filterFilePath)
@ -672,7 +672,7 @@ func (filter *filter) save() error {
return nil return nil
} }
// loads filter contents from the file in config.ourDataDir // loads filter contents from the file in dataDir
func (filter *filter) load() error { func (filter *filter) load() error {
if !filter.Enabled { if !filter.Enabled {
// No need to load a filter that is not enabled // No need to load a filter that is not enabled
@ -704,7 +704,7 @@ func (filter *filter) load() error {
// Path to the filter contents // Path to the filter contents
func (filter *filter) Path() string { func (filter *filter) Path() string {
return filepath.Join(config.ourBinaryDir, config.ourDataDir, FiltersDir, strconv.FormatInt(filter.ID, 10)+".txt") return filepath.Join(config.ourBinaryDir, dataDir, filterDir, strconv.FormatInt(filter.ID, 10)+".txt")
} }
// ------------ // ------------

View File

@ -45,7 +45,7 @@ func upgradeConfig() error {
return err return err
} }
if schemaVersion == CurrentSchemaVersion { if schemaVersion == currentSchemaVersion {
// do nothing // do nothing
return nil return nil
} }