-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
108 lines (92 loc) · 3.36 KB
/
server.py
File metadata and controls
108 lines (92 loc) · 3.36 KB
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
105
106
107
108
import RNS
import RNS.vendor.umsgpack as msgpack
from pathlib import Path
import time
import urllib.request
class server:
def __init__(
self,
name=None,
title=None,
hls=None,
streampath="http://localhost:8888",
configpath="./config",
):
self.streampath = streampath
self.configpath = Path(configpath)
self.identitypath = self.configpath.joinpath("/identity")
self.configpath.mkdir(parents=True, exist_ok=True)
RNS.Reticulum()
if self.identitypath.is_file():
self.identity = RNS.Identity.from_file(self.identitypath)
RNS.log("Loaded identity from file", RNS.LOG_INFO)
else:
RNS.log("No Primary Identity file found, creating new...", RNS.LOG_INFO)
self.identity = RNS.Identity()
self.identity.to_file(self.identitypath)
self.destination = RNS.Destination(
self.identity,
RNS.Destination.IN,
RNS.Destination.SINGLE,
"live",
"stream",
)
self.destination.register_request_handler(
"/files",
response_generator=self.files_generator,
allow=RNS.Destination.ALLOW_ALL,
)
self.destination.set_link_established_callback(self.client_connected)
RNS.log(f"Address: {RNS.prettyhexrep(self.destination.hash)}", RNS.LOG_INFO)
self.app_data = {}
if name is not None:
self.app_data["name"] = name
if title is not None:
self.app_data["title"] = title
if hls is not None:
self.app_data["hls"] = hls
if self.app_data != {}:
self.destination.set_default_app_data(msgpack.packb(self.app_data))
self.announce_interval = 20
self.server_loop()
def client_connected(self, link: RNS.Link):
pass
def files_generator(
self, path, data, request_id, link_id, remote_identity, requested_at
):
try:
filename = data.decode("utf-8")
url = f"{self.streampath}/{filename}"
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as response:
return response.read()
except Exception as e:
RNS.log(
f"Error while generating response to request {RNS.prettyhexrep(request_id)} on link {RNS.prettyhexrep(link_id)}, {e}",
RNS.LOG_ERROR,
)
return b""
def announce(self):
self.destination.announce()
RNS.log("Sent announce from " + RNS.prettyhexrep(self.destination.hash))
RNS.log(f"Active links: {len(self.destination.links)}")
def server_loop(self):
while True:
self.announce()
time.sleep(self.announce_interval * 60)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="rns live streaming")
parser.add_argument("--name")
parser.add_argument("--title")
parser.add_argument("--hls", default="mystream/index.m3u8")
parser.add_argument("--streampath", default="http://localhost:8888")
parser.add_argument("--config", default="./config")
args = parser.parse_args()
server = server(
name=args.name,
title=args.title,
hls=args.hls,
streampath=args.streampath,
configpath=args.config,
)