2020-12-29 15:14:19 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"layeh.com/gumble/gumble"
|
2021-01-05 13:16:03 -05:00
|
|
|
"layeh.com/gumble/gumbleutil"
|
2020-12-29 15:14:19 -05:00
|
|
|
)
|
|
|
|
|
2021-01-03 20:20:03 -05:00
|
|
|
type BridgeState struct {
|
|
|
|
ActiveConn chan bool
|
|
|
|
Connected bool
|
|
|
|
Client *gumble.Client
|
2021-01-03 20:48:34 -05:00
|
|
|
DiscordUsers map[string]bool
|
2021-01-03 20:20:03 -05:00
|
|
|
MumbleUserCount int
|
|
|
|
DiscordUserCount int
|
|
|
|
AutoChan chan bool
|
|
|
|
}
|
|
|
|
|
2020-12-29 15:14:19 -05:00
|
|
|
func startBridge(discord *discordgo.Session, discordGID string, discordCID string, config *gumble.Config, mumbleAddr string, mumbleInsecure bool, die chan bool) {
|
|
|
|
dgv, err := discord.ChannelVoiceJoin(discordGID, discordCID, false, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer dgv.Speaking(false)
|
|
|
|
defer dgv.Close()
|
|
|
|
discord.ShouldReconnectOnError = true
|
|
|
|
|
|
|
|
// MUMBLE Setup
|
|
|
|
|
2020-12-29 18:19:44 -05:00
|
|
|
m := MumbleDuplex{
|
|
|
|
Close: make(chan bool),
|
|
|
|
}
|
2020-12-29 15:14:19 -05:00
|
|
|
|
|
|
|
var tlsConfig tls.Config
|
|
|
|
if mumbleInsecure {
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
}
|
2021-01-05 13:16:03 -05:00
|
|
|
config.Attach(gumbleutil.Listener{
|
|
|
|
Connect: mumbleConnect,
|
|
|
|
})
|
2020-12-29 15:14:19 -05:00
|
|
|
mumble, err := gumble.DialWithDialer(new(net.Dialer), mumbleAddr, config, &tlsConfig)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer mumble.Disconnect()
|
2021-01-03 20:20:03 -05:00
|
|
|
Bridge.Client = mumble
|
2020-12-29 15:14:19 -05:00
|
|
|
// Shared Channels
|
|
|
|
// Shared channels pass PCM information in 10ms chunks [480]int16
|
|
|
|
var toMumble = mumble.AudioOutgoing()
|
|
|
|
var toDiscord = make(chan []int16, 100)
|
|
|
|
|
|
|
|
log.Println("Mumble Connected")
|
|
|
|
|
|
|
|
// Start Passing Between
|
|
|
|
// Mumble
|
2020-12-29 18:19:44 -05:00
|
|
|
go m.fromMumbleMixer(toDiscord, die)
|
|
|
|
det := config.AudioListeners.Attach(m)
|
2021-01-05 13:16:03 -05:00
|
|
|
|
2020-12-29 15:14:19 -05:00
|
|
|
//Discord
|
|
|
|
go discordReceivePCM(dgv, die)
|
2020-12-29 18:19:44 -05:00
|
|
|
go fromDiscordMixer(toMumble, die)
|
2020-12-29 15:14:19 -05:00
|
|
|
go discordSendPCM(dgv, toDiscord, die)
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(500 * time.Millisecond)
|
|
|
|
for {
|
|
|
|
<-ticker.C
|
|
|
|
if mumble.State() != 2 {
|
|
|
|
log.Println("Lost mumble connection " + strconv.Itoa(int(mumble.State())))
|
2021-01-03 16:19:49 -05:00
|
|
|
select {
|
|
|
|
default:
|
|
|
|
close(die)
|
|
|
|
case <-die:
|
|
|
|
//die is already closed
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
close(m.Close)
|
|
|
|
case <-m.Close:
|
|
|
|
//m.Close is already closed
|
|
|
|
}
|
|
|
|
return
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-03 20:48:34 -05:00
|
|
|
//Setup initial discord state
|
|
|
|
g, err := discord.State.Guild(discordGID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error finding guild")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, vs := range g.VoiceStates {
|
|
|
|
if vs.ChannelID == discordCID {
|
|
|
|
Bridge.DiscordUserCount = Bridge.DiscordUserCount + 1
|
|
|
|
u, err := discord.User(vs.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error looking up username")
|
|
|
|
Bridge.DiscordUsers[u.Username] = true
|
2021-01-05 11:41:33 -05:00
|
|
|
Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false)
|
2021-01-03 20:48:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 11:41:33 -05:00
|
|
|
Bridge.Connected = true
|
2021-01-03 20:48:34 -05:00
|
|
|
|
2020-12-29 15:14:19 -05:00
|
|
|
select {
|
|
|
|
case sig := <-c:
|
|
|
|
log.Printf("\nGot %s signal. Terminating Mumble-Bridge\n", sig)
|
|
|
|
case <-die:
|
|
|
|
log.Println("\nGot internal die request. Terminating Mumble-Bridge")
|
|
|
|
dgv.Disconnect()
|
2020-12-29 18:19:44 -05:00
|
|
|
det.Detach()
|
2021-01-03 16:19:49 -05:00
|
|
|
close(die)
|
|
|
|
close(m.Close)
|
|
|
|
close(toMumble)
|
2021-01-03 15:32:59 -05:00
|
|
|
Bridge.Connected = false
|
2021-01-03 20:20:03 -05:00
|
|
|
Bridge.Client = nil
|
|
|
|
Bridge.MumbleUserCount = 0
|
|
|
|
Bridge.DiscordUserCount = 0
|
2021-01-05 11:41:33 -05:00
|
|
|
Bridge.DiscordUsers = make(map[string]bool)
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 19:49:12 -05:00
|
|
|
func discordStatusUpdate(dg *discordgo.Session, host, port string) {
|
|
|
|
status := ""
|
2020-12-29 15:14:19 -05:00
|
|
|
curr := 0
|
2021-01-03 19:49:12 -05:00
|
|
|
m, _ := time.ParseDuration("30s")
|
2020-12-29 15:14:19 -05:00
|
|
|
for {
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
resp, err := gumble.Ping(host+":"+port, -1, m)
|
|
|
|
|
2021-01-03 19:49:12 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error pinging mumble server %v\n", err)
|
|
|
|
dg.UpdateListeningStatus("an error pinging mumble")
|
2020-12-29 15:14:19 -05:00
|
|
|
} else {
|
2021-01-03 19:49:12 -05:00
|
|
|
curr = resp.ConnectedUsers
|
|
|
|
if Bridge.Connected {
|
|
|
|
curr = curr - 1
|
|
|
|
}
|
|
|
|
if curr != Bridge.MumbleUserCount {
|
|
|
|
Bridge.MumbleUserCount = curr
|
|
|
|
}
|
|
|
|
if curr == 0 {
|
|
|
|
status = ""
|
|
|
|
} else {
|
|
|
|
status = fmt.Sprintf("%v users in Mumble\n", curr)
|
|
|
|
}
|
|
|
|
dg.UpdateListeningStatus(status)
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func AutoBridge(s *discordgo.Session) {
|
|
|
|
log.Println("beginning auto mode")
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-Bridge.AutoChan:
|
|
|
|
log.Println("ending automode")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
if !Bridge.Connected && Bridge.MumbleUserCount > 0 && Bridge.DiscordUserCount > 0 {
|
|
|
|
log.Println("users detected in mumble and discord, bridging")
|
|
|
|
die := make(chan bool)
|
|
|
|
Bridge.ActiveConn = die
|
|
|
|
go startBridge(s, BridgeConf.GID, BridgeConf.CID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, die)
|
|
|
|
}
|
|
|
|
if Bridge.Connected && Bridge.MumbleUserCount == 0 && Bridge.DiscordUserCount <= 1 {
|
|
|
|
log.Println("no one online, killing bridge")
|
|
|
|
Bridge.ActiveConn <- true
|
2021-01-03 15:45:12 -05:00
|
|
|
MumbleReset()
|
|
|
|
DiscordReset()
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|