-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·148 lines (110 loc) · 3.89 KB
/
server.py
File metadata and controls
executable file
·148 lines (110 loc) · 3.89 KB
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
#!/usr/bin/env python3
import socket
import sys
import os
import atexit
import logging
import pickle
import numpy as np
from authenticator import Authenticator
TRAIN_RUNS = [6, 10]
AUTH_RUNS = [14]
NUM_SUBJECTS = 35
def train_model(auth):
'''
Trains the model on the established EEG data.
Normally this is where data from a DB or scans would be read in and
processed but we don't have that so we're pulling from the eegbci dataset.
Parameters:
auth (Authenticator): The Authenticator object used for authenticating users
'''
subjects = list(range(1, NUM_SUBJECTS + 1))
auth.train(subjects, TRAIN_RUNS)
def authenticate(auth, user):
'''
Authenticates a user with the Authenticator object. Because we're pulling
data from the mne library, it's easier to get the data from the
Authenticator object.
Parameters:
auth (Authenticator): The authenticator object used to authenticate users
user (int): The ID number of the user being authenticated
Returns:
bool: Whether or not the user was successfully authenticated
'''
_, data, _ = auth.get_user_data(user, AUTH_RUNS, dict(T0=user, T1=user, T2=user))
labels = auth.authenticate(user, data)
unique, counts = np.unique(labels, return_counts=True)
label_counts = dict(zip(unique, counts))
confidence = float(label_counts[user]) / float(len(labels))
if confidence >= 0.75:
return True
else:
return False
def cleanup(auth, save_path):
'''
Cleanup function to be run when the script exits
'''
logging.debug('Saving Authenticator to {}'.format(save_path))
with open(save_path, 'wb') as save_file:
pickle.dump(auth, save_file)
def main():
if not os.getenv('SAVE_DIR'):
save_dir = '/var/local/eeg/'
else:
save_dir = os.getenv('SAVE_DIR')
save_path = os.path.join(save_dir, 'auth.bin')
if not os.getenv('SOCK_PATH'):
sock_path = '/tmp/eeg.sock'
else:
sock_path = os.getenv('SOCK_PATH')
# Restore or create the Authenticator object
if os.path.exists(save_path):
if not os.path.exists(save_dir):
logging.debug('Creating {}'.format(save_dir))
try:
os.mkdir(save_dir)
except OSError:
logging.error('Creation of directory failed')
sys.exit(1)
# Restore from file
logging.info('Saved authenticator found at {}'.format(save_path))
auth = pickle.load(open(save_path, 'rb'))
else:
# If no saved file is found, create a new object and train the model
logging.info('Saved authenticator not found, creating from scratch')
auth = Authenticator()
train_model(auth)
# Run the cleanup function to save the Authenticator object on exit
atexit.register(cleanup, auth, save_path)
# Create the unix socket
try:
os.unlink(sock_path)
except OSError:
if os.path.exists(sock_path):
logging.error('Error removing socket')
sys.exit(1)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
logging.debug('Starting socket at {}'.format(sock_path))
sock.bind(sock_path)
sock.listen(1)
while True:
conn, addr = sock.accept()
try:
logging.info('Accepted connection from {}'.format(addr))
message = ''
while True:
data = conn.recv(1024)
if data:
message += data.decode('utf-8')
if authenticate(auth, int(message)):
conn.sendall(b'Authentication success')
else:
conn.sendall(b'Authentication failure')
break
else:
break
finally:
print(message)
conn.close()
if __name__ == '__main__':
main()