Twitter-Over-DNS/main.py
2020-04-16 12:55:26 -04:00

42 lines
1.3 KiB
Python

# -*- coding: utf-8 -*-
from dnslib import RR,QTYPE,RCODE,TXT
from dnslib.label import DNSLabel
from dnslib.server import DNSServer,BaseResolver
import twitter
class TwitterResolver(BaseResolver):
def resolve(self,request,handler):
reply = request.reply()
qname = request.q.qname
if qname.matchSuffix("twitter.han.do."):
try:
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
tweets = api.GetUserTimeline(screen_name=str(qname).split(".")[-5], count=10)
for tweet in tweets:
reply.add_answer(RR(qname,QTYPE.TXT,ttl=60,
rdata=TXT(tweet.text)))
except:
reply.header.rcode = RCODE.NXDOMAIN
else:
reply.header.rcode = RCODE.NXDOMAIN
return reply
if __name__ == '__main__':
import time
resolver = TwitterResolver()
udp_server = DNSServer(resolver,
port=53,
address="")
udp_server.start_thread()
while udp_server.isAlive():
time.sleep(1)