Mercurial Hosting > luan
annotate src/luan/host/main.luan @ 1205:8f14426f8ada
change cipher_suite
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 06 Mar 2018 20:15:02 -0700 |
parents | 0b55a1af5a44 |
children | 5ba660381bd5 |
rev | line source |
---|---|
1135 | 1 java() |
2 local Luan = require "luan:Luan.luan" | |
3 local error = Luan.error | |
1164 | 4 local type = Luan.type or error() |
1135 | 5 local ipairs = Luan.ipairs or error() |
6 local try = Luan.try or error() | |
7 local Io = require "luan:Io.luan" | |
8 local print = Io.print or error() | |
9 local Rpc = require "luan:Rpc.luan" | |
10 local Thread = require "luan:Thread.luan" | |
11 local String = require "luan:String.luan" | |
12 local literal = String.literal or error() | |
13 local lower = String.lower or error() | |
14 local matches = String.matches or error() | |
15 local Hosting = require "luan:host/Hosting.luan" | |
16 local Logging = require "luan:logging/Logging.luan" | |
17 local logger = Logging.logger "main" | |
1182 | 18 local WebHandler = Hosting.WebHandler or error() |
1181
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
19 local Util = require "classpath:luan/host/Util.luan" |
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
20 local read_password = Util.read_password or error() |
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
21 local write_password = Util.write_password or error() |
1135 | 22 |
23 | |
24 local sites_dir = Io.schemes.file(Hosting.sites_dir) | |
25 | |
26 sites_dir.mkdir() | |
27 | |
28 local function delete_unused(file) | |
29 if file.is_directory() then | |
30 if file.name() == "local" then | |
31 return false | |
32 end | |
33 local all_deleted = true | |
34 for _,child in ipairs(file.children()) do | |
35 all_deleted = delete_unused(child) and all_deleted | |
36 end | |
37 if not all_deleted then | |
38 return false | |
39 end | |
40 end | |
41 file.delete() | |
42 return true | |
43 end | |
44 | |
45 | |
46 local fns = Rpc.functions | |
47 | |
48 local function get_dir(domain,password) | |
1164 | 49 type(domain)=="string" or error() |
50 type(password)=="string" or error() | |
1135 | 51 domain = lower(domain) |
52 local dir = sites_dir.child(domain) | |
53 if dir.exists() then | |
1181
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
54 local pwd = read_password(domain) |
1135 | 55 if pwd ~= password then |
56 error "wrong password" | |
57 end | |
58 return dir.child("site") | |
59 else | |
60 return nil | |
61 end | |
62 end | |
63 | |
64 function fns.get(domain,password) | |
65 local site_dir = get_dir(domain,password) | |
66 if site_dir == nil then | |
67 return nil | |
68 end | |
69 | |
70 local children, file_info | |
71 | |
72 function children(dir) | |
73 if dir.name() == "local" then | |
74 return {} | |
75 end | |
76 local rtn = {} | |
77 for _,child in ipairs(dir.children()) do | |
78 local info = file_info(child) | |
79 if info ~= nil then | |
80 rtn[info.name] = info | |
81 end | |
82 end | |
83 return rtn | |
84 end | |
85 | |
86 function file_info(file) | |
87 local info = { name = file.name(), path = file.to_string() } | |
88 if file.is_directory() then | |
89 info.children = children(file) | |
90 elseif file.is_file() then | |
91 info.checksum = file.checksum() | |
92 else | |
93 return nil | |
94 end | |
95 return info | |
96 end | |
97 | |
98 return file_info(site_dir) | |
99 end | |
100 | |
101 function fns.create(domain,password) | |
1164 | 102 type(domain)=="string" or error() |
103 type(password)=="string" or error() | |
1135 | 104 domain = lower(domain) |
105 local dir = sites_dir.child(domain) | |
106 dir.exists() and error "already exists" | |
107 dir.mkdir() | |
1181
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
108 write_password(domain,password) |
1135 | 109 dir = dir.child("site") |
110 dir.mkdir() | |
111 return { name = dir.name(), path = dir.to_string(), children = {} } | |
112 end | |
113 | |
114 local function security(site_dir,file) | |
115 matches( file.to_string(), "^"..literal(site_dir.to_string()) ) or error "security violation" | |
116 end | |
117 | |
118 function fns.copy_file(domain,password,dir,name,content) | |
119 local site_dir = get_dir(domain,password) | |
120 site_dir or error "domain not found" | |
121 local file = Io.schemes.file(dir).child(name) | |
122 security(site_dir,file) | |
123 file.delete() | |
124 file.write(content) | |
125 end | |
126 | |
127 function fns.mkdir(domain,password,dir,name) | |
128 local site_dir = get_dir(domain,password) | |
129 site_dir or error "domain not found" | |
130 local file = Io.schemes.file(dir).child(name) | |
131 security(site_dir,file) | |
132 file.mkdir() | |
133 return { name = file.name(), path = file.to_string(), children = {} } | |
134 end | |
135 | |
136 function fns.delete_unused(domain,password,path) | |
137 local site_dir = get_dir(domain,password) | |
138 site_dir or error "domain not found" | |
139 local file = Io.schemes.file(path) | |
140 security(site_dir,file) | |
141 return delete_unused(file) | |
142 end | |
143 | |
144 function fns.update_handler(domain,password) | |
145 local site_dir = get_dir(domain,password) | |
146 site_dir or error "domain not found" | |
147 domain = lower(domain) | |
148 WebHandler.removeHandler(domain) | |
149 WebHandler.loadHandler(domain) | |
150 end | |
151 | |
152 function fns.delete(domain,password) | |
153 local site_dir = get_dir(domain,password) | |
154 site_dir or error "domain not found" | |
155 site_dir.parent().delete() | |
156 domain = lower(domain) | |
157 WebHandler.removeHandler(domain) | |
158 end | |
159 | |
160 function fns.exists(domain) | |
1164 | 161 type(domain)=="string" or error() |
1135 | 162 domain = lower(domain) |
163 return sites_dir.child(domain).exists() | |
164 end | |
165 | |
166 function fns.change_domain(old_domain,new_domain,password) | |
167 local old_dir = get_dir(old_domain,password) | |
168 old_dir or error "domain not found" | |
169 old_dir = old_dir.parent() | |
1164 | 170 type(new_domain)=="string" or error() |
1135 | 171 new_domain = lower(new_domain) |
172 local new_dir = sites_dir.child(new_domain) | |
173 new_dir.exists() and error "new_domain already exists" | |
174 WebHandler.removeHandler(old_domain) | |
175 old_dir.rename_to(new_dir.to_string()) | |
176 WebHandler.removeHandler(old_domain) | |
177 WebHandler.loadHandler(new_domain) | |
178 end | |
179 | |
180 function fns.change_password(domain,old_password,new_password) | |
181 local site_dir = get_dir(domain,old_password) | |
182 site_dir or error "domain not found" | |
1181
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
183 write_password(domain,new_password) |
1135 | 184 WebHandler.removeHandler(domain) |
185 WebHandler.loadHandler(domain) | |
186 end | |
187 | |
188 fns.call = WebHandler.callSite | |
189 | |
190 Thread.fork(Rpc.serve) |