forked from alliedmodders/blaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblaster.go
executable file
·400 lines (356 loc) · 10.4 KB
/
blaster.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// vim: set ts=5 sw=4 tw=99 noet:
//
// Blaster (C) Copyright 2014 AlliedModders LLC
// Licensed under the GNU General Public License, version 3 or higher.
// See LICENSE.txt for more details.
package main
import (
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"runtime"
"strings"
"sync"
"time"
batch "github.com/alliedmodders/blaster/batch"
valve "github.com/alliedmodders/blaster/valve"
"github.com/joho/godotenv"
"golang.org/x/net/proxy"
tb "gopkg.in/tucnak/telebot.v2"
)
const (
NORM int8 = 3
ERR int8 = 1
)
var (
sOutputLock sync.Mutex
sOutputBuffer io.Writer
sOutputFormat string
sResultMap map[int]string = make(map[int]string)
sNumServers int64
)
type ErrorObject struct {
Ip string `json:"ip"`
Error string `json:"error"`
}
type ServerObject struct {
Address string `json:"ip"`
// LocalAddress string `json:"local_ip,omitempty"`
// Protocol uint8 `json:"protocol"`
Name string `json:"name"`
MapName string `json:"map"`
// Folder string `json:"folder"`
Game string `json:"game"`
Players uint8 `json:"players"`
MaxPlayers uint8 `json:"max_players"`
// Bots uint8 `json:"bots"`
// Type string `json:"type"`
// Os string `json:"os"`
Visibility string `json:"visibility"`
// Vac bool `json:"vac"`
// Only available from The Ship.
// Ship *valve.TheShipInfo `json:"theship,omitempty"`
// Only available on Source.
// AppId valve.AppId `json:"appid,omitempty"`
// GameVersion string `json:"game_version,omitempty"`
// Port uint16 `json:"port,omitempty"`
// SteamId string `json:"steamid,omitempty"`
// GameMode string `json:"game_mode,omitempty"`
// GameId string `json:"gameid,omitempty"`
// SpecTvPort uint16 `json:"spectv_port,omitempty"`
// SpecTvName string `json:"spectv_name,omitempty"`
// Only available on Half-Life 1.
Mod *valve.ModInfo `json:"mod,omitempty"`
Rules map[string]string `json:"rules"`
}
const (
BOT_ORG_NAME string = "Blaster"
NOT_ENOUGH_ARGS string = "Not Enough Args! Give me some information for search ..."
// NICE string = "NICE"
NOT_CORRECT_ARGS string = "You didn't give me the correct arguments"
)
func greetings() string {
return fmt.Sprintf(
"Welcome to the %s bot!\nThis is the alternatie version of the Blaster program for the telegram\nIf you have any question use msg to tell us",
BOT_ORG_NAME,
)
}
func StrToBool(cond string) bool {
return cond == "true"
}
func isEmpty(data string) string {
if data == "" {
return ""
}
return ""
}
func SendMsg(mode int8, to tb.Recipient, bot *tb.Bot, msg string) {
var res string = ""
switch mode {
case ERR:
res = "[ERROR] " + msg
case NORM:
res = "[INFO] " + msg
bot.Send(to, res)
}
}
func Parse(line string) (batch.Config, error) {
spl_data := strings.Split(line, " ")
if len(spl_data) <= 1 {
return batch.Config{}, errors.New(NOT_ENOUGH_ARGS)
}
// /search <game> <map> <plen>
return batch.Config{
Flag_name: spl_data[1], // save the first element inside the batch.Config.g_name (the game name)
Flag_appids: "",
Flag_appid: 0,
Flag_fmap: spl_data[2],
Flag_master: valve.MasterServer,
Flag_j: 30,
Flag_norules: false,
}, nil
}
func buildClientWithProxy(addr string) (*http.Client, error) {
if addr != "" {
dialer, err := proxy.SOCKS5("tcp", addr, nil, proxy.Direct)
if err != nil {
return nil, err
}
// Patch client transport
httpTransport := &http.Transport{Dial: dialer.Dial}
hc := &http.Client{Transport: httpTransport}
return hc, nil
}
return nil, nil // use default
}
func mapCompare(r_map string, u_map string) bool {
if r_map == u_map {
return true
}
return false
}
func addResult(results *map[int]string, key int, value string) {
sOutputLock.Lock()
if _, ok := (*results)[key]; ok {
(*results)[key] += value
} else {
(*results)[key] = value
}
defer sOutputLock.Unlock()
}
func addJson(hostAndPort string, obj interface{}, bot *tb.Bot, sender *tb.Message, gameFilter *batch.Config) {
if _, err := obj.(*ErrorObject); err { // If we have error, don't even process it
return
}
var isFilterTrue bool = false
log.Println("[INFO] Processing Game Server ...")
// If we had any problem with tel objects
if bot == nil || sender == nil {
return
}
serv_obj_org, res := obj.(*ServerObject)
if res {
}
if mapCompare(serv_obj_org.MapName, gameFilter.Flag_fmap) {
isFilterTrue = true
} else {
isFilterTrue = false
}
if isFilterTrue {
addResult(&sResultMap, sender.ID, fmt.Sprintf("Name: %s\nPlayers: %d\nMap: %s\n------------\n", serv_obj_org.Name, serv_obj_org.Players, serv_obj_org.MapName))
}
log.Println("[INFO] Processing Game Server Succsessfully")
}
func addError(hostAndPort string, err error) {
addJson(hostAndPort, &ErrorObject{
Ip: hostAndPort,
Error: err.Error(),
}, nil, nil, nil)
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("ERROR: Can't read the env file for reason:", err.Error())
}
token := os.Getenv("BOT_TOKEN")
if token == "" {
log.Println("Set token via environment\nBOT_TOKEN=<your_token>")
return
}
client, err := buildClientWithProxy(os.Getenv("BOT_PROXY"))
if err != nil {
log.Fatal(err)
return
}
b, err := tb.NewBot(tb.Settings{
Token: token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
Client: client,
})
if err != nil {
log.Fatal(err)
return
}
log.Printf("Bot started[%s]", b.Me.Username)
b.Handle("/start", func(m *tb.Message) {
_, err := b.Send(m.Sender, greetings())
if err != nil {
log.Fatal("ERROR:", err)
return
}
})
b.Handle("/search", func(m *tb.Message) {
// waiting, err := b.Send(m.Sender, "Waiting ....")
err := b.Notify(m.Sender, tb.Typing)
// m.voice
if err != nil {
log.Println("ERROR:", err.Error())
}
config, err := Parse(m.Text)
fmt.Println("Telegram Arguments Parsed")
if err != nil {
b.Send(m.Sender, err)
return
}
sOutputFormat = config.Flag_outfile
// Flag_game := flag.String("game", "", "Game (hl1, hl2)")
// Flag_appid := flag.Int("appid", 0, "Query a single AppID")
// Flag_appids := flag.String("appids", "", "Comma-delimited list of AppIDs")
// Flag_master := flag.String("master", valve.MasterServer, "Master server address")
// Flag_j := flag.Int("j", 20, "Number of concurrent requests (more will introduce more timeouts)")
// Flag_timeout := flag.Duration("timeout", time.Second*3, "Timeout for querying servers")
// Flag_format := flag.String("format", "list", "JSON format (list, map, or lines)")
// Flag_outfile := flag.String("outfile", "", "Output to a file")
// Flag_norules := flag.Bool("norules", false, "Don't query server rules")
// Flag.Usage = func() {
// fmt.Fprintf(os.Stderr, "Usage: -game or -appids\n")
// Flag.PrintDefaults()
// }
// Flag.Parse()
appids := []valve.AppId{}
//fmt.Printf("This is the game name: |%s|\n", config.Flag_name)
if config.Flag_name != "" {
switch config.Flag_name {
case "hl1":
appids = append(appids, valve.HL1Apps...)
//appids = append(appids, 70)
case "hl2":
appids = append(appids, valve.HL2Apps...)
case "halflife":
appids = append(appids, 70)
case "css":
appids = append(appids, 240)
default:
SendMsg(NORM, m.Sender, b,
fmt.Sprintf("Unrecognized game: %s", config.Flag_name))
return
}
}
if len(appids) == 0 {
fmt.Fprintf(os.Stderr, "At least one AppID or game must be specified.\n")
os.Exit(1)
}
runtime.GOMAXPROCS(runtime.NumCPU())
// Create a connection to the master server.
master, err := valve.NewMasterServerQuerier(config.Flag_master)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not query master: %s", err.Error())
}
defer master.Close()
// Set up the filter list.
master.FilterAppIds(appids)
bp := batch.NewBatchProcessor(func(gameFilter *batch.Config, item interface{}) {
addr := item.(*net.TCPAddr)
Flag_timeout := time.Second * 3
query, err := valve.NewServerQuerier(addr.String(), Flag_timeout)
if err != nil {
addError(addr.String(), err)
return
}
defer query.Close()
info, err := query.QueryInfo()
if err != nil {
addError(addr.String(), err)
return
}
out := &ServerObject{
Address: addr.String(),
// Protocol: info.Protocol,
Name: info.Name,
MapName: info.MapName,
// Folder: info.Folder,
Game: info.Game,
Players: info.Players,
MaxPlayers: info.MaxPlayers,
//Bots: info.Bots,
// Type: info.Type.String(),
// Os: info.OS.String(),
// Ship: info.TheShip,
Mod: info.Mod,
}
// if info.Vac == 1 {
// out.Vac = true
// }
if info.Visibility == 0 {
out.Visibility = "public"
} else {
out.Visibility = "private"
}
//if info.Ext != nil {
// out.AppId = info.Ext.AppId
// out.GameVersion = info.Ext.GameVersion
// out.Port = info.Ext.Port
// out.SteamId = fmt.Sprintf("%d", info.Ext.SteamId)
// out.GameMode = info.Ext.GameModeDescription
// out.GameId = fmt.Sprintf("%d", info.Ext.GameId)
// }
// if info.InfoVersion == valve.S2A_INFO_GOLDSRC {
// out.LocalAddress = info.Address
// }
// if info.SpecTv != nil {
// out.SpecTvPort = info.SpecTv.Port
// out.SpecTvName = info.SpecTv.Name
// }
// We can't query rules for CSGO servers anymore because Valve.
// csgo := (info.Ext != nil && info.Ext.AppId == valve.App_CSGO)
// if !csgo && !config.Flag_norules {
// log.Println("I', Running this side")
// rules, err := query.QueryRules()
// if err != nil {
// out.Rules = map[string]string{
// "error": err.Error(),
// }
// } else {
// out.Rules = rules
// }
// }
out.Rules = map[string]string{"Rules": "NULL"}
addJson(addr.String(), out, b, m, gameFilter)
}, config.Flag_j)
bp.SetConfig(config) // set the user config as gameFilter
defer bp.Terminate()
// Query the master.
err = master.Query(func(servers valve.ServerList) error {
bp.AddBatch(servers)
//bp.Finish()
return nil
})
if err != nil {
SendMsg(ERR, m.Sender, b, "There is a error when query the valve's servers")
return
}
// Wait for batch processing to complete.
bp.Finish()
if _, err := b.Send(m.Sender, sResultMap[m.ID][:400]); err != nil {
log.Println("ERROR sending text to user cause:", err.Error())
}
log.Println("[INFO] All Tasks are done!")
SendMsg(NORM, m.Sender, b, "That's it, Those are your desire results :D")
})
b.Start()
// log.Printf("Server does started\n")
}