Merge lp:~aj00200/anonplus/trunk into lp:anonplus

Proposed by aj00200
Status: Merged
Merged at revision: 198
Proposed branch: lp:~aj00200/anonplus/trunk
Merge into: lp:anonplus
Diff against target: 241 lines (+76/-45)
5 files modified
src/libs/friends.py (+38/-36)
src/libs/packets.py (+9/-2)
src/libs/storage/classes.py (+8/-1)
src/tunnels/tor.py (+19/-5)
src/vomun.py (+2/-1)
To merge this branch: bzr merge lp:~aj00200/anonplus/trunk
Reviewer Review Type Date Requested Status
pharno Approve
Review via email: mp+89621@code.launchpad.net

Commit message

* Wrote code to handle the 0x0100 UUK Storage Request packet from the mock-storage-request blueprint.
* Updated libs.friends to use the Registrar which is setup by tunnels.__init__ instead of importing them manually.

Description of the change

Added some code for the mock-storage-implementation blueprint. Please make a note on the blueprint whiteboard if more work is needed on this.

To post a comment you must log in.
lp:~aj00200/anonplus/trunk updated
211. By aj00200

Moved code as per discussion on IRC. Also set up the start of the Tor tunnel. Also fixed the tunnel loading and listener starting.

Revision history for this message
pharno (pharno) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/libs/friends.py'
2--- src/libs/friends.py 2012-01-15 16:14:19 +0000
3+++ src/libs/friends.py 2012-01-24 22:43:26 +0000
4@@ -6,7 +6,7 @@
5 import time
6
7 import libs
8-from tunnels import directudp, directudpv6, tor
9+import tunnels
10 from libs.events import broadcast
11 import libs.encryption.rsa
12 from libs.packets import parse_packets, packets_by_id, make_packet
13@@ -43,33 +43,6 @@
14 broadcast('logthis', 'Loading friend: %s\n%s' % (friend, error),
15 level = 5)
16
17-@register_with_api
18-def save_friends():
19- '''Write ~/.vomun/friends.json'''
20- friendlistw = open(friendlistpath, 'w+')
21- json_template = '''[
22-%s
23-]'''
24- friendsjson = ','.join([friend._json() for friend in libs.globals['friends'].values()])
25- friendlistw.write(json_template % friendsjson)
26- friendlistw.flush()
27-
28-@register_with_api
29-def add_friend(keyid, ip, port, name, tunnel):
30- '''Add a friend to our friends list'''
31- friend_obj = Friend(keyid, ip, port, name, tunnel)
32- libs.globals['friends'][keyid] = friend_obj
33- save_friends()
34-
35-@register_with_api
36-def del_friend(keyid):
37- '''Delete a friend'''
38- try:
39- del libs.globals['friends'][keyid]
40- except:
41- broadcast('logthis', 'Friend %s does not exist.' % keyid)
42-
43-
44 class Friend:
45 '''Friend class, stores data related to a friend such as the Connection
46 object and the encryption object. Handles data transfer and encryption.
47@@ -84,9 +57,15 @@
48
49 # Set default values.
50 self.connected = False
51- self.rconnection = None
52- self.wconnection = None
53 self.data = ''
54+
55+ # Setup Tunnel
56+ if self.tunnel_type == 'directudp':
57+ self.wconnection = tunnels.registered['directudp'].Tunnel(self)
58+ elif self.tunnel_type == 'directudpv6':
59+ self.wconnection = tunnels.registered['directudpv6'].Tunnel(self)
60+ elif self.tunnel_type == 'tor':
61+ self.wconnection = tunnels.registered['tor'].Tunnel(self)
62
63 # Setup encryption.
64 broadcast('logthis', 'Setting up encryption. %s:%s' % (
65@@ -130,6 +109,9 @@
66 elif packet_id == 'Message':
67 broadcast('got_message', packet)
68
69+ elif packet_id == 'UUKStorageRequest':
70+ broadcast('uuk_storage_request', packet)
71+
72 else:
73 print(packet_id, packet)
74
75@@ -147,12 +129,6 @@
76
77 def connect(self):
78 '''Connect to the friend'''
79- if self.tunnel_type == 'directudp':
80- self.wconnection = directudp.Tunnel(self)
81- elif self.tunnel_type == 'directudpv6':
82- self.wconnection = directudpv6.Tunnel(self)
83- elif self.tunnel_type == 'tor':
84- self.wconnection = tor.Tunnel(self)
85 self.wconnection.connect()
86
87 def send(self, data, system=0):
88@@ -260,3 +236,29 @@
89 friend = get_friend_by_name(friend)
90 friend.connect()
91 friend._begin_handshake()
92+
93+@register_with_api
94+def save_friends():
95+ '''Write ~/.vomun/friends.json'''
96+ friendlistw = open(friendlistpath, 'w+')
97+ json_template = '''[
98+%s
99+]'''
100+ friendsjson = ','.join([friend._json() for friend in libs.globals['friends'].values()])
101+ friendlistw.write(json_template % friendsjson)
102+ friendlistw.flush()
103+
104+@register_with_api
105+def add_friend(keyid, ip, port, name, tunnel):
106+ '''Add a friend to our friends list'''
107+ friend_obj = Friend(keyid, ip, port, name, tunnel)
108+ libs.globals['friends'][keyid] = friend_obj
109+ save_friends()
110+
111+@register_with_api
112+def del_friend(keyid):
113+ '''Delete a friend'''
114+ try:
115+ del libs.globals['friends'][keyid]
116+ except:
117+ broadcast('logthis', 'Friend %s does not exist.' % keyid)
118\ No newline at end of file
119
120=== modified file 'src/libs/packets.py'
121--- src/libs/packets.py 2011-11-03 00:17:29 +0000
122+++ src/libs/packets.py 2012-01-24 22:43:26 +0000
123@@ -4,6 +4,7 @@
124 DUMP_ALL_PACKETS = False
125
126 packets = {
127+ # 0x0000 - 0x00ff
128 0: Struct('ConnectionRequest',
129 Enum(UBInt32('encryptionmethod'),
130 rsa=0,
131@@ -34,7 +35,13 @@
132 UBInt32('timestamp'),
133 UBInt32('message_length'),
134 MetaField('message', lambda ctx: ctx['message_length'])
135- )
136+ ),
137+
138+ # 0x0100 - 0x01ff
139+ 256: Struct('UUKStorageRequest',
140+ UBInt32('data_length'),
141+ MetaField('data', lambda ctx: ctx['data_length'])
142+ )
143
144 }
145
146@@ -101,4 +108,4 @@
147 print('Parsed packet %d' % packet[0])
148 print(packet[1])
149
150- return l, leftovers
151\ No newline at end of file
152+ return l, leftovers
153
154=== modified file 'src/libs/storage/classes.py'
155--- src/libs/storage/classes.py 2011-12-05 22:51:42 +0000
156+++ src/libs/storage/classes.py 2012-01-24 22:43:26 +0000
157@@ -75,6 +75,13 @@
158 message = packet.message
159 self.add_uuk(Block(message))
160
161+ def uuk_storage_request(self, packet):
162+ '''Received a packet requesting that we store the included data.
163+ As per the mock-storage-implementation blueprint, we will store
164+ this data, no questions asked. There is no limits.
165+ '''
166+ self.add_uuk(Block(packet.data))
167+
168 def got_request(self, query):
169 '''Got a request for a data block.'''
170 self.search(query)
171@@ -88,4 +95,4 @@
172 network for the requested data.'''
173 def __init__(self, blocktype, id):
174 self.type = blocktype
175- self.id = id
176\ No newline at end of file
177+ self.id = id
178
179=== modified file 'src/tunnels/tor.py'
180--- src/tunnels/tor.py 2012-01-08 22:19:56 +0000
181+++ src/tunnels/tor.py 2012-01-24 22:43:26 +0000
182@@ -1,5 +1,5 @@
183-'''Tunnel to use to connect to other nodes over tor.'''
184-import libs.socks
185+'''Tunnel to use to connect to other nodes over Tor.'''
186+import socks
187 import libs.threadmanager
188
189 class Tunnel(object):
190@@ -17,14 +17,23 @@
191 def disconnect(self, nodeid):
192 '''Disconnect from a node at `nodeid`'''
193 pass
194+
195+ def send(self, message):
196+ self.connection.send(message)
197
198 class Connection(object):
199 '''A class to store a connection to a peer'''
200 def __init__(self, node):
201- self.sock = libs.socks.socksocket()
202- self.sock.setproxy(libs.socks.PROXY_TYPE_SOCKS5, addr='localhost',
203+ self.sock = socks.socksocket()
204+ self.sock.setproxy(socks.PROXY_TYPE_SOCKS5, addr='localhost',
205 port=9050, rdns=True)
206- self.sock.connect((node.ip, node.port))
207+ # node.ip must not be unicode because of this bug in python-socks:
208+ # https://code.google.com/p/httplib2/issues/detail?id=179
209+ self.sock.connect((str(node.ip), node.port))
210+
211+ def send(self, message):
212+ broadcast('logthis', 'Sending: ' + message, level = 0)
213+ self.sock.send(message)
214
215 class Listener(libs.threadmanager.Thread):
216 '''A class to listen for connections sent to our Tor onion address.
217@@ -44,3 +53,8 @@
218 # TODO: find out which friend this is without IP address
219 connection = self.sock.accept() # TODO: this blocks!!!!!
220 # TODO: convert this connection to a Connection object!!
221+
222+def start():
223+ listener = Listener()
224+ listener.start()
225+ libs.threadmanager.register(listener)
226\ No newline at end of file
227
228=== modified file 'src/vomun.py'
229--- src/vomun.py 2012-01-21 05:29:37 +0000
230+++ src/vomun.py 2012-01-24 22:43:26 +0000
231@@ -13,8 +13,9 @@
232 def start():
233 morado.start()
234 friends.load_friends()
235- tunnels.directudp.start()
236 storage.manager.start()
237+ for tunnel in tunnels.registered:
238+ tunnels.registered[tunnel].start()
239
240 uis.web.manager.start()
241 atexit.register(cleanup)

Subscribers

People subscribed via source and target branches

to all changes: