generated from altid/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixfs.go
136 lines (118 loc) · 2.89 KB
/
matrixfs.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
package matrixfs
import (
"context"
"fmt"
"net/url"
//"strings"
"github.com/altid/libs/config"
"github.com/altid/libs/mdns"
"github.com/altid/libs/service"
"github.com/altid/libs/service/listener"
"github.com/altid/libs/store"
"github.com/altid/matrixfs/internal/commands"
"github.com/altid/matrixfs/internal/session"
)
type Matrixfs struct {
run func() error
session *session.Session
name string
debug bool
mdns *mdns.Entry
ctx context.Context
}
var defaults *session.Defaults = &session.Defaults{
Address: "https://matrix.chat",
Port: 443,
SSL: "simple",
Auth: "password",
User: "guest",
Logdir: "",
TLSCert: "",
TLSKey: "",
}
func CreateConfig(srv string, debug bool) error {
return config.Create(defaults, srv, "", debug)
}
func Register(ldir bool, addr string, port int, srv string, debug bool) (*Matrixfs, error) {
var err error
if e := config.Marshal(defaults, srv, "", debug); e != nil {
return nil, e
}
if defaults.Address, err = toaddr(defaults); err != nil {
return nil, err
}
l, err := tolisten(defaults, addr, port, debug)
if err != nil {
return nil, err
}
s := tostore(defaults, ldir, debug)
session := session.NewSession(defaults, debug)
session.Parse()
m := &Matrixfs{
session: session,
name: srv,
debug: debug,
}
c := service.New(srv, addr, debug)
c.WithListener(l)
c.WithStore(s)
c.WithContext(session.Ctx)
c.WithCallbacks(session)
c.WithRunner(session)
c.SetCommands(commands.Commands)
m.run = c.Listen
return m, nil
}
func (matrix *Matrixfs) Run() error {
return matrix.run()
}
func (matrix *Matrixfs) Broadcast() error {
url := fmt.Sprintf("%s:%d", matrix.session.Defaults.Address, matrix.session.Defaults.Port)
entry, err := mdns.ParseURL(url, matrix.name)
if err != nil {
return err
}
if e := mdns.Register(entry); e != nil {
return e
}
matrix.mdns = entry
return nil
}
func (matrix *Matrixfs) Cleanup() {
if matrix.mdns != nil {
matrix.mdns.Cleanup()
}
matrix.session.Quit()
}
func (matrix *Matrixfs) Session() *session.Session {
return matrix.session
}
func tolisten(d *session.Defaults, addr string, port int, debug bool) (listener.Listener, error) {
//if ssh {
// return listener.NewListenSsh()
//}
dial := fmt.Sprintf("%s:%d", addr, port)
if d.TLSKey == "none" && d.TLSCert == "none" {
return listener.NewListen9p(dial, "", "", debug)
}
return listener.NewListen9p(dial, d.TLSCert, d.TLSKey, debug)
}
func tostore(d *session.Defaults, ldir, debug bool) store.Filer {
if ldir {
return store.NewLogstore(d.Logdir.String(), debug)
}
return store.NewRamstore(debug)
}
func toaddr(d *session.Defaults) (string, error) {
// Sanitize our URL
dial, err := url.Parse(d.Address)
if err != nil {
return "", err
}
if !dial.IsAbs() {
dial.Scheme = "https"
// Because... adding a scheme doesn't set the host correctly
dial, _ = url.Parse(dial.String())
}
return dial.String(), nil
}