Browse Source

Massive bugfixes for twitter-rfc1149

seba 8 years ago
parent
commit
1662510847
2 changed files with 91 additions and 28 deletions
  1. 41
    11
      tunnel/rfc1149/rfc1149.py
  2. 50
    17
      tunnel/rfc1149/uphelper.py

+ 41
- 11
tunnel/rfc1149/rfc1149.py View File

@@ -2,6 +2,7 @@
2 2
 # -*- coding: utf-8 -*-
3 3
 
4 4
 import base64
5
+import collections
5 6
 import logging
6 7
 import math
7 8
 import re
@@ -21,13 +22,34 @@ class TwittStreamHandler(tweepy.StreamListener):
21 22
 	def __init__(self, dev):
22 23
 		super(TwittStreamHandler, self).__init__()
23 24
 		self.dev = dev
24
-		self.fragments = {}
25
+		self.fragments = collections.defaultdict(str)
25 26
 	
26 27
 	def on_status( self, status ):
27
-		print '-' * 20
28
-		print "incoming:", unicode(status.text)
29
-		print "hex:", binToHexStr(status.text), len(status.text)
30
-		# reassemble messages, put them together if nessecary.
28
+		#print '-' * 20
29
+		#print "incoming:", unicode(status.text), "from", dir(status)
30
+		#print "hex:", binToHexStr(status.text), len(status.text)
31
+		# reassemble messages, write them to dev when complete
32
+		(isFragment, packetLen, packetId, packet) = None, None, None, None
33
+		try:
34
+			(isFragment, packetLen, packetId, packet) = UPHelper.decode(status.text)
35
+		except ValueError, e:
36
+			print "Could not decode tweet, omitting (Error was: %s).\n\tText was: %s" % (e, status.text)
37
+			raise
38
+			return
39
+		#print "Parsed packet:", (isFragment, packetLen, packetId)
40
+		#print "\t contents:", packet
41
+		if isFragment:
42
+			self.fragments[packetId] += packet
43
+			print " >+ Added fragment with id", packetId
44
+		else:
45
+			toSend = None
46
+			if self.fragments.has_key(packetId):
47
+				toSend = self.fragments[packetId] + packet
48
+			else:
49
+				toSend = packet
50
+			print " >> Received packet with id", packetId
51
+			print repr(toSend)
52
+			self.dev.write(toSend)
31 53
 	
32 54
 	def on_limit(self, track):
33 55
 		print "We got limited ", track
@@ -41,7 +63,7 @@ class TwittStreamHandler(tweepy.StreamListener):
41 63
 			sys.exit(2)
42 64
 		else:
43 65
 			print "At the moment there is no error-handling for this, so we just kill everything. Remember: This software doesn't even deserve the label 'alpha' ;)"
44
-			sys.exit(1)
66
+			#sys.exit(1)
45 67
 	
46 68
 	def on_timeout(self, status_code):
47 69
 		print "Got an timeout: ", status_code
@@ -51,15 +73,18 @@ class DownstreamThread(threading.Thread):
51 73
 	def __init__(self, dev, auth, endpoint):
52 74
 		threading.Thread.__init__(self)
53 75
 		self.auth = auth
76
+		self.api = tweepy.API(self.auth)
54 77
 		self.endpoint = endpoint
55 78
 		self.dev = dev
56 79
 		self.daemon = True
57 80
 	
58 81
 	def run(self):
59 82
 		stream = tweepy.Stream(auth=self.auth, listener=TwittStreamHandler(self.dev))
60
-		stream.userstream()
61
-		stream.filter(self.endpoint, None)
62
-		stream.sample()
83
+		user = self.api.get_user(self.endpoint)
84
+		print "Endpoint is", self.endpoint, "with id", user.id
85
+		stream.filter([user.id])
86
+		#stream.userstream()
87
+		#stream.sample()
63 88
 
64 89
 class RFC1149(Ether2Any):
65 90
 	def __init__(self):
@@ -113,14 +138,19 @@ if not Conf['twitter']['ACCESS_KEY']:
113 138
 			authConf.close()
114 139
 		self.auth.set_access_token(self.twitterConf['ACCESS_KEY'], self.twitterConf['ACCESS_SECRET'])
