-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmtracker.go
More file actions
executable file
·183 lines (152 loc) · 4.06 KB
/
gmtracker.go
File metadata and controls
executable file
·183 lines (152 loc) · 4.06 KB
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
/*
gmtracker - the garry's mod 12 server browser
Copyright (C) 2024 patapancakes <patapancakes@pagefault.games>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net"
"net/http"
"net/url"
"os"
"time"
)
var (
cache Cache
key string
tmpl = template.Must(template.New("list.html").Funcs(template.FuncMap{"region": region, "platform": platform}).ParseFiles("templates/list.html"))
)
type Cache struct {
LastUpdate time.Time
Servers []ServerInfo
}
type GetServerListResponse struct {
Response struct {
Servers []ServerInfo `json:"servers"`
} `json:"response"`
}
type ServerInfo struct {
Address string `json:"addr"`
GamePort int `json:"gameport"`
SteamID string `json:"steamid"`
Name string `json:"name"`
AppID int `json:"appid"`
GameDirectory string `json:"gamedir"`
Version string `json:"version"`
Product string `json:"product"`
Region int `json:"region"`
Players int `json:"players"`
MaxPlayers int `json:"max_players"`
Bots int `json:"bots"`
Map string `json:"map"`
Secure bool `json:"secure"`
Dedicated bool `json:"dedicated"`
OS string `json:"os"`
GameType string `json:"gametype"`
}
func main() {
apikey := flag.String("apikey", "", "steam web api key")
proto := flag.String("proto", "tcp", "proto for web server")
addr := flag.String("addr", "127.0.0.1:80", "address for web server")
flag.Parse()
if *apikey == "" {
log.Fatal("an api key is required! get one at https://steamcommunity.com/dev/apikey")
}
key = *apikey
http.HandleFunc("/", handle)
if *proto == "unix" {
err := os.Remove(*addr)
if err != nil && !os.IsNotExist(err) {
log.Fatalf("failed to delete unix socket: %s", err)
}
}
l, err := net.Listen(*proto, *addr)
if err != nil {
log.Fatalf("failed to create web server listener: %s", err)
}
defer l.Close()
if *proto == "unix" {
err = os.Chmod(*addr, 0777)
if err != nil {
log.Fatalf("failed to set unix socket permissions: %s", err)
}
}
http.Serve(l, nil)
}
func handle(w http.ResponseWriter, r *http.Request) {
si, err := update()
if err != nil {
log.Printf("update failed: %s", err)
}
err = tmpl.Execute(w, si)
if err != nil {
log.Printf("response generation failed: %s", err)
http.Error(w, "failed to generate response!", http.StatusInternalServerError)
}
}
func update() (Cache, error) {
if time.Now().Before(cache.LastUpdate.Add(time.Minute)) {
return cache, nil
}
v := make(url.Values)
v.Add("filter", `\appid\4000\version_match\1.*`)
v.Add("key", key)
r, err := http.Get(fmt.Sprintf("https://api.steampowered.com/IGameServersService/GetServerList/v1/?%s", v.Encode()))
if err != nil {
return cache, err
}
var response GetServerListResponse
err = json.NewDecoder(r.Body).Decode(&response)
if err != nil {
return cache, err
}
cache.LastUpdate = time.Now()
cache.Servers = response.Response.Servers
return cache, nil
}
func region(region int) string {
switch region {
case 0:
return "US - East"
case 1:
return "US - West"
case 2:
return "South America"
case 3:
return "Europe"
case 4:
return "Asia"
case 5:
return "Australia"
case 6:
return "Middle East"
case 7:
return "Africa"
}
return "World"
}
func platform(platform string) string {
switch platform {
case "w":
return "Windows"
case "m":
return "Mac"
case "l":
return "Linux"
}
return "Other"
}