2020-12-29 15:14:19 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-01 16:03:38 -05:00
|
|
|
"context"
|
2020-12-29 15:14:19 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
2021-03-05 14:12:34 -05:00
|
|
|
"os"
|
2020-12-29 15:14:19 -05:00
|
|
|
"strconv"
|
2021-01-19 01:06:08 -05:00
|
|
|
"sync"
|
2020-12-29 15:14:19 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"layeh.com/gumble/gumble"
|
|
|
|
)
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
type discordUser struct {
|
|
|
|
username string
|
|
|
|
seen bool
|
|
|
|
dm *discordgo.Channel
|
|
|
|
}
|
|
|
|
|
2021-01-08 14:38:21 -05:00
|
|
|
//BridgeState manages dynamic information about the bridge during runtime
|
2021-01-03 20:20:03 -05:00
|
|
|
type BridgeState struct {
|
2021-01-19 01:06:08 -05:00
|
|
|
// The configuration data for this bridge
|
|
|
|
BridgeConfig *BridgeConfig
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
// External requests to kill the bridge
|
2021-01-19 01:06:08 -05:00
|
|
|
BridgeDie chan bool
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
// Lock to only allow one bridge session at a time
|
|
|
|
lock sync.Mutex
|
|
|
|
|
|
|
|
// Wait for bridge to exit cleanly
|
|
|
|
WaitExit *sync.WaitGroup
|
|
|
|
|
2021-04-07 01:24:17 -04:00
|
|
|
// Bridge State Mutex
|
|
|
|
BridgeMutex sync.Mutex
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
// Bridge connection
|
|
|
|
Connected bool
|
|
|
|
|
|
|
|
// The bridge mode constant, auto, manual. Default is constant.
|
|
|
|
Mode bridgeMode
|
|
|
|
|
|
|
|
// Discord session. This is created and outside the bridge state
|
|
|
|
DiscordSession *discordgo.Session
|
|
|
|
|
|
|
|
// Discord voice connection. Empty if not connected.
|
|
|
|
DiscordVoice *discordgo.VoiceConnection
|
|
|
|
|
|
|
|
// Mumble client. Empty if not connected.
|
|
|
|
MumbleClient *gumble.Client
|
|
|
|
|
|
|
|
// Map of Discord users tracked by this bridge.
|
|
|
|
DiscordUsers map[string]discordUser
|
|
|
|
DiscordUsersMutex sync.Mutex
|
|
|
|
|
|
|
|
// Map of Mumble users tracked by this bridge
|
2021-01-05 19:00:28 -05:00
|
|
|
MumbleUsers map[string]bool
|
2021-01-19 01:06:08 -05:00
|
|
|
MumbleUsersMutex sync.Mutex
|
|
|
|
|
2021-01-19 13:48:51 -05:00
|
|
|
// Total Number of Mumble users
|
|
|
|
MumbleUserCount int
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
// Kill the auto connect routine
|
|
|
|
AutoChanDie chan bool
|
|
|
|
|
|
|
|
// Discord Duplex and Event Listener
|
|
|
|
DiscordStream *DiscordDuplex
|
|
|
|
DiscordListener *DiscordListener
|
|
|
|
|
|
|
|
// Mumble Duplex and Event Listener
|
|
|
|
MumbleStream *MumbleDuplex
|
|
|
|
MumbleListener *MumbleListener
|
2021-02-07 15:44:20 -05:00
|
|
|
|
|
|
|
// Discord Voice channel to join
|
|
|
|
DiscordChannelID string
|
2021-01-03 20:20:03 -05:00
|
|
|
}
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
// startBridge established the voice connection
|
|
|
|
func (b *BridgeState) startBridge() {
|
2021-02-01 16:03:38 -05:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
2021-01-19 01:06:08 -05:00
|
|
|
|
|
|
|
b.BridgeDie = make(chan bool)
|
2021-02-01 16:03:38 -05:00
|
|
|
defer close(b.BridgeDie)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
b.WaitExit = &wg
|
2021-01-19 01:06:08 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// DISCORD Connect Voice
|
2021-02-01 16:03:38 -05:00
|
|
|
log.Println("Attempting to join Discord voice channel")
|
2021-02-07 15:44:20 -05:00
|
|
|
if b.DiscordChannelID == "" {
|
|
|
|
log.Println("Tried to start bridge but no Discord channel specified")
|
|
|
|
return
|
2021-02-07 15:25:02 -05:00
|
|
|
}
|
2021-02-07 15:44:20 -05:00
|
|
|
b.DiscordVoice, err = b.DiscordSession.ChannelVoiceJoin(b.BridgeConfig.GID, b.DiscordChannelID, false, false)
|
2021-02-07 15:25:02 -05:00
|
|
|
|
2020-12-29 15:14:19 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2021-02-01 16:03:38 -05:00
|
|
|
b.DiscordVoice.Disconnect()
|
2020-12-29 15:14:19 -05:00
|
|
|
return
|
|
|
|
}
|
2021-02-01 16:03:38 -05:00
|
|
|
defer b.DiscordVoice.Disconnect()
|
2021-01-19 01:06:08 -05:00
|
|
|
defer b.DiscordVoice.Speaking(false)
|
2021-02-01 16:03:38 -05:00
|
|
|
log.Println("Discord Voice Connected")
|
2020-12-29 15:14:19 -05:00
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
// MUMBLE Connect
|
2020-12-29 15:14:19 -05:00
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
b.MumbleStream = &MumbleDuplex{}
|
2021-01-19 01:06:08 -05:00
|
|
|
det := b.BridgeConfig.MumbleConfig.AudioListeners.Attach(b.MumbleStream)
|
2021-02-01 16:03:38 -05:00
|
|
|
defer det.Detach()
|
2020-12-29 15:14:19 -05:00
|
|
|
|
|
|
|
var tlsConfig tls.Config
|
2021-01-19 01:06:08 -05:00
|
|
|
if b.BridgeConfig.MumbleInsecure {
|
2020-12-29 15:14:19 -05:00
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
}
|
2021-01-06 19:12:56 -05:00
|
|
|
|
2021-02-25 16:22:06 -05:00
|
|
|
if b.BridgeConfig.MumbleCertificate != "" {
|
2021-03-05 14:15:17 -05:00
|
|
|
keyFile := b.BridgeConfig.MumbleCertificate
|
|
|
|
if certificate, err := tls.LoadX509KeyPair(keyFile, keyFile); err != nil {
|
2021-02-25 16:22:06 -05:00
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err)
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, certificate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
log.Println("Attempting to join Mumble")
|
2021-01-19 01:06:08 -05:00
|
|
|
b.MumbleClient, err = gumble.DialWithDialer(new(net.Dialer), b.BridgeConfig.MumbleAddr, b.BridgeConfig.MumbleConfig, &tlsConfig)
|
2020-12-29 15:14:19 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2021-01-26 10:33:47 -05:00
|
|
|
b.DiscordVoice.Disconnect()
|
2020-12-29 15:14:19 -05:00
|
|
|
return
|
|
|
|
}
|
2021-01-19 01:06:08 -05:00
|
|
|
defer b.MumbleClient.Disconnect()
|
2021-02-01 16:03:38 -05:00
|
|
|
log.Println("Mumble Connected")
|
2021-01-19 01:06:08 -05:00
|
|
|
|
2020-12-29 15:14:19 -05:00
|
|
|
// Shared Channels
|
|
|
|
// Shared channels pass PCM information in 10ms chunks [480]int16
|
2021-01-19 01:06:08 -05:00
|
|
|
// These channels are internal and are not added to the bridge state.
|
|
|
|
var toMumble = b.MumbleClient.AudioOutgoing()
|
2020-12-29 15:14:19 -05:00
|
|
|
var toDiscord = make(chan []int16, 100)
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
defer close(toDiscord)
|
|
|
|
defer close(toMumble)
|
2020-12-29 15:14:19 -05:00
|
|
|
|
|
|
|
// Start Passing Between
|
2021-01-05 13:16:03 -05:00
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
// From Mumble
|
2021-02-01 16:03:38 -05:00
|
|
|
go b.MumbleStream.fromMumbleMixer(ctx, &wg, toDiscord)
|
2021-01-19 01:06:08 -05:00
|
|
|
|
|
|
|
// From Discord
|
|
|
|
b.DiscordStream = &DiscordDuplex{
|
|
|
|
Bridge: b,
|
|
|
|
fromDiscordMap: make(map[uint32]fromDiscord),
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
go b.DiscordStream.discordReceivePCM(ctx, &wg, cancel)
|
|
|
|
go b.DiscordStream.fromDiscordMixer(ctx, &wg, toMumble)
|
2021-01-19 01:06:08 -05:00
|
|
|
|
|
|
|
// To Discord
|
2021-02-01 16:03:38 -05:00
|
|
|
go b.DiscordStream.discordSendPCM(ctx, &wg, cancel, toDiscord)
|
2020-12-29 15:14:19 -05:00
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
// Monitor Mumble
|
2021-04-06 22:34:38 -04:00
|
|
|
wg.Add(1)
|
2020-12-29 15:14:19 -05:00
|
|
|
go func() {
|
2021-02-01 16:03:38 -05:00
|
|
|
wg.Add(1)
|
2020-12-29 15:14:19 -05:00
|
|
|
ticker := time.NewTicker(500 * time.Millisecond)
|
|
|
|
for {
|
2021-02-01 16:03:38 -05:00
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if b.MumbleClient == nil || b.MumbleClient.State() != 2 {
|
|
|
|
if b.MumbleClient != nil {
|
|
|
|
log.Println("Lost mumble connection " + strconv.Itoa(int(b.MumbleClient.State())))
|
|
|
|
} else {
|
|
|
|
log.Println("Lost mumble connection due to bridge dieing")
|
|
|
|
}
|
|
|
|
cancel()
|
2021-01-03 16:19:49 -05:00
|
|
|
}
|
2021-02-01 16:03:38 -05:00
|
|
|
case <-ctx.Done():
|
|
|
|
wg.Done()
|
|
|
|
return
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-01-19 01:06:08 -05:00
|
|
|
|
2021-04-07 01:24:17 -04:00
|
|
|
b.BridgeMutex.Lock()
|
2021-01-19 01:06:08 -05:00
|
|
|
b.Connected = true
|
2021-04-07 01:24:17 -04:00
|
|
|
b.BridgeMutex.Unlock()
|
2021-01-03 20:48:34 -05:00
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
// Hold until cancelled or external die request
|
2020-12-29 15:14:19 -05:00
|
|
|
select {
|
2021-02-01 16:03:38 -05:00
|
|
|
case <-ctx.Done():
|
|
|
|
log.Println("Bridge internal context cancel")
|
2021-01-19 01:06:08 -05:00
|
|
|
case <-b.BridgeDie:
|
2021-02-01 16:03:38 -05:00
|
|
|
log.Println("Bridge die request received")
|
|
|
|
cancel()
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
2021-02-01 16:03:38 -05:00
|
|
|
|
2021-04-07 01:24:17 -04:00
|
|
|
b.BridgeMutex.Lock()
|
2021-02-01 16:03:38 -05:00
|
|
|
b.Connected = false
|
2021-04-07 01:24:17 -04:00
|
|
|
b.BridgeMutex.Unlock()
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
wg.Wait()
|
|
|
|
log.Println("Terminating Bridge")
|
|
|
|
b.MumbleUsersMutex.Lock()
|
|
|
|
b.MumbleUsers = make(map[string]bool)
|
|
|
|
b.MumbleUsersMutex.Unlock()
|
|
|
|
b.DiscordUsers = make(map[string]discordUser)
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
func (b *BridgeState) discordStatusUpdate() {
|
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)
|
2021-01-19 01:06:08 -05:00
|
|
|
resp, err := gumble.Ping(b.BridgeConfig.MumbleAddr, -1, m)
|
|
|
|
status := ""
|
2020-12-29 15:14:19 -05:00
|
|
|
|
2021-01-03 19:49:12 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error pinging mumble server %v\n", err)
|
2021-01-19 01:06:08 -05:00
|
|
|
b.DiscordSession.UpdateListeningStatus("an error pinging mumble")
|
2020-12-29 15:14:19 -05:00
|
|
|
} else {
|
2021-01-19 01:06:08 -05:00
|
|
|
b.MumbleUsersMutex.Lock()
|
2021-04-07 01:24:17 -04:00
|
|
|
b.BridgeMutex.Lock()
|
2021-01-19 13:48:51 -05:00
|
|
|
b.MumbleUserCount = resp.ConnectedUsers
|
2021-01-19 01:06:08 -05:00
|
|
|
if b.Connected {
|
2021-01-19 13:48:51 -05:00
|
|
|
b.MumbleUserCount = b.MumbleUserCount - 1
|
2021-01-03 19:49:12 -05:00
|
|
|
}
|
2021-01-19 13:48:51 -05:00
|
|
|
if b.MumbleUserCount == 0 {
|
2021-01-19 14:06:19 -05:00
|
|
|
status = ""
|
2021-01-03 19:49:12 -05:00
|
|
|
} else {
|
2021-01-19 01:06:08 -05:00
|
|
|
if len(b.MumbleUsers) > 0 {
|
2021-01-19 13:48:51 -05:00
|
|
|
status = fmt.Sprintf("%v/%v users in Mumble\n", len(b.MumbleUsers), b.MumbleUserCount)
|
2021-01-05 19:00:28 -05:00
|
|
|
} else {
|
2021-01-19 13:48:51 -05:00
|
|
|
status = fmt.Sprintf("%v users in Mumble\n", b.MumbleUserCount)
|
2021-01-05 19:00:28 -05:00
|
|
|
}
|
2021-01-03 19:49:12 -05:00
|
|
|
}
|
2021-04-07 01:24:17 -04:00
|
|
|
b.BridgeMutex.Unlock()
|
2021-01-19 01:06:08 -05:00
|
|
|
b.MumbleUsersMutex.Unlock()
|
|
|
|
b.DiscordSession.UpdateListeningStatus(status)
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
// AutoBridge starts a goroutine to check the number of users in discord and mumble
|
|
|
|
// when there is at least one user on both, starts up the bridge
|
|
|
|
// when there are no users on either side, kills the bridge
|
|
|
|
func (b *BridgeState) AutoBridge() {
|
2021-01-03 15:32:59 -05:00
|
|
|
log.Println("beginning auto mode")
|
2021-01-19 01:06:08 -05:00
|
|
|
ticker := time.NewTicker(3 * time.Second)
|
|
|
|
|
2021-01-03 15:32:59 -05:00
|
|
|
for {
|
|
|
|
select {
|
2021-01-19 01:06:08 -05:00
|
|
|
case <-ticker.C:
|
|
|
|
case <-b.AutoChanDie:
|
2021-01-03 15:32:59 -05:00
|
|
|
log.Println("ending automode")
|
|
|
|
return
|
|
|
|
}
|
2021-01-19 01:06:08 -05:00
|
|
|
|
|
|
|
b.MumbleUsersMutex.Lock()
|
|
|
|
b.DiscordUsersMutex.Lock()
|
2021-04-07 01:24:17 -04:00
|
|
|
b.BridgeMutex.Lock()
|
2021-01-19 01:06:08 -05:00
|
|
|
|
2021-01-19 13:48:51 -05:00
|
|
|
if !b.Connected && b.MumbleUserCount > 0 && len(b.DiscordUsers) > 0 {
|
2021-01-03 15:32:59 -05:00
|
|
|
log.Println("users detected in mumble and discord, bridging")
|
2021-01-19 01:06:08 -05:00
|
|
|
go b.startBridge()
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
2021-01-19 13:48:51 -05:00
|
|
|
if b.Connected && b.MumbleUserCount == 0 && len(b.DiscordUsers) <= 1 {
|
2021-01-03 15:32:59 -05:00
|
|
|
log.Println("no one online, killing bridge")
|
2021-01-19 01:06:08 -05:00
|
|
|
b.BridgeDie <- true
|
|
|
|
}
|
|
|
|
|
2021-04-07 01:24:17 -04:00
|
|
|
b.BridgeMutex.Unlock()
|
2021-01-19 01:06:08 -05:00
|
|
|
b.MumbleUsersMutex.Unlock()
|
|
|
|
b.DiscordUsersMutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BridgeState) discordSendMessageAll(msg string) {
|
|
|
|
if b.BridgeConfig.DiscordDisableText {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.DiscordUsersMutex.Lock()
|
|
|
|
for id := range b.DiscordUsers {
|
|
|
|
du := b.DiscordUsers[id]
|
|
|
|
if du.dm != nil {
|
|
|
|
b.DiscordSession.ChannelMessageSend(du.dm.ID, msg)
|
2021-01-03 15:32:59 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 01:06:08 -05:00
|
|
|
b.DiscordUsersMutex.Unlock()
|
2020-12-29 15:14:19 -05:00
|
|
|
}
|