-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwClientSocket.py
143 lines (125 loc) · 4.33 KB
/
dwClientSocket.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
#!/usr/bin/python3
from dwCrypt import *
import sys
import socket
import threading
from time import sleep
daIP = '10.1.1.8'
daPort = 60000
serverConfig = '~/.darkWater'
fileLocation = '/home/jimmy/HOST/'
buff = 1024
sList = []
def rmvSocket(thsSocket, thsUser):
global sList
global usrName
print('Socket closed for : ' + thsUser.usrName)
sList.remove(thsSocket)
thsSocket.destroy()
del thsUser
usrName = None
def srvLogin(thsUser):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((daIP, daPort))
except Exception as e:
print('Connection failed as ' + str(e))
sys.exit()
else:
data = s.recv(buff).decode('utf-8')
if not data == '001':
print('Unknown error - server sent : ' + data)
return None
else:
s.send(bytes(thsUser.usrName, 'utf-8'))
data = s.recv(buff).decode('utf-8')
if not data == '002':
print('Unknown error - server sent : ' + data)
return None
else:
s.send(bytes(thsUser.keyHash, 'utf-8'))
data = s.recv(buff).decode('utf-8')
if not data == '111':
print('Unknown error - server sent : ' + data)
return None
else:
print('OK ' + data)
s.settimeout(300)
secSock = cryptSock(s, thsUser.key)
return secSock
def readLoop(secSock, buff, thsUser):
global sList
global fileLocation
while secSock in sList:
try:
thsCMD = secSock.sRead(buff)
if ':' in thsCMD:
numCode = thsCMD.split(':')[0]
pos = thsCMD.index(':')
if not thsCMD[pos + 1] is None:
thsArg = thsCMD.split(':')[1]
else:
numCode = thsCMD
if numCode.lower() == 'ping':
secSock.sWrite('pong')
elif numCode.lower() == '000':
print('You have been booted')
rmvSocket(secSock, thsUser)
elif numCode.lower() == '200':
try:
f = open(thsUser.keyFile, 'w')
f.write(thsArg)
f.close()
print('You have a new cryptoKey.')
secSock.sWrite('201')
except:
print('Error adding new key')
elif numCode == '202':
print('Your remote key is : ' + thsArg)
elif numCode == '300':
secSock.sWrite('301')
data = ' '
while not data == '302':
data = secSock.sRead(buff)
if not data == '302':
print(data)
elif numCode == '310':
filSize = int(thsArg.split('^')[0])
thsFile = thsArg.split('^')[1]
if (thsFile is None) or (filSize is None):
secSock.sWrite('399')
print('Error getting incoming file data : ' + thsArg + ' : ' + numCode)
else:
print('Incoming file transfer : ' + thsFile + ' : ' + str(filSize))
nf = open(fileLocation + thsFile, 'wb')
secSock.sWrite('311')
while not filSize == 0:
data = secSock.sRead(buff)
nf.write(data)
filSize = filSize - len(data)
nf.close()
else:
print(numCode)
except socket.timeout:
print('Server timeout.')
rmvSocket(secSock, thsUser)
while True:
usrName = input('Please input user name: ')
authUser = thsUser(usrName, serverConfig)
if authUser.keyHash is None:
print('User ID is invalid')
usrName = None
else:
secSock = srvLogin(authUser)
if secSock is None:
break
else:
sList.append(secSock)
cTh = threading.Thread(target=readLoop, args=(secSock, buff, authUser))
cTh.start()
while secSock in sList:
cmd = input(' #: ')
try:
secSock.sWrite(cmd)
except:
pass