Uses 2568K of libraries. Click here for Pure Java version (16468L/91K).
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 | |
78 | switchable bool alwaysWikify; |
79 | |
80 | start { |
81 | dbIndexing(Session, 'cookie, CookieUser, 'cookie, GoogleUser, 'googleEmail); |
82 | dm_restartOnFieldChange enabled(); |
83 | if (!enabled) ret; |
84 | set redirectHttpToHttps; |
85 | start_webServers(serverSocketFactory_botCompanyEtc()); |
86 | } |
87 | |
88 | O webServe(S uri, SS params) { |
89 | Pair<Int, Bool> spamCheck = spamBlocker.checkRequest(uri, serveHttp_clientIP()); |
90 | if (spamCheck.b) { |
91 | sleepSeconds(60.0); |
92 | ret print("go away"); |
93 | } |
94 | printVars("webServe", +uri); |
95 | |
96 | // Serve Let's encrypt challenges |
97 | |
98 | if (startsWith(uri, "/.well-known/")) |
99 | ret loadTextFile(userDir("validation.txt")); |
100 | |
101 | // make WebRequest |
102 | |
103 | WebRequest req = new(NanoHTTPD.currentSession!, uri, params); |
104 | req.cookie = nu ServeHttp_CookieHandler(verbose := true).handle(); |
105 | |
106 | // Get session |
107 | req.session = nempty(req.cookie) ? uniq Session(cookie := req.cookie) : null; |
108 | print(session := req.session); |
109 | |
110 | // Serve Google verification |
111 | |
112 | if (eqic(uri, "/google-verify")) |
113 | ret serveGoogleVerify(req); |
114 | |
115 | // Serve Rewrite DB |
116 | |
117 | S uri2 = appendSlash(uri); |
118 | new Matches m; |
119 | if (startsWith(uri2, "/db/", m)) { |
120 | S mod = null; |
121 | while licensed { |
122 | mod = dm_findModuleWithParams("#1028965/RewriteDBWebBot", enabled := true); |
123 | if (mod == null |
124 | && dm_moduleIsReloading(dm_findModule("#1028965/RewriteDBWebBot")) |
125 | || dm_moduleIsStarting(mod)) { |
126 | printWithTime("Module is starting, waiting"); |
127 | sleep(100); |
128 | } else break; |
129 | } |
130 | |
131 | if (mod != null) { |
132 | dm_call(mod, 'setEleu, mc()); |
133 | req.subURI = "/" + m.rest(); |
134 | ret dm_call(mod, 'html, req); |
135 | } |
136 | ret "DB not loaded"; |
137 | } |
138 | |
139 | // Serve WIKIFY |
140 | |
141 | if (alwaysWikify || domainIsUnder(req.domain(), "wikify.live") || swic(dropPrefixIC("www.", req.domain()), "wikify.")) { |
142 | S wikify = dm_findModuleWithParams("#1029430/WikifyLive", enabled := true); |
143 | if (wikify != null) { |
144 | dm_call(wikify, 'setEleu, mc()); |
145 | ret dm_call(wikify, 'html, req); |
146 | } |
147 | } |
148 | |
149 | // Serve CRUDDIE |
150 | |
151 | S cruddie = dm_findModuleWithParams("#1028961/Cruddie", enabled := true); |
152 | if (cruddie != null) { |
153 | dm_call(cruddie, 'setEleu, mc()); |
154 | ret dm_call(cruddie, 'html, req); |
155 | } |
156 | |
157 | ret "hello"; |
158 | } |
159 | |
160 | O serveGoogleVerify(WebRequest req) { |
161 | Payload payload = googleVerifyUserToken2(req.googleClientID(), req.params.get("token")); |
162 | S email = payload == null ? null : payload.getEmail(); |
163 | if (empty(email)) ret print("google-verify", "No"); |
164 | |
165 | // create/update an object for the user |
166 | GoogleUser user = uniqCI_sync GoogleUser(googleEmail := email); |
167 | cset(user, |
168 | googleEmailVerified := payload.getEmailVerified(), |
169 | googleFirstName := strOrNull(payload.get("given_name")), |
170 | googleLastName := strOrNull(payload.get("family_name")), |
171 | googleLogInDate := now()); |
172 | |
173 | // link user to session |
174 | cset(req.session, +user); |
175 | |
176 | ret print("google-verify", payload.getEmail() + " " + (payload.getEmailVerified() ? "(verified)" : "(not verified)")); |
177 | } |
178 | } |
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: | #1029494 |
Snippet name: | Eleu 3 [backup without WebSockets] |
Eternal ID of this version: | #1029494/1 |
Text MD5: | fab249407ef5544e9dc0094152d49c8b |
Transpilation MD5: | 5642cdb35ecd183bac069adef8b2bcc7 |
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-15 14:32:41 |
Source code size: | 5051 bytes / 178 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 159 / 224 |
Referenced in: | [show references] |