-
Notifications
You must be signed in to change notification settings - Fork 61
/
main.go
202 lines (163 loc) · 4.48 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
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"os"
"strings"
"time"
bolt "go.etcd.io/bbolt"
"github.com/42wim/matterircd/config"
irckit "github.com/42wim/matterircd/mm-go-irckit"
"github.com/google/gops/agent"
prefixed "github.com/matterbridge/logrus-prefixed-formatter"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
version = "0.28.1-dev"
githash string
logger *logrus.Entry
v *viper.Viper
LastViewedSaveDB *bolt.DB
)
func main() {
ourlog := logrus.New()
ourlog.Formatter = &prefixed.TextFormatter{
PrefixPadding: 11,
FullTimestamp: true,
}
logger = ourlog.WithFields(logrus.Fields{"prefix": "matterircd"})
config.Logger = logger
// config related. instantiate a new config.Config to store flags
flagConfig := flag.String("conf", "matterircd.toml", "config file")
// bools for showing version/enabling debug
flag.Bool("version", false, "show version")
flag.Bool("debug", false, "enable debug logging")
// bind related cfg
flag.String("bind", "127.0.0.1:6667", "interface:port to bind to, or a path to bind to a Unix socket.")
// TLS related cfg
flag.String("tlsbind", "", "interface:port to bind to. (e.g 127.0.0.1:6697)")
flag.String("tlsdir", ".", "directory to look for key.pem and cert.pem.")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
// Attempt to load values from the config file
if _, err := os.Stat(*flagConfig); err == nil {
v, err = config.LoadConfig(*flagConfig)
if err != nil {
log.Fatal(err)
}
} else {
v = viper.New()
}
v.BindPFlags(pflag.CommandLine)
if v.GetBool("debug") {
logger.Info("enabling debug")
ourlog.Level = logrus.DebugLevel
irckit.SetLogLevel("debug")
}
if v.GetBool("trace") {
logger.Info("enabling trace")
ourlog.Level = logrus.TraceLevel
irckit.SetLogLevel("trace")
}
if v.GetBool("gops") {
if err := agent.Listen(agent.Options{}); err != nil {
log.Fatal(err)
}
}
if v.GetBool("version") {
fmt.Printf("version: %s %s\n", version, githash)
return
}
irckit.SetLogger(logger)
logger.Infof("Running version %s %s", version, githash)
if strings.Contains(version, "-dev") {
logger.Infof("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.")
}
if v.GetString("tlsbind") != "" {
go func() {
logger.Infof("Listening on %s (TLS)", v.GetString("tlsbind"))
socket := tlsbind()
defer socket.Close()
start(socket)
}()
}
mmLastViewedFile := "matterircd-lastsaved.db"
if statePath := v.GetString("mattermost.LastViewedSaveFile"); statePath != "" {
mmLastViewedFile = statePath
}
db, err := bolt.Open(mmLastViewedFile, 0o600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
logger.Fatal(err)
}
defer db.Close()
LastViewedSaveDB = db
// backwards compatible
if v.GetString("bind") != "" {
go func() {
var network string
if strings.ContainsRune(v.GetString("bind"), os.PathSeparator) {
network = "unix"
} else {
network = "tcp"
}
socket, err := net.Listen(network, v.GetString("bind"))
if err != nil {
logger.Errorf("Can not listen on %s: %v", v.GetString("bind"), err)
os.Exit(1)
}
logger.Infof("Listening on %s", v.GetString("bind"))
defer socket.Close()
start(socket)
}()
}
select {}
}
func tlsbind() net.Listener {
certPath := v.GetString("tlsdir") + "/cert.pem"
keyPath := v.GetString("tlsdir") + "/key.pem"
if v.GetString("tlscert") != "" {
certPath = v.GetString("tlscert")
}
if v.GetString("tlskey") != "" {
keyPath = v.GetString("tlskey")
}
kpr, err := NewKeypairReloader(certPath, keyPath)
if err != nil {
logger.Errorf("could not load TLS, incorrect directory? Error: %s", err)
os.Exit(1)
}
tlsConfig := tls.Config{
GetCertificate: kpr.GetCertificateFunc(),
}
listenerTLS, err := tls.Listen("tcp", v.GetString("tlsbind"), &tlsConfig)
if err != nil {
logger.Errorf("Can not listen on %s: %v\n", v.GetString("tlsbind"), err)
os.Exit(1)
}
logger.Info("TLS listening on ", v.GetString("tlsbind"))
return listenerTLS
}
func start(socket net.Listener) {
for {
conn, err := socket.Accept()
if err != nil {
logger.Errorf("Failed to accept connection: %v", err)
return
}
go func() {
newsrv := irckit.ServerConfig{Name: "matterircd", Version: version}.Server()
logger.Infof("New connection: %s", conn.RemoteAddr())
user := irckit.NewUserBridge(conn, newsrv, v, LastViewedSaveDB)
err = newsrv.Connect(user)
if err != nil {
logger.Errorf("Failed to join: %v", err)
return
}
}()
}
}