-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfatmanOld.py
266 lines (229 loc) · 7.98 KB
/
fatmanOld.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import socket
import threading
import pyaudio
import wave
import sys
import time
import queue
import json
#import lz4 # compression library
#import gzip #compression library
import Adafruit_CharLCD as LCD #LCD library
from LCD_Control import LCD_Control #LCD library
import nacl.secret
import nacl.utils
from nacl.public import PrivateKey, PublicKey,Box
from nacl.encoding import HexEncoder
import jsonpickle
secretNotKnown = True
global listen_secret_box
import subprocess
from queue import *
from opus import OpusCodec
def write_to_stream():
global listener_stream
global callInProgress
while(callInProgress):
try:
item = jitter_buf.get()
if (not item is None) and (jitter_buf.qsize() <= 5):
listener_stream.write(item)
time.sleep(.005) #trying to slow down this thread
except Exception:
print("write to stream error:",sys.exc_info())
print("write to stream stopped")
# client thread
def talk():
global shared_secret
global secretNotKnown
global nonce
global nonce2
global callInProgress
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.connect((Listener_HOST, Listener_PORT))
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=Talk_CHUNK)
print("*recording")
global skbob
pkalice = jsonpickle.decode(firebase.get('/littleboy/pk',None))
pkalice = PublicKey(pkalice,encoder=HexEncoder)
bob_box = Box(skbob, pkalice)
####PUBLIC KEY AGREEMENT
data = stream.read(Talk_CHUNK)
compressed_data = oc.encode(data)
encrypted = bob_box.encrypt(compressed_data, nonce2)
s.send(encrypted)
####PUBLIC KEY AGREEMENT
while secretNotKnown:
time.sleep(.01)
listen_secret_box = nacl.secret.SecretBox(shared_secret)
print("talk:",shared_secret)
forwardsecret=0
try:
while(callInProgress):#for i in range(0, int(RATE/Talk_CHUNK*RECORD_SECONDS)):
data = stream.read(Talk_CHUNK)
compressed_data = oc.encode(data)
encrypted = listen_secret_box.encrypt(compressed_data,nonce) #nonce
if(len(encrypted)==168):
bytes_sent = s.send(encrypted)
time.sleep(.01)
except Exception:
print("problem occured",sys.exc_info())
stream.stop_stream()
stream.close()
p.terminate()
s.close()
callInProgress=False
print("talk stopped")
# server thread
def listen():
global shared_secret
global listener_stream
global secretNotKnown
global nonce
global nonce2
global callInProgress
p = pyaudio.PyAudio()
listener_stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
output=True,
frames_per_buffer=Listen_CHUNK)
PORT = 50008#23555#50007 changed to 50007 from 50008 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.bind(('', PORT))
s.listen(1)
#caller = threading.Thread(target=call)
#caller.start()
conn, addr = s.accept()#here the thread waits for a connection
global waitingForCall
if waitingForCall :
if(conn):
waitingForCall = False
talker = threading.Thread(target=talk)
talker.start()
print ('Connected by', addr)
time.sleep(2)
data = conn.recv(Listen_CHUNK)# #1024
#might need this ? nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE)
i=1
print("first data received")
print(len(data))
writer.start()
#https://github.com/pyca/pynacl/blob/51acad0e34e125378d166d6bb9662408056702e0/tests/test_public.py
#alice_box = Box(skalice, pkbob)
global skbob
pkalice = jsonpickle.decode(firebase.get('/littleboy/pk',None))
pkalice = PublicKey(pkalice,encoder=HexEncoder)
bob_box = Box(skbob, pkalice)
data = bob_box.decrypt(data)
shared_secret = bob_box.shared_key()
talk_secret_box = nacl.secret.SecretBox(shared_secret)
print("listen:",shared_secret)
secretNotKnown = False
data = conn.recv(Listen_CHUNK)
global callInProgress
while data != '':
try:
data = talk_secret_box.decrypt(data)
data = oc.decode(data)
jitter_buf.put(data)
data = conn.recv(Listen_CHUNK)# #1024
i=i+1
except Exception:
print("Error warning:",sys.exc_info()[0])
print("length of shit data",len(data))
data = conn.recv(Listen_CHUNK-len(data))# turrible derter destreryer
time.sleep(.05)
data = conn.recv(Listen_CHUNK)# #1024s
if(not callInProgress):
break
continue
#break
listener_stream.stop_stream()
listener_stream.close()
p.terminate()
conn.close()
print("listen stopped")
callInProgress = False
def call():
myInput = control.getUserInput()
global waitingForCall
global callInProgress
controlEnd = LCD_Control(LCD)
if waitingForCall:
waitingForCall = False
#talk()
talker = threading.Thread(target=talk)
talker.start()
myInput = controlEnd.getUserInput()
callInProgress=False
print("call ended")
else:
print("call ended")
callInProgress=False
print("call stopped")
while(True):
intf = 'wlan0'
intf_ip = subprocess.getoutput("ip address show dev " + intf).split()
intf_ip = intf_ip[intf_ip.index('inet')+1].split('/')[0]
from firebase import firebase
firebase = firebase.FirebaseApplication('https://pysnac.firebaseio.com',None)
fatmanIP = firebase.put(url = 'https://pysnac.firebaseio.com', name = '/fatman/ip',data = intf_ip)
littleboyIP = firebase.get('/littleboy/ip',None)
#encryption
#encryption_key = (12345).to_bytes(32,byteorder='big')
#length = 32
#shared_secret = encryption_key
#some public key stuff
skbob = PrivateKey.generate()
pkbob = skbob.public_key
pkalice = HexEncoder.encode(bytes(pkbob))
frozen = jsonpickle.encode(pkalice)
firebase.put(url = 'https://pysnac.firebaseio.com', name = '/fatman/pk',data = frozen)
#
nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE)
nonce2 = nacl.utils.random(Box.NONCE_SIZE)
#audio setup
#Talk_CHUNK = 1024
#Listen_CHUNK = 2088
#CHANNELS = 1
RECORD_SECONDS = 80000
FORMAT = pyaudio.paInt16
#RATE = 28000
#WIDTH = 2
#opus constants
Talk_CHUNK = 960#2880#1920#2880#4800#3840#4800#960#2880
Listen_CHUNK = 168#424#424 # len of encrypted packet at 48000 is 168, 24000 is 176
CHANNELS = 1
RATE = 48000#24000#48000 #24000 is also ok, but need to change opus.py if changed
WIDTH = 2
oc = OpusCodec() #ALSA 7843 underrun causing static?
#silence = chr(0)*Listen_CHUNK
#network
Listener_HOST = littleboyIP#fatmanIP#littleboyIP #'172.23.39.163'#'172.23.48.9'#'127.0.0.1'#'192.168.1.19' # The remote host
Listener_PORT = 50007#50007#23555#50007 # The same port as used by the server
#global variable to see whether call was made or received
waitingForCall = True
#global variable to see if call has finished
callInProgress = True
#Initializing LCD control
control = LCD_Control(LCD)
listener_stream = 0
jitter_buf = Queue()
writer = threading.Thread(target=write_to_stream)
listener =threading.Thread(target=listen)
listener.start()
caller = threading.Thread(target=call)
caller.start()
while(callInProgress):
time.sleep(5)
print("program restarted")