-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconf.go
312 lines (296 loc) · 8.01 KB
/
conf.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"
"time"
"github.com/pelletier/go-toml"
"github.com/pion/webrtc/v3"
"github.com/tuzig/webexec/httpserver"
"github.com/tuzig/webexec/peers"
"go.uber.org/zap/zapcore"
)
const defaultHTTPServer = httpserver.AddressType("127.0.0.1:7777")
const defaultConf = `# webexec's configuration. in toml.
# to learn more: https://github.com/tuzig/webexec/blob/master/docs/conf.md
[log]
level = "info"
# for absolute path by starting with a /
# relative path is /var/log/webexec.$USER
file = "webexec.log"
error = "webexec.err"
# next can be uncommented to debug pion components
# pion_levels = { trace = "sctp" }
[net]
http_server = "0.0.0.0:7777"
udp_port_min = 60000
udp_port_max = 61000
[timeouts]
ack = 3000
disconnect = 3000
failed = 6000
ice_gathering = 5000
keep_alive = 500
peerbook = 3000
[[ice_servers]]
urls = [ "stun:stun.l.google.com:19302" ]
[env]
COLORTERM = "truecolor"
TERM = "xterm-256color"
`
const abConfTemplate = `%s[peerbook]
host = "%s"
user_id = "%s"
name = "%s"
`
const defaultPeerbookHost = "api.peerbook.io"
type ICEServer struct {
URLs []string `toml:"urls"`
Username string `toml:"username,omitempty"`
Password string `toml:"password,omitempty"`
}
// Conf hold the configuration variables
var Conf struct {
logFilePath string
logLevel zapcore.Level
errFilePath string
peerbookTimeout time.Duration
iceServers []webrtc.ICEServer
peerbookHost string
insecure bool
peerbookUID string
name string
peerConf *peers.Conf
T *toml.Tree
}
var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
// parseConf loads a configuration from a toml string and fills all Conf value.
//
// If a key is missing LoadConf will load the default value
func parseConf(s string) (*peers.Conf, httpserver.AddressType, error) {
t, err := toml.Load(s)
if err != nil {
return nil, "", fmt.Errorf("toml parsing failed: %s", err)
}
Conf.T = t
Conf.logFilePath = logFilePath("log.file", "webexec.log")
Conf.errFilePath = logFilePath("log.error", "webexec.err")
Conf.logLevel = zapcore.ErrorLevel
v := Conf.T.Get("log.level")
if v != nil {
l := v.(string)
if l == "info" {
Conf.logLevel = zapcore.InfoLevel
} else if l == "warn" {
Conf.logLevel = zapcore.WarnLevel
} else if l == "debug" {
Conf.logLevel = zapcore.DebugLevel
}
} else {
Conf.logLevel = zapcore.WarnLevel
}
v = t.Get("timeouts.peerbook")
if v != nil {
Conf.peerbookTimeout = time.Duration(v.(int64)) * time.Millisecond
} else {
Conf.peerbookTimeout = 3 * time.Second
}
// start of peers configuration
peersConf := &peers.Conf{}
v = t.Get("timeouts.disconnect")
if v != nil {
peersConf.DisconnectTimeout = time.Duration(v.(int64)) * time.Millisecond
} else {
peersConf.DisconnectTimeout = 3 * time.Second
}
v = t.Get("timeouts.failed")
if v != nil {
peersConf.FailedTimeout = time.Duration(v.(int64)) * time.Millisecond
} else {
peersConf.FailedTimeout = 6 * time.Second
}
v = t.Get("timeouts.keep_alive")
if v != nil {
peersConf.KeepAliveInterval = time.Duration(v.(int64)) * time.Millisecond
} else {
peersConf.KeepAliveInterval = 1 * time.Second
}
v = t.Get("timeouts.ice_gathering")
if v != nil {
peersConf.GatheringTimeout = time.Duration(v.(int64)) * time.Millisecond
} else {
peersConf.GatheringTimeout = 3 * time.Second
}
v = t.Get("timeouts.ack")
if v != nil {
peersConf.AckTimeout = time.Duration(v.(int64)) * time.Millisecond
} else {
peersConf.AckTimeout = 3 * time.Second
}
v = t.Get("ice_servers")
if v != nil {
Conf.iceServers = []webrtc.ICEServer{}
for _, u2 := range v.([]*toml.Tree) {
var u ICEServer
err := u2.Unmarshal(&u)
if err != nil {
return nil, "", fmt.Errorf("failed to parse ice server configuration: %s", err)
}
s := webrtc.ICEServer{
URLs: u.URLs,
Username: u.Username,
Credential: u.Password,
CredentialType: webrtc.ICECredentialTypePassword,
}
Conf.iceServers = append(Conf.iceServers, s)
}
}
// no address is set, let's see if the conf file has it
var addr httpserver.AddressType
v = t.Get("net.http_server")
if v != nil {
addr = httpserver.AddressType(v.(string))
} else {
// when no address is given, this is the default address
addr = defaultHTTPServer
}
// get the udp ports
v = t.Get("net.udp_port_min")
if v != nil {
peersConf.PortMin = uint16(v.(int64))
} else {
peersConf.PortMin = 60000
}
v = t.Get("net.udp_port_max")
if v != nil {
peersConf.PortMax = uint16(v.(int64))
} else {
peersConf.PortMax = 61000
}
// unsecured cotrol which shema to use
v = t.Get("peerbook.insecure")
if v != nil {
Conf.insecure = v.(bool)
}
// get env vars
peersConf.Env = map[string]string{"WEBEXEC": GetSockFP()}
m := t.Get("env")
if m != nil {
for k, v := range m.(*toml.Tree).ToMap() {
peersConf.Env[k] = v.(string)
}
}
v = t.Get("peerbook.user_id")
if v != nil {
Conf.peerbookUID = v.(string)
host := t.Get("peerbook.host")
if host != nil {
Conf.peerbookHost = host.(string)
} else {
Conf.peerbookHost = defaultPeerbookHost
}
name := t.Get("peerbook.name")
if name != nil {
Conf.name = name.(string)
} else {
Conf.name, err = os.Hostname()
if err != nil {
Logger.Warnf("Failed to get hostname, using `anonymous`")
name = "anonymous"
}
}
}
Conf.peerConf = peersConf
return peersConf, addr, nil
}
func logFilePath(path string, def string) string {
v := Conf.T.Get(path)
if v == nil {
return LogPath(def)
}
ret := v.(string)
if ret[0] != '/' {
ret = LogPath(def)
}
return ret
}
// loadConf load the conf file
func LoadConf(certs []webrtc.Certificate) (*peers.Conf, httpserver.AddressType, error) {
confPath := ConfPath("webexec.conf")
_, err := os.Stat(confPath)
if os.IsNotExist(err) {
return nil, "", fmt.Errorf("Missing conf file, run `webexec init` to create")
}
b, err := ioutil.ReadFile(confPath)
if err != nil {
return nil, "", fmt.Errorf("Failed to read conf file %q: %s", confPath,
err)
}
conf, addr, err := parseConf(string(b))
if err != nil {
return nil, "", fmt.Errorf("Failed to parse conf file %q: %s", confPath,
err)
}
conf.Certificate = &certs[0]
conf.Logger = Logger
conf.GetICEServers = GetICEServers
conf.GetWelcome = GetWelcome
conf.OnCTRLMsg = handleCTRLMsg
return conf, addr, err
}
func isValidEmail(email string) bool {
if len(email) < 3 && len(email) > 254 {
return false
}
return emailRegex.MatchString(email)
}
// createConf creates the configuration files based on the defaults and user
// id, host and name
func createConf(peerbookUID string, pbHost string, name string) (string, error) {
conf := defaultConf
if pbHost == "" {
pbHost = defaultPeerbookHost
}
if peerbookUID != "" {
conf = fmt.Sprintf(abConfTemplate, conf, pbHost, peerbookUID, name)
}
confPath := ConfPath("webexec.conf")
confFile, err := os.Create(confPath)
defer confFile.Close()
if err != nil {
return "", fmt.Errorf("Failed to create config file: %q", err)
}
_, err = confFile.WriteString(conf)
if err != nil {
return "", fmt.Errorf("Failed to write to configuration file: %s", err)
}
return confPath, nil
}
// ConfPath returns the full path of a configuration file
func ConfPath(suffix string) string {
usr, _ := user.Current()
return filepath.Join(usr.HomeDir, ".config", "webexec", suffix)
}
// RunPath returns the full path of a run file: socket & pid
func RunPath(suffix string) string {
usr, _ := user.Current()
dir := filepath.Join(usr.HomeDir, ".local", "state", "webexec")
_, err := os.Stat(dir)
if os.IsNotExist(err) {
os.MkdirAll(dir, 0755)
}
return filepath.Join(dir, suffix)
}
// LogPath returns the full path of a run file: socket & pid
func LogPath(suffix string) string {
usr, _ := user.Current()
dir := filepath.Join(usr.HomeDir, ".local", "state", "webexec")
_, err := os.Stat(dir)
if os.IsNotExist(err) {
os.MkdirAll(dir, 0755)
}
return filepath.Join(dir, suffix)
}