This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibreCarrier.py
105 lines (90 loc) · 3.12 KB
/
libreCarrier.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
105
'''
LibreCarrier
Description ----------------------------------
LibreCarrier makes any websocket client into a private communication channel.
This allows to easily connect applications and electronic objects to a network.
----------------------------------------------
Author : Remi Sarrailh (madnerd.org)
Email : [email protected]
License : MIT
Based on this tutorial by Simply Automationized
https://simplyautomationized.blogspot.fr/2015/09/raspberry-pi-create-websocket-api.html
'''
from twisted.internet import protocol, reactor, ssl
from twisted.web.server import Site
from twisted.web.static import File
from twisted.python import log
import argparse
import ConfigParser
import os
import sys
import socket
import threading
# Arguments/ Time
import time
from autobahn.twisted.websocket import (WebSocketServerFactory,
WebSocketServerProtocol, listenWS)
# Websocket
from modules import Settings
from modules.WebSocket import LibreClient
# Hashed password
import _cffi_backend # Need for pyinstaller
# LibreConnect Protocol
from autobahn.twisted.websocket import (WebSocketServerProtocol)
from autobahn.twisted.resource import WebSocketResource
# Autobahn/Twisted websocket
import json
# Get parameters from terminal or settings file
args = Settings.get()
if args["nogui"] is False:
from modules import Gui
failed_start = False
print("LibreCarrier - version " + args["version"])
print("By madnerd.org (https://github.com/madnerdorg/librecarrier)")
print("----------------------------------------------------------")
if args["debug"] is True:
print("Arguments -------------")
print(args)
settings_text = ""
if args["ssl"]:
settings_text = settings_text + "SSL "
ws_type = "wss://"
else:
ws_type = "ws://"
if args["offline"]:
settings_text = settings_text + "OFFLINE "
interface = "localhost"
else:
interface = ""
if args["password"] or args["password_hash"]:
settings_text = settings_text + "PASSWORD "
address = ws_type+"0.0.0.0"+":"+str(args["port"])
#############################
# WebSocket Server #
#############################
factory = WebSocketServerFactory(address)
if args["debug"] is True:
log.startLogging(sys.stdout)
ssl_private = args["certs"] + "/privkey.pem"
ssl_public = args["certs"] + "/cert.pem"
if args["ssl"]:
if os.path.isfile(ssl_private) and os.path.isfile(ssl_public):
contextFactory = ssl.DefaultOpenSSLContextFactory(ssl_private,
ssl_public)
resource = WebSocketResource(factory)
else:
print("[ERROR]: I can't find " + ssl_private + " and/or " + ssl_public)
failed_start = True
else:
resource = WebSocketResource(factory)
if not failed_start:
print(ws_type + args["server_ip"] + ":" + str(args["port"]) + " " + settings_text)
factory.protocol = LibreClient
root = File("web/")
root.putChild(b"ws", resource)
site = Site(root)
if args["ssl"]:
reactor.listenSSL(int(args["port"]), site, contextFactory, interface=interface)
else:
reactor.listenTCP(int(args["port"]), site, interface=interface)
reactor.run()