summaryrefslogtreecommitdiff
path: root/plugin.video.cmore/service.py
blob: 568ddbbc87eaeaafa1743c9ee8e2041306dea55c (plain)
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
# Author: asciidisco
# Module: service
# Created on: 13.01.2017
# License: MIT https://goo.gl/5bMj3H

import threading
import SocketServer
import socket
from xbmc import Monitor
from resources.lib.kodihelper import KodiHelper
from resources.lib.WidevineHTTPRequestHandler import WidevineHTTPRequestHandler

# helper function to select an unused port on the host machine
def select_unused_port():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('127.0.0.1', 0))
    addr, port = sock.getsockname()
    sock.close()
    return port

helper = KodiHelper()

# pick & store a port for the proxy service
wv_proxy_port = select_unused_port()
helper.set_setting('wv_proxy_port', str(wv_proxy_port))
helper.log('Port {0} selected'.format(str(wv_proxy_port)))

# server defaults
SocketServer.TCPServer.allow_reuse_address = True
# configure the proxy server
wv_proxy = SocketServer.TCPServer(('127.0.0.1', wv_proxy_port), WidevineHTTPRequestHandler)
wv_proxy.server_activate()
wv_proxy.timeout = 1

if __name__ == '__main__':
    monitor = Monitor()
    # start thread for proxy server
    proxy_thread = threading.Thread(target=wv_proxy.serve_forever)
    proxy_thread.daemon = True
    proxy_thread.start()

    # kill the services if kodi monitor tells us to
    while not monitor.abortRequested():
        if monitor.waitForAbort(5):
            wv_proxy.shutdown()
            break

    # wv-proxy service shutdown sequence
    wv_proxy.server_close()
    wv_proxy.socket.close()
    wv_proxy.shutdown()
    helper.log('wv-proxy stopped')