/
config.go
138 lines (118 loc) · 3.56 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
SynoEdit - A Synology package and HTML user interface to edit files
Copyright (C) 2018 Sebastian Schmidt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path"
"path/filepath"
"github.com/BurntSushi/toml"
)
// Config holds the configuration for many Applications specified in the toml database
type Config struct {
Applications map[string]ApplicationConfig `toml:"app"`
}
// ApplicationConfig holds the configuration for a single Application
type ApplicationConfig struct {
Name string `toml:"name"`
Directory string `toml:"directory"`
// Path *string `toml:"path"`
Files []string `toml:"files"`
// Files map[string] `toml:"files"`
// Files map[string]string `toml:"files"`
Action ActionConfig `toml:"action"`
}
// ActionConfig holds the configuration for an custom action for an Application
type ActionConfig struct {
Label string `toml:"button_label"`
Exec string `toml:"exec"`
Args []string `toml:"args"`
Dir string `toml:"dir"`
OutputFile string `toml:"out-file"`
}
func newConfig() Config {
return Config{}
}
var dev *bool
var config Config
// root directory for packages "/var/packages/"
var rootDir string
func verifyFile(filePath string, sha256checksum string) bool {
file, err := os.Open(filePath)
if err != nil {
logError("Unable open file")
}
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
logError("Unable read file")
}
checksum := hash.Sum(nil)
// fmt.Printf("%s\n", sha256checksum)
// fmt.Printf("%x\n", checksum)
return hex.EncodeToString(checksum) == sha256checksum
}
// borrowed from dnscrypt-proxy
// ConfigLoad loads the configuration file
func ConfigLoad(configFile *string) error {
foundConfigFile, err := findConfigFile(configFile)
if err != nil {
logError("Unable to load the configuration file")
return err
}
// This is to prevent damage from modifying unapproved files.
if !verifyFile(foundConfigFile, DefaultDatabaseSHA256Checksum) {
logError("Warning! the database.toml file has been modified! Are you sure you want to continue?")
}
config = newConfig()
md, err := toml.DecodeFile(foundConfigFile, &config)
if err != nil {
return err
}
undecoded := md.Undecoded()
if len(undecoded) > 0 {
return fmt.Errorf("unsupported key in configuration file: [%s]", undecoded[0])
}
return nil
}
// borrowed from dnscrypt-proxy
func findConfigFile(configFile *string) (string, error) {
if _, err := os.Stat(*configFile); os.IsNotExist(err) {
cdLocal()
if _, err := os.Stat(*configFile); err != nil {
return "", err
}
}
pwd, err := os.Getwd()
if err != nil {
return "", err
}
if filepath.IsAbs(*configFile) {
return *configFile, nil
}
return path.Join(pwd, *configFile), nil
}
// borrowed from dnscrypt-proxy
func cdLocal() {
exeFileName, err := os.Executable()
if err != nil {
logError("Unable to determine the executable directory")
return
}
os.Chdir(filepath.Dir(exeFileName))
}