1 | !7 |
2 | |
3 | // for sub-bots |
4 | please include function serveRedirect. |
5 | please include function serve403. |
6 | please include function serve404. |
7 | please include function serve500. |
8 | please include function serveFile. |
9 | please include function serveFileWithName. |
10 | please include function serveFile_maxCache. |
11 | please include function serveByteArray. |
12 | please include function serveByteArray_maxCache. |
13 | |
14 | sclass WebRequest { |
15 | NanoHTTPD.IHTTPSession httpSession; |
16 | S uri, subURI; |
17 | SS params; |
18 | S cookie; |
19 | Session session; |
20 | |
21 | *(NanoHTTPD.IHTTPSession *httpSession, S *uri, SS *params) {} |
22 | |
23 | S clientIP() { |
24 | ret unnull(getClientIPFromHeaders(httpSession.getHeaders())); |
25 | } |
26 | |
27 | S cookie() { ret cookie; } |
28 | |
29 | S domain() { ret mapGet(httpSession.getHeaders(), "host"); } |
30 | |
31 | User loggedInUser() { |
32 | ret session == null ? null : session.user(); |
33 | } |
34 | |
35 | S googleClientID() { |
36 | S domain = lower(domain()); |
37 | File jsonFile = googleClientSecretFileForDomain(domain); |
38 | if (!fileExists(jsonFile)) null; |
39 | |
40 | Map map = decodeJSONMap(loadTextFile(jsonFile)); |
41 | map = (Map) map.get("web"); |
42 | ret (S) map.get("client_id"); |
43 | } |
44 | } |
45 | |
46 | concept User > ConceptWithGlobalID { |
47 | long lastSeen; |
48 | } |
49 | |
50 | concept CookieUser > User { |
51 | S cookie; |
52 | |
53 | toString { ret "[cookieUser " + md5(cookie) + "]"; } |
54 | } |
55 | |
56 | concept GoogleUser > User { |
57 | long googleLogInDate; |
58 | S googleEmail, googleFirstName, googleLastName; |
59 | bool googleEmailVerified; |
60 | |
61 | toString { ret googleFirstName + " " + googleLastName; } |
62 | } |
63 | |
64 | concept Session { |
65 | S cookie; |
66 | User user; |
67 | |
68 | User user() { |
69 | if (user == null) |
70 | cset(this, user := uniq CookieUser(+cookie)); |
71 | ret user; |
72 | } |
73 | } |
74 | |
75 | cmodule Eleu3 > DynPrintLogAndEnabled { |
76 | //!include #1027628 // HTTP+HTTPS servers |
77 | !include #1029492 // HTTP+HTTPS servers with WebSockets |
78 | |
79 | switchable bool alwaysWikify; |
80 | |
81 | start { |
82 | dbIndexing(Session, 'cookie, CookieUser, 'cookie, GoogleUser, 'googleEmail); |
83 | dm_restartOnFieldChange enabled(); |
84 | if (!enabled) ret; |
85 | set redirectHttpToHttps; |
86 | start_webServers(serverSocketFactory_botCompanyEtc()); |
87 | } |
88 | |
89 | WebSocket newWebSocket(NanoHTTPD.IHTTPSession handshake) enter { |
90 | S domain = mapGet(handshake.getHeaders(), "host"); |
91 | print("Making WebSocket. domain: " + domain); |
92 | WebSocket ws = new(handshake); |
93 | |
94 | S module = moduleForDomain(domain); |
95 | if (nempty(module)) pcall { |
96 | dm_callOpt(module, 'handleWebSocket, ws); |
97 | } else |
98 | ws.close(); // Hopefully just a reload |
99 | |
100 | ret ws; |
101 | } |
102 | |
103 | O webServe(S uri, SS params) { |
104 | Pair<Int, Bool> spamCheck = spamBlocker.checkRequest(uri, serveHttp_clientIP()); |
105 | if (spamCheck.b) { |
106 | sleepSeconds(60.0); |
107 | ret print("go away"); |
108 | } |
109 | printVars("webServe", +uri); |
110 | |
111 | // Serve Let's encrypt challenges |
112 | |
113 | if (startsWith(uri, "/.well-known/")) |
114 | ret loadTextFile(userDir("validation.txt")); |
115 | |
116 | // make WebRequest |
117 | |
118 | WebRequest req = new(NanoHTTPD.currentSession!, uri, params); |
119 | req.cookie = nu ServeHttp_CookieHandler(verbose := true).handle(); |
120 | |
121 | // Get session |
122 | req.session = nempty(req.cookie) ? uniq Session(cookie := req.cookie) : null; |
123 | print(session := req.session); |
124 | |
125 | // Serve Google verification |
126 | |
127 | if (eqic(uri, "/google-verify")) |
128 | ret serveGoogleVerify(req); |
129 | |
130 | // Serve Rewrite DB |
131 | |
132 | S uri2 = appendSlash(uri); |
133 | new Matches m; |
134 | if (startsWith(uri2, "/db/", m)) { |
135 | S mod = null; |
136 | while licensed { |
137 | mod = dm_findModuleWithParams("#1028965/RewriteDBWebBot", enabled := true); |
138 | if (mod == null |
139 | && dm_moduleIsReloading(dm_findModule("#1028965/RewriteDBWebBot")) |
140 | || dm_moduleIsStarting(mod)) { |
141 | printWithTime("Module is starting, waiting"); |
142 | sleep(100); |
143 | } else break; |
144 | } |
145 | |
146 | if (mod != null) { |
147 | dm_call(mod, 'setEleu, mc()); |
148 | req.subURI = "/" + m.rest(); |
149 | ret dm_call(mod, 'html, req); |
150 | } |
151 | ret "DB not loaded"; |
152 | } |
153 | |
154 | S module = moduleForDomain(req.domain()); |
155 | if (nempty(module)) { |
156 | dm_call(module, 'setEleu, mc()); |
157 | ret dm_call(module, 'html, req); |
158 | } |
159 | |
160 | ret "I'm probably reloading a module, please check back in a few seconds"; |
161 | } |
162 | |
163 | S moduleForDomain(S domain) { |
164 | // WIKIFY |
165 | |
166 | /*if (domainIsUnder(domain, "test.wikify.live")) |
167 | try answer dm_findModuleWithParams("#1029430/WikifyLive", enabled := true);*/ |
168 | |
169 | if (alwaysWikify || domainIsUnder(domain, "wikify.live") || swic(dropPrefixIC("www.", domain), "wikify.")) |
170 | try answer dm_findModuleWithParams("#1029430/WikifyLive", enabled := true); |
171 | |
172 | // CRUDDIE |
173 | |
174 | try answer dm_findModuleWithParams("#1028961/Cruddie", enabled := true); |
175 | |
176 | null; |
177 | } |
178 | |
179 | O serveGoogleVerify(WebRequest req) { |
180 | Payload payload = googleVerifyUserToken2(req.googleClientID(), req.params.get("token")); |
181 | S email = payload == null ? null : payload.getEmail(); |
182 | if (empty(email)) ret print("google-verify", "No"); |
183 | |
184 | // create/update an object for the user |
185 | GoogleUser user = uniqCI_sync GoogleUser(googleEmail := email); |
186 | cset(user, |
187 | googleEmailVerified := payload.getEmailVerified(), |
188 | googleFirstName := strOrNull(payload.get("given_name")), |
189 | googleLastName := strOrNull(payload.get("family_name")), |
190 | googleLogInDate := now()); |
191 | |
192 | // link user to session |
193 | cset(req.session, +user); |
194 | |
195 | ret print("google-verify", payload.getEmail() + " " + (payload.getEmailVerified() ? "(verified)" : "(not verified)")); |
196 | } |
197 | } |
Began life as a copy of #1028671
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1029617 |
Snippet name: | Eleu 3 [backup before DynEleu] |
Eternal ID of this version: | #1029617/1 |
Text MD5: | 7363ac2af53e433d34478c09768e697a |
Author: | stefan |
Category: | javax |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-08-28 15:33:51 |
Source code size: | 5698 bytes / 197 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 175 / 193 |
Referenced in: | [show references] |