-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
294 lines (273 loc) · 6.89 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/howeyc/gopass"
"github.com/nathan-osman/i5/conman"
"github.com/nathan-osman/i5/db"
"github.com/nathan-osman/i5/dbman"
"github.com/nathan-osman/i5/dockmon"
"github.com/nathan-osman/i5/logger"
"github.com/nathan-osman/i5/server"
"github.com/nathan-osman/i5/status"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "i5",
Usage: "reverse proxy for Docker containers",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
EnvVars: []string{"DEBUG"},
Usage: "enable debug mode",
},
&cli.StringFlag{
Name: "docker-host",
Value: "unix:///var/run/docker.sock",
EnvVars: []string{"DOCKER_HOST"},
Usage: "host running Docker",
},
&cli.StringFlag{
Name: "email",
EnvVars: []string{"EMAIL"},
Usage: "email address to use for challenges",
},
&cli.StringFlag{
Name: "geolocation-db-type",
EnvVars: []string{"GEOLOCATION_DB_TYPE"},
Usage: "type of IP gelocation database",
},
&cli.StringFlag{
Name: "geolocation-db-path",
EnvVars: []string{"GEOLOCATION_DB_PATH"},
Usage: "path to IP geolocation database",
},
&cli.StringFlag{
Name: "http-addr",
Value: ":http",
EnvVars: []string{"HTTP_ADDR"},
Usage: "HTTP address to listen on",
},
&cli.StringFlag{
Name: "https-addr",
Value: ":https",
EnvVars: []string{"HTTPS_ADDR"},
Usage: "HTTPS address to listen on",
},
&cli.BoolFlag{
Name: "mysql",
EnvVars: []string{"MYSQL"},
Usage: "connect to MySQL",
},
&cli.IntFlag{
Name: "mysql-port",
Value: 3306,
EnvVars: []string{"MYSQL_PORT"},
Usage: "port for MySQL server",
},
&cli.StringFlag{
Name: "mysql-host",
Value: "localhost",
EnvVars: []string{"MYSQL_HOST"},
Usage: "hostname of MySQL server",
},
&cli.StringFlag{
Name: "mysql-user",
Value: "root",
EnvVars: []string{"MYSQL_USER"},
Usage: "username for connecting to MySQL",
},
&cli.StringFlag{
Name: "mysql-password",
EnvVars: []string{"MYSQL_PASSWORD"},
Usage: "password for connecting to MySQL",
},
&cli.BoolFlag{
Name: "postgres",
EnvVars: []string{"POSTGRES"},
Usage: "connect to PostgreSQL",
},
&cli.IntFlag{
Name: "postgres-port",
Value: 5432,
EnvVars: []string{"POSTGRES_PORT"},
Usage: "port for PostgreSQL server",
},
&cli.StringFlag{
Name: "postgres-host",
Value: "localhost",
EnvVars: []string{"POSTGRES_HOST"},
Usage: "hostname of PostgreSQL server",
},
&cli.StringFlag{
Name: "postgres-user",
Value: "postgres",
EnvVars: []string{"POSTGRES_USER"},
Usage: "username for connecting to PostgreSQL",
},
&cli.StringFlag{
Name: "postgres-password",
EnvVars: []string{"POSTGRES_PASSWORD"},
Usage: "password for connecting to PostgreSQL",
},
&cli.StringFlag{
Name: "status-domain",
EnvVars: []string{"STATUS_DOMAIN"},
Usage: "domain name for internal server",
},
&cli.StringFlag{
Name: "status-key",
EnvVars: []string{"STATUS_KEY"},
Usage: "secret key for encoding cookies",
},
&cli.BoolFlag{
Name: "status-insecure",
EnvVars: []string{"STATUS_INSECURE"},
Usage: "allow insecure connections to the internal server",
},
&cli.StringFlag{
Name: "storage-dir",
Value: "/var/lib/i5",
EnvVars: []string{"STORAGE_DIR"},
Usage: "directory for storing files related to i5",
},
},
Commands: []*cli.Command{
installCommand,
{
Name: "createuser",
Usage: "create a new user account",
Action: func(c *cli.Context) error {
// Attempt to open the Bolt database
d, err := db.New(&db.Config{
StorageDir: c.String("storage-dir"),
})
if err != nil {
return err
}
defer d.Close()
// Prompt for the username
var username string
fmt.Print("Username? ")
fmt.Scanln(&username)
// Prompt for the password, hiding the input
fmt.Print("Password? ")
p, err := gopass.GetPasswd()
if err != nil {
return err
}
// Create the user
if err := d.CreateUser(username, string(p)); err != nil {
return nil
}
fmt.Println("New user created successfully.")
return nil
},
},
},
Action: func(c *cli.Context) error {
// Attempt to open the Bolt database
d, err := db.New(&db.Config{
StorageDir: c.String("storage-dir"),
})
if err != nil {
return err
}
defer d.Close()
// Check if the status website was enabled
var statusDomain = c.String("status-domain")
// Create the logger
l, err := logger.New(&logger.Config{
GeolocationDBType: c.String("geolocation-db-type"),
GeolocationDBPath: c.String("geolocation-db-path"),
})
if err != nil {
return err
}
defer l.Close()
// Create the Docker monitor
dm, err := dockmon.New(&dockmon.Config{
Host: c.String("docker-host"),
Logger: l,
})
if err != nil {
return err
}
defer dm.Close()
// Create the database manager
dbm := dbman.NewManager()
// Connect to MySQL if requested
if c.Bool("mysql") {
msql, err := dbman.NewMySQL(
c.String("mysql-host"),
c.Int("mysql-port"),
c.String("mysql-user"),
c.String("mysql-password"),
)
if err != nil {
return err
}
dbm.Register(msql)
}
// Connect to PostgreSQL if requested
if c.Bool("postgres") {
psql, err := dbman.NewPostgres(
c.String("postgres-host"),
c.Int("postgres-port"),
c.String("postgres-user"),
c.String("postgres-password"),
)
if err != nil {
return err
}
dbm.Register(psql)
}
// Create the container manager
cm := conman.New(&conman.Config{
EventChan: dm.EventChan,
Dbman: dbm,
})
defer cm.Close()
// If a domain name for the status server was specified, use it
if statusDomain != "" {
s := status.New(&status.Config{
Key: c.String("status-key"),
Debug: c.Bool("debug"),
Domain: statusDomain,
Insecure: c.Bool("status-insecure"),
DB: d,
Conman: cm,
Dockmon: dm,
Dbman: dbm,
Logger: l,
})
defer s.Close()
cm.Add(s.Container)
}
// Create the server
sv, err := server.New(&server.Config{
Debug: c.Bool("debug"),
Email: c.String("email"),
HTTPAddr: c.String("http-addr"),
HTTPSAddr: c.String("https-addr"),
StorageDir: c.String("storage-dir"),
Conman: cm,
})
if err != nil {
return err
}
defer sv.Close()
// Wait for SIGINT or SIGTERM
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
return nil
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "fatal: %s\n", err.Error())
}
}