-
Notifications
You must be signed in to change notification settings - Fork 9
/
sim.py
executable file
·230 lines (184 loc) · 8.18 KB
/
sim.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
"""
SIM Server Script
"""
import os
import sys
import ssl
import socket
import struct
import logging
import argparse
import base64
import time
from mobileatlas.simprovider.sim_provider import SimProvider
from mobileatlas.simprovider.tunnel.sim_tunnel import SimTunnel
from moatt_clients.provider_client import ProviderClient, register_sims, SIM
from moatt_clients.errors import AuthError
from moatt_clients.moat_management import register_provider, deregister_provider
from moatt_types.connect import Token, ConnectStatus, Imsi, Iccid, SimId, SimIndex
PORT_DEFAULT = 6666
def init_server(host, port, tls_ctx):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = (host, port)
logging.info('starting up on {} port {}'.format(*server_address))
s.bind(server_address)
s.listen(1)
except Exception as e:
s.close()
raise e
return tls_ctx.wrap_socket(s, server_side=True)
def accept_connection(s):
logging.info('waiting for a connection')
connection, client_address = s.accept()
logging.info('accept connection ' + str(client_address))
return connection
def provides_sim(sim_provider, req):
sim = get_sim(sim_provider, req.identifier)
return ConnectStatus.Success if sim is not None else ConnectStatus.NotFound
def get_sims(sim_provider):
return [SIM(id=i, iccid=x.iccid, imsi=x.imsi) for i, x in enumerate(sim_provider.get_sims())]
def get_sim(sim_provider, ident):
sims = sim_provider.get_sims()
if isinstance(ident, SimId):
if ident.id < len(sims):
return sims[ident.id]
elif isinstance(ident, SimIndex):
if ident.index < len(sims):
return sims[ident.index]
else:
sim = next((x for x in sims if Imsi(x.imsi) == ident or Iccid(x.iccid) == ident), None)
if sim is not None:
return sim
return None
def direct_connection(host, port, sim_provider, tls_ctx):
srv = init_server(host, port, tls_ctx)
while True:
connection = accept_connection(srv)
# First 8 Byte is the IMSI
requested_imsi = struct.unpack('!Q', connection.recv(8))[0]
device = next((x for x in sim_provider.get_sims() if x.imsi == str(requested_imsi)), None)
if device:
logging.info(f"requested imsi {requested_imsi} is on device {device.device_name}")
# Start SimTunnel for connection to serial device
tunnel = SimTunnel(connection, device.sl, device.iccid, direct_connection=True)
tunnel.start()
else:
logging.info(f"requested imsi {requested_imsi} is currently not connected to the system")
connection.unwrap()
connection.shutdown(socket.SHUT_RDWR)
connection.close()
def main():
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser(conflict_handler="resolve")
parser.add_argument('-h', '--host', required=True, help="SIM server address")
parser.add_argument('-p', '--port', type=int, default=PORT_DEFAULT, help="SIM server port (default: %(default)d)")
parser.add_argument('-b', '--bluetooth-mac', type=str, required=False,
help='MAC address of the bluetooth device (rSAP)')
parser.add_argument('--cafile', help='CA certificates used to verify SIM server certificate. \
(File of concatenated certificates in PEM format.)')
parser.add_argument('--capath', help='Path to find CA certificates used to verify SIM server \
certificate.')
subparsers = parser.add_subparsers(title='subcommands', required=True, dest='subcommand')
server_subcmd = 'server'
server_parser = subparsers.add_parser(server_subcmd)
direct_subcmd = 'direct'
direct_parser = subparsers.add_parser(direct_subcmd)
server_parser.add_argument('-a', '--api-url', required=True,
help="MobileAtlas-tunnel-server REST API URL")
server_parser.add_argument('--tls-server-name', help='SIM server name used in certificate \
verification. (defaults to the value of --host)')
direct_parser.add_argument('--cert', required=True, help='Server Certificate')
direct_parser.add_argument('--key', required=True, help='Server Certificate Key')
args = parser.parse_args()
if args.subcommand == direct_subcmd:
# TODO: implement client certificate verification
# SSLContext.wrap_socket does not support setting server_side=True and server_hostname
# sni_callback cannot be used to verify the peer cert because it is not available
# when the callback runs
sim_provider = SimProvider(args.bluetooth_mac)
tls_ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH, cafile=args.cafile, capath=args.capath)
tls_ctx.verify_mode = ssl.VerifyMode.CERT_REQUIRED
tls_ctx.load_cert_chain(args.cert, args.key)
direct_connection(args.host, args.port, sim_provider, tls_ctx)
return
env_token = os.environ.get("API_TOKEN")
if env_token is None:
logging.error("API_TOKEN environment variable is unset.")
return
try:
token = Token(base64.b64decode(env_token))
except:
logging.error("API_TOKEN environment variable contains a malformed API token.")
return
sim_provider = SimProvider(args.bluetooth_mac)
sims = get_sims(sim_provider)
try:
session_token = register_provider(args.api_url, token)
except:
logging.exception("Registration with management server failed.")
return
try:
sim_provider.set_device_change_callback(lambda: register_sims(args.api_url, session_token, get_sims(sim_provider)))
try:
register_sims(args.api_url, session_token, sims)
except:
logging.exception("SIM card registration failed.")
return
tls_ctx = ssl.create_default_context(cafile=args.cafile, capath=args.capath)
if args.tls_server_name is None:
server_hostname = args.host
else:
server_hostname = args.tls_server_name
srv = ProviderClient(
session_token,
args.host,
args.port,
lambda x: provides_sim(sim_provider, x),
tls_ctx=tls_ctx,
server_hostname=server_hostname,
)
failed_connections = 0
while True:
id_connection = None
try:
id_connection = srv.wait_for_connection()
except AuthError as e:
logging.error(f"Authentication with SIM tunnel failed: {e}\nStopping...")
return
except Exception as e:
logging.warn(f"Error while establishing connection: {e}")
failed_connections += 1
if failed_connections > 10:
logging.error("Multiple consecutive connection attempts failed. Stopping...")
return
time.sleep(1)
continue
failed_connections = 0
requested_sim = id_connection[0]
connection = id_connection[1]
sim_info = get_sim(sim_provider, requested_sim)
if sim_info is not None:
logging.info(f"requested imsi {requested_sim} is on device {sim_info.device_name}")
# Start SimTunnel for connection to serial device
#tunnel = SimTunnel(connection, SerialSimLink(device))
#tunnel = SimTunnel(connection, BluetoothSapSimLink("80:5A:04:0E:90:F6"))
#tunnel = SimTunnel(connection, ModemATCommandLink("/dev/ttyACM0"))
tunnel = SimTunnel(connection, sim_info.sl, sim_info.iccid)
tunnel.start()
else:
logging.info(f"requested imsi {requested_sim} is currently not connected to the system")
connection.close()
finally:
deregister_provider(args.api_url, session_token)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt as keyboard_interrupt:
print('Interrupted')
try:
sys.exit(1)
except SystemExit as system_exit:
os._exit(system_exit.code)