-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcontrol.go
More file actions
221 lines (192 loc) · 5.64 KB
/
Copy pathcontrol.go
File metadata and controls
221 lines (192 loc) · 5.64 KB
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
package fedbox
import (
"bufio"
"fmt"
"io"
"net/url"
"os"
"sync/atomic"
"git.sr.ht/~mariusor/lw"
"git.sr.ht/~mariusor/storage-all"
"github.com/alecthomas/kong"
"github.com/go-ap/errors"
ap "github.com/go-ap/fedbox/activitypub"
"github.com/go-ap/fedbox/internal/config"
"github.com/go-ap/fedbox/internal/env"
"golang.org/x/crypto/ssh/terminal"
)
type Storage struct {
Type storage.Type `help:"Type of the backend to use. Possible values: ${storageTypes}"`
Reset ResetCmd `cmd:"" help:"Reset an existing storage."`
FixCollections FixCollections `cmd:"" help:"Fix storage collections."`
}
type SSH struct {
Pub Pub `cmd:"" name:"pub" alt:"ap" help:"ActivityPub management helper"`
OAuth OAuth `cmd:"" name:"oauth"`
Storage Storage `cmd:""`
Accounts Accounts `cmd:"" help:"Accounts helper."`
Debug Debug `cmd:"" help:"Toggle debug mode for the running FedBOX server."`
Maintenance Maintenance `cmd:"" help:"Toggle maintenance mode for the running FedBOX server."`
Reload Reload `cmd:"" help:"Reload the running FedBOX server configuration."`
Stop Stop `cmd:"" help:"Stops the running FedBOX server configuration."`
}
type CTL struct {
SSH `embed:""`
Url *url.URL `help:"The URL used by the application."`
Env env.Type `enum:"${envTypes}" help:"The environment to use. Expected values: ${envTypes}" default:"${defaultEnv}"`
Verbose int `name:"verbose" short:"v" default:"0" type:"counter" help:"Increase verbosity of the log output" `
Path string `path:"" help:"The path for the storage folder or socket" env:"STORAGE_PATH"`
Version kong.VersionFlag `short:"V"`
// Commands
Run Serve `cmd:"" name:"run" help:"Run the ${name} instance server (version: ${version})" default:"withargs"`
}
var DefaultLogLevel = lw.WarnLevel
func InitControl(c *CTL) (*Base, error) {
opt := config.Options{
LogLevel: DefaultLogLevel,
AppName: AppName,
Version: AppVersion,
}
if c.Env != opt.Env {
opt.Env = c.Env
}
if c.Url != nil {
opt.Hostname = c.Url.Hostname()
opt.Secure = c.Url.Scheme == "https"
opt.BaseURL = c.Url.String()
}
if c.Path != "" {
opt.StoragePath = c.Path
}
ct := Base{
in: os.Stdin,
out: os.Stdout,
err: os.Stderr,
Conf: opt,
}
if err := setup(&ct, opt, c.Verbose); err != nil {
return nil, err
}
errors.SetIncludeBacktrace(opt.LogLevel == lw.TraceLevel)
return &ct, nil
}
func (ctl *Base) LoadServiceActor() error {
if ctl.Conf.BaseURL == "" {
return errors.Errorf("no HOSTNAME configured for service")
}
selfIRI := ap.DefaultServiceIRI(ctl.Conf.BaseURL)
actor, err := ap.LoadActor(ctl.Storage, selfIRI)
if err != nil {
return err
}
key, err := ctl.Storage.LoadKey(selfIRI)
if err != nil {
return err
}
ctl.Service = actor
ctl.ServicePrivateKey = key
return nil
}
func NewBase(db storage.FullStorage, conf config.Options, l lw.Logger) (*Base, error) {
return &Base{
Conf: conf,
Storage: db,
Logger: l,
in: os.Stdin,
out: os.Stdout,
err: os.Stderr,
}, nil
}
func setup(ct *Base, conf config.Options, verbose int) error {
path := conf.StoragePath
err := config.Load(&conf, path)
if err != nil {
return errors.Annotatef(err, "failed to load %s files in path %s", conf.Env, path)
}
var out io.WriteCloser
if conf.LogOutput != "" {
if out, err = os.Open(conf.LogOutput); err != nil {
return errors.Newf("Unable to output logs to %s: %s", conf.LogOutput, err)
}
defer func() {
if err := out.Close(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Unable to close log output: %s", err)
}
}()
}
if ct.Logger == nil {
if verbose > 0 {
conf.LogLevel = lw.Level(max(int(lw.TraceLevel), int(conf.LogLevel)-(4*verbose)))
}
if conf.Env.IsDev() {
ct.Logger = lw.Dev(lw.SetLevel(conf.LogLevel), lw.SetOutput(out)).WithContext(lw.Ctx{"host": conf.Hostname})
} else {
ct.Logger = lw.Prod(lw.SetLevel(conf.LogLevel), lw.SetOutput(out)).WithContext(lw.Ctx{"host": conf.Hostname})
}
}
_l.Store(ct.Logger)
typ := conf.Storage
if typ != "" {
conf.Storage = typ
}
if conf.StoragePath == "" && path != "" {
conf.StoragePath = path
}
ct.Conf = conf
initFn, err := conf.StorageInitFns(ct.Logger)
if err != nil {
return err
}
if ct.Storage, err = storage.New(initFn...); err != nil {
return err
}
if metaSaver, ok := ct.Storage.(storage.MetadataStorage); ok {
keysType := ap.KeyTypeED25519
if conf.MastodonCompatible {
keysType = ap.KeyTypeRSA
}
ct.Logger.Debugf("Setting actor key generator %T[%s]", metaSaver, keysType)
ct.keyGenerator = ap.KeyGenerator(metaSaver, keysType)
}
return nil
}
type muxReadWriter struct {
io.Reader
io.Writer
}
func (w muxReadWriter) Read(p []byte) (n int, err error) {
if w.Reader != nil {
return w.Reader.Read(p)
}
return 0, nil
}
func (w muxReadWriter) Write(p []byte) (n int, err error) {
if w.Writer != nil {
return w.Writer.Write(p)
}
return 0, nil
}
var _ io.Reader = muxReadWriter{}
func loadPwFromStdin(rw io.ReadWriter, prompt string) ([]byte, error) {
term := terminal.NewTerminal(rw, prompt)
pw1, _ := term.ReadPassword("Password: ")
if len(pw1) == 0 {
return nil, errors.Errorf("empty password")
}
pw2, _ := term.ReadPassword(" Confirm: ")
if pw1 != pw2 {
return nil, errors.Errorf("passwords do not match")
}
return []byte(pw1), nil
}
func loadFromStdin(s string, params ...any) ([]byte, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(s+": ", params...)
input, _ := reader.ReadBytes('\n')
return input[:len(input)-1], nil
}
var _l atomic.Value
func Errf(out io.Writer, s string, par ...any) {
_, _ = fmt.Fprintf(out, s+"\n", par...)
return
}