/
config.go
230 lines (220 loc) · 6.75 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"code.google.com/p/goconf/conf"
"flag"
"fmt"
"github.com/snarlysodboxer/sshPortForward"
"html/template"
"log"
"net/http"
"os"
)
const configHeader = "Configuration file for BTSyncInator:"
var (
configFilePath = flag.String("config", ".btsyncinator.conf", "path to config file.")
debug = flag.Bool("debug", false, "enable debug mode.")
apiDebug = flag.Bool("apiDebug", false, "enable debug mode on btsync-api library.")
config = conf.NewConfigFile()
settings = Settings{}
)
type Settings struct {
PrivateKeyPath string
ServeAddress string
DigestPath string
UseTLS bool
TLSKeyPath string
TLSCertPath string
}
func readSlashCreateConfig() {
if _, err := os.Stat(*configFilePath); os.IsNotExist(err) {
config.WriteConfigFile(*configFilePath, 0600, configHeader)
} else {
config, err = conf.ReadConfigFile(*configFilePath)
if err != nil {
log.Fatal("Error with ReadConfigFile:", err)
}
}
}
// Get general settings from default section of config file
func loadSettings() {
readSlashCreateConfig()
// Private key file path
privateKeyPath, err := config.GetString("default", "privateKeyPath")
if err != nil {
if *debug {
log.Printf("Error with config.GetString: %s", err)
}
}
if privateKeyPath == "" {
if *debug {
log.Println("Private key path not set, using $HOME/.ssh/id_rsa")
}
privateKeyPath = (os.Getenv("HOME") + "/.ssh/id_rsa")
}
settings.PrivateKeyPath = privateKeyPath
// Serve Address
serveAddress, err := config.GetString("default", "serveaddress")
if err != nil {
if *debug {
log.Printf("Error with config.GetString: %s", err)
}
}
if serveAddress == "" {
if *debug {
log.Println("Serve Address not set, using localhost:10000")
}
serveAddress = "localhost:10000"
}
settings.ServeAddress = serveAddress
// Digest File
digestPath, err := config.GetString("default", "digestPath")
if err != nil {
if *debug {
log.Printf("Error with config.GetString: %s", err)
}
}
if digestPath == "" {
if *debug {
log.Println("Digest Path not set, not using login authorization")
}
//digestPath = ".btsync-digest"
digestPath = ""
}
settings.DigestPath = digestPath
// TLS private key file path
tlsKeyPath, err := config.GetString("default", "tlsKeyPath")
if err != nil {
if *debug {
log.Printf("Error with config.GetString: %s", err)
}
}
settings.TLSKeyPath = tlsKeyPath
// TLS cert file path
tlsCertPath, err := config.GetString("default", "tlsCertPath")
if err != nil {
if *debug {
log.Printf("Error with config.GetString: %s", err)
}
}
settings.TLSCertPath = tlsCertPath
// Use TLS bool
useTLS, err := config.GetBool("default", "useTLS")
if err != nil {
if *debug {
log.Printf("Error with config.GetString: %s", err)
}
}
if useTLS == false {
if *debug {
log.Println("Use TLS set to false.")
}
} else if settings.TLSKeyPath == "" && settings.TLSCertPath == "" {
if *debug {
log.Println("UseTLS set to true, but TLS key path and/or cert path not set, generating self-signed cert.")
}
hostname, _ := os.Hostname()
genCACert(hostname, 4)
settings.TLSKeyPath = hostname + ".key"
settings.TLSCertPath = hostname + ".crt"
} else {
if *debug {
log.Println("Use TLS set to true.")
}
}
settings.UseTLS = useTLS
// Write changes to config file
config.AddOption("default", "privateKeyPath", privateKeyPath)
config.AddOption("default", "serveAddress", serveAddress)
config.AddOption("default", "useTLS", fmt.Sprintf("%t", useTLS))
config.AddOption("default", "tlsKeyPath", tlsKeyPath)
config.AddOption("default", "tlsCertPath", tlsCertPath)
config.AddOption("default", "digestPath", digestPath)
err = config.WriteConfigFile(*configFilePath, 0600, configHeader)
if err != nil {
log.Fatalf("Error with WriteConfigFile: %s", err)
}
}
func setupDaemonsFromConfig() {
// copy old Daemons
oldDaemons := daemons
readSlashCreateConfig()
allSections := config.GetSections()
// Get Daemons from config file
sects := allSections[1:]
sections := §s
daemons = []Daemon{}
for sectionIndex, section := range *sections {
daemon := &Daemon{}
daemon.Name = section
daemon.Addresses.SSHUserString, _ = config.GetString(section, "sshUserString")
daemon.Addresses.ServerAddrString, _ = config.GetString(section, "serverAddrString")
daemon.Addresses.RemoteAddrString, _ = config.GetString(section, "daemonAddrString")
daemon.Addresses.LocalAddrString = fmt.Sprintf("localhost:%d", 9000+sectionIndex)
daemon.Addresses.PrivateKeyPathString = settings.PrivateKeyPath
daemons = append(daemons, *daemon)
// Setup port-forward
if len(oldDaemons) != 0 {
for oldIndex, _ := range oldDaemons {
if oldDaemons[oldIndex].Name == daemon.Name {
if oldDaemons[oldIndex].Forwarded == false {
go sshPortForward.ConnectAndForward(daemons[sectionIndex].Addresses)
}
daemons[sectionIndex].Forwarded = true
}
}
} else {
go sshPortForward.ConnectAndForward(daemons[sectionIndex].Addresses)
daemons[sectionIndex].Forwarded = true
}
}
}
func configViewHandler(writer http.ResponseWriter, request *http.Request) {
tmpl, err := template.ParseFiles("config-view.html")
if err != nil {
log.Fatalf("Error with ParseFiles! %s", err)
}
tmpl.Execute(writer, daemons)
}
func configCreateHandler(writer http.ResponseWriter, request *http.Request) {
readSlashCreateConfig()
// AddSection and AddOption return boolean
if config.AddSection(request.FormValue("Name")) {
if config.AddOption(request.FormValue("Name"), "sshUserString", request.FormValue("sshUserName")) {
if config.AddOption(request.FormValue("Name"), "serverAddrString", request.FormValue("serverAddress")) {
if config.AddOption(request.FormValue("Name"), "daemonAddrString", request.FormValue("daemonAddress")) {
err := config.WriteConfigFile(*configFilePath, 0600, configHeader)
if err != nil {
log.Fatalf("Error with WriteConfigFile: %s", err)
}
}
dmns, err := conf.ReadConfigFile(*configFilePath)
if err != nil {
log.Fatalf("Error with ReadConfigFile: %s", err)
} else {
config = dmns
setupDaemonsFromConfig()
http.Redirect(writer, request, "/config", http.StatusFound)
}
}
}
}
}
func configDeleteHandler(writer http.ResponseWriter, request *http.Request) {
readSlashCreateConfig()
if config.RemoveSection(request.FormValue("DeleteName")) {
err := config.WriteConfigFile(*configFilePath, 0600, configHeader)
if err != nil {
log.Fatalf("Error with WriteConfigFile: %s", err)
}
dmns, err := conf.ReadConfigFile(*configFilePath)
if err != nil {
log.Fatalf("Error with ReadConfigFile: %s", err)
} else {
config = dmns
setupDaemonsFromConfig()
http.Redirect(writer, request, "/config", http.StatusFound)
}
} else {
log.Fatal(writer, "Error with RemoveSection!")
}
}