Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

216
LINES

< > BotCompany Repo | #1028671 // Eleu Cruddie [old]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 2568K of libraries. Click here for Pure Java version (17725L/99K).

1  
!7
2  
3  
// TODO: use DynEleu
4  
5  
// for sub-bots
6  
please include function serveRedirect.
7  
please include function serve403.
8  
please include function serve404.
9  
please include function serve500.
10  
please include function serveFile.
11  
please include function serveFileWithName.
12  
please include function serveFile_maxCache.
13  
please include function serveByteArray.
14  
please include function serveByteArray_maxCache.
15  
16  
sclass WebRequest {
17  
  NanoHTTPD.IHTTPSession httpSession;
18  
  S uri, subURI;
19  
  SS params;
20  
  S cookie;
21  
  Session session;
22  
  
23  
  *(NanoHTTPD.IHTTPSession *httpSession, S *uri, SS *params) {}
24  
  
25  
  S uri() { ret uri; }
26  
  SS params() { ret params; }
27  
  
28  
  S clientIP() {
29  
    ret unnull(getClientIPFromHeaders(httpSession.getHeaders()));
30  
  }
31  
  
32  
  S cookie() { ret cookie; }
33  
  
34  
  S domain() { ret mapGet(httpSession.getHeaders(), "host"); }
35  
  
36  
  User loggedInUser() {
37  
    ret session == null ? null : session.user();
38  
  }
39  
  
40  
  S googleClientID() {
41  
    S domain = lower(domain());
42  
    File jsonFile = googleClientSecretFileForDomain(domain);
43  
    if (!fileExists(jsonFile)) null;
44  
    
45  
    Map map = decodeJSONMap(loadTextFile(jsonFile));
46  
    map = (Map) map.get("web");
47  
    ret (S) map.get("client_id");
48  
  }
49  
}
50  
51  
concept User > ConceptWithGlobalID {
52  
  long lastSeen;
53  
}
54  
55  
concept CookieUser > User {
56  
  S cookie;
57  
58  
  toString { ret "[cookieUser " + md5(cookie) + "]"; }
59  
}
60  
61  
concept GoogleUser > User {
62  
  long googleLogInDate;
63  
  S googleEmail, googleFirstName, googleLastName;
64  
  bool googleEmailVerified;
65  
66  
  toString { ret googleFirstName + " " + googleLastName; }
67  
}
68  
69  
concept Session {
70  
  S cookie;
71  
  User user;
72  
  
73  
  User user() {
74  
    if (user == null)
75  
      cset(this, user := uniq CookieUser(+cookie));
76  
    ret user;
77  
  }
78  
}
79  
80  
cmodule Eleu3 > DynPrintLogAndEnabled {
81  
  //!include #1027628 // HTTP+HTTPS servers
82  
  !include #1029492 // HTTP+HTTPS servers with WebSockets
83  
  
84  
  switchable bool alwaysWikify;
85  
  
86  
  start {
87  
    dbIndexing(Session, 'cookie, CookieUser, 'cookie, GoogleUser, 'googleEmail);
88  
    dm_restartOnFieldChange enabled();
89  
    if (!enabled) ret;
90  
    set redirectHttpToHttps;
91  
    start_webServers(serverSocketFactory_botCompanyEtc());
92  
  }
93  
  
94  
  WebSocket newWebSocket(NanoHTTPD.IHTTPSession handshake) enter {
95  
    S domain = mapGet(handshake.getHeaders(), "host");
96  
    print("Making WebSocket. domain: " + domain);
97  
    WebSocket ws = new(handshake);
98  
    
99  
    S module = moduleForDomain(domain);
100  
    if (nempty(module)) pcall {
101  
      dm_callOpt(module, 'handleWebSocket, ws);
102  
    } else
103  
      ws.close(); // Hopefully just a reload
104  
      
105  
    ret ws;
106  
  }
107  
  
108  
  O webServe(S uri, SS params) {
109  
    Pair<Int, Bool> spamCheck = spamBlocker.checkRequest(uri, serveHttp_clientIP());
110  
    if (spamCheck.b) {
111  
      sleepSeconds(60.0);
112  
      ret print("go away");
113  
    }
114  
    printVars("webServe", +uri);
115  
116  
    // Serve Let's encrypt challenges
117  
    
118  
    if (startsWith(uri, "/.well-known/"))
119  
      ret loadTextFile(userDir("validation.txt"));
120  
      
121  
    // make WebRequest
122  
    
123  
    WebRequest req = new(NanoHTTPD.currentSession!, uri, params);
124  
    req.cookie = nu ServeHttp_CookieHandler(verbose := true).handle();
125  
    
126  
    // Get session
127  
    req.session = nempty(req.cookie) ? uniq Session(cookie := req.cookie) : null;
128  
    print(session := req.session);
129  
130  
    // Serve Google verification
131  
    
132  
    if (eqic(uri, "/google-verify"))
133  
      ret serveGoogleVerify(req);
134  
      
135  
    // Serve Rewrite DB
136  
    
137  
    S uri2 = appendSlash(uri);
138  
    new Matches m;
139  
    if (startsWith(uri2, "/db/", m)) {
140  
      S mod = null;
141  
      while licensed {
142  
        mod = dm_findModuleWithParams("#1028965/RewriteDBWebBot", enabled := true);
143  
        if (mod == null
144  
          && dm_moduleIsReloading(dm_findModule("#1028965/RewriteDBWebBot"))
145  
          || dm_moduleIsStarting(mod)) {
146  
          printWithTime("Module is starting, waiting");
147  
          sleep(100);
148  
        } else break;
149  
      }
150  
      
151  
      if (mod != null) {
152  
        req.subURI = "/" + m.rest();
153  
        ret eleu_callModuleHTMLMethod(mod, req);
154  
      }
155  
      ret "DB not loaded";
156  
    }
157  
    
158  
    S module = moduleForDomain(req.domain());    
159  
    if (nempty(module))
160  
      ret eleu_callModuleHTMLMethod(module, req);
161  
162  
    ret "I'm probably reloading a module, please check back in a few seconds";
163  
  }
164  
165  
  S moduleForDomain(S domain) {  
166  
    // WIKIFY
167  
    
168  
    /*if (domainIsUnder(domain, "test.wikify.live"))
169  
      try answer dm_findModuleWithParams("#1029430/WikifyLive", enabled := true);*/
170  
      
171  
    if (alwaysWikify || domainIsUnder(domain, "wikify.live") || swic(dropPrefixIC("www.", domain), "wikify."))
172  
      try answer dm_findModuleWithParams("#1029430/WikifyLive", enabled := true);
173  
      
174  
    if (domainIsUnder(domain, "avor.botcompany.de"))
175  
      try answer dm_findModule("#1029802/AvorDemonstrator");
176  
177  
    if (domainIsUnder(domain, "actualbackground.website"))
178  
      try answer dm_findModule("#1029697/ScribbleDemo");
179  
      
180  
    if (domainIsUnder(domain, "ir.masterbot.online"))
181  
      try answer dm_findModule("#1029824/ImageRecogDemoBot");
182  
183  
    if (domainIsUnder(domain, "masterbot.online"))
184  
      try answer dm_findModule("#1029807/WebCamDemo");
185  
186  
    // bibo.digital
187  
    
188  
    if (domainIsUnder(domain, "bibo.digital"))
189  
      try answer dm_findModule("#1029671/Bibo");
190  
      
191  
    // CRUDDIE as default
192  
193  
    try answer dm_findModuleWithParams("#1028961/Cruddie", enabled := true);
194  
    
195  
    null;
196  
  }
197  
198  
  O serveGoogleVerify(WebRequest req) {
199  
    Payload payload = googleVerifyUserToken2(req.googleClientID(), req.params.get("token"));
200  
    S email = payload == null ? null : payload.getEmail();
201  
    if (empty(email)) ret print("google-verify", "No");
202  
    
203  
    // create/update an object for the user
204  
    GoogleUser user = uniqCI_sync GoogleUser(googleEmail := email);
205  
    cset(user,
206  
      googleEmailVerified := payload.getEmailVerified(),
207  
      googleFirstName := strOrNull(payload.get("given_name")),
208  
      googleLastName := strOrNull(payload.get("family_name")),
209  
      googleLogInDate := now());
210  
    
211  
    // link user to session
212  
    cset(req.session, +user);
213  
      
214  
    ret print("google-verify", payload.getEmail() + " " + (payload.getEmailVerified() ? "(verified)" : "(not verified)"));
215  
  }
216  
}

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: #1028671
Snippet name: Eleu Cruddie [old]
Eternal ID of this version: #1028671/69
Text MD5: d8533915f50bbf3a69674d0ac4ac6c26
Transpilation MD5: 137f6f75c950aa759dac2ede5cd68956
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-23 11:35:12
Source code size: 6329 bytes / 216 lines
Pitched / IR pitched: No / No
Views / Downloads: 389 / 9916
Version history: 68 change(s)
Referenced in: [show references]