-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.py
142 lines (101 loc) · 3.84 KB
/
server.py
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
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
# server - Class for managing a server in rcontool
# Copyright (c) 2012 Gavin Langdon <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#------------------------------------------------------------------------------
import SourceLib
import socket
import pinggraph
def get_default_info():
return { 'ip' : '', 'port' : -1, 'hostname' : '???', 'numplayers' : 0, 'maxplayers' : 0, 'ping' : 1000, 'map' : 'Unknown', 'gamedesc' : 'Unknown'}
class Gameserver(object):
'''Stores data about a server and handles rcon and querying'''
def __init__(self, ip, port, rcon_visible=False, rcon_password=None, save_rcon=True):
self.ip = ip
self.port = port
self.rcon_visible = rcon_visible
self.rcon_password = rcon_password
self.rcon = None
self.logging = False
self.pings = []
self.info = get_default_info()
self.info['ip'] = ip
self.info['port'] = port
self.player = []
self.save_rcon = save_rcon
def __reduce__(self):
'''Magical pickle packer, creates a new "gameserver" instance with the following args'''
return Gameserver, (self.ip,self.port,self.rcon_visible, self.rcon_password if self.save_rcon else None, self.save_rcon)
def __key(self):
return (self.ip, self.port)
def __hash__(self):
'''Servers should be unique by IP'''
return self.__key().__hash__()
def __cmp__(self, other):
return self.__hash__() < other.__hash__()
def add_ping_history(self):
self.pings.append(self.info['ping'])
if len(self.pings) > pinggraph.PING_LIMIT:
self.pings.pop(0)
def rcon_cmd(self, command):
if not self.rcon_connected():
return False, "Password not set, ignoring"
try:
return True, self.rcon.rcon(command)
except SourceLib.SourceRcon.SourceRconError as e:
self.rcon = None
return False, str(e)
except socket.gaierror as e:
self.rcon = None
return False, str(e)
def query(self):
try:
q = SourceLib.SourceQuery.SourceQuery(self.ip, self.port)
info = q.info()
if info:
self.info.update(info)
else:
return False, "Failed to connect to the server"
player = q.player()
if len(player):
self.player = player
if 'ping' in self.info:
self.info['ping'] = int(self.info['ping'] * 1000)
else:
self.info['ping'] = 9001 # There's no way that can be right
return True, ""
except (SourceLib.SourceQuery.SourceQueryError, socket.gaierror) as e:
error = e
except socket.timeout as e:
error = "Request timed out"
except socket.error as e:
error = e
self.info['ping'] = 9001
return False, error
def cleanup(self):
if self.rcon_connected():
self.rcon.disconnect()
def set_rcon_visibility(self, enabled):
self.rcon_visible = enabled
def rcon_connected(self):
return self.rcon != None
def set_rcon(self, newpass):
if not newpass:
return
if self.rcon_password != newpass or not self.rcon:
self.rcon_password = newpass
self.rcon = SourceLib.SourceRcon.SourceRcon(self.ip, self.port, self.rcon_password)