aotrautils-srv 0.0.55 → 0.0.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- /*utils SERVER library associated with aotra version : «1.0.0.000 (11/07/2022-23:49:33)»*/
3
+ /*utils SERVER library associated with aotra version : «1.0.0.000 (12/07/2022-23:38:30)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -337,12 +337,14 @@ WebsocketImplementation={
337
337
  },
338
338
  };
339
339
 
340
- // DBG
341
- console.log("ADD RECEPTION ENTRY POINT channelName:«"+channelName+"»! nodeServerInstance.receptionEntryPoints.length:",nodeServerInstance.receptionEntryPoints.length);
342
340
 
343
341
  nodeServerInstance.receptionEntryPoints.push(receptionEntryPoint);
344
342
 
345
343
 
344
+ // DBG
345
+ console.log("ADD RECEPTION ENTRY POINT channelName:«"+channelName+"»! nodeServerInstance.receptionEntryPoints.length:",nodeServerInstance.receptionEntryPoints.length);
346
+
347
+
346
348
  return nodeServerInstance;
347
349
  },
348
350
 
@@ -417,8 +419,8 @@ WebsocketImplementation={
417
419
  console.log("SERVER : ON CONNECTION !");
418
420
 
419
421
 
420
- // if(contains(nodeServerInstance.clientsSockets, clientSocket)) return;
421
- // nodeServerInstance.clientsSockets.push(clientSocket);
422
+ // if(contains(nodeServerInstance.clientsSockets, clientSocket)) return;
423
+ // nodeServerInstance.clientsSockets.push(clientSocket);
422
424
 
423
425
 
424
426
  // DBG
@@ -979,7 +981,7 @@ initNodeServer=function(doOnClientConnection=null, doOnFinalizeServer=null, /*OP
979
981
  // TRACE
980
982
  console.log("Server launched.");
981
983
  console.log("Usage : node <server.js> conf {port:[port], sslCertPath:[ssl certificate path | unsecure ], sslKeyPath:[ssl key path], serverConfig:[JSON server configuration]}");
982
- console.log("Or (to generate password hash) : node <server.js> hash <clientId@repositoryName> <clearText>");
984
+ console.log("Or (to generate password hash) : node <server.js> hash <clientId@repositoryName> <clearTextSecretString>");
983
985
  // EXAMPLE : node orita-srv.js hash orita.global@saltkey 1234567890
984
986
  console.log("Server launched.");
985
987
 
@@ -1019,8 +1021,7 @@ initNodeServer=function(doOnClientConnection=null, doOnFinalizeServer=null, /*OP
1019
1021
  }
1020
1022
  });
1021
1023
  isForceUnsecure=(argCLCertPath==="unsecure");
1022
-
1023
-
1024
+
1024
1025
 
1025
1026
  const aotraNodeServer={config:serverConfig};
1026
1027
  aotraNodeServer.serverManager={ start:()=>{/*DEFAULT START FUNCTION, WILL BE OVERRIDEN LATER*/}};
