Skip to content

Commit

Permalink
subprocesses - allow direct access to CSettings // fix DNSServer+WebS…
Browse files Browse the repository at this point in the history
…erver to "run on their own"
  • Loading branch information
iBaa committed Jun 21, 2013
1 parent 9d6211b commit 573b7ba
Showing 4 changed files with 40 additions and 29 deletions.
27 changes: 17 additions & 10 deletions DNSServer.py
Original file line number Diff line number Diff line change
@@ -59,8 +59,9 @@
import sys
import socket
import struct
import Queue # inter process communication
from multiprocessing import Pipe # inter process communication

import Settings
from Debug import * # dprint()


@@ -127,6 +128,10 @@ def printDNSPaket(paket):
def Run(cmdPipe, param):
dinit(__name__, param) # init logging, DNSServer process

cfg_IP_self = param['IP_self']
cfg_IP_DNSMaster = param['CSettings'].getSetting('ip_dnsmaster')
cfg_HostToIntercept = 'trailers.apple.com'

try:
DNS = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
DNS.settimeout(5.0)
@@ -147,8 +152,8 @@ def Run(cmdPipe, param):

dprint(__name__, 0, "***")
dprint(__name__, 0, "Starting up.")
dprint(__name__, 1, "intercept "+param['HostToIntercept']+": "+param['IP_self'])
dprint(__name__, 1, "forward other to higher level DNS: "+param['IP_DNSMaster'])
dprint(__name__, 1, "intercept "+cfg_HostToIntercept+": "+cfg_IP_self)
dprint(__name__, 1, "forward other to higher level DNS: "+cfg_IP_DNSMaster)
dprint(__name__, 0, "***")

try:
@@ -174,7 +179,7 @@ def Run(cmdPipe, param):
dprint(__name__, 1, "Domain: "+domain)

paket=''
if domain==param['HostToIntercept']:
if domain==cfg_HostToIntercept:
dprint(__name__, 1, "***intercept request")
paket+=data[:2] # 0:1 - ID
paket+="\x81\x80" # 2:3 - flags
@@ -185,12 +190,12 @@ def Run(cmdPipe, param):
paket+=data[12:] # original query
paket+='\xc0\x0c' # pointer to domain name/original query
paket+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04' # response type, ttl and resource data length -> 4 bytes
paket+=str.join('',map(lambda x: chr(int(x)), param['IP_self'].split('.'))) # 4bytes of IP
dprint(__name__, 1, "-> DNS response: "+param['IP_self'])
paket+=str.join('',map(lambda x: chr(int(x)), cfg_IP_self.split('.'))) # 4bytes of IP
dprint(__name__, 1, "-> DNS response: "+cfg_IP_self)

else:
dprint(__name__, 1, "***forward request")
DNS_forward.sendto(data, (param['IP_DNSMaster'], 53))
DNS_forward.sendto(data, (cfg_IP_DNSMaster, 53))
paket, addr_master = DNS_forward.recvfrom(1024)
# todo: double check: ID has to be the same!
# todo: spawn thread to wait in parallel
@@ -218,11 +223,13 @@ def Run(cmdPipe, param):


if __name__ == '__main__':
cmd = Queue.Queue()\
cmdPipe = Pipe()\

cfg = Settings.CSettings()
param = {}
param['CSettings'] = cfg

param['IP_self'] = '192.168.178.20'
param['IP_DNSMaster'] = '8.8.8.8'
param['HostToIntercept'] = 'trailers.apple.com'

Run(cmd, param)
Run(cmdPipe[1], param)
11 changes: 4 additions & 7 deletions PlexConnect.py
Original file line number Diff line number Diff line change
@@ -40,6 +40,10 @@ def getIP_self():

# Settings
cfg = Settings.CSettings()
param['CSettings'] = cfg

param['IP_self'] = getIP_self()
param['HostToIntercept'] = 'trailers.apple.com'

# Logfile, re-init
param['LogLevel'] = cfg.getSetting('loglevel')
@@ -49,13 +53,6 @@ def getIP_self():
pipe_DNSServer = Pipe() # endpoint [0]-PlexConnect, [1]-DNSServer
pipe_WebServer = Pipe() # endpoint [0]-PlexConnect, [1]-WebServer

param['IP_self'] = getIP_self()
param['IP_DNSMaster'] = cfg.getSetting('ip_dnsmaster')
param['HostToIntercept'] = 'trailers.apple.com'

param['IP_WebServer'] = cfg.getSetting('ip_webserver')
param['Port_WebServer'] = int(cfg.getSetting('port_webserver'))

# default PMS
param['IP_PMS'] = cfg.getSetting('ip_pms')
param['Port_PMS'] = cfg.getSetting('port_pms')
20 changes: 10 additions & 10 deletions WebServer.py
Original file line number Diff line number Diff line change
@@ -12,14 +12,14 @@
import string, cgi, time
from os import sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import Queue # inter process communication
from multiprocessing import Pipe # inter process communication

try:
import xml.etree.cElementTree as etree
except ImportError:
import xml.etree.ElementTree as etree

import ATVSettings
import Settings, ATVSettings
from Debug import * # dprint()
import XMLConverter # XML_PMS2aTV, XML_PlayVideo

@@ -170,14 +170,14 @@ def do_GET(self):
def Run(cmdPipe, param):
dinit(__name__, param) # init logging, WebServer process

#Protocol = "HTTP/1.0"
# todo: IP, port
cfg_IP_WebServer = param['CSettings'].getSetting('ip_webserver')
cfg_Port_WebServer = param['CSettings'].getSetting('port_webserver')
try:
server = HTTPServer((param['IP_WebServer'],int(param['Port_WebServer'])), MyHandler)
server = HTTPServer((cfg_IP_WebServer,int(cfg_Port_WebServer)), MyHandler)
server.timeout = 1
sa = server.socket.getsockname()
except Exception, e:
dprint(__name__, 0, "Failed to connect to HTTP on {0} port {1}: {2}", param['IP_WebServer'], param['Port_WebServer'], e)
dprint(__name__, 0, "Failed to connect to HTTP on {0} port {1}: {2}", cfg_IP_WebServer, cfg_Port_WebServer, e)
sys.exit(1)

dprint(__name__, 0, "***")
@@ -211,10 +211,10 @@ def Run(cmdPipe, param):


if __name__=="__main__":
cmd = Queue.Queue()
cmdPipe = Pipe()

cfg = Settings.CSettings()
param = {}
param['IP_WebServer'] = '0.0.0.0'
param['Port_WebServer'] = '80'
param['CSettings'] = cfg

Run(cmd, param)
Run(cmdPipe[1], param)
11 changes: 9 additions & 2 deletions XMLConverter.py
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@
from urlparse import urlparse
from urllib import quote_plus

import ATVSettings
import Settings, ATVSettings
from Debug import * # dprint()


@@ -846,7 +846,13 @@ def ATTRIB_getDurationString(self, src, srcXML, param):


if __name__=="__main__":
setParams({'Addr_PMS':'*Addr_PMS*'})
cfg = Settings.CSettings()
param = {}
param['CSettings'] = cfg

param['Addr_PMS'] = '*Addr_PMS*'
param['HostToIntercept'] = 'trailers.apple.com'
setParams(param)

cfg = ATVSettings.CATVSettings()
setATVSettings(cfg)
@@ -883,6 +889,7 @@ def ATTRIB_getDurationString(self, src, srcXML, param):
print
print "unpack PlexConnect COPY/CUT commands"
options = {}
options['PlexConnectUDID'] = '007'
g_CommandCollection = CCommandCollection(options, PMSroot, '/library/sections/')
XML_ExpandTree(aTVroot, PMSroot, 'main')
XML_ExpandAllAttrib(aTVroot, PMSroot, 'main')

0 comments on commit 573b7ba

Please sign in to comment.