This repository was archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathserver.py
206 lines (171 loc) · 5.2 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
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# GR-GSM based TMSI sniffer
#
# Research purposes only
# Use at your own risk
#
# Copyright (C) 2016 Vadim Yanitskiy <[email protected]>
#
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import getopt
import signal
from lib.network import *
from lib.log import *
class CommandLine:
def handle_rx_event(self):
cmd = sys.stdin.readline()
self.handle_cmd(cmd)
def write(self, data):
sys.stdout.write(data)
sys.stdout.flush()
def print_help(self):
self.write("\n")
self.write(" Control interface help\n")
self.write(" ======================\n\n")
self.write(" help this text\n")
self.write(" clear clear screen\n")
self.write(" exit shutdown server\n")
self.write("\n")
self.write(" rxtune tunes slaves to a given frequency in kHz\n")
self.write(" paging TMSI mapping\n")
self.write(" | start start recording\n")
self.write(" | stop stop recording\n")
self.write(" | cross show results\n")
self.write(" | flush reset all recordings\n")
self.write("\n")
def print_unknown(self):
self.write("Unknown command, see help.\n")
def print_prompt(self):
self.write("CTRL# ")
def handle_cmd(self, request):
# Strip spaces and \0
request = request.strip().strip("\0")
# Split into a command and arguments
request = request.split(" ")
argv = request[1:]
argc = len(argv)
cmd = request[0]
# Application specific
if cmd == "exit":
app.shutdown()
elif cmd == "help":
self.print_help()
elif cmd == "clear":
os.system("clear")
# Server specific
elif cmd == "rxtune" and argc == 1:
app.server.broadcast("CMD RXTUNE %s\n" % argv[0])
elif cmd == "paging":
if argc == 1:
subcmd = argv[0]
if subcmd == "start":
app.server.broadcast("CMD START\n")
elif subcmd == "stop":
app.server.broadcast("CMD STOP\n")
elif subcmd == "cross":
app.server.broadcast("CMD CROSS\n")
elif subcmd == "flush":
app.server.broadcast("CMD FLUSH\n")
# Unknown command
elif cmd != "":
self.print_unknown()
class Server(TCPServer):
def __init__(self):
TCPServer.__init__(self)
def handle_close_event(self):
printl(DCTL, DINFO, "A slave node disconnected")
def handle_rx_data(self, data):
printl(DCTL, DINFO, "Some slave says:")
app.ctrl.write(data)
class Application:
# Application variables
listen_port = 8888
def __init__(self):
self.print_copyright()
self.parse_argv()
# Set up signal handlers
signal.signal(signal.SIGINT, self.sig_handler)
def run(self):
self.ctrl = CommandLine()
self.server = Server()
self.server.listen(self.listen_port)
printl(DAPP, DINFO, "Init complete")
self.ctrl.print_help()
# Enter main loop
while True:
# Keep working
self.loop()
def loop(self):
# Provide prompt
# TODO: How to save a previous command?
self.ctrl.print_prompt()
# Blocking select
r_list = [sys.stdin, self.server.sock] + self.server.connections
r_event, w_event, x_event = select.select(r_list, [], [])
# Check for incoming CTRL commands
if sys.stdin in r_event:
self.ctrl.handle_rx_event()
# Check for incoming data
elif self.server.sock in r_event:
self.server.accept()
# Maybe something from slaves?
else:
self.server.handle_rx_event(r_event)
def shutdown(self):
printl(DAPP, DINFO, "Shutting down...")
self.server.close()
sys.exit(0)
def print_copyright(self):
s = "Copyright (C) 2016 by Vadim Yanitskiy <[email protected]>\n" \
"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n" \
"This is free software: you are free to change and redistribute it.\n" \
"There is NO WARRANTY, to the extent permitted by law.\n"
print s
def print_help(self):
s = " Usage: " + sys.argv[0] + " [options]\n\n" \
" Some help...\n" \
" -h --help this text\n" \
" -p --port Specify a port to listen (default 8888)\n"
print s
def parse_argv(self):
try:
opts, args = getopt.getopt(sys.argv[1:],
"p:h", ["help", "port="])
except getopt.GetoptError as err:
# Print help and exit
self.print_help()
print "[!] " + str(err)
sys.exit(2)
for o, v in opts:
if o in ("-h", "--help"):
self.print_help()
sys.exit(2)
elif o in ("-p", "--port"):
if int(v) >= 0 and int(v) <= 65535:
self.listen_port = int(v)
else:
print "[!] Port number should be in range [0-65536]"
sys.exit(2)
def sig_handler(self, signum, frame):
print "Signal %d received" % signum
if signum is signal.SIGINT:
self.shutdown()
if __name__ == '__main__':
app = Application()
app.run()