-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverctx.go
187 lines (156 loc) · 4.26 KB
/
serverctx.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
package main
import (
"github.com/hfern/goseq"
"log"
"time"
)
type ServerQueryOptions struct {
NoPlayers bool `long:"no-players" short:"P" default:"false" description:"Don't list players."`
NoInfo bool `long:"no-info" short:"I" default:"false" description:"Don't list general info."`
NoRules bool `long:"no-rules" short:"R" default:"false" description:"Don't list server rules."`
Serial bool `long:"serial" short:"s" default:"false" description:"Force serial querying of server attributes."`
Json bool `long:"json" default:"false" description:"Output as JSON to StdOut"`
Timeout uint `long:"timeout" short:"t" default:"2" description:"Timeout for attribute queries in seconds."`
OnlyKeywords bool `long:"only-keywords" short:"K" default:"false" description:"Only list the keywords of the servers one per line."`
}
type DoneChannel chan int
const DONE int = 0
type MaybePlayers struct {
Error error
Players []goseq.Player
}
type MaybeInfo struct {
Error error
Info goseq.ServerInfo
}
type MaybeRules struct {
Error error
Rules goseq.RuleMap
}
type ServerQueryableAttributes struct {
Address string
Players MaybePlayers
Info MaybeInfo
Rules MaybeRules
}
type ServerAttrPair struct {
Server goseq.Server
Attrs ServerQueryableAttributes
}
type doIf struct {
cond bool
call func(DoneChannel)
}
var serverSingleOptions ServerQueryOptions
func serverctx(serverAddresses []string) {
options := &serverSingleOptions
if !assertLogicalServerFlags(options) {
return
}
if len(serverAddresses) == 0 {
log.Fatal(
"The first argument to sourceq server must be the address of the server. \n" +
"e.g. sourceq server google.com\n" +
"or sourceq server google.com:8080\n" +
"or sourceq server 192.168.1.1:6060\n" +
"or, for multiple servers: \n\tsourceq server google.com:80 example.org 123.156.178")
return
}
timeout := time.Duration(options.Timeout) * time.Second
done := make(DoneChannel)
servers := make([]ServerAttrPair, len(serverAddresses))
for i, serverAddr := range serverAddresses {
servers[i].Attrs.Address = serverAddr
server := goseq.NewServer()
err := server.SetAddress(serverAddr)
if err != nil {
log.Fatal(err)
}
servers[i].Server = server
}
for i, _ := range servers {
server := &servers[i]
go queryServer(options, &server.Server, &server.Attrs, timeout, done)
if options.Serial {
<-done
}
}
if !options.Serial {
for _ = range servers {
<-done
}
}
if options.Json {
viewServerJSON(options, servers)
} else {
viewServerText(options, servers)
}
}
func assertLogicalServerFlags(options *ServerQueryOptions) bool {
if options.OnlyKeywords {
if options.Json {
log.Fatal("--only-keywords cannot be used with --json")
return false
}
options.NoRules = true
options.NoPlayers = true
options.NoInfo = false
}
return true
}
func queryServer(
options *ServerQueryOptions,
server *goseq.Server,
attrs *ServerQueryableAttributes,
timeout time.Duration,
serverIsDone DoneChannel) {
defer func() { serverIsDone <- DONE }()
done := make(DoneChannel)
callbacks := []doIf{
{
cond: !options.NoInfo,
call: func(donner DoneChannel) {
getServerInfo(server, &attrs.Info, timeout, donner)
},
},
{
cond: !options.NoRules,
call: func(donner DoneChannel) {
getServerRules(server, &attrs.Rules, timeout, donner)
},
},
{
cond: !options.NoPlayers,
call: func(donner DoneChannel) {
getServerPlayers(server, &attrs.Players, timeout, donner)
},
},
}
for _, cb := range callbacks {
if cb.cond {
go cb.call(done)
if options.Serial {
<-done
}
}
}
if !options.Serial {
for _, cb := range callbacks {
if cb.cond {
<-done
}
}
}
}
func getServerInfo(server *goseq.Server, info *MaybeInfo, timeout time.Duration, donner DoneChannel) {
defer func() { donner <- DONE }()
info.Info, info.Error = (*server).Info(timeout)
}
func getServerRules(server *goseq.Server, rules *MaybeRules, timeout time.Duration, donner DoneChannel) {
defer func() { donner <- DONE }()
rules.Rules, rules.Error = (*server).Rules(timeout)
}
func getServerPlayers(server *goseq.Server, plys *MaybePlayers, timeout time.Duration, donner DoneChannel) {
defer func() { donner <- DONE }()
plys.Players, plys.Error = (*server).Players(timeout)
}