-
Notifications
You must be signed in to change notification settings - Fork 1
/
sourcetvRelayServer.py
144 lines (120 loc) · 3.42 KB
/
sourcetvRelayServer.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
141
142
143
144
#!/usr/bin/python
import time
import sqlite3
import subprocess
DATABASE_FILE = 'relays.db'
def setupDatabase():
conn = sqlite3.connect(DATABASE_FILE)
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS relays (
id integer primary key,
sourceTvIP text,
sourceTvPort integer,
sourceTvPassword text,
localPort integer,
startTime integer,
active integer,
processId integer
) """)
conn.commit()
"""
Get a relay server by its ID
Returns the relay server, or None if no server found with the given ID
"""
def getRelayById(relayId):
conn = sqlite3.connect(DATABASE_FILE)
c = conn.cursor()
c.execute("""
SELECT id, sourceTvIP, sourceTvPort, sourceTvPassword, localPort, startTime, active, processId
FROM relays
WHERE id = ?
""", (relayId, ))
row = c.fetchone()
if row is None:
return None
return {
"id": row[0],
"sourceTvIP": row[1],
"sourceTvPort": row[2],
"sourceTvPassword": row[3],
"localPort": row[4],
"startTime": row[5],
"active": row[6],
"processId": row[7]
}
def getActiveRelayServers():
conn = sqlite3.connect(DATABASE_FILE)
c = conn.cursor()
c.execute("""
SELECT id, sourceTvIP, sourceTvPort, sourceTvPassword, localPort, startTime, active, processId
FROM relays
WHERE active = 1
""")
relays = []
for row in c:
relay = {
"id": row[0],
"sourceTvIP": row[1],
"sourceTvPort": row[2],
"sourceTvPassword": row[3],
"localPort": row[4],
"startTime": row[5],
"active": row[6],
"processId": row[7]
}
relays.append(relay)
return relays
"""
Starts a STV relay server.
Returns the server details, or False in case of error
"""
def startRelayServer(sourceTvIP, sourceTvPort, sourceTvPassword):
#TODO: actually start a STV relay
if sourceTvPort != 12345:
return False
localPort = getLocalPort()
startTime = int(time.time())
pid = subprocess.Popen(["nohup", "/Users/davidh/Documents/tf2center/relays/srcds_run", "-game", "tf", "-console",
"+tv_enable", "\"1\"", "+tv_port", "%s" % (localPort), "+tv_relay",
"%s:%s" % (sourceTvIP, sourceTvPort)]).pid
conn = sqlite3.connect(DATABASE_FILE)
c = conn.cursor()
c.execute("""
INSERT INTO relays (sourceTvIP, sourceTvPort, sourceTvPassword, localPort, startTime, active, processId)
VALUES (?, ?, ?, ?, ?, 1, ?)
""",
(
sourceTvIP,
sourceTvPort,
sourceTvPassword,
localPort,
startTime,
pid
))
relayId = c.lastrowid
conn.commit()
print relayId
return getRelayById(relayId)
"""
Get a free local port to use.
Returns the port, or None if no available ports found
"""
def getLocalPort():
return 27025
"""
Stops a relay server
Returns True if stopped, or False if an error occurred
"""
def stopRelayServer(relayServer):
#TODO actually stop the STV relay
conn = sqlite3.connect(DATABASE_FILE)
c = conn.cursor()
c.execute("""
UPDATE relays
SET active=0, processId=NULL
WHERE id = ?
""", (relayServer['id'],))
conn.commit()
return True
def init():
setupDatabase()