forked from Philipp15b/go-steam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
steam_directory.go
109 lines (90 loc) · 2.72 KB
/
steam_directory.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
package steam
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/Flo4604/go-steam/v5/netutil"
)
// Load initial server list from Steam Directory Web API.
// Call InitializeSteamDirectory() before Connect() to use
// steam directory server list instead of static one.
func InitializeSteamDirectory() error {
return steamDirectoryCache.Initialize()
}
var steamDirectoryCache *steamDirectory = &steamDirectory{}
type steamDirectory struct {
sync.RWMutex
servers []Server
isInitialized bool
}
type Server struct {
Endpoint string `json:"endpoint"`
LegacyEndpoint string `json:"legacy_endpoint"`
Type string `json:"type"`
Dc string `json:"dc"`
Realm string `json:"realm"`
Load int `json:"load"`
WtdLoad float64 `json:"wtd_load"`
}
type ServerListResponse struct {
Response struct {
ServerList []Server `json:"serverlist"`
Success bool `json:"success"`
Message string `json:"message"`
} `json:"response"`
}
// Get server list from steam directory and save it for later
func (sd *steamDirectory) Initialize() error {
sd.Lock()
defer sd.Unlock()
client := new(http.Client)
resp, err := client.Get("https://api.steampowered.com/ISteamDirectory/GetCMListForConnect/v0001/?cellid=0")
if err != nil {
return err
}
defer resp.Body.Close()
r := ServerListResponse{}
if err = json.NewDecoder(resp.Body).Decode(&r); err != nil {
return err
}
if !r.Response.Success {
return fmt.Errorf("failed to get steam directory, result: %v, message: %v\n", r.Response.Success, r.Response.Message)
}
if len(r.Response.ServerList) == 0 {
return fmt.Errorf("steam returned zero servers for steam directory request\n")
}
// filter out servers that are not in realm == 'steamglobal' and not netfilter
var filteredServers []Server
for _, server := range r.Response.ServerList {
if server.Realm == "steamglobal" && server.Type == "netfilter" {
filteredServers = append(filteredServers, server)
}
}
sd.servers = filteredServers
sd.isInitialized = true
return nil
}
func (sd *steamDirectory) GetRandomCM() *netutil.PortAddr {
sd.RLock()
defer sd.RUnlock()
if !sd.isInitialized {
panic("steam directory is not initialized")
}
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
randomServer := sd.servers[rng.Int31n(int32(len(sd.servers)))].Endpoint
split := strings.Split(randomServer, ":")
ip, _ := net.DefaultResolver.LookupIPAddr(context.Background(), split[0])
return netutil.ParsePortAddr(fmt.Sprintf("%s:%s", ip[0].String(), split[1]))
}
func (sd *steamDirectory) IsInitialized() bool {
sd.RLock()
defer sd.RUnlock()
isInitialized := sd.isInitialized
return isInitialized
}