Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/server/StarServerQueryThread.cpp
blob: 441fa6332dd8898022f9cb4bcc9b3e5b174baf17 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "StarServerQueryThread.hpp"
#include "StarLogging.hpp"
#include "StarRoot.hpp"
#include "StarConfiguration.hpp"
#include "StarVersion.hpp"
#include "StarUniverseServer.hpp"
#include "StarIterator.hpp"

namespace Star {

ServerQueryThread::ServerQueryThread(UniverseServer* universe, HostAddressWithPort const& bindAddress)
  : Thread("QueryServer"),
    m_universe(universe),
    m_queryServer(bindAddress),
    m_stop(true),
    m_lastChallengeCheck(Time::monotonicMilliseconds()) {
  m_playersResponse.resize(A2S_PACKET_SIZE);
  m_playersResponse.setByteOrder(ByteOrder::LittleEndian);
  m_playersResponse.setNullTerminatedStrings(true);

  m_rulesResponse.resize(A2S_PACKET_SIZE);
  m_rulesResponse.setByteOrder(ByteOrder::LittleEndian);
  m_rulesResponse.setNullTerminatedStrings(true);

  m_generalResponse.resize(A2S_PACKET_SIZE);
  m_generalResponse.setByteOrder(ByteOrder::LittleEndian);
  m_generalResponse.setNullTerminatedStrings(true);

  m_serverPort = 0;
  m_lastActiveTime = 0;

  auto& root = Root::singleton();
  auto cfg = root.configuration();

  m_maxPlayers = cfg->get("maxPlayers").toUInt();
  m_serverName = cfg->get("serverName").toString();

  m_lastPlayersResponse = 0;
  m_lastRulesResponse = 0;
}

ServerQueryThread::~ServerQueryThread() {
  stop();
  join();
}

void ServerQueryThread::start() {
  m_stop = false;
  Thread::start();
  m_lastActiveTime = Time::monotonicMilliseconds();
}

void ServerQueryThread::stop() {
  m_stop = true;
  m_queryServer.close();
}

void ServerQueryThread::sendTo(HostAddressWithPort const& address, DataStreamBuffer* ds) {
  m_queryServer.send(address, ds->ptr(), ds->size());
}

uint8_t ServerQueryThread::serverPlayerCount() {
  return m_universe->numberOfClients();
}

bool ServerQueryThread::serverPassworded() {
  // TODO: implement
  return false;
}

String ServerQueryThread::serverWorldNames() {
  auto activeWorlds = m_universe->activeWorlds();
  if (activeWorlds.empty())
    return String("Unknown");

  return StringList(activeWorlds.transformed(printWorldId)).join(",");
}

const char* ServerQueryThread::serverPlugins() {
  // TODO: implement
  return "none";
}

bool ServerQueryThread::processPacket(HostAddressWithPort const& address, char const* data, size_t length) {
  uint8_t* buf = (uint8_t*)data;
  if (length < 5 || buf[0] != 0xff || buf[1] != 0xff || buf[2] != 0xff || buf[3] != 0xff) {
    // short packet or missing header
    return false;
  }

  // Process packet
  switch (buf[4]) {
    case A2S_INFO_REQUEST: {
      // We use -6 and not -5 as the string should be NULL terminated
      // but instead of the std::string constructor stopping at the NULL
      // it includes it :(
      std::string str((const char*)(buf + 5), length - 6);
      if (str.compare(A2S_INFO_REQUEST_STRING) != 0) {
        // Invalid request
        return false;
      }

      m_generalResponse.clear();
      m_generalResponse << A2S_HEAD_INT << A2S_INFO_REPLY << A2S_VERSION << m_serverName << serverWorldNames()
                        << GAME_DIR << GAME_DESC << A2S_APPID // Should be SteamAppId but this isn't a short :(
                        << serverPlayerCount() << m_maxPlayers << (uint8_t)0x00 // bots
                        << A2S_TYPE_DEDICATED // dedicated
#ifdef STAR_SYSTEM_FAMILY_WINDOWS
                        << A2S_ENV_WINDOWS // os
#elif defined(STAR_SYSTEM_MACOS)
                        << A2S_ENV_MAC // os
#else
                        << A2S_ENV_LINUX // os
#endif
                        << serverPassworded() << A2S_VAC_OFF // secure
                        << StarVersionString << A2S_EDF_PORT // EDF
                        << m_serverPort;

      sendTo(address, &m_generalResponse);
      return true;
    }
    case A2S_CHALLENGE_REQUEST:
      sendChallenge(address);
      return true;

    case A2S_PLAYER_REQUEST:
      if (challengeRequest(address, data, length))
        return true;

      if (!validChallenge(address, data, length))
        return false;

      buildPlayerResponse();
      sendTo(address, &m_playersResponse);
      return true;

    case A2S_RULES_REQUEST:
      if (challengeRequest(address, data, length))
        return true;

      if (!validChallenge(address, data, length))
        return false;

      buildRuleResponse();
      sendTo(address, &m_rulesResponse);
      return true;
  }

  return false;
}

void ServerQueryThread::buildPlayerResponse() {
  int64_t now = Time::monotonicMilliseconds();
  if (now < m_lastPlayersResponse + responseCacheTime) {
    return;
  }

  auto clientIds = m_universe->clientIdsAndCreationTime();
  uint8_t cnt = (uint8_t)clientIds.count();
  int32_t kills = 0; // Not currently supported

  m_playersResponse.clear();
  m_playersResponse << A2S_HEAD_INT << A2S_PLAYER_REPLY << cnt;

  uint8_t i = 0;
  for (auto& pair : clientIds) {
    auto timeConnected = float(now - pair.second) / 1000.f;
    m_playersResponse << i++ << m_universe->clientNick(pair.first) << kills << timeConnected;
  }

  m_lastPlayersResponse = now;
}

void ServerQueryThread::buildRuleResponse() {
  int64_t now = Time::monotonicMilliseconds();
  if (now < m_lastRulesResponse + responseCacheTime) {
    return;
  }

  uint16_t cnt = 1;
  m_rulesResponse.clear();
  m_rulesResponse << A2S_HEAD_INT << A2S_RULES_REPLY << cnt << "plugins" << serverPlugins();

  m_lastRulesResponse = now;
}

void ServerQueryThread::sendChallenge(HostAddressWithPort const& address) {
  auto challenge = make_shared<RequestChallenge>();

  m_validChallenges[address.address()] = challenge;
  m_generalResponse.clear();
  m_generalResponse << A2S_HEAD_INT << A2S_CHALLENGE_RESPONSE << challenge->getChallenge();

  sendTo(address, &m_generalResponse);
}

void ServerQueryThread::pruneChallenges() {
  int64_t now = Time::monotonicMilliseconds();
  if (now < m_lastChallengeCheck + challengeCheckInterval) {
    return;
  }

  auto expire = now - challengeCheckInterval;
  auto it = makeSMutableMapIterator(m_validChallenges);
  while (it.hasNext()) {
    auto const& pair = it.next();
    if (pair.second->before(expire)) {
      it.remove();
    }
  }
  m_lastChallengeCheck = now;
}

void ServerQueryThread::run() {
  HostAddressWithPort udpAddress;
  char udpData[MaxUdpData];
  while (!m_stop) {
    try {
      auto len = m_queryServer.receive(&udpAddress, udpData, MaxUdpData, 100);
      pruneChallenges();
      if (len != 0)
        processPacket(udpAddress, udpData, len);
    } catch (SocketClosedException const&) {
    } catch (std::exception const& e) {
      Logger::error("ServerQueryThread exception caught: {}", outputException(e, true));
    }
  }
}

ServerQueryThread::RequestChallenge::RequestChallenge()
  : m_time(Time::monotonicMilliseconds()), m_challenge(Random::randi32()) {}

bool ServerQueryThread::RequestChallenge::before(uint64_t time) {
  return m_time < time;
}

int ServerQueryThread::RequestChallenge::getChallenge() {
  return m_challenge;
}

bool ServerQueryThread::validChallenge(HostAddressWithPort const& address, char const* data, size_t len) {
  if (len != 9) {
    // too much or too little data
    return false;
  }

  if (m_validChallenges.count(address.address()) == 0) {
    // Don't know this source address ignore
    return false;
  }

  uint8_t const* b = (uint8_t const*)data;
  int32_t challenge = ((int32_t)b[8] & 0xff) << 24 | ((int32_t)b[7] & 0xff) << 16 | ((int32_t)b[6] & 0xff) << 8
      | ((int32_t)b[5] & 0xff);
  // Note: No byte order swapping needed as protcol performs no conversion
  if (m_validChallenges.get(address.address())->getChallenge() != challenge) {
    // Challenges didnt match ignore
    return false;
  }

  // All good
  return true;
}

bool ServerQueryThread::challengeRequest(HostAddressWithPort const& address, char const* data, size_t len) {
  if (len != 9) {
    // too much or too little data
    return false;
  }

  uint8_t const* buf = (uint8_t const*)data;
  if ((buf[5] == 0xff) && (buf[6] == 0xff) && (buf[7] == 0xff) && (buf[8] == 0xff)) {
    sendChallenge(address);
    return true;
  }

  return false;
}

}