-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
243 lines (217 loc) · 7.38 KB
/
client.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
package dota2
import (
"encoding/base64"
"fmt"
"log"
"sync"
"time"
"github.com/pkg/errors"
"github.com/vvekic/go-steam"
"github.com/vvekic/go-steam/dota/protocol/protobuf"
"github.com/vvekic/go-steam/protocol"
"github.com/vvekic/go-steam/protocol/gamecoordinator"
"github.com/vvekic/go-steam/protocol/steamlang"
)
const AppId = 570
var readyTimeout time.Duration = time.Second * 30
func init() {
if err := steam.InitializeSteamDirectory(91); err != nil {
log.Printf("error initializing Steam Directory, using built-in server list")
}
}
// To use any methods of this, you'll need to SetPlaying(true) and wait for
// the GCReadyEvent.
type Client struct {
Id int
helloTicker *time.Ticker
sc *steam.Client // Steam client, of course!
readyLock sync.Mutex
readyChan chan struct{}
quitChan chan struct{}
gcReady bool // Used internally to prevent sending GC reqs when we don't have a GC connection
Creds *steam.LogOnDetails
connected bool
Timeouts int
jobs map[protocol.JobId]chan *gamecoordinator.GCPacket // Set of channels. Used to sync up go-steam's event-based GC calls.
lastJobID protocol.JobId // Last job ID. We will increment this for each job we create
jobsLock sync.RWMutex
}
// Creates a new Dota2 instance and registers it as a packet handler
func NewClient() *Client {
c := &Client{
sc: steam.NewClient(),
readyChan: make(chan struct{}),
quitChan: make(chan struct{}),
gcReady: false,
jobs: make(map[protocol.JobId]chan *gamecoordinator.GCPacket),
connected: false,
}
c.sc.GC.RegisterPacketHandler(c)
go c.loop()
return c
}
// Handles all GC packets that come from Steam and routes them to their relevant handlers.
func (c *Client) HandleGCPacket(packet *gamecoordinator.GCPacket) {
if packet.AppId != AppId {
return
}
// If we have a handler channel for this, pipe the GC packet straight there,
// otherwise use our own routing.
c.jobsLock.RLock()
handlerChan, ok := c.jobs[packet.TargetJobId]
c.jobsLock.RUnlock()
if ok {
handlerChan <- packet
} else {
// All key types are derived from int32, so cast to int32 to allow us to use a single switch for all types.
switch int32(packet.MsgType) {
case int32(protobuf.EGCBaseClientMsg_k_EMsgGCClientWelcome):
// log.Printf("Client %d Received ClientWelcome", c.Id)
c.handleWelcome(packet)
case int32(protobuf.EGCBaseClientMsg_k_EMsgGCClientConnectionStatus):
c.handleConnectionStatus(packet)
case int32(protobuf.EDOTAGCMsg_k_EMsgDOTAGetEventPointsResponse):
c.handleGetEventPointsResponse(packet)
case int32(protobuf.ESOMsg_k_ESOMsg_CacheSubscribed):
c.handleCacheSubscribed(packet)
default:
log.Printf("client %s recieved GC message without a handler, type %d", c.Creds.Username, packet.MsgType)
}
}
}
// SetPlaying tells Steam we're playing Dota 2
func (c *Client) setPlaying(playing bool) {
if playing {
// log.Printf("Setting GamesPlayed to Dota 2")
c.sc.GC.SetGamesPlayed(AppId)
} else {
// log.Print("Setting GamesPlayed to nil")
c.sc.GC.SetGamesPlayed()
}
}
// sendHello sends ClientHello to request a connection to the Dota 2 GC
// Continually send "Hello" to the Dota 2 GC to initialize a connection. Will send hello every 5 seconds until the GC responds with "Welcome"
func (c *Client) sendHello() {
// Send ClientHello every 5 seconds. This ticker will be stopped when we get ClientWelcome from the GC
c.helloTicker = time.NewTicker(5 * time.Second)
go func() {
for range c.helloTicker.C {
// log.Printf("Client %d Sending ClientHello", c.Id)
c.sc.GC.Write(gamecoordinator.NewGCMsgProtobuf(
AppId,
uint32(protobuf.EGCBaseClientMsg_k_EMsgGCClientHello),
&protobuf.CMsgClientHello{
// set engine to Source 2
Engine: protobuf.ESourceEngine_k_ESE_Source2.Enum(),
}))
}
}()
}
func (c *Client) ConnectWithCreds(creds *steam.LogOnDetails) error {
if err := c.sc.Connect(); err != nil {
return errors.Wrap(err, "error connecting to Steam server")
}
select {
case <-c.readyChan:
return nil
case <-time.After(readyTimeout):
return fmt.Errorf("timeout waiting for GC to become ready")
case <-c.quitChan:
return fmt.Errorf("client disconnected")
}
}
func (c *Client) Connect(username, password, sentry, authCode string) error {
// log.Printf("client %d connecting(%s, %s, %s, %s)", c.Id, username, password, sentry, authCode)
// Check for credentials
c.Creds = &steam.LogOnDetails{
Username: username,
Password: password,
AuthCode: authCode,
}
if sentry != "" {
decodedSentry, err := base64.StdEncoding.DecodeString(sentry)
if err != nil {
log.Printf("error decoding sentry")
} else {
c.Creds.SentryFileHash = decodedSentry
}
}
if c.Creds.Username == "" {
return fmt.Errorf("username not set")
}
if c.Creds.Password == "" {
return fmt.Errorf("password not set")
}
return c.ConnectWithCreds(c.Creds)
}
func (c *Client) reconnect() {
log.Printf("reconnecting Steam account %s", c.Creds.Username)
if err := c.ConnectWithCreds(c.Creds); err != nil {
c.sc.Emit(&steam.DisconnectedEvent{})
}
}
func (c *Client) Disconnect() {
c.sc.Disconnect()
}
func (c *Client) Close() {
c.sc.DisconnectNoEvent()
}
func (c *Client) loop() {
for event := range c.sc.Events() {
switch e := event.(type) {
// Steam events
case *steam.ConnectedEvent:
// log.Printf("Client %d Connected to Steam", c.Id)
c.onSteamConnected()
case *steam.MachineAuthUpdateEvent:
log.Printf("Client %s Received new sentry: %s", c.Creds.Username, base64.StdEncoding.EncodeToString(e.Hash))
case *steam.LoggedOnEvent:
// log.Printf("Client %d Logged on to Steam", c.Id)
c.onSteamLogon()
case *steam.LogOnFailedEvent:
log.Printf("Client %s Log on failed, result: %s", c.Creds.Username, e.Result)
c.sc.DisconnectNoEvent()
case *steam.LoggedOffEvent:
log.Printf("Client %s Logged off, result: %s", c.Creds.Username, e.Result)
if e.Result == steamlang.EResult_LogonSessionReplaced {
c.Disconnect()
}
case *steam.DisconnectedEvent:
log.Printf("Client %s Disconnected from Steam.", c.Creds.Username)
c.gcReady = false
c.reconnect()
case *steam.AccountInfoEvent:
// log.Printf("Client %d Account name: %s, Country: %s, Authorized machines: %d, Flags: %s", c.Id, e.PersonaName, e.Country, e.CountAuthedComputers, e.AccountFlags)
case *steam.LoginKeyEvent:
// log.Printf("Client %d Login Key: %s", c.Id, e.LoginKey)
case *steam.WebSessionIdEvent, *steam.PersonaStateEvent, *steam.FriendsListEvent:
// mute
case *steam.ClientCMListEvent:
// log.Printf("Client %d received CM list (%d servers)", c.Id, len(e.Addresses))
if e.Addresses != nil && len(e.Addresses) > 0 {
steam.UpdateSteamDirectory(e.Addresses)
}
// custom events
case *GCReadyEvent:
c.readyChan <- struct{}{}
// log.Printf("Client %d Dota 2 Game Coordinator ready!", c.Id)
// errors
case steam.FatalErrorEvent, error:
log.Printf("Client %s error: %v", c.Creds.Username, e)
default:
log.Printf("Client %s unknown steam event: %#v", c.Creds.Username, e)
}
}
}
func (c *Client) onSteamConnected() {
// log.Printf("Client %d Logging on as %s", c.Id, c.Creds.Username)
c.sc.Auth.LogOn(c.Creds)
}
func (c *Client) onSteamLogon() {
// log.Printf("Client %d Setting social status to Offline", c.Id)
// Set steam social status to 'offline')
c.sc.Social.SetPersonaState(steamlang.EPersonaState_Offline)
// Launch Dota 2
c.setPlaying(true)
c.sendHello()
}