115 140
 		self.api = tweepy.API(self.auth)
141
+		self.me = self.api.me()
142
+		print "Logged in as %s" % (self.me.screen_name,)
116 143
 	
117 144
 	def _dec(self, s):
118 145
 		""" ... """
119 146
 		return "".join(map(lambda x: chr(x+48), reversed(s)))
120 147
 	
121 148
 	def sendToNet(self, packet):
122
-		for subPacket in UPHelper.encode(packet):
123
-			print subPacket
149
+		fragments = UPHelper.encode(packet)
150
+		print " >> Sending out %d bytes in %d tweet%s" % (len(packet), len(fragments), len(fragments)!=1 and "s" or "")
151
+		for fragment in fragments:
152
+			# FIXME: catch tweepy.error.TweepError
153
+			self.api.update_status(fragment)
124 154
 
125 155
 if __name__ == '__main__':
126 156
 	rfc = RFC1149()

+ 50
- 17
tunnel/rfc1149/uphelper.py View File

@@ -1,8 +1,8 @@
1 1
 #!/usr/bin/python
2 2
 # -*- coding: utf-8 -*-
3 3
 
4
-import random
5 4
 import bitarray
5
+import random
6 6
 
7 7
 class UPHelper():
8 8
 	""" The Unicode Packet Helper
@@ -15,7 +15,7 @@ class UPHelper():
15 15
 	The header in the metadata is as following:
16 16
 	<fragment bit (1 if packet is a fragment, 0 if last in row)>
17 17
 	<9 bits length of payload>
18
-	<32 bit random paket id>"""
18
+	<32 bit random paket id greater than 0>"""
19 19
 
20 20
 	@staticmethod
21 21
 	def intToBits(n, length):
@@ -36,10 +36,16 @@ class UPHelper():
36 36
 			ret = (ret << 1) | (i and 1 or 0)
37 37
 		return ret
38 38
 	
39
+	@staticmethod
40
+	def toBin(val, leading):
41
+		""" Binary formatter for python versions without str.format() """
42
+		x = bin(val).replace("0b", "")
43
+		return "0" * (leading-len(x)) + x
44
+	
39 45
 	@staticmethod
40 46
 	def encode(data):
41 47
 		""" Generate list of packets with a header from data. """
42
-		packetId = random.randint(0, 2**32)
48
+		packetId = random.randint(1, 2**32)
43 49
 		fragments = []
44 50
 		while len(data) > 280:
45 51
 			fragments.append(data[0:280])
@@ -60,7 +66,7 @@ class UPHelper():
60 66
 			# write header (bits: 1 fragment, 9 length, 32 id)
61 67
 			header = bitarray.bitarray(1)
62 68
 			# write fragment-bit
63
-			header[0] = (y+1 == len(fragments))
69
+			header[0] = not (y+1 == len(fragments))
64 70
 			# append packet length
65 71
 			header.extend(UPHelper.intToBits(lenX, 9))
66 72
 			# add packet id
@@ -90,27 +96,54 @@ class UPHelper():
90 96
 			raise ValueError("This is not a valid packet, header is too short (should be at least 11, is %d)" % len(packet))
91 97
 		header = bitarray.bitarray()
92 98
 		for i in range(11):
93
-			header.extend("{:04b}".format(ord(packet[i]) >> 16))
99
+			# format with binary is not understood by older python versions
100
+			#header.extend("{:04b}".format(ord(packet[i]) >> 16))
101
+			header.extend(UPHelper.toBin((ord(packet[i]) >> 16), 4))
94 102
 		isFragmented = header[0]
95 103
 		
96
-		packetLen = UPHelper.bitsToInt(header[1:9])
97
-		packetId = UPHelper.bitsToInt(header[9:])
104
+		packetLen = UPHelper.bitsToInt(header[1:10])
105
+		packetId = UPHelper.bitsToInt(header[10:])
106
+		if packetId == 0:
107
+			raise ValueError("Packet id cannot be 0")
108
+
98 109
 		rawData = map(lambda x: ord(x) & 0xFFFF, packet)
99 110
 		data = []
100 111
 		for p in rawData:
101 112
 			data.append(chr(p >> 8))
102 113
 			data.append(chr(p & 255))
103 114
 		data = "".join(data)
104
-		
105
-		return (isFragmented, packetLen, packetId, data)
115
+		return (isFragmented, packetLen, packetId, data[:packetLen])
106 116
 
107 117
 if __name__ == '__main__':
108
-	print UPHelper.encode("foo")
109
-	print UPHelper.decode(UPHelper.encode("foo")[0])
110
-	msg = "".join([chr(i) for i in range(256)])
111
-	msg += "".join([chr(i) for i in range(256)])
112
-	p = UPHelper.encode(msg)
113
-	print repr(msg)
114
-	for x in p:
115
-		print UPHelper.decode(x)
118
+	msg = '\x00\x00\x08\x00E\x00\x00T\x00\x00@\x00@\x01\x12\x81\n\n\n\x0b\n\n\n\n\x08\x00\xd7Gt\xd2\x00\x01[U\xf1Nl=\x08\x00\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567'
119
+	msg = u'\U000c1011\U00061213\U00011415\U000a1617\U00031819\U00041a1b\U000a1c1d\U000a1e1f\U000c2021\u2223\U000c2425\u2627\u2829\u2a2b\u2c2d\u2e2f\u3031\u3233\u3435\u3637\u3839\u3a3b\u3c3d\u3e3f\u4041\u4243\u4445\u4647\u4849\u4a4b\u4c4d\u4e4f\u5051\u5253\u5455\u5657\u5859\u5a5b\u5c5d\u5e5f\u6061\u6263\u6465\u6667\u6869\u6a6b\u6c6d\u6e6f\u7071\u7273\u7475\u7677\u7879\u7a7b\u7c7d\u7e7f\u8081\u8283\u8485\u8687\u8889\u8a8b\u8c8d\u8e8f\u9091\u9293\u9495\u9697\u9899\u9a9b\u9c9d\u9e9f\ua0a1\ua2a3\ua4a5\ua6a7\ua8a9\uaaab\uacad\uaeaf\ub0b1\ub2b3\ub4b5\ub6b7\ub8b9\ubabb\ubcbd\ubebf\uc0c1\uc2c3\uc4c5\uc6c7\uc8c9\ucacb\ucccd\ucecf\ud0d1\ud2d3\ud4d5\ud6d7\ud8d9\udadb\udcdd\udedf\ue0e1\ue2e3\ue4e5\ue6e7\ue8e9\ueaeb\ueced\ueeef\uf0f1\uf2f3\uf4f5\uf6f7\uf8f9\ufafb\ufcfd\ufeff\x01\u0203\u0405\u0607\u0809\u0a0b\u0c0d\u0e0f\u1011\u1213\u1415\u1617\u1819\u1a1b\u1c1d\u1e1f\u2021\u2223\u2425\u2627'
120
+	msg = '\x00\x00\x08\x00E\x00\x04\x04\x00\x00@\x00@\x01\x0e\xd1\n\n\n\x0b\n\n\n\n\x08\x005f\x03\xec\x00\x01\xe7\xf7\xf1NR\xf2\r\x00\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x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\xe2\xe3\xe4\xe5\xe6\xe7'
121
+	enc = UPHelper.encode(msg)
122
+	print "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
123
+	print "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
124
+	print "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
125
+	print enc
126
+	ret = ""
127
+	for e in enc:
128
+		e = UPHelper.decode(e)
129
+		print e[0:3]
130
+		print e[3]
131
+		ret += e[3]
132
+	m = UPHelper.decode(UPHelper.encode(msg)[0])
133
+	#print m
134
+	if ret == msg:
135
+		print "success"
136
+	else:
137
+		print "failure"
138
+	print "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
139
+	print "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
140
+	print "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
141
+	print UPHelper.decode(UPHelper.encode("X"*281)[0])
142
+	print UPHelper.decode(UPHelper.encode("X"*281)[1])
143
+	#msg = "".join([chr(i) for i in range(256)])
144
+	#msg += "".join([chr(i) for i in range(256)])
145
+	#p = UPHelper.encode(msg)
146
+	#print repr(msg)
147
+	#for x in p:
148
+	#	print UPHelper.decode(x)
116 149
 

Loading…
Cancel
Save