This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fclient.py
139 lines (104 loc) · 3.26 KB
/
fclient.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
#!/env/bin/python
import base64
import hashlib
import json
import os
import random
import string
import sys
import time
import zmq
from termcolor import colored
import ffile
def toHex(data):
return base64.b16encode(data)
def hexToDec(data):
return base64.b16decode(data)
def client_data(data):
for i in data:
print colored(i, 'magenta')
def client_info(client):
print colored('#############', 'magenta')
print colored('My IP -->' + client['ip'] + ':' + client['port'], 'magenta')
print colored('FILES:', 'magenta')
client_data(client['data'])
print colored('#############', 'magenta')
def options():
print colored('Options', 'blue', attrs=['bold'])
print colored('exit -> Close client connection',
'blue')
print colored('-g, get <sha256> -> Get file', 'blue')
print colored('-h, help -> Get help', 'blue')
print colored('ls -> List of my files in DHT',
'blue')
print colored('-rm, remove <sha256> -> Remove file from DHT',
'blue')
print colored('-s or send <filename.ext> -> Send a file', 'blue')
def list_file(client):
for i in client['data']:
print colored(
client['data'][i]['name'], 'magenta',
attrs=['bold']) + ':' + colored(i, 'magenta')
def printJSON(varJSON):
print colored(
json.dumps(varJSON, indent=2, sort_keys=True), 'cyan', attrs=['bold'])
def clear():
sys.stderr.write("\x1b[2J\x1b[H")
def send_msg(client, to, inp):
data = ffile.get_file(inp[1])
if data:
file_info = json.dumps({
'req': 'save',
'from': client['ip'] + ':' + client['port'],
'to': to,
'data': data,
'name': ffile.get_filename(inp[1]),
'id': ffile.sha256(ffile.get_file(inp[1]))
})
file_info_json = json.loads(file_info)
client['data'][ffile.sha256(ffile.get_file(inp[1]))] = {
'name': ffile.get_filename(inp[1]),
'data': data
}
client_info(client)
printJSON(file_info_json)
return file_info_json
else:
return 'Err: No file'
def remove_local(client, inp):
flag = False
for i in client['data']:
print i
if i == inp[1]:
flag = True
# client['data'][i] = {}
del client['data'][i]
break
return flag
def remove_msg(client, to, inp):
flag = remove_local(client, inp)
if flag:
file_info = json.dumps({
'req': 'remove',
'from': client['ip'] + ':' + client['port'],
'to': to,
'id': inp[1]
})
file_info_json = json.loads(file_info)
client_info(client)
printJSON(file_info_json)
return file_info_json
else:
return 'Err: No file'
def get_msg(client, to, inp):
file_info = json.dumps({
'req': 'get',
'from': client['ip'] + ':' + client['port'],
'to': to,
'id': inp[1],
'node_origin': to,
'client_origin': client['ip'] + ':' + client['port']
})
file_info_json = json.loads(file_info)
printJSON(file_info_json)
return file_info_json