ws_bert_login.d 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. alias int8 = byte; // -128 — 127
  2. alias int16 = short; // -32768 — 32767
  3. alias int32 = int; // -2147483648 — 2147483647
  4. alias int64 = long; // -9223372036854775808 — 9223372036854775807
  5. alias uint8 = ubyte; // 0 — 255
  6. alias uint16 = ushort; // 0 — 65535
  7. alias uint32 = uint; // 0 — 4294967295
  8. alias uint64 = ulong; // 0 — 18446744073709551615
  9. alias f32 = float;
  10. alias f64 = double;
  11. // char '\xFF' unsigned 8 bit (UTF-8 code unit)
  12. // wchar '\uFFFF' unsigned 16 bit (UTF-16 code unit)
  13. // dchar '\U0000FFFF' unsigned 32 bit (UTF-32 code unit)
  14. import vibe.core.core;
  15. import vibe.http.router;
  16. import vibe.http.server;
  17. import vibe.http.fileserver;
  18. import vibe.http.websockets;
  19. import vibe.core.log;
  20. import std.stdio;
  21. import std.string;
  22. import std.array;
  23. import std.conv : to;
  24. import bert; // https://github.com/221V/dlang_erl_bert https://git.4dev.win/game1/dlang_erl_bert
  25. import secured.symmetric; // https://github.com/221V/SecureD // https://github.com/221V/js_AES256CBC
  26. import secured.rsa;
  27. import session;
  28. import mustache;
  29. alias MustacheEngine!(string) Mustache;
  30. string byte_arr_to_str(ubyte[] arr){
  31. string[] str_arr;
  32. foreach(elem; arr){
  33. str_arr ~= to!string(elem);
  34. }
  35. return "[" ~ str_arr.join(",") ~ "]";
  36. }
  37. ubyte[] str_to_byte_arr(string str_arr){
  38. str_arr = str_arr.replace("[", "").replace("]", "");
  39. string[] parts = str_arr.split(",");
  40. ubyte[] result;
  41. result.reserve(parts.length);
  42. foreach(part; parts){
  43. result ~= to!ubyte(part.strip());
  44. }
  45. return result;
  46. }
  47. void ws_bert_handle(scope WebSocket sock){
  48. //foreach(pair; req.headers.byKeyValue()){
  49. // writeln("Header: ", pair.key, " = ", pair.value);
  50. //}
  51. // simple echo server + :)
  52. //string client_id = req.attributes.get("client_id", "");
  53. //writeln("96 client_id = ", client_id);
  54. // https://vibed.org/api/vibe.http.websockets/WebSocket
  55. // https://vibed.org/api/vibe.http.websockets/WebSocket.request
  56. // https://vibed.org/api/vibe.http.server/HTTPServerRequest
  57. //writeln("sock.request = ", sock.request); // GET /ws_login_test HTTP/1.1
  58. //writeln("sock.request.headers = ", sock.request.headers); // ["Host": "127.0.0.1:8080", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0", "Accept": "*/*", "Accept-Language": "uk-UA,uk;q=0.8,en-US;q=0.5,en;q=0.3", "Accept-Encoding": "gzip, deflate, br, zstd", "Sec-WebSocket-Version": "13", "Origin": "http://127.0.0.1:8080", "Sec-WebSocket-Extensions": "permessage-deflate", "Sec-WebSocket-Key": "NW+zQGtSdkuSRHWuU/VevA==", "DNT": "1", "Sec-GPC": "1", "Connection": "keep-alive, Upgrade", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "websocket", "Sec-Fetch-Site": "same-origin", "Pragma": "no-cache", "Cache-Control": "no-cache", "Upgrade": "websocket"]
  59. //writeln("sock.request.context = ", sock.request.context); // []
  60. //writeln("sock.request.params = ", sock.request.params); // []
  61. //string client_id = req.params.get("client_id", "");
  62. while(sock.connected){
  63. //auto msg = sock.receiveText();
  64. //sock.send(msg ~ " :)");
  65. auto msg = sock.receiveBinary();
  66. if(msg == "1"){
  67. sock.send("0"); // ws PING - PONG
  68. }else{
  69. writeln("Received: ", msg);
  70. auto decoder = new BertDecoder( cast(ubyte[])msg.dup );
  71. auto decoded = decoder.decode();
  72. msg_match(decoded, sock);
  73. }
  74. }
  75. // todo delete data in userSessions here
  76. }
  77. void msg_match(BertValue decoded, WebSocket sock){
  78. writeln("Decoded: ", decoded.toString());
  79. auto rsa_keypair = new RSA(2048); // Only allows for (2048/8)-42 = 214 bytes to be asymmetrically RSA encrypted
  80. scope(exit) rsa_keypair.destroy();
  81. ubyte[] rsa_private_key = rsa_keypair.getPrivateKey(null);
  82. ubyte[] rsa_public_key = rsa_keypair.getPublicKey();
  83. //writeln("rsa_private_key = ", rsa_private_key); // [45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 80, 82, 73, 86, 65, 84, 69, 32, 75, 69, 89, 45, 45, 45, 45, 45, 10, 77, 73, 73, 69, 118, 103, 73, 66, 65, 68, 65, 78, 66, 103, 107, 113, 104, 107, 105, 71, 57, 119, 48, 66, 65, 81, 69, 70, 65, 65, 83, 67, 66, 75, 103, 119, 103, 103, 83, 107, 65, 103, 69, 65, 65, 111, 73, 66, 65, 81, 67, 79, 71, 120, 100, 122, 90, 73, 105, 53, 50, 52, 51, 111, 10, 90, 47, 48, 102, 118, 119, 86, 122, 66, 111, 118, 109, 52, 83, 43, 87, 82, 43, 118, 116, 79, 121, 50, 55, 117, 54, 50, 68, 108, 104, 97, 72, 115, 98, 121, 73, 108, 56, 52, 101, 53, 100, 100, 78, 49, 48, 112, 69, 71, 82, 84, 115, 65, 97, 89, 73, 102, 100, 120, 119, 56, 82, 50, 65, 10, 47, 80, 83, 77, 120, 80, 106, 102, 97, 113, 88, 119, 52, 71, 98, 90, 54, 69, 72, 72, 74, 74, 113, 99, 43, 56, 53, 53, 65, 50, 89, 100, 89, 77, 57, 99, 103, 83, 105, 72, 99, 84, 102, 77, 115, 56, 69, 80, 73, 90, 119, 89, 69, 100, 122, 117, 65, 55, 73, 74, 106, 57, 82, 75, 10, 77, 56, 122, 73, 104, 47, 108, 52, 90, 98, 81, 85, 47, 68, 47, 83, 53, 97, 57, 109, 65, 113, 52, 48, 85, 86, 84, 48, 75, 53, 104, 87, 56, 109, 115, 69, 118, 73, 51, 70, 81, 116, 79, 88, 73, 52, 51, 105, 82, 80, 99, 101, 122, 108, 47, 104, 82, 111, 74, 65, 104, 82, 65, 102, 10, 117, 102, 103, 101, 66, 65, 69, 113, 83, 88, 66, 77, 57, 108, 70, 103, 117, 116, 108, 67, 76, 73, 106, 88, 90, 75, 104, 67, 98, 51, 87, 68, 85, 106, 70, 88, 74, 81, 111, 102, 76, 48, 114, 100, 83, 86, 88, 78, 100, 57, 86, 90, 114, 106, 110, 90, 54, 83, 114, 69, 73, 88, 43, 116, 10, 50, 66, 75, 119, 47, 103, 50, 77, 55, 89, 117, 117, 104, 107, 49, 73, 66, 75, 84, 65, 84, 120, 108, 103, 84, 86, 107, 121, 66, 103, 107, 109, 116, 110, 99, 51, 119, 69, 78, 78, 107, 43, 71, 89, 77, 86, 53, 111, 67, 99, 116, 73, 98, 112, 104, 111, 55, 79, 103, 57, 107, 57, 49, 87, 10, 121, 51, 105, 109, 110, 57, 49, 47, 65, 103, 77, 66, 65, 65, 69, 67, 103, 103, 69, 65, 75, 113, 120, 47, 67, 57, 78, 43, 98, 90, 87, 57, 108, 84, 79, 90, 73, 48, 51, 112, 86, 73, 80, 56, 81, 106, 54, 81, 88, 69, 104, 72, 49, 73, 116, 72, 98, 103, 57, 114, 105, 85, 55, 113, 10, 103, 67, 66, 68, 78, 104, 43, 107, 77, 99, 111, 105, 115, 69, 106, 57, 68, 57, 115, 97, 87, 99, 56, 97, 55, 89, 84, 105, 84, 72, 90, 84, 118, 108, 98, 52, 70, 48, 102, 86, 86, 114, 52, 47, 105, 56, 106, 80, 105, 104, 79, 66, 111, 85, 120, 103, 77, 57, 69, 52, 84, 85, 88, 85, 10, 104, 102, 68, 55, 66, 70, 80, 121, 43, 69, 49, 114, 68, 77, 108, 85, 75, 102, 101, 86, 87, 80, 77, 81, 84, 100, 101, 70, 81, 80, 76, 85, 116, 78, 50, 43, 87, 116, 47, 101, 98, 105, 88, 89, 103, 77, 55, 74, 87, 99, 116, 73, 115, 49, 69, 78, 119, 68, 57, 71, 84, 68, 90, 83, 10, 115, 71, 98, 50, 100, 122, 66, 108, 102, 110, 69, 78, 83, 121, 84, 50, 119, 87, 47, 105, 53, 49, 43, 115, 122, 105, 51, 106, 87, 73, 65, 89, 75, 73, 67, 121, 111, 119, 65, 99, 87, 105, 118, 76, 87, 98, 74, 102, 88, 50, 105, 105, 47, 101, 66, 117, 43, 72, 103, 119, 54, 106, 108, 100, 10, 88, 103, 75, 118, 66, 47, 56, 76, 67, 65, 56, 104, 54, 90, 113, 109, 57, 73, 80, 100, 77, 88, 73, 55, 74, 119, 72, 109, 72, 73, 81, 82, 101, 112, 120, 69, 97, 121, 57, 105, 75, 122, 111, 107, 50, 81, 117, 111, 97, 111, 74, 73, 83, 101, 79, 122, 109, 68, 65, 76, 115, 71, 77, 109, 10, 75, 72, 120, 83, 118, 90, 74, 78, 49, 74, 81, 65, 54, 110, 88, 70, 79, 70, 52, 84, 55, 74, 90, 119, 72, 121, 103, 87, 112, 90, 87, 106, 86, 105, 81, 120, 65, 49, 53, 89, 85, 81, 75, 66, 103, 81, 67, 47, 102, 109, 98, 74, 116, 105, 104, 70, 65, 88, 67, 52, 74, 43, 103, 53, 10, 113, 50, 71, 114, 100, 47, 121, 83, 116, 43, 67, 110, 97, 104, 118, 79, 97, 113, 102, 109, 80, 112, 73, 106, 103, 81, 75, 100, 65, 81, 53, 79, 119, 112, 74, 85, 102, 49, 72, 54, 80, 121, 101, 105, 108, 82, 51, 67, 70, 80, 84, 68, 97, 122, 114, 105, 103, 84, 66, 117, 56, 72, 88, 109, 10, 55, 90, 116, 112, 120, 87, 114, 82, 73, 107, 105, 104, 105, 98, 53, 108, 47, 47, 51, 88, 116, 97, 117, 83, 106, 102, 77, 100, 106, 98, 67, 67, 121, 50, 71, 78, 57, 51, 117, 77, 97, 56, 101, 53, 66, 49, 102, 50, 80, 99, 104, 99, 75, 113, 77, 76, 112, 118, 122, 72, 55, 108, 55, 73, 10, 105, 105, 70, 75, 70, 66, 122, 49, 118, 109, 74, 52, 84, 76, 73, 79, 101, 84, 54, 97, 83, 122, 48, 107, 77, 81, 75, 66, 103, 81, 67, 57, 43, 97, 47, 48, 119, 49, 85, 104, 66, 89, 52, 88, 119, 52, 84, 90, 115, 70, 66, 88, 105, 122, 120, 72, 54, 119, 47, 100, 122, 54, 47, 90, 10, 69, 107, 68, 55, 67, 113, 80, 49, 43, 66, 111, 101, 66, 115, 82, 86, 51, 116, 50, 79, 85, 108, 116, 85, 90, 57, 73, 56, 100, 68, 101, 47, 121, 74, 122, 71, 51, 100, 85, 118, 107, 102, 75, 56, 100, 110, 68, 113, 56, 110, 111, 47, 90, 111, 43, 51, 48, 73, 114, 53, 88, 79, 104, 81, 10, 77, 85, 89, 73, 79, 51, 43, 116, 106, 107, 54, 102, 83, 69, 76, 57, 55, 43, 73, 121, 56, 65, 103, 120, 103, 86, 108, 101, 81, 51, 109, 87, 79, 77, 120, 89, 57, 79, 116, 71, 100, 73, 84, 85, 115, 47, 100, 99, 49, 82, 81, 99, 68, 65, 120, 115, 117, 76, 114, 68, 105, 73, 101, 103, 10, 81, 117, 103, 99, 71, 113, 52, 103, 114, 119, 75, 66, 103, 81, 67, 109, 73, 43, 106, 83, 81, 84, 83, 88, 67, 106, 84, 100, 48, 101, 72, 116, 104, 118, 47, 113, 89, 79, 80, 70, 57, 55, 114, 50, 66, 86, 50, 121, 114, 86, 54, 87, 113, 78, 114, 112, 54, 116, 104, 49, 108, 54, 68, 117, 10, 80, 71, 110, 119, 51, 98, 89, 109, 81, 47, 48, 68, 86, 104, 84, 72, 101, 112, 56, 82, 110, 85, 115, 116, 107, 97, 67, 80, 52, 66, 105, 114, 68, 113, 79, 48, 70, 82, 65, 106, 43, 110, 98, 66, 107, 70, 121, 99, 54, 111, 107, 50, 83, 101, 120, 100, 55, 115, 74, 114, 100, 97, 56, 98, 10, 67, 53, 72, 77, 78, 108, 102, 43, 56, 78, 81, 76, 101, 73, 43, 99, 109, 79, 88, 111, 56, 83, 85, 99, 121, 118, 105, 117, 68, 101, 89, 50, 88, 52, 71, 97, 88, 47, 68, 119, 86, 81, 110, 105, 68, 99, 105, 43, 80, 102, 116, 79, 113, 116, 67, 86, 81, 81, 75, 66, 103, 81, 67, 119, 10, 86, 76, 101, 114, 101, 106, 114, 106, 52, 97, 73, 89, 69, 74, 83, 89, 56, 102, 120, 65, 112, 97, 52, 89, 71, 117, 105, 112, 79, 52, 75, 43, 43, 121, 66, 120, 89, 49, 106, 52, 106, 52, 97, 43, 90, 65, 50, 77, 110, 115, 89, 75, 117, 103, 113, 110, 100, 52, 52, 43, 75, 113, 107, 115, 10, 83, 43, 113, 111, 56, 51, 74, 84, 75, 121, 97, 104, 73, 49, 90, 47, 51, 80, 122, 51, 75, 66, 48, 120, 119, 108, 71, 99, 84, 48, 52, 97, 85, 81, 72, 116, 49, 106, 119, 87, 80, 99, 90, 103, 78, 100, 82, 111, 109, 50, 88, 98, 88, 99, 120, 100, 70, 118, 66, 54, 102, 67, 113, 52, 10, 56, 112, 78, 74, 53, 108, 115, 84, 70, 69, 102, 79, 110, 104, 82, 49, 105, 66, 109, 89, 82, 116, 54, 112, 99, 51, 86, 50, 49, 74, 122, 43, 100, 100, 117, 51, 97, 108, 82, 66, 90, 81, 75, 66, 103, 68, 110, 105, 110, 116, 90, 43, 81, 49, 111, 52, 98, 90, 102, 105, 122, 83, 113, 65, 10, 114, 122, 115, 43, 50, 112, 88, 87, 51, 97, 78, 71, 52, 108, 82, 76, 76, 104, 113, 67, 98, 53, 48, 73, 81, 114, 107, 73, 66, 119, 90, 47, 48, 69, 80, 75, 72, 66, 121, 112, 103, 81, 86, 54, 117, 119, 102, 71, 115, 117, 75, 114, 114, 119, 55, 107, 83, 103, 70, 121, 88, 67, 122, 113, 10, 74, 103, 73, 112, 55, 121, 43, 48, 84, 107, 101, 57, 112, 113, 90, 84, 65, 116, 75, 101, 69, 100, 56, 86, 66, 71, 110, 97, 104, 90, 75, 54, 111, 116, 55, 97, 43, 49, 85, 98, 99, 80, 51, 121, 120, 69, 104, 71, 104, 77, 76, 84, 77, 101, 116, 77, 88, 111, 102, 100, 103, 110, 48, 98, 10, 88, 51, 113, 47, 107, 77, 56, 68, 55, 71, 119, 89, 77, 89, 77, 104, 98, 99, 112, 43, 100, 49, 115, 99, 10, 45, 45, 45, 45, 45, 69, 78, 68, 32, 80, 82, 73, 86, 65, 84, 69, 32, 75, 69, 89, 45, 45, 45, 45, 45, 10]
  84. //writeln("rsa_public_key = ", rsa_public_key); // [45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 80, 85, 66, 76, 73, 67, 32, 75, 69, 89, 45, 45, 45, 45, 45, 10, 77, 73, 73, 66, 73, 106, 65, 78, 66, 103, 107, 113, 104, 107, 105, 71, 57, 119, 48, 66, 65, 81, 69, 70, 65, 65, 79, 67, 65, 81, 56, 65, 77, 73, 73, 66, 67, 103, 75, 67, 65, 81, 69, 65, 106, 104, 115, 88, 99, 50, 83, 73, 117, 100, 117, 78, 54, 71, 102, 57, 72, 55, 56, 70, 10, 99, 119, 97, 76, 53, 117, 69, 118, 108, 107, 102, 114, 55, 84, 115, 116, 117, 55, 117, 116, 103, 53, 89, 87, 104, 55, 71, 56, 105, 74, 102, 79, 72, 117, 88, 88, 84, 100, 100, 75, 82, 66, 107, 85, 55, 65, 71, 109, 67, 72, 51, 99, 99, 80, 69, 100, 103, 80, 122, 48, 106, 77, 84, 52, 10, 51, 50, 113, 108, 56, 79, 66, 109, 50, 101, 104, 66, 120, 121, 83, 97, 110, 80, 118, 79, 101, 81, 78, 109, 72, 87, 68, 80, 88, 73, 69, 111, 104, 51, 69, 51, 122, 76, 80, 66, 68, 121, 71, 99, 71, 66, 72, 99, 55, 103, 79, 121, 67, 89, 47, 85, 83, 106, 80, 77, 121, 73, 102, 53, 10, 101, 71, 87, 48, 70, 80, 119, 47, 48, 117, 87, 118, 90, 103, 75, 117, 78, 70, 70, 85, 57, 67, 117, 89, 86, 118, 74, 114, 66, 76, 121, 78, 120, 85, 76, 84, 108, 121, 79, 78, 52, 107, 84, 51, 72, 115, 53, 102, 52, 85, 97, 67, 81, 73, 85, 81, 72, 55, 110, 52, 72, 103, 81, 66, 10, 75, 107, 108, 119, 84, 80, 90, 82, 89, 76, 114, 90, 81, 105, 121, 73, 49, 50, 83, 111, 81, 109, 57, 49, 103, 49, 73, 120, 86, 121, 85, 75, 72, 121, 57, 75, 51, 85, 108, 86, 122, 88, 102, 86, 87, 97, 52, 53, 50, 101, 107, 113, 120, 67, 70, 47, 114, 100, 103, 83, 115, 80, 52, 78, 10, 106, 79, 50, 76, 114, 111, 90, 78, 83, 65, 83, 107, 119, 69, 56, 90, 89, 69, 49, 90, 77, 103, 89, 74, 74, 114, 90, 51, 78, 56, 66, 68, 84, 90, 80, 104, 109, 68, 70, 101, 97, 65, 110, 76, 83, 71, 54, 89, 97, 79, 122, 111, 80, 90, 80, 100, 86, 115, 116, 52, 112, 112, 47, 100, 10, 102, 119, 73, 68, 65, 81, 65, 66, 10, 45, 45, 45, 45, 45, 69, 78, 68, 32, 80, 85, 66, 76, 73, 67, 32, 75, 69, 89, 45, 45, 45, 45, 45, 10]
  85. //ubyte[214] plaintext214 = 2; // 2 being an arbitrary value
  86. auto plaintext214 = cast(ubyte[])"12345678testтест";
  87. ubyte[] encMessage214 = rsa_keypair.encrypt(plaintext214);
  88. //writeln("encMessage214.length = ", encMessage214.length); // 256
  89. //writeln("encMessage214 = ", encMessage214); // [18, 27, 216, 71, 100, 90, 235, 225, 149, 43, 161, 168, 63, 98, 157, 173, 6, 58, 5, 142, 19, 227, 162, 87, 138, 201, 98, 78, 23, 149, 100, 174, 159, 235, 205, 100, 35, 84, 144, 148, 158, 75, 100, 4, 11, 79, 99, 194, 171, 199, 247, 97, 8, 70, 15, 147, 67, 245, 94, 240, 122, 56, 40, 170, 41, 90, 9, 80, 108, 168, 208, 123, 218, 174, 72, 154, 225, 184, 132, 127, 248, 164, 74, 50, 55, 233, 215, 178, 229, 205, 123, 10, 197, 21, 35, 135, 85, 11, 1, 123, 15, 94, 242, 103, 69, 209, 209, 113, 190, 63, 229, 232, 84, 168, 162, 231, 90, 202, 182, 168, 166, 85, 96, 1, 67, 190, 42, 72, 155, 220, 168, 80, 57, 51, 149, 173, 240, 204, 72, 210, 52, 163, 219, 248, 168, 70, 132, 32, 168, 160, 240, 150, 187, 128, 153, 190, 76, 125, 101, 150, 202, 194, 130, 100, 149, 238, 207, 225, 17, 29, 97, 141, 230, 31, 249, 227, 96, 214, 142, 24, 174, 41, 20, 178, 242, 196, 214, 59, 26, 168, 9, 238, 19, 95, 181, 75, 169, 0, 249, 78, 35, 172, 117, 138, 159, 124, 148, 50, 97, 160, 110, 96, 69, 248, 69, 168, 19, 131, 18, 208, 161, 88, 49, 217, 93, 113, 159, 49, 201, 33, 123, 80, 121, 196, 110, 91, 26, 179, 177, 11, 75, 91, 102, 219, 108, 166, 180, 53, 15, 222, 226, 79, 188, 52, 214, 132]
  90. ubyte[] decMessage214 = rsa_keypair.decrypt(encMessage214);
  91. //writeln("decMessage214 = ", decMessage214);
  92. writeln("decMessage214 = ", cast(string)decMessage214);
  93. ubyte[] key;
  94. ubyte[] iv;
  95. if(decoded.type_ == BertType.Tuple){
  96. auto decoded1 = decoded.tupleValue;
  97. if(decoded1.length == 1){
  98. if(auto num1 = cast(uint8)decoded1[0].intValue){ // we can use js AES for additional password encrypt for login-logout // ws.send(enc(tuple( number(1) )));
  99. if(num1 == 1){ // init login -- get key + iv for encrypt password and send to server
  100. /*
  101. ubyte[] test_pass = cast(ubyte[])"12345678";
  102. SymmetricKey key = generateSymmetricKey( SymmetricAlgorithm.AES256_CBC );
  103. EncryptedData enc = encrypt(key, test_pass);
  104. //auto iv_hex = toHexString!(LetterCase.lower)(enc.iv);
  105. //auto encrypt = toHexString!(LetterCase.lower)(enc.cipherText);
  106. auto iv_hex = enc.iv;
  107. auto encrypt = enc.cipherText;
  108. writeln("Key: ", key.key);
  109. //writeln("Key: ", key.toString); // base64 encoded string
  110. //writeln("IV: ", toHexString!(LetterCase.lower)(enc.iv));
  111. writeln("IV: ", iv_hex);
  112. //writeln("Encrypt: ", enc);
  113. writeln("Encrypt: ", encrypt);
  114. */
  115. SymmetricKeyIV rand_key_iv = generateSymmetricKeyIV(); // default SymmetricAlgorithm.AES256_CBC
  116. writeln("rand key: ", rand_key_iv.key);
  117. writeln("rand iv: ", rand_key_iv.iv);
  118. string client_id = generateCookie();
  119. userSessions[client_id ~ "_key"] = rand_key_iv.key;
  120. userSessions[client_id ~ "_iv"] = rand_key_iv.iv;
  121. /*
  122. //auto test_pass = cast(ubyte[])"12345678";
  123. auto test_pass = cast(ubyte[])"12345678testтест";
  124. auto key = cast(ubyte[])[34, 74, 12, 214, 126, 234, 101, 147, 13, 32, 244, 185, 45, 217, 142, 33, 213, 116, 63, 179, 84, 23, 138, 187, 134, 130, 234, 54, 48, 66, 20, 152];
  125. auto iv = cast(ubyte[])[62, 133, 213, 219, 194, 200, 76, 142, 202, 16, 12, 237, 163, 147, 65, 93];
  126. auto encrypted = encrypt(key, iv, test_pass, SymmetricAlgorithm.AES256_CBC);
  127. writeln("Encrypted: ", encrypted.cipherText); // [223, 86, 210, 55, 192, 240, 144, 50, 159, 4, 238, 182, 171, 185, 80, 48] // [90, 85, 212, 32, 94, 33, 182, 43, 20, 183, 121, 59, 232, 45, 180, 158, 153, 9, 54, 45, 244, 32, 85, 24, 162, 206, 56, 235, 107, 194, 143, 192]
  128. //auto encrypted_data = cast(ubyte[])[223, 86, 210, 55, 192, 240, 144, 50, 159, 4, 238, 182, 171, 185, 80, 48];
  129. auto encrypted_data = cast(ubyte[])[90, 85, 212, 32, 94, 33, 182, 43, 20, 183, 121, 59, 232, 45, 180, 158, 153, 9, 54, 45, 244, 32, 85, 24, 162, 206, 56, 235, 107, 194, 143, 192];
  130. ubyte[] decrypted = decrypt(key, iv, encrypted_data, SymmetricAlgorithm.AES256_CBC);
  131. writeln("Decrypted: ", decrypted); // [49, 50, 51, 52, 53, 54, 55, 56] // [49, 50, 51, 52, 53, 54, 55, 56, 116, 101, 115, 116, 209, 130, 208, 181, 209, 129, 209, 130]
  132. writeln("Decrypted: ", cast(string)decrypted); // "12345678" // "12345678testтест"
  133. */
  134. sock.send("{window.key = new Uint8Array(" ~ byte_arr_to_str(rand_key_iv.key) ~ ");" ~
  135. "window.iv = new Uint8Array(" ~ byte_arr_to_str(rand_key_iv.iv) ~ ");" ~
  136. "window.uid = '" ~ client_id ~ "';" ~
  137. "do_log_in();}");
  138. /*
  139. ubyte[] key = sock.context.get("aes_key", "");
  140. ubyte[] iv = sock.context.get("aes_iv", "");
  141. if( key.empty || iv.empty ){}else{
  142. sock.send("{window.key = new Uint8Array(" ~ byte_arr_to_str(key) ~ ");" ~
  143. "window.iv = new Uint8Array(" ~ byte_arr_to_str(iv) ~ ");" ~
  144. "do_log_in();}");
  145. }
  146. */
  147. } // else do nothing
  148. } // else do nothing
  149. }else if(decoded1.length == 4){ // {2, "uid", "login", "encrypted_pass"}
  150. if(auto code2 = cast(uint8)decoded1[0].intValue){ // 2
  151. if(code2 == 2){
  152. if(auto client_id = cast(string)decoded1[1].binaryValue){
  153. writeln("client_id = ", client_id);
  154. if(auto login_str = cast(string)decoded1[2].binaryValue){
  155. writeln("login_str = ", login_str);
  156. if(auto maybe_encrypted_pass = cast(string)decoded1[3].binaryValue){
  157. writeln("maybe_encrypted_pass = ", maybe_encrypted_pass, " ", typeof(maybe_encrypted_pass).stringof); // Decoded: {2, <<116,101,115,116,49>>, <<91,49,50,53,44,50,51,54,44,50,50,48,44,50,53,53,44,49,50,48,44,49,54,57,44,49,56,51,44,49,48,50,44,50,49,49,44,51,53,44,50,52,54,44,50,49,55,44,55,49,44,50,54,44,50,49,50,44,56,56,93>>} // maybe_encoded_pass = [125,236,220,255,120,169,183,102,211,35,246,217,71,26,212,88] string
  158. //sock.send("{console.log('" ~ str1 ~ " la-la-la" ~ "')}"); //
  159. try{
  160. auto encrypted_pass = str_to_byte_arr(maybe_encrypted_pass);
  161. writeln("encoded_pass = ", encrypted_pass);
  162. if( (client_id ~ "_key") in userSessions){
  163. key = userSessions[client_id ~ "_key"];
  164. }
  165. if( (client_id ~ "_iv") in userSessions){
  166. iv = userSessions[client_id ~ "_iv"];
  167. }
  168. writeln("key = ", key);
  169. writeln("iv = ", iv);
  170. ubyte[] pass = decrypt(key, iv, encrypted_pass, SymmetricAlgorithm.AES256_CBC);
  171. writeln("Decrypted: ", pass); // byte array
  172. writeln("Decrypted: ", cast(string)pass); // string
  173. }catch(Exception e){} // skip err, do nothing
  174. }
  175. } // else do nothing
  176. } // else do nothing
  177. } // else do nothing
  178. } // else do nothing
  179. }else if(decoded1.length == 3){
  180. if(auto num1 = cast(uint8)decoded1[0].intValue){
  181. writeln("num1 = ", num1, " ", typeof(num1).stringof); // ws.send(enc(tuple( number(1), number(42), number(777) ))); // Decoded: {1, 42, 777} // num1 = 1 ubyte
  182. sock.send("{console.log(" ~ to!string(num1 + 42) ~ ")}"); // got 43 in browser console
  183. } // else do nothing
  184. if(auto str1 = cast(string)decoded1[1].binaryValue){
  185. writeln("str1 = ", str1, " ", typeof(str1).stringof); // ws.send(enc(tuple( number(1), bin('blabla'), number(777) ))); // Decoded: {1, <<98,108,97,98,108,97>>, 777} // str1 = blabla string
  186. sock.send("{console.log('" ~ str1 ~ " la-la-la" ~ "')}"); // got 'blabla la-la-la' in browser console
  187. } // else do nothing
  188. // var big_value = bigInt("61196067033413");
  189. // ws.send(enc(tuple( number(1), bin('9'), bignum( big_value ) ))); // got as long for auto
  190. if(decoded1[2].type_ == BertType.Int){
  191. if(auto num3 = decoded1[2].intValue){
  192. writeln("num3 = ", num3, " ", typeof(num3).stringof); // ws.send(enc(tuple( number(1), bin('9'), number(1) ))); // Decoded: {1, <<57>>, 1} // num3 = 1 long
  193. } // else do nothing
  194. } // else do nothing
  195. // var big_value = bigInt("6119606703341361196067033413");
  196. // ws.send(enc(tuple( number(1), bin('9'), bignum( big_value ) ))); // got as BigInt
  197. if(decoded1[2].type_ == BertType.BigInt){
  198. if(auto num3b = decoded1[2].bigintValue){
  199. writeln("num3b = ", num3b, " ", typeof(num3b).stringof); // Decoded: {1, <<57>>, 6119606703341361196067033413} // num3b = 6119606703341361196067033413 BigInt
  200. } // else do nothing
  201. } // else do nothing
  202. if(decoded1[2].type_ == BertType.List){
  203. auto list1 = decoded1[2].listValue; // ws.send(enc(tuple( number(1), bin('blabla'), list( number(1), number(2), number(3) ) ))); // Decoded: {1, <<98,108,97,98,108,97>>, [1, 2, 3]}
  204. if(list1.length == 3){
  205. //if(auto num31 = cast(uint32)list1[0].intValue){
  206. if(auto num31 = list1[0].intValue){
  207. writeln("num31 = ", num31, " ", typeof(num31).stringof); // ws.send(enc(tuple( number(1), bin('9'), list( number(1), number(2), number(3) ) ))); // Decoded: {1, <<57>>, [1, 2, 3]} // num31 = 1 uint
  208. sock.send("{console.log(" ~ to!string(num31 + 77) ~ ")}"); // got 78 in browser console
  209. } // else do nothing
  210. } // else do nothing
  211. } // else do nothing
  212. } // else do nothing
  213. } // else do nothing
  214. }
  215. void login_test(HTTPServerRequest req, HTTPServerResponse res){
  216. //string client_id = generateCookie();
  217. //writeln("client_id: ", client_id); // client_id: tkoy8ybolXM95fpEqYRY
  218. //req.context["client_id"] = generateCookie();
  219. //req.params["client_id"] = generateCookie();
  220. //writeln("req.context = ", req.context); // req.context = ["client_id": MM2YjBQvSOSAdzLDZPNH]
  221. //writeln("req.params = ", req.params); // req.params = ["client_id": "Io-oRQ2DFXqBIShxs5z0"]
  222. Mustache mustache;
  223. auto context = new Mustache.Context;
  224. mustache.path = "priv";
  225. mustache.ext = "dtl";
  226. context["lang"] = "en";
  227. context["number1"] = 42;
  228. context.useSection("maybe1");
  229. context["part1"] = "<span>777</span>";
  230. context["result1"] = "Hello, World!\n";
  231. res.headers["Content-Type"] = "text/html; charset=utf-8";
  232. //res.writeBody("Hello, World!\n" ~ result);
  233. res.writeBody( mustache.render("login_test", context) );
  234. }