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

197
LINES

< > BotCompany Repo | #1029617 // Eleu 3 [backup before DynEleu]

JavaX source code (Dynamic Module) - run with: Stefan's OS

!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
  !include #1029492 // HTTP+HTTPS servers with WebSockets
  
  switchable bool alwaysWikify;
  
  start {
    dbIndexing(Session, 'cookie, CookieUser, 'cookie, GoogleUser, 'googleEmail);
    dm_restartOnFieldChange enabled();
    if (!enabled) ret;
    set redirectHttpToHttps;
    start_webServers(serverSocketFactory_botCompanyEtc());
  }
  
  WebSocket newWebSocket(NanoHTTPD.IHTTPSession handshake) enter {
    S domain = mapGet(handshake.getHeaders(), "host");
    print("Making WebSocket. domain: " + domain);
    WebSocket ws = new(handshake);
    
    S module = moduleForDomain(domain);
    if (nempty(module)) pcall {
      dm_callOpt(module, 'handleWebSocket, ws);
    } else
      ws.close(); // Hopefully just a reload
      
    ret ws;
  }
  
  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";
    }
    
    S module = moduleForDomain(req.domain());    
    if (nempty(module)) {
      dm_call(module, 'setEleu, mc());
      ret dm_call(module, 'html, req);
    }
    
    ret "I'm probably reloading a module, please check back in a few seconds";
  }

  S moduleForDomain(S domain) {  
    // WIKIFY
    
    /*if (domainIsUnder(domain, "test.wikify.live"))
      try answer dm_findModuleWithParams("#1029430/WikifyLive", enabled := true);*/
      
    if (alwaysWikify || domainIsUnder(domain, "wikify.live") || swic(dropPrefixIC("www.", domain), "wikify."))
      try answer dm_findModuleWithParams("#1029430/WikifyLive", enabled := true);
      
    // CRUDDIE

    try answer dm_findModuleWithParams("#1028961/Cruddie", enabled := true);
    
    null;
  }

  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: #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: 174 / 192
Referenced in: [show references]