-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
191 lines (146 loc) · 3.98 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/0xAX/notificator"
"github.com/codegangsta/negroni"
"github.com/cratonica/trayhost"
)
var laddr *net.UDPAddr
var raddr *net.UDPAddr
var con *net.UDPConn
var servers = make(map[string]Server)
var arrServers []Server
var notify *notificator.Notificator
func main() {
// EnterLoop must be called on the OS's main thread
runtime.LockOSThread()
go func() {
trayhost.SetUrl("http://localhost:5050")
mux := http.NewServeMux()
mux.HandleFunc("/servers.json", handleServers)
n := negroni.Classic()
n.UseHandler(mux)
n.Run(":5050")
}()
addresses, _ := net.InterfaceAddrs()
currentAddr := addresses[0]
notify = notificator.New(notificator.Options{
DefaultIcon: "icon/cod.png",
AppName: "CoD4 Server Notifier",
})
laddr, _ = net.ResolveUDPAddr("udp", ":28961")
raddr, _ = net.ResolveUDPAddr("udp", "255.255.255.255:28960")
var errs error
con, errs = net.DialUDP("udp", laddr, raddr)
if errs != nil {
fmt.Println("Error dialing UDP:", errs)
return
}
udpAddress, err2 := net.ResolveUDPAddr("udp4", currentAddr.String()+":28961")
if err2 != nil {
fmt.Println("Error resolving UDP address:", err2)
return
}
ln, err := net.ListenUDP("udp", udpAddress)
if err != nil {
fmt.Println("There was a problem listening:", err)
return
}
go func() {
for {
time.Sleep(1000 * time.Millisecond)
var buf []byte = make([]byte, 500)
n, address, err := ln.ReadFromUDP(buf)
if err != nil {
fmt.Println("Error reading from connection:", err)
return
}
if address != nil && n > 0 {
parseMessage(address.String(), strings.TrimSpace(string(buf)))
}
}
}()
trayhost.EnterLoop("Call of Duty 4 Server Browser", IconData)
}
func handleServers(w http.ResponseWriter, req *http.Request) {
askForServer()
time.Sleep(1000 * time.Millisecond)
data, _ := json.Marshal(arrServers)
w.Write(data)
}
func askForServer() {
_, err := con.Write([]byte{0xff, 0xff, 0xff, 0xff, 0x67, 0x65, 0x74, 0x69, 0x6e, 0x66, 0x6f, 0x20, 0x78, 0x78, 0x78})
if err != nil {
fmt.Println("Error writing broadcast for servers:", err)
return
}
}
func parseMessage(ip string, msg string) {
// Ignore getInfo requests
if strings.Contains(msg, "getinfo xxx") {
return
}
if strings.Contains(msg, "infoResponse") {
parseServer(ip, msg)
}
}
func parseServer(ip string, msg string) {
split := strings.Split(msg, "\\")
server := Server{
Hostname: split[6],
Map: split[8],
GameType: split[12],
}
server.IP = ip
server.MaxPlayers, _ = strconv.ParseInt(split[10], 10, 64)
server.Pure = parseBool(split[14])
server.FriendlyFire = parseBool(split[16])
server.Hardcore = parseBool(split[18])
server.Something, _ = strconv.ParseInt(split[20], 10, 64)
server.Modded = parseBool(split[22])
server.Voice = parseBool(split[24])
server.Punkbuster = parseBool(split[26])
if _, ok := servers[server.Hostname]; !ok {
servers[server.Hostname] = server
dir, _ := os.Getwd()
notify.Push("New Server!", server.String(), dir+"/icon/cod.png")
}
}
type Server struct {
IP string
Hostname string
Map string
MaxPlayers int64
GameType string
Pure bool
FriendlyFire bool
Hardcore bool
Something int64
Modded bool
Voice bool
Punkbuster bool
}
func (s Server) String() (data string) {
data += "Hostname: " + s.Hostname + "\n"
data += "Map: " + s.Map + "\n"
data += "MaxPlayers: " + strconv.FormatInt(s.MaxPlayers, 10) + "\n"
data += "GameType: " + s.GameType + "\n"
data += "Pure: " + strconv.FormatBool(s.Pure) + "\n"
data += "FriendlyFire: " + strconv.FormatBool(s.FriendlyFire) + "\n"
data += "Hardcore: " + strconv.FormatBool(s.Hardcore) + "\n"
data += "Modded: " + strconv.FormatBool(s.Modded) + "\n"
data += "Voice: " + strconv.FormatBool(s.Voice) + "\n"
data += "Punkbuster: " + strconv.FormatBool(s.Punkbuster) + "\n"
return data
}
func parseBool(str string) bool {
return str == "1"
}