@@ -1299,7 +1300,7 @@ function getListManager(config){
1299
1300
 
1300
1301
 
1301
1302
 
1302
- /*utils COMMONS library associated with aotra version : «1.0.0.000 (11/07/2022-23:49:33)»*/
1303
+ /*utils COMMONS library associated with aotra version : «1.0.0.000 (12/07/2022-23:38:30)»*/
1303
1304
  /*-----------------------------------------------------------------------------*/
1304
1305
 
1305
1306
 
@@ -2743,6 +2744,299 @@ aotest.profile=function(rootObject,methodName,visited=[]){
2743
2744
 
2744
2745
 
2745
2746
 
2747
+
2748
+ //================================================================
2749
+ //================= Cryptography utility methods =================
2750
+ //================================================================
2751
+
2752
+
2753
+ // NOT AOTESTABLE !
2754
+ //TODO : develop :
2755
+ //getHashedString=monitorProgression(100000,
2756
+ window.getHashedString=function(str,/*OPTIONAL*/isHeavyTreatment){
2757
+
2758
+ //CAUTION : YOU MUST *NOT* CHANGE THIS VALUE, UNLESS THE HASH HEAVY TREATMENT VALUE IN YOUR AOTRA SERVER-SIDE CODEIS THE SAME !
2759
+ const HEAVY_TREATMENT_LOOP_COUNT=100000;
2760
+
2761
+ if(isHeavyTreatment){
2762
+
2763
+ // If needed, we make sure the hashing will take a long time, so that
2764
+ // if the private salt key is leaked, it will be fairly difficult to crack
2765
+ // password write protection and content encryption :
2766
+
2767
+ let result=str;
2768
+ for(var i=0;i<HEAVY_TREATMENT_LOOP_COUNT;i++){
2769
+ result=getHashedString(result,false);
2770
+ }
2771
+
2772
+
2773
+ return result;
2774
+
2775
+ }else{
2776
+
2777
+
2778
+ // SHA-256 hash (sucks less) :
2779
+ // DEPENDENCY : SJCL JS libray (Stanford Javascript Crypto Library)
2780
+ if(typeof(sjcl)!="undefined"){
2781
+ let result=sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(str));
2782
+
2783
+ // TODO : develop :
2784
+ // hash.progressionMonitor.progress(1);
2785
+
2786
+
2787
+ return result;
2788
+ }
2789
+
2790
+ // if not found : MD5 hash (sucks) :
2791
+ //TRACE
2792
+ log("WARN : Could not find SHA hashing, using MD5. This sucks, and this very likely will prevent your aotra client to be able to write data to aotra server.");
2793
+
2794
+ return hex_md5(str);
2795
+ }
2796
+ }
2797
+ //);
2798
+
2799
+ function encrypt(strToEncrypt,key,/*OPTIONAL*/mode){
2800
+ if(!mode || typeof(sjcl)=="undefined") mode="xor";
2801
+ mode=mode.trim();
2802
+
2803
+ if(contains(mode,"aes") && contains(mode,"xor")){
2804
+
2805
+ // XOR :
2806
+ var xorStr=btoa(encodeXORNoPattern(strToEncrypt,key));
2807
+
2808
+ var aesStr=
2809
+ // AES
2810
+ sjcl.encrypt(key,xorStr);
2811
+
2812
+ return aesStr;
2813
+ }
2814
+
2815
+ // DEPENDENCY : SJCL JS libray (Stanford Javascript Crypto Library)
2816
+ if(mode==="aes")
2817
+ return sjcl.encrypt(key,strToEncrypt);
2818
+ return btoa(encodeXORNoPattern(strToEncrypt,key));
2819
+
2820
+ }
2821
+
2822
+ function decrypt(strToDecrypt,key,/*OPTIONAL*/mode){
2823
+ if(!mode || typeof(sjcl)=="undefined") mode="xor";
2824
+ mode=mode.trim();
2825
+
2826
+ if(contains(mode,"aes") && contains(mode,"xor")){
2827
+
2828
+ // AES
2829
+ var aesStr=sjcl.decrypt(key,strToDecrypt);
2830
+
2831
+ var xorStr=
2832
+ // XOR :
2833
+ encodeXORNoPattern(atob(aesStr),key);
2834
+
2835
+ return xorStr;
2836
+
2837
+ }
2838
+
2839
+ // DEPENDENCY : SJCL JS libray (Stanford Javascript Crypto Library)
2840
+ if(mode==="aes")
2841
+ return sjcl.decrypt(key,strToDecrypt);
2842
+ return encodeXORNoPattern(atob(strToDecrypt),key);
2843
+
2844
+
2845
+ }
2846
+
2847
+ function encodeXORNoPattern(strToEncode,key){
2848
+ let result="";
2849
+ let keyChunk=key;
2850
+ let keyLength=keyChunk.length;
2851
+ let previousChars="";
2852
+ let cnt=0;
2853
+ for(let i=0;i<strToEncode.length;i++){
2854
+ let char=strToEncode.charAt(i);
2855
+ let keyByte=keyChunk.charAt(cnt);
2856
+
2857
+ result+=String.fromCharCode(char.charCodeAt(0)^keyByte.charCodeAt(0));
2858
+
2859
+ if(cnt%keyLength===(keyLength-1)){
2860
+ cnt=0;
2861
+ // NO : TOO LONG TREATMENT !: We hash with all the previous blocks too :
2862
+ // We hash with a character of the previous chunk (they add up at each new chunk):
2863
+ keyChunk=getHashedString(previousChars+keyChunk);
2864
+
2865
+ previousChars+=keyChunk.charAt(keyChunk.length-1);
2866
+ }else{
2867
+ cnt++;
2868
+ }
2869
+ }
2870
+ return result;
2871
+ }
2872
+
2873
+ function generateRandomString(length,/*NULLABLE*/mode){
2874
+
2875
+ // This list must be very conservative, because we want to be able to pass it through GET URLs !
2876
+ // OLD (not enough conservative ?) : const ALLOWED_CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0912346789_-£¢¤¬²³¼½¾";
2877
+ const ALLOWED_CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0912346789!-:;";
2878
+
2879
+
2880
+ if(typeof(length)==="string"){
2881
+ if(contains(length,"->")){
2882
+ length=Math.getRandomInRange(length);
2883
+ }else{
2884
+ length=64;
2885
+ }
2886
+ }
2887
+
2888
+ var result="";
2889
+
2890
+ if(mode==="simple"){
2891
+
2892
+ for(var i=0;i<length;i++){
2893
+ var value=Math.getRandomInArray(ALLOWED_CHARS);
2894
+ result+=value;
2895
+ }
2896
+
2897
+ return result;
2898
+ }else if(mode==="human"){
2899
+
2900
+ const markovGraph={
2901
+ // For simplicity sakes, we ignore the consonants and vowels doubling possibilities:
2902
+ consonants:{// They always accept vowels at their following
2903
+ // Indicate ^ to specify wich of the other group letter to exclude as direct follower of the given letter :
2904
+ "b":["r","l"],
2905
+ "c":["h","t","k","l","r"],
2906
+ "d":["r"],
2907
+ "f":["r","l"],
2908
+ "g":["r","l"],
2909
+ "h":[],
2910
+ "j":["r"],
2911
+ "k":["r","l","t","h"],
2912
+ "l":["t"],
2913
+ "m":["b","p"],
2914
+ "n":["t","d"],
2915
+ "p":["h","r","s","t","l"],
2916
+ "q":["r","l"],
2917
+ "r":["t","c","d","k","q","h"],
2918
+ "s":["t","c","d","k","q","h"],
2919
+ "t":["r","h"],
2920
+ "v":["r","l"],
2921
+ "w":["r"],
2922
+ "x":["p"],
2923
+ "z":["r","h"],
2924
+ },
2925
+ vowels:{// They always accept consonants at their following
2926
+ "a":["i","u","y"],
2927
+ "e":["i","u","y"],
2928
+ "i":["e","o"],
2929
+ "o":["i","u"],
2930
+ "u":["i"],
2931
+ "y":[],
2932
+ },
2933
+ };
2934
+
2935
+ const LETTERS_TYPES=Object.keys(markovGraph);
2936
+
2937
+ const CURR_LETTER_MODE=Math.getRandomInArray(LETTERS_TYPES);
2938
+ const POSSIBLE_LETTERS=Object.keys(markovGraph[CURR_LETTER_MODE]);
2939
+ const CURR_LETTER=Math.getRandomInArray(POSSIBLE_LETTERS);
2940
+
2941
+ var automataState={
2942
+ currentLetterMode:CURR_LETTER_MODE,
2943
+ possibleLetters:POSSIBLE_LETTERS,
2944
+ currentLetter:CURR_LETTER,
2945
+
2946
+ calculateLetter:function(){
2947
+
2948
+ this.currentLetter=Math.getRandomInArray(this.possibleLetters);
2949
+
2950
+ var sameLetters=Object.keys( markovGraph[this.currentLetterMode] );
2951
+ if(!contains(sameLetters,this.currentLetter)){
2952
+ // If we picked from the same group : we don't change of group to pick
2953
+ // If we picked from the other group : we do change of group to pick
2954
+
2955
+ var otherLetterMode=foreach(LETTERS_TYPES,(item)=>{
2956
+ if(item!==this.currentLetterMode) return item;
2957
+ });
2958
+
2959
+ this.currentLetterMode=otherLetterMode;
2960
+ }
2961
+
2962
+ },
2963
+
2964
+ updatePossibleLettersForLetter:function(){
2965
+
2966
+ var nextPossibleLetters=[];
2967
+ var nextNotPossibleLetters=[];
2968
+
2969
+ // We store those of the other group marked «^»
2970
+ foreach(markovGraph[this.currentLetterMode][this.currentLetter],(letter)=>{
2971
+ if(letter.charAt(0)==="^"){
2972
+ nextNotPossibleLetters.push(letter.charAt(1));
2973
+ }else{
2974
+ nextPossibleLetters.push(letter);
2975
+ }
2976
+ });
2977
+
2978
+ var otherLetterMode=foreach(LETTERS_TYPES,(item)=>{
2979
+ if(item!==this.currentLetterMode) return item;
2980
+ });
2981
+ var otherLetters=Object.keys(markovGraph[otherLetterMode]);
2982
+
2983
+ // We exclude those marked «^»
2984
+ foreach( otherLetters ,(letter)=>{
2985
+ if(!contains(nextNotPossibleLetters,letter)){
2986
+ nextPossibleLetters.push(letter);
2987
+ }
2988
+ });
2989
+
2990
+ this.possibleLetters=nextPossibleLetters;
2991
+ }
2992
+ };
2993
+
2994
+ var i=0;
2995
+ do{
2996
+
2997
+ result+=automataState.currentLetter;
2998
+
2999
+ automataState.updatePossibleLettersForLetter();
3000
+ automataState.calculateLetter();
3001
+
3002
+ i++;
3003
+ }while(i<length);
3004
+
3005
+ return result;
3006
+ }
3007
+
3008
+ for(var i=0;i<length;i++){
3009
+ var value=Math.getRandomInt(255,0);
3010
+ result+=String.fromCharCode(value);
3011
+ }
3012
+
3013
+ if(mode==="base64"){
3014
+ return btoa(result);
3015
+ }
3016
+
3017
+ return result;
3018
+ }
3019
+
3020
+
3021
+
3022
+ // NOT AOTESTABLE !
3023
+ function getUniqueIdWithDate(){
3024
+ return getHashedString(getNow() + "");
3025
+ }
3026
+
3027
+
3028
+
3029
+
3030
+
3031
+
3032
+ //================================================================
3033
+ //================= Miscelanneous utility methods =================
3034
+ //================================================================
3035
+
3036
+
3037
+
3038
+
3039
+
2746
3040
  // NOT AOTESTABLE !
2747
3041
 
2748
3042
  // This function is for simple variables, like strings, booleans, or Objects you
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.55",
3
+ "version": "0.0.58",
4
4
  "main": "aotrautils-srv.build.js",
5
5
  "description": "A library for vanilla javascript utils (server-side) used in aotra javascript CMS",
6
6
  "author": "Jeremie Ratomposon <info@alqemia.com> (https://alqemia.com)",