-
Notifications
You must be signed in to change notification settings - Fork 1
/
mumble_createchannels.go
221 lines (203 loc) · 5.23 KB
/
mumble_createchannels.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
package main
import (
"crypto/tls"
"net"
"os"
"strconv"
"time"
"gopkg.in/yaml.v3"
"github.com/romnn/flags4urfavecli/flags"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"layeh.com/gumble/gumble"
)
// Rev is set on build time to the git HEAD
var Rev = ""
// Version is incremented using bump2version
const Version = "0.1.1"
func main() {
app := &cli.App{
Version: Version,
Usage: "Create channel hierarchies on mumble server",
Name: "mumble_createchannels",
ArgsUsage: "server_address channel_map.yaml",
Flags: []cli.Flag{
&flags.LogLevelFlag,
&cli.PathFlag{
Name: "cert",
Usage: "Path to certificate `FILE` (PEM)",
//Required: true,
},
&cli.PathFlag{
Name: "key",
Usage: "Path to key `FILE` (PEM)",
//Required: true,
},
&cli.StringFlag{
Name: "pass",
Usage: "Server password",
},
&cli.IntFlag{
Name: "port",
Usage: "Server `PORT`",
Value: gumble.DefaultPort,
},
&cli.StringFlag{
Name: "user",
Usage: "Username to use",
Value: "gumble_channel_bot",
},
&cli.BoolFlag{
Name: "insecure",
Usage: "Do not verify server certificate",
Value: false,
},
&cli.IntFlag{
Name: "wait",
Usage: "wait for server ping for this many seconds",
Value: 5,
},
},
Action: func(ctx *cli.Context) error {
if level, err := log.ParseLevel(ctx.String("log")); err == nil {
log.SetLevel(level)
}
if ctx.Args().Len() < 1 {
log.Fatal("Server address missing")
cli.ShowAppHelpAndExit(ctx, 1)
}
if ctx.Args().Len() < 2 {
log.Fatal("Channel map is missing")
cli.ShowAppHelpAndExit(ctx, 1)
}
yfile, err := os.ReadFile(ctx.Args().Get(1))
if err != nil {
log.Fatal(err)
return cli.Exit("Cannot open yaml file", 1)
}
yamldata := make(map[string]interface{})
err = yaml.Unmarshal(yfile, &yamldata)
if err != nil {
log.Fatal(err)
return cli.Exit("Cannot parse yaml file", 1)
}
//log.Info("yamldata: ", pp.Sprint(yamldata))
channels, ok := yamldata["channels"]
if !ok {
log.Error("No 'channels' key at root level in YAML")
return cli.Exit("Nothing to do", 1)
}
var tlsConfig tls.Config
if ctx.Bool("insecure") {
tlsConfig.InsecureSkipVerify = true
}
cert := ctx.Path("cert")
if cert != "" {
key := ctx.Path("key")
if key == "" {
// In case cert and key are in same file
key = cert
}
certificate, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
log.Fatal(err)
return cli.Exit("Cannot load certificate", 1)
}
tlsConfig.Certificates = append(tlsConfig.Certificates, certificate)
}
config := gumble.NewConfig()
config.Username = ctx.String("user")
serverpass := ctx.String("pass")
if serverpass != "" {
config.Password = serverpass //pragma: allowlist secret
}
interval := time.Second * 1
timeout := time.Second * time.Duration(ctx.Int("wait"))
address := net.JoinHostPort(ctx.Args().First(), strconv.Itoa(ctx.Int("port")))
log.Info("Pinging ", address)
_, err = gumble.Ping(address, interval, timeout)
if err != nil {
log.Fatal(err)
return cli.Exit("Could not ping", 1)
}
log.Info("Dialing ", address)
client, err := gumble.DialWithDialer(new(net.Dialer), address, config, &tlsConfig)
if err != nil {
log.Fatal(err)
return cli.Exit("Could not connect", 1)
}
recurseChannelMap(client, client.Channels[0], channels)
log.Info("Disconnecting")
err = client.Disconnect()
if err != nil {
log.Fatal(err)
return cli.Exit("Could not disconnect", 1)
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
// Add channel and check if it got added
func addAndCheck(client *gumble.Client, parent *gumble.Channel, name string) *gumble.Channel {
log.Info("Trying to create channel ", name, " under ", parent.Name)
var retval *gumble.Channel = nil
// FIXME: listen for events with some timeout instead
for i := range [4]int{} {
client.Do(func() {
parent.Add(name, false)
})
time.Sleep(250 * time.Millisecond)
client.Do(func() {
child := parent.Find(name)
if child != nil {
retval = child
}
})
if retval != nil {
break
}
log.Warning("Did not find channel, trying again in 1s, this was attempt #", i)
time.Sleep(1000 * time.Millisecond)
}
if retval == nil {
log.Error("Looks like create failed")
}
return retval
}
func recurseChannelMap(client *gumble.Client, parent *gumble.Channel, children interface{}) bool {
for idx, item := range children.([]interface{}) {
childconf := item.(map[string]interface{})
name, found := childconf["name"]
if !found {
log.Error("'name' not found in item #", idx)
continue
}
childname := ""
switch name.(type) {
case string:
childname = name.(string)
case int:
childname = strconv.Itoa(name.(int))
}
childch := addAndCheck(client, parent, childname)
if childch == nil {
log.Error("Could not create child ", name)
continue
}
description, found := childconf["description"]
if found {
client.Do(func() {
childch.SetDescription(description.(string))
})
}
grandchildren, ok := childconf["channels"]
if ok {
recurseChannelMap(client, childch, grandchildren)
}
}
return true
}