-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwServerSocket.py
169 lines (140 loc) · 5.2 KB
/
dwServerSocket.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
#!/usr/bin/python3
from dwCrypt import *
import socket
import threading
import sys
from dwUtils import *
def newSocket(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((ip, port))
s.listen(1)
return s
class serverSettings:
def __init__(self, settingsFile=None):
self.ip = None
self.port = None
self.keyLocation = None
self.fileLocation = None
self.filemapLocation = None
self.buff = None
self.sList = []
if settingsFile is None:
sFile = '/etc/darkWater/darkWater.conf'
else:
sFile = settingsFile
try:
with open(sFile) as f:
for line in f:
line = line.lower().replace(' ', '').replace('\n', '')
item = line.split(':')[0]
value = line.split(':')[1]
if item == 'ip':
self.ip = value
elif item == 'port':
self.port = int(value)
elif item == 'key_location':
self.keyLocation = value
elif item == 'file_location':
self.fileLocation = value
elif item == 'file_mapping':
self.fileLocation = value
elif item == 'server_mem_buffer':
self.buff = value
except:
print('Bad config file. : ' + sFile)
f.close()
else:
f.close()
def rmvSocket(self, thsSocket, thsUser):
print('Socket closed for : ' + thsUser.usrName)
self.sList.remove(thsSocket)
thsSocket.destroy()
del thsUser
class serverCMD:
def __init__(self, serverSettings):
self.srvSettings = serverSettings
def fileList(thsSocket):
fList = fileSearch(self.srvSettings.fileLocation, True)
thsSocket.sWrite('300')
r = thsSocket.sRead(self.srvSettings.buff)
if not r == '301':
break
else:
for i in fList:
thsSocket.sWrite(i)
thsSocket.sWrite('')
def newKey(thsSocket, thsUser):
nk = newKey()
thsSocket.sWrite('200:' + nk)
ok = thsSocket.sRead(self.srvSettings.buff)
if '201' in ok:
f = open(thsUser.keyFile, 'w')
f.write(nk)
f.close()
print('New key added for ' + thsUser.usrName)
serverSettings.rmvSocket(thsSocket, thsUser)
def getFile(thsSocket, thsArg):
thsBuff = self.srvSettings.buff
thsFile = fileSearch(thsArg.split('^')[0], self.srvSettings.file_mapping)
if thsFile is None:
thsSocket.sWrite('399')
else:
thsChunk = thsArg.split('^')[1]
if thsChunk == '0':
fileSze
tf = open(thsFile, 'r')
while not fileSize == 0:
print(str(fileSize))
if fileSize < thsBuff:
thsBuff = fileSize
chunk = tf.read(thsBuff)
thsSocket.sWrite(chunk)
fileSize = fileSize - thsBuff
ok = thsSocket.sRead(buff)
if ok == '399':
print('Err on client side')
elif ok == '311':
tf.close()
def srvLoop(srvConfig, thsSocket):
global fileLocation
secSock, thsUser = serverCMD.usrVerify(thsSocket)
if None in {secSock, thsUser}:
thsSocket.close()
del secSock
del thsUser
else:
srvConfig.sList.extend(secSock)
while secSock in srvConfig.sList:
try:
thsCMD = thsSocket.sRead(buff)
except socket.timeout:
print('Connection timeout for : ' + thsUser.usrName)
rmvSocket(thsSocket, thsUser)
except Exception as e:
print('Error for ' + thsUser.usrName + ' : ' + str(e))
srvConfig.rmvSocket(secSock, thsUser)
if thsCMD is None:
rmvSocket(thsSocket, thsUser)
else:
numCode, thsArg = getCMD(thsCMD)
if numCode == 'list':
serverCMD.fileList(secSock)
elif numCode == 'newkey':
serverCMD.newkey(secSock, thsUser)
elif numCode == 'getfile':
serverCMD.getfile(secSock, thsArg)
else:
print(thsCMD)
# Main kick off loop - needs Queue passing to shutdown clean
try:
srvSet = sys.argv[1]
except:
serverConfig = serverSettings()
else:
serverConfig = serverSettings(srvSet)
scmd = serverCMD(serverConfig)
s = newSocket(serverConfig.ip, serverConfig.port)
while 1:
srvSock, add = s.accept()
usrTH = threading.Thread(target=srvLoop, args=(scmd, srvSock))
usrTH.start()