-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.go
338 lines (268 loc) · 9.03 KB
/
main.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// A webapp for looking at and searching through files.
package main
import (
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/pelletier/go-toml"
flag "github.com/spf13/pflag"
"io/ioutil"
"log"
"net"
"os"
"strings"
"sync"
)
const scriptDescription = `
Usage: tailon -c <config file>
Usage: tailon [options] <filespec> [<filespec> ...]
Tailon is a webapp for searching through files and streams.
`
const scriptEpilog = `
Tailon can be configured via a TOML config file or command-line flags.
The command-line interface expects one or more <filespec> arguments, which
specify the files to serve. The format is:
[alias=name,group=name]<source>
The "source" specifier can be a file name, glob or directory. The optional
"alias=" and "group=" specifiers change the display name of files in the UI
and the group in which they appear.
A file specifier points to a single, possibly non-existent file. The file
name can be overwritten with "alias=". For example:
tailon alias=error.log,/var/log/apache/error.log
A glob evaluates to the list of files that match a shell filename pattern.
The pattern is evaluated each time the file list is refreshed. An "alias="
specifier overwrites the parent directory of each matched file in the UI.
tailon "/var/log/apache/*.log" "alias=nginx,/var/log/nginx/*.log"
If a directory is given, all files under it are served recursively.
tailon /var/log/apache/ /var/log/nginx/
Example usage:
tailon file1.txt file2.txt file3.txt
tailon alias=messages,/var/log/messages "/var/log/*.log"
tailon -b localhost:8080,localhost:8081 -c config.toml
See "--help-config" for configuration file usage.
`
const configFileHelp = `
The following options can be set through the config file:
# The <title> element of the of the webapp.
title = "Tailon file viewer"
# The root of the web application.
relative-root = "/"
# The addresses to listen on. Can be an address:port combination or an unix socket.
listen-addr = [":8080"]
# Allow downloading of known files (i.e those matched by a filespec).
allow-download = true
# Commands that will appear in the UI.
allow-commands = ["tail", "grep", "sed", "awk"]
# A table of commands that the backend can execute. This is best illustrated by
# the default configuration listed below.
[commands]
# File, glob and dir filespecs are similar in principle to their
# command-line counterparts.
At startup tailon loads the following default configuration:
`
const defaultTomlConfig = `
title = "Tailon file viewer"
relative-root = "/"
listen-addr = [":8080"]
allow-download = true
allow-commands = ["tail", "grep", "sed", "awk"]
[commands]
[commands.tail]
action = ["tail", "-n", "$lines", "-F", "$path"]
[commands.grep]
stdin = "tail"
action = ["grep", "--text", "--line-buffered", "--color=never", "-e", "$script"]
default = ".*"
[commands.sed]
stdin = "tail"
action = ["sed", "-u", "-e", "$script"]
default = "s/.*/&/"
[commands.awk]
stdin = "tail"
action = ["awk", "--sandbox", "$script"]
default = "{print $0; fflush()}"
`
// CommandSpec defines a command that the server can execute.
type CommandSpec struct {
Stdin string
Action []string
Default string
}
func parseTomlConfig(config string) (*toml.Tree, map[string]CommandSpec) {
cfg, err := toml.Load(config)
if err != nil {
log.Fatal("Error parsing config: ", err)
}
commands := make(map[string]CommandSpec)
cfgCommands := cfg.Get("commands").(*toml.Tree).ToMap()
for key, value := range cfgCommands {
command := CommandSpec{}
err := mapstructure.Decode(value, &command)
if err != nil {
log.Fatal(err)
}
commands[key] = command
}
return cfg, commands
}
// FileSpec is an instance of a file to be monitored. These are mapped to
// os.Args or the [files] elements in the config file.
type FileSpec struct {
Path string
Type string
Alias string
Group string
}
// Parse a string into a filespec. Example inputs are:
//
// alias=1,group=2,/var/log/messages
// /var/log/
// /var/log/*
func parseFileSpec(spec string) (FileSpec, error) {
var filespec FileSpec
var path string
parts := strings.Split(spec, ",")
if length := len(parts); length == 1 {
path = parts[0]
} else {
// The last part is the path. We'll probably need a more robust
// solution in the future.
path, parts = parts[len(parts)-1], parts[:len(parts)-1]
}
if strings.ContainsAny(path, "*?[]") {
filespec.Type = "glob"
} else {
stat, err := os.Lstat(path)
if os.IsNotExist(err) || stat.Mode().IsRegular() {
filespec.Type = "file"
} else if stat.Mode().IsDir() {
filespec.Type = "dir"
}
}
for _, part := range parts {
if strings.HasPrefix(part, "group=") {
group := strings.SplitN(part, "=", 2)[1]
group = strings.Trim(group, "'\" ")
filespec.Group = group
} else if strings.HasPrefix(part, "alias=") {
filespec.Alias = strings.SplitN(part, "=", 2)[1]
}
}
if filespec.Type == "" {
filespec.Type = "file"
}
filespec.Path = path
return filespec, nil
}
// Config contains all backend and frontend configuration options and relevant state.
type Config struct {
RelativeRoot string
BindAddr []string
ConfigPath string
WrapLinesInitial bool
TailLinesInitial int
AllowCommandNames []string
AllowDownload bool
CommandSpecs map[string]CommandSpec
CommandScripts map[string]string
FileSpecs []FileSpec
}
func makeConfig(configContent string) *Config {
defaults, commandSpecs := parseTomlConfig(configContent)
// Convert the list of bind addresses from []interface{} to []string.
addrsA := defaults.Get("listen-addr").([]interface{})
addrsB := make([]string, len(addrsA))
for i := range addrsA {
addrsB[i] = addrsA[i].(string)
}
config := Config{
BindAddr: addrsB,
RelativeRoot: defaults.Get("relative-root").(string),
AllowDownload: defaults.Get("allow-download").(bool),
CommandSpecs: commandSpecs,
}
mapstructure.Decode(defaults.Get("allow-commands"), &config.AllowCommandNames)
return &config
}
var config = &Config{}
func main() {
config = makeConfig(defaultTomlConfig)
printHelp := flag.BoolP("help", "h", false, "Show this help message and exit")
printConfigHelp := flag.BoolP("help-config", "e", false, "Show configuration file help and exit")
bindAddr := flag.StringP("bind", "b", strings.Join(config.BindAddr, ","), "Address and port to listen on")
flag.StringVarP(&config.RelativeRoot, "relative-root", "r", config.RelativeRoot, "Webapp relative root")
flag.BoolVarP(&config.AllowDownload, "allow-download", "a", config.AllowDownload, "Allow file downloads")
flag.StringVarP(&config.ConfigPath, "config", "c", "", "Path to TOML configuration file")
flag.Parse()
flag.Usage = func() {
fmt.Fprintln(os.Stderr, strings.TrimLeft(scriptDescription, "\n"))
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, strings.TrimRight(scriptEpilog, "\n"))
os.Exit(0)
}
if *printHelp {
flag.Usage()
os.Exit(0)
}
if *printConfigHelp {
fmt.Fprintf(os.Stderr, "%s\n\n%s\n", strings.Trim(configFileHelp, "\n"), strings.Trim(defaultTomlConfig, "\n"))
os.Exit(0)
}
config.BindAddr = strings.Split(*bindAddr, ",")
// If a configuration file is specified, read all options from it. This discards all options set on the command-line.
if config.ConfigPath != "" {
if b, err := ioutil.ReadFile(config.ConfigPath); err != nil {
fmt.Fprintf(os.Stderr, "Error reading config file '%s': %s\n", config.ConfigPath, err)
os.Exit(1)
} else {
config = makeConfig(string(b))
}
}
// Ensure that relative root is always '/' or '/$arg/'.
config.RelativeRoot = "/" + strings.TrimLeft(config.RelativeRoot, "/")
config.RelativeRoot = strings.TrimRight(config.RelativeRoot, "/") + "/"
// Handle command-line file specs.
filespecs := make([]FileSpec, len(flag.Args()))
for _, spec := range flag.Args() {
if filespec, err := parseFileSpec(spec); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing argument '%s': %s\n", spec, err)
os.Exit(1)
} else {
filespecs = append(filespecs, filespec)
}
}
config.FileSpecs = filespecs
if len(config.FileSpecs) == 0 {
fmt.Fprintln(os.Stderr, "No files specified on command-line or in config file")
os.Exit(2)
}
config.CommandScripts = make(map[string]string)
for cmd, values := range config.CommandSpecs {
config.CommandScripts[cmd] = values.Default
}
log.Print("Generate initial file listing")
createListing(config.FileSpecs)
var wg sync.WaitGroup
for _, addr := range config.BindAddr {
wg.Add(1)
go startServer(config, addr)
}
wg.Wait()
}
func startServer(config *Config, bindAddr string) {
loggerHTML := log.New(os.Stdout, "", log.LstdFlags)
loggerHTML.Printf("Server start, relative-root: %s, bind-addr: %s\n", config.RelativeRoot, bindAddr)
server := setupServer(config, bindAddr, loggerHTML)
if strings.Contains(bindAddr, ":") {
server.ListenAndServe()
} else {
os.Remove(bindAddr)
unixAddr, _ := net.ResolveUnixAddr("unix", bindAddr)
unixListener, err := net.ListenUnix("unix", unixAddr)
unixListener.SetUnlinkOnClose(true)
if err != nil {
panic(err)
}
defer unixListener.Close()
server.Serve(unixListener)
}
}