Mercurial Hosting > luan
annotate src/luan/host/WebHandler.java @ 1321:307e76ccd0d6
generalize separate logging
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 05 Feb 2019 22:36:55 -0700 |
parents | 5763597ca5c0 |
children | f41919741100 |
rev | line source |
---|---|
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
1 package luan.host; |
1135 | 2 |
3 import java.io.File; | |
4 import org.slf4j.Logger; | |
5 import org.slf4j.LoggerFactory; | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
6 import luan.webserver.Handler; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
7 import luan.webserver.Request; |
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
8 import luan.webserver.Response; |
1315 | 9 import luan.webserver.handlers.DomainHandler; |
1135 | 10 import luan.LuanState; |
11 import luan.LuanException; | |
12 import luan.LuanTable; | |
13 import luan.modules.IoLuan; | |
14 import luan.modules.JavaLuan; | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
15 import luan.modules.http.LuanHandler; |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
16 import luan.modules.logging.LuanLogger; |
1135 | 17 |
18 | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
19 public class WebHandler implements Handler { |
1135 | 20 private static final Logger logger = LoggerFactory.getLogger(WebHandler.class); |
21 | |
1315 | 22 private static final DomainHandler.Factory factory = new DomainHandler.Factory() { |
23 public Handler newHandler(String domain) { | |
24 File dir = new File(sitesDir,domain); | |
25 if( !dir.exists() /* && !recover(dir) */ ) | |
26 return null; | |
27 String dirStr = dir.toString(); | |
1230 | 28 |
1315 | 29 String logDir = dirStr + "/site/private/local/logs/web"; |
30 new File(logDir).mkdirs(); | |
1230 | 31 |
1315 | 32 LuanState luan = new LuanState(); |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
33 try { |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
34 LuanLogger.newLoggerRepository(luan); |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
35 } catch(LuanException e) { |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
36 throw new RuntimeException(e); |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
37 } |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
38 initLuan(luan,dirStr,domain,true); |
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
39 return new LuanHandler(luan); |
1230 | 40 } |
1315 | 41 }; |
1230 | 42 |
1202 | 43 public static String allowJavaFileName = "allow_java"; // change for security |
1315 | 44 private static final DomainHandler domainHandler = new DomainHandler(factory); |
1202 | 45 private static String sitesDir = null; |
1135 | 46 |
47 public static boolean isServing() { | |
48 return sitesDir != null; | |
49 } | |
50 | |
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
51 public WebHandler(String dir) { |
1135 | 52 if( sitesDir != null ) |
53 throw new RuntimeException("already set"); | |
54 if( !new File(dir).exists() ) | |
55 throw new RuntimeException(); | |
1230 | 56 sitesDir = dir; |
1135 | 57 } |
58 | |
1315 | 59 @Override public Response handle(Request request) { |
60 return domainHandler.handle(request); | |
1135 | 61 } |
62 | |
63 public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException { | |
1315 | 64 LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain); |
65 return luanHandler.runLuan(sourceText,sourceName); | |
1135 | 66 } |
67 | |
68 public static Object callSite(String domain,String fnName,Object... args) throws LuanException { | |
1315 | 69 LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain); |
70 return luanHandler.call_rpc(fnName,args); | |
1135 | 71 } |
72 | |
73 /* | |
74 private static boolean recover(File dir) { | |
75 File backups = new File(dir.getParentFile().getParentFile(),"backups"); | |
76 if( !backups.exists() ) | |
77 return false; | |
78 String name = dir.getName(); | |
79 File from = null; | |
80 for( File backup : backups.listFiles() ) { | |
81 File d = new File(backup,"current/"+name); | |
82 if( d.exists() && (from==null || from.lastModified() < d.lastModified()) ) | |
83 from = d; | |
84 } | |
85 if( from == null ) | |
86 return false; | |
87 if( !from.renameTo(dir) ) | |
88 throw new RuntimeException("couldn't rename "+from+" to "+dir); | |
89 logger.info("recovered "+name+" from "+from); | |
90 return true; | |
91 } | |
92 */ | |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
93 static LuanTable initLuan(LuanState luan,String dir,String domain,boolean logging) { |
1135 | 94 LuanTable init; |
95 try { | |
96 init = (LuanTable)luan.eval( | |
97 "local Luan = require 'luan:Luan.luan'\n" | |
98 +"local f = Luan.load_file 'classpath:luan/host/Init.luan'\n" | |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
99 +"return f('"+dir+"','"+domain+"',"+logging+")\n" |
1135 | 100 ); |
101 } catch(LuanException e) { | |
102 throw new RuntimeException(e); | |
103 } | |
104 File allowJavaFile = new File(dir,"site/private/"+allowJavaFileName); | |
105 if( !allowJavaFile.exists() ) { | |
106 JavaLuan.setSecurity( luan, javaSecurity ); | |
107 IoLuan.setSecurity( luan, ioSecurity(dir) ); | |
108 } | |
109 return init; | |
110 } | |
111 | |
1315 | 112 public static void removeHandler(String domain) { |
113 domainHandler.removeHandler(domain); | |
1135 | 114 } |
115 | |
116 public static void loadHandler(String domain) { | |
1315 | 117 domainHandler.getHandler(domain); |
1135 | 118 } |
119 | |
120 private static final IoLuan.Security ioSecurity(String dir) { | |
1174
bdf27aa2a65c
fix luanhost security bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1136
diff
changeset
|
121 final String siteUri = "file:" + dir + "/site"; |
1135 | 122 return new IoLuan.Security() { |
123 public void check(LuanState luan,String name) throws LuanException { | |
124 if( name.startsWith("file:") ) { | |
125 if( name.contains("..") ) | |
126 throw new LuanException("Security violation - '"+name+"' contains '..'"); | |
1174
bdf27aa2a65c
fix luanhost security bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1136
diff
changeset
|
127 if( !(name.equals(siteUri) || name.startsWith(siteUri+"/")) ) |
1135 | 128 throw new LuanException("Security violation - '"+name+"' outside of site dir"); |
129 } | |
130 else if( name.startsWith("classpath:luan/host/") ) { | |
131 throw new LuanException("Security violation"); | |
132 } | |
133 else if( name.startsWith("os:") || name.startsWith("bash:") ) { | |
134 throw new LuanException("Security violation"); | |
135 } | |
136 } | |
137 }; | |
138 } | |
139 | |
140 private static final JavaLuan.Security javaSecurity = new JavaLuan.Security() { | |
141 public void check(LuanState luan,String name) throws LuanException { | |
1238 | 142 if( !(name.startsWith("luan:") || name.matches("^file:[^/]+$")) ) |
1135 | 143 throw new LuanException("Security violation - only luan:* modules can load Java"); |
144 if( name.equals("luan:logging/Logging") ) | |
145 throw new LuanException("Security violation - cannot reload Logging"); | |
146 } | |
147 }; | |
148 } |