2011-12-21 03:27:59 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
import base64
2011-12-22 05:32:32 +01:00
import collections
2011-12-21 03:27:59 +01:00
import logging
import math
import sys
import threading
import time
import tweepy
sys . path . append ( " ../../../ " )
from conf import Conf
from ether2any import Ether2Any
class TwittStreamHandler ( tweepy . StreamListener ) :
2011-12-25 23:01:50 +01:00
def __init__ ( self , dev , coder , debug = False ) :
2011-12-21 03:27:59 +01:00
super ( TwittStreamHandler , self ) . __init__ ( )
self . dev = dev
2011-12-22 05:32:32 +01:00
self . fragments = collections . defaultdict ( str )
2011-12-24 10:08:13 +01:00
self . debug = debug
2011-12-25 23:01:50 +01:00
self . coder = coder
2011-12-21 03:27:59 +01:00
def on_status ( self , status ) :
2011-12-23 03:14:00 +01:00
""" On statis, decode and reassemble packet-status-texts. If complete, write them to the tun-dev. """
2011-12-25 23:01:50 +01:00
sourcePacket = status . text
2011-12-24 10:08:13 +01:00
if self . debug :
print " in uni: " , repr ( sourcePacket )
2011-12-22 05:32:32 +01:00
( isFragment , packetLen , packetId , packet ) = None , None , None , None
try :
2011-12-25 23:01:50 +01:00
( isFragment , packetLen , packetId , packet ) = self . coder . decode ( sourcePacket )
2011-12-22 05:32:32 +01:00
except ValueError , e :
2011-12-23 03:14:00 +01:00
print " Could not decode tweet, omitting (Error was: %s ). \n \t Text was: %s " % ( e , repr ( sourcePacket ) )
2011-12-22 05:32:32 +01:00
raise
return
if isFragment :
self . fragments [ packetId ] + = packet
print " >+ Added fragment with id " , packetId
else :
toSend = None
if self . fragments . has_key ( packetId ) :
toSend = self . fragments [ packetId ] + packet
else :
toSend = packet
2011-12-25 23:01:50 +01:00
if toSend == ' ' :
print " >! Received packet with id " , packetId , " is BROKEN "
else :
print " >> Received packet with id " , packetId
if self . debug :
print repr ( toSend )
self . dev . write ( toSend )
2011-12-21 03:27:59 +01:00
def on_limit ( self , track ) :
print " We got limited " , track
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 ' ;) "
sys . exit ( 1 )
def on_error ( self , status_code ) :
print " We got an error code: " , status_code
if status_code == 401 :
print " Better check ' yer twitter-credentials. "
sys . exit ( 2 )
else :
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 ' ;) "
2011-12-24 10:08:13 +01:00
sys . exit ( 1 )
2011-12-21 03:27:59 +01:00
def on_timeout ( self , status_code ) :
print " Got an timeout: " , status_code
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 ' ;) "
2011-12-24 10:08:13 +01:00
sys . exit ( 1 )
2011-12-21 03:27:59 +01:00
2011-12-24 10:08:13 +01:00
# TODO: Thread is not needed, tweepy has its own threading. remove it
2011-12-21 03:27:59 +01:00
class DownstreamThread ( threading . Thread ) :
2011-12-25 23:01:50 +01:00
def __init__ ( self , dev , coder , auth , endpoint , debug = False ) :
2011-12-21 03:27:59 +01:00
threading . Thread . __init__ ( self )
2011-12-24 10:08:13 +01:00
self . debug = debug
2011-12-21 03:27:59 +01:00
self . auth = auth
2011-12-25 23:01:50 +01:00
self . coder = coder
2011-12-22 05:32:32 +01:00
self . api = tweepy . API ( self . auth )
2011-12-21 03:27:59 +01:00
self . endpoint = endpoint
self . dev = dev
self . daemon = True
def run ( self ) :
2011-12-25 23:01:50 +01:00
stream = tweepy . Stream ( auth = self . auth , listener = TwittStreamHandler ( self . dev , self . coder , self . debug ) )
2011-12-22 05:32:32 +01:00
user = self . api . get_user ( self . endpoint )
print " Endpoint is " , self . endpoint , " with id " , user . id
stream . filter ( [ user . id ] )
2011-12-21 03:27:59 +01:00
class RFC1149 ( Ether2Any ) :
2011-12-24 10:08:13 +01:00
def __init__ ( self , debug = False ) :
2011-12-21 03:27:59 +01:00
Ether2Any . __init__ ( self , tap = False )
2011-12-24 10:08:13 +01:00
self . debug = debug
2011-12-21 03:27:59 +01:00
network = Conf . get ( " network " , { ' mtu ' : 1400 } )
2011-12-25 23:01:50 +01:00
self . coder = Conf . get ( " coder " , None )
2011-12-21 03:27:59 +01:00
self . twitterConf = Conf . get ( " twitter " , None )
self . endpoint = self . twitterConf [ ' endpoint ' ]
if not self . endpoint :
print " No endpoint in configuration, please add one. "
sys . exit ( 1 )
self . dev . ifconfig ( * * network )
self . dev . up ( )
self . _setupTwitter ( )
2011-12-25 23:01:50 +01:00
self . downstream = DownstreamThread ( dev = self . dev , coder = self . coder , auth = self . auth , endpoint = self . endpoint , debug = self . debug )
2011-12-21 03:27:59 +01:00
self . downstream . start ( )
def _requestTwitterTokens ( self ) :
auth = tweepy . OAuthHandler ( self . _dec ( self . ck ) , self . _dec ( self . cs ) )
pass
def _setupTwitter ( self ) :
ck = [ 17 , 39 , 65 , 39 , 25 , 22 , 38 , 30 , 20 , 38 , 33 , 69 , 27 , 0 , 61 ,
31 , 61 , 34 , 42 , 32 ]
cs = [ 71 , 37 , 71 , 37 , 57 , 36 , 18 , 2 , 59 , 64 , 63 , 58 , 23 , 61 , 74 ,
30 , 34 , 68 , 38 , 33 , 56 , 1 , 3 , 74 , 29 , 9 , 41 , 32 , 33 , 7 , 52 ,
8 , 70 , 20 , 30 , 25 , 38 , 51 , 57 , 66 , 53 , 0 , 42 ]
self . auth = tweepy . OAuthHandler ( self . _dec ( ck ) , self . _dec ( cs ) )
if not self . twitterConf [ ' ACCESS_KEY ' ] :
# request tokens, get access, write tokens down.
auth_url = self . auth . get_authorization_url ( )
print " We have no access token for a twitter account. Please visit the "
print " url printed down below, login and report back with the PIN. "
print
print " Authorization URL: %s " % auth_url
print
verifier = raw_input ( ' PIN: ' ) . strip ( )
self . auth . get_access_token ( verifier )
self . twitterConf [ ' ACCESS_KEY ' ] = self . auth . access_token . key
self . twitterConf [ ' ACCESS_SECRET ' ] = self . auth . access_token . secret
authConf = open ( " conf_auth.py " , " w " )
authConf . write ( """ from conf import Conf
# WARNING: This config was overwritten! If you change it be sure
# that you know, what you are doing.
if not Conf [ ' twitter ' ] [ ' ACCESS_KEY ' ] :
Conf [ ' twitter ' ] [ ' ACCESS_KEY ' ] = " %s "
Conf [ ' twitter ' ] [ ' ACCESS_SECRET ' ] = " %s "
""" % (self.twitterConf[ ' ACCESS_KEY ' ], self.twitterConf[ ' ACCESS_SECRET ' ]))
authConf . close ( )
self . auth . set_access_token ( self . twitterConf [ ' ACCESS_KEY ' ] , self . twitterConf [ ' ACCESS_SECRET ' ] )
self . api = tweepy . API ( self . auth )
2011-12-22 05:32:32 +01:00
self . me = self . api . me ( )
print " Logged in as %s " % ( self . me . screen_name , )
2011-12-21 03:27:59 +01:00
def _dec ( self , s ) :
""" ... """
return " " . join ( map ( lambda x : chr ( x + 48 ) , reversed ( s ) ) )
def sendToNet ( self , packet ) :
2011-12-25 23:01:50 +01:00
fragments = self . coder . encode ( packet )
2011-12-24 10:08:13 +01:00
if self . debug :
print " out raw: " , repr ( packet )
print " out frag: " , repr ( fragments )
2011-12-22 05:32:32 +01:00
print " >> Sending out %d bytes in %d tweet %s " % ( len ( packet ) , len ( fragments ) , len ( fragments ) != 1 and " s " or " " )
for fragment in fragments :
2011-12-24 10:08:13 +01:00
try :
self . api . update_status ( fragment )
except tweepy . error . TweepError , e :
# TODO: Proper handling of
# - over 140 chars limit (bug in this software)
# - connection refused (retransmit)
# - limit reached (terminate or wait)
print " >! ERROR - Either connection refused or limited. Not sent. "
print " >! ERROR - Either connection refused or limited. Not sent. "
print " >! ERROR - Either connection refused or limited. Not sent. "
print " >! ERROR - Either connection refused or limited. Not sent. "
print " YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY "
print repr ( fragment )
print " YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY "
print e
print " >! ERROR - Either connection refused or limited. Not sent. "
print " >! ERROR - Either connection refused or limited. Not sent. "
print " >! ERROR - Either connection refused or limited. Not sent. "
2011-12-21 03:27:59 +01:00
if __name__ == ' __main__ ' :
2011-12-25 23:01:50 +01:00
rfc = RFC1149 ( debug = True )
2011-12-21 03:27:59 +01:00
print " Starting RFC1149 ip-over-twitter service... "
rfc . run ( )