forked from iBaa/PlexConnect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlexConnect.py
executable file
·104 lines (78 loc) · 3.02 KB
/
PlexConnect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
"""
PlexConnect
Sources:
inter-process-communication (queue): http://pymotw.com/2/multiprocessing/communication.html
"""
import sys, time
from os import sep
import socket
from multiprocessing import Process, Queue
import PlexGDM
import DNSServer, WebServer
import Settings
from Debug import * # dprint()
def getIP_self():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('1.2.3.4', 1000))
IP = s.getsockname()[0]
dprint('PlexConnect', 0, "IP_self: "+IP)
return IP
if __name__=="__main__":
param = {}
param['LogFile'] = sys.path[0] + sep + 'PlexConnect.log'
dinit('PlexConnect', param, True) # init logging, new file, main process
dprint('PlexConnect', 0, "***")
dprint('PlexConnect', 0, "PlexConnect")
dprint('PlexConnect', 0, "Press ENTER to shut down.")
dprint('PlexConnect', 0, "***")
# Settings
cfg = Settings.CSettings()
# Logfile, re-init
param['LogLevel'] = cfg.getSetting('loglevel')
dinit('PlexConnect', param) # re-init logfile with loglevel
if cfg.getSetting('enable_dnsserver')=='True':
cmd_DNSServer = Queue()
cmd_WebServer = Queue()
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')
param['Addr_PMS'] = param['IP_PMS']+':'+param['Port_PMS']
if cfg.getSetting('enable_plexgdm')=='True':
if PlexGDM.Run()>0:
param['IP_PMS'] = PlexGDM.getIP_PMS()
param['Port_PMS'] = PlexGDM.getPort_PMS()
param['Addr_PMS'] = param['IP_PMS']+':'+param['Port_PMS']
dprint('PlexConnect', 0, "PMS: {0}", param['Addr_PMS'])
if cfg.getSetting('enable_dnsserver')=='True':
p_DNSServer = Process(target=DNSServer.Run, args=(cmd_DNSServer, param))
p_DNSServer.start()
time.sleep(0.1)
if not p_DNSServer.is_alive():
dprint('PlexConnect', 0, "DNSServer not alive. Shutting down.")
sys.exit(1)
p_WebServer = Process(target=WebServer.Run, args=(cmd_WebServer, param))
p_WebServer.start()
time.sleep(0.1)
if not p_WebServer.is_alive():
dprint('PlexConnect', 0, "WebServer not alive. Shutting down.")
if cfg.getSetting('enable_dnsserver')=='True':
cmd_DNSServer.put('shutdown')
p_DNSServer.join()
sys.exit(1)
try:
key = raw_input()
except KeyboardInterrupt:
dprint('PlexConnect', 0, "^C received.")
finally:
dprint('PlexConnect', 0, "Shutting down.")
if cfg.getSetting('enable_dnsserver')=='True':
cmd_DNSServer.put('shutdown')
p_DNSServer.join()
cmd_WebServer.put('shutdown')
p_WebServer.join()