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

178
LINES

< > BotCompany Repo | #1029494 // Eleu 3 [backup without WebSockets]

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

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

!7

// for sub-bots
please include function serveRedirect.
please include function serve403.
please include function serve404.
please include function serve500.
please include function serveFile.
please include function serveFileWithName.
please include function serveFile_maxCache.
please include function serveByteArray.
please include function serveByteArray_maxCache.

sclass WebRequest {
  NanoHTTPD.IHTTPSession httpSession;
  S uri, subURI;
  SS params;
  S cookie;
  Session session;
  
  *(NanoHTTPD.IHTTPSession *httpSession, S *uri, SS *params) {}
  
  S clientIP() {
    ret unnull(getClientIPFromHeaders(httpSession.getHeaders()));
  }
  
  S cookie() { ret cookie; }
  
  S domain() { ret mapGet(httpSession.getHeaders(), "host"); }
  
  User loggedInUser() {
    ret session == null ? null : session.user();
  }
  
  S googleClientID() {
    S domain = lower(domain());
    File jsonFile = googleClientSecretFileForDomain(domain);
    if (!fileExists(jsonFile)) null;
    
    Map map = decodeJSONMap(loadTextFile(jsonFile));
    map = (Map) map.get("web");
    ret (S) map.get("client_id");
  }
}

concept User > ConceptWithGlobalID {
  long lastSeen;
}

concept CookieUser > User {
  S cookie;

  toString { ret "[cookieUser " + md5(cookie) + "]"; }
}

concept GoogleUser > User {
  long googleLogInDate;
  S googleEmail, googleFirstName, googleLastName;
  bool googleEmailVerified;

  toString { ret googleFirstName + " " + googleLastName; }
}

concept Session {
  S cookie;
  User user;
  
  User user() {
    if (user == null)
      cset(this, user := uniq CookieUser(+cookie));
    ret user;
  }
}

cmodule Eleu3 > DynPrintLogAndEnabled {
  !include #1027628 // HTTP+HTTPS servers

  switchable bool alwaysWikify;
  
  start {
    dbIndexing(Session, 'cookie, CookieUser, 'cookie, GoogleUser, 'googleEmail);
    dm_restartOnFieldChange enabled();
    if (!enabled) ret;
    set redirectHttpToHttps;
    start_webServers(serverSocketFactory_botCompanyEtc());
  }
  
  O webServe(S uri, SS params) {
    Pair<Int, Bool> spamCheck = spamBlocker.checkRequest(uri, serveHttp_clientIP());
    if (spamCheck.b) {
      sleepSeconds(60.0);
      ret print("go away");
    }
    printVars("webServe", +uri);

    // Serve Let's encrypt challenges
    
    if (startsWith(uri, "/.well-known/"))
      ret loadTextFile(userDir("validation.txt"));
      
    // make WebRequest
    
    WebRequest req = new(NanoHTTPD.currentSession!, uri, params);
    req.cookie = nu ServeHttp_CookieHandler(verbose := true).handle();
    
    // Get session
    req.session = nempty(req.cookie) ? uniq Session(cookie := req.cookie) : null;
    print(session := req.session);

    // Serve Google verification
    
    if (eqic(uri, "/google-verify"))
      ret serveGoogleVerify(req);
      
    // Serve Rewrite DB
    
    S uri2 = appendSlash(uri);
    new Matches m;
    if (startsWith(uri2, "/db/", m)) {
      S mod = null;
      while licensed {
        mod = dm_findModuleWithParams("#1028965/RewriteDBWebBot", enabled := true);
        if (mod == null
          && dm_moduleIsReloading(dm_findModule("#1028965/RewriteDBWebBot"))
          || dm_moduleIsStarting(mod)) {
          printWithTime("Module is starting, waiting");
          sleep(100);
        } else break;
      }
      
      if (mod != null) {
        dm_call(mod, 'setEleu, mc());
        req.subURI = "/" + m.rest();
        ret dm_call(mod, 'html, req);
      }
      ret "DB not loaded";
    }
    
    // Serve WIKIFY
    
    if (alwaysWikify || domainIsUnder(req.domain(), "wikify.live") || swic(dropPrefixIC("www.", req.domain()), "wikify.")) {
      S wikify = dm_findModuleWithParams("#1029430/WikifyLive", enabled := true);
      if (wikify != null) {
        dm_call(wikify, 'setEleu, mc());
        ret dm_call(wikify, 'html, req);
      }
    }
    
    // Serve CRUDDIE
    
    S cruddie = dm_findModuleWithParams("#1028961/Cruddie", enabled := true);
    if (cruddie != null) {
      dm_call(cruddie, 'setEleu, mc());
      ret dm_call(cruddie, 'html, req);
    }
      
    ret "hello";
  }

  O serveGoogleVerify(WebRequest req) {
    Payload payload = googleVerifyUserToken2(req.googleClientID(), req.params.get("token"));
    S email = payload == null ? null : payload.getEmail();
    if (empty(email)) ret print("google-verify", "No");
    
    // create/update an object for the user
    GoogleUser user = uniqCI_sync GoogleUser(googleEmail := email);
    cset(user,
      googleEmailVerified := payload.getEmailVerified(),
      googleFirstName := strOrNull(payload.get("given_name")),
      googleLastName := strOrNull(payload.get("family_name")),
      googleLogInDate := now());
    
    // link user to session
    cset(req.session, +user);
      
    ret print("google-verify", payload.getEmail() + " " + (payload.getEmailVerified() ? "(verified)" : "(not verified)"));
  }
}

Author comment

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: 158 / 222
Referenced in: [show references]