forked from EternityForest/KaithemAutomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (116 loc) · 4.57 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#Copyright Daniel Black 2013
#This file is part of Kaithem Automation.
#Kaithem Automation is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, version 3.
#Kaithem Automation is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with Kaithem Automation. If not, see <http://www.gnu.org/licenses/>.
#This file is the main entry point of the app
import pages
import weblogin
import auth
import cherrypy
import ManageUsers
import directories
import os,sys
import modules
import settings
import usrpages
#2 and 3 have basically the same module with diferent names
if sys.version_info < (3,0):
from urllib2 import urlopen
else:
from urllib.request import urlopen
#Yes, This really is the only way i know of to get your public IP.
try:
u= urlopen("http://ipecho.net/plain")
MyExternalIPAdress = u.read()
except:
MyExternalIPAdress = "unknown"
finally:
u.close()
#Initialize the autorization module
auth.initializeAuthentication()
#Load all modules from the active modules directory
modules.loadAll()
#This class represents the "/" root o the web app
class webapproot():
#"/" is mapped to this
@cherrypy.expose
def index(self,*path,**data):
#Was there a reason not to use pages.require
if 'auth' in cherrypy.request.cookie.keys():
if auth.checkTokenPermission(cherrypy.request.cookie['auth'].value,"/admin/mainpage.view"):
return pages.get_template('index.html').render(
user = cherrypy.request.cookie['user'].value,
)
return self.login.index()
@cherrypy.expose
def save(self,*path,**data):
pages.require('/admin/settings.edit')
return pages.get_template('save.html').render()
#docs,about,helpmenu, and license are just static pages
@cherrypy.expose
def docs(self,*path,**data):
return pages.get_template('help/help.html').render()
@cherrypy.expose
def about(self,*path,**data):
return pages.get_template('help/about.html').render(myip = MyExternalIPAdress)
@cherrypy.expose
def helpmenu(self,*path,**data):
return pages.get_template('help/index.html').render()
@cherrypy.expose
def license(self,*path,**data):
return pages.get_template('help/license.html').render()
class Errors():
@cherrypy.expose
def permissionerror(self,):
return pages.get_template('errors/permissionerror.html').render()
@cherrypy.expose
def alreadyexists(self,):
return pages.get_template('errors/alreadyexists.html').render()
@cherrypy.expose
def loginerror(self,):
return pages.get_template('errors/loginerror.html').render()
@cherrypy.expose
def wrongmethod(self,):
return pages.get_template('errors/wrongmethod.html').render()
#There are lots of other objects ad classes represeting subfolders of the website so we attatch them
root = webapproot()
root.login = weblogin.LoginScreen()
root.auth = ManageUsers.ManageAuthorization()
root.modules = modules.WebInterface()
root.settings = settings.Settings()
root.errors = Errors()
root.pages = usrpages.KaithemPage()
#Start cherrrypy
dn = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
server_config={
'server.socket_host': '0.0.0.0',
'server.socket_port':8001,
'server.ssl_module':'builtin',
'server.ssl_certificate':os.path.join(dn,'ssl/certificate.cert'),
'server.ssl_private_key':os.path.join(dn,'ssl/certificate.key'),
}
cherrypy.config["tools.encode.on"] = True
cherrypy.config["tools.encode.encoding"] = "utf-8"
cherrypy.config["tools.decode.on"] = True
cherrypy.config["tools.decode.encoding"] = "utf-8"
conf = {
'/static':
{'tools.staticdir.on': True,
'tools.staticdir.dir':os.path.join(dn,'static'),
'tools.expires.on' : True,
'tools.expires.secs' : 1000,
"tools.sessions.on": False,
'tools.caching.on' : True,
'tools.caching.delay' : 3600
}
}
cherrypy.config.update(server_config)
cherrypy.quickstart(root,'/',config=conf)