forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure-sites.go
274 lines (250 loc) · 7.88 KB
/
configure-sites.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
package cmd
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/manifoldco/promptui"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/proto/install"
)
var sitesCmd = &cobra.Command{
Use: "sites",
Short: "Manage site addresses",
Long: `
DESCRIPTION
Manage how Cells is binding to the local host's network interfaces addresses and accepting traffic coming from external URLs.
This is the main tool for listing, editing, adding and removing sites. Additional sub-commands allow you to directly create/delete sites.
Each site has following parameters:
1. <Bind Addresse(s)>: one or more <ip/hostname:port> to bind Cells to local host's network interfaces addresses.
2. <TLS Settings>: TLS configuration for HTTPS support.
3. <External URL>: Accept traffic coming from this url and redirect to one of the bind address.
Typically if the app is behind a reverse-proxy or inside a container with ports mapping.
4. <Maintenance Mode> [On|Off]: expose a maintenance page on this endpoint, although Cells is running.
EXAMPLES
1. Default value (used when no sites is configured)
- Bind Address: 0.0.0.0:8080
- TLS : SelfSigned
- External URL: [left empty]
2. Provided certificate
- Bind Address: 0.0.0.0:8080
- TLS : Your own key/cert pair
- External URL: https://share.mydomain.tld
3. Auto-ssl using Let's Encrypt
- Bind Address: share.mydomain.tld:443
- TLS : LetsEncrypt
- External URL: https://share.mydomain.tld
4. Self Signed
- Bind Address: IP:1515 # internal port
- TLS : SelfSigned
- External URL: https://IP:8080 # external port mapped by docker
5. HTTP only
- Bind Address: localhost:8080
- TLS : Disabled
- External URL: http://localhost:8080 # Non-secured local installation
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initConfig()
},
Run: func(cmd *cobra.Command, args []string) {
sites, e := config.LoadSites(true)
fatalIfError(cmd, e)
if len(sites) == 0 {
fmt.Println("No site is currently configured. Cells exposes automatically the following URLs : ")
ss, _ := config.LoadSites()
autoSite := ss[0]
for _, u := range autoSite.GetBindURLs() {
fmt.Println(" - " + u)
}
p := &promptui.Prompt{
Label: "Do you want to create a new site (it will replace the default one)",
IsConfirm: true,
}
if _, e := p.Run(); e == nil {
sitesAdd.Run(cmd, args)
} else if e == promptui.ErrInterrupt {
fatalIfError(cmd, e)
}
} else {
fmt.Println("The following sites are currently defined:")
listSites(cmd, sites)
editString := "Edit an existing site"
deleteString := "Delete an existing site"
if len(sites) == 1 {
editString = "Edit current site"
deleteString = "Delete current site (will fallback to defaults)"
}
actionP := promptui.Select{
Items: []string{
"Add a new site",
editString,
deleteString,
"Quit",
},
Label: "What do you want to do",
}
action, _, e := actionP.Run()
fatalIfError(cmd, e)
switch action {
case 0:
sitesAdd.Run(cmd, args)
case 1:
index := 0
if len(sites) > 1 {
p := &promptui.Prompt{
Label: "Provide the site number to edit",
Validate: func(s string) error {
i, e := strconv.ParseInt(s, 10, 64)
if e != nil {
return e
}
if int(i) >= len(sites) {
return fmt.Errorf("please provide a number smaller than %d", len(sites))
}
return nil
},
}
if n, e := p.Run(); e != nil || n == "" {
fatalIfError(cmd, e)
} else if idx, e := strconv.ParseInt(n, 10, 64); e == nil && int(idx) < len(sites) {
index = int(idx)
}
}
e := promptSite(sites[index], true)
fatalIfError(cmd, e)
e = confirmAndSave(cmd, args, sites)
fatalIfError(cmd, e)
case 2:
index := "0"
if len(sites) > 1 {
p := &promptui.Prompt{
Label: "Provide the site number to be removed",
Validate: func(s string) error {
i, e := strconv.ParseInt(s, 10, 64)
if e != nil {
return e
}
if int(i) >= len(sites) {
return fmt.Errorf("please provide a number smaller than %d", len(sites))
}
return nil
},
}
if n, e := p.Run(); e != nil || n == "" {
return
} else if idx, e := strconv.ParseInt(n, 10, 64); e == nil && int(idx) < len(sites) {
index = n
}
}
sitesDelete.Run(cmd, []string{index})
case 3:
return
}
cmd.Run(cmd, args)
}
},
}
func listSites(cmd *cobra.Command, sites []*install.ProxyConfig) {
oneHasMaintenance := false
for _, s := range sites {
if s.Maintenance {
oneHasMaintenance = true
break
}
}
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetRowLine(true)
headers := []string{"#", "Bind(s)", "TLS", "External URL"}
if oneHasMaintenance {
headers = append(headers, "Maintenance Mode")
}
table.SetHeader(headers)
for i, s := range sites {
tlsString := "No Tls"
if s.TLSConfig != nil {
// TLSConfig
switch s.TLSConfig.(type) {
case *install.ProxyConfig_SelfSigned:
tlsString = "Self-signed"
case *install.ProxyConfig_LetsEncrypt:
tlsString = "Lets Encrypt"
case *install.ProxyConfig_Certificate:
tlsString = "Custom Certificate"
}
}
data := []string{fmt.Sprintf("%d", i), strings.Join(s.GetBindURLs(), ", "), tlsString, s.ReverseProxyURL}
if oneHasMaintenance {
m := ""
if s.Maintenance {
m = "On"
if len(s.MaintenanceConditions) > 0 {
m = strings.Join(s.MaintenanceConditions, ",")
}
}
data = append(data, m)
}
table.Append(data)
}
table.Render()
}
func confirmAndSave(cmd *cobra.Command, args []string, sites []*install.ProxyConfig) error {
if len(args) > 0 && args[0] == "skipConfirm" {
return config.SaveSites(sites, common.PydioSystemUsername, "Updating config sites")
}
has1024 := false
hasLE := false
for _, s := range sites {
for _, b := range s.Binds {
if _, p, e := net.SplitHostPort(b); e == nil {
if i, er := strconv.ParseInt(p, 10, 32); er == nil && i < 1024 {
has1024 = true
break
}
}
}
if s.GetLetsEncrypt() != nil {
hasLE = true
}
}
// Reprint before saving
cmd.Println("*************************************************")
cmd.Println(" Please review your parameters before saving ")
cmd.Println("*************************************************")
listSites(cmd, sites)
if has1024 || hasLE {
cmd.Println("")
cmd.Println(promptui.IconWarn + " WARNING")
cmd.Println("Binding to a port below 1024 (typically 80 or 443) requires specific permissions on some OS.")
if hasLE {
cmd.Println("Note that Let's Encrypt validation protocol requires a temporary access to port 80.")
}
cmd.Println("On Linux, run (as root user): " + promptui.Styler(promptui.FGBold)("$> setcap 'cap_net_bind_service=+ep' "+os.Args[0]))
cmd.Println("")
}
confirm := promptui.Prompt{Label: "Do you want to save this configuration", IsConfirm: true}
if _, e := confirm.Run(); e == nil {
e = config.SaveSites(sites, common.PydioSystemUsername, "Updating config sites")
if e != nil {
cmd.Println("***********************************************")
cmd.Println("[ERROR] Could not save config : " + e.Error())
cmd.Println("***********************************************")
return e
} else {
cmd.Println("********************************************************")
cmd.Println(" Config has been updated, please restart Cells now. ")
cmd.Println("********************************************************")
}
} else {
cmd.Println("***********************************************")
cmd.Println(" Operation aborted, nothing has been saved ")
cmd.Println("***********************************************")
}
return nil
}
func init() {
ConfigureCmd.AddCommand(sitesCmd)
}