Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ygopro.ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) {
  2.     var check, decrypted_buffer, finish, i, id, j, k, len, len1, name, ref, ref1, replay_id, room, secret;
  3.     info.pass = info.pass.trim();
  4.     if (settings.modules.stop) {
  5.       ygopro.stoc_die(client, settings.modules.stop);
  6.     } else if (info.pass.toUpperCase() === "R" && settings.modules.cloud_replay.enabled) {
  7.       ygopro.stoc_send_chat(client, "${cloud_replay_hint}", ygopro.constants.COLORS.BABYBLUE);
  8.       redisdb.lrange(client.ip + ":replays", 0, 2, function(err, result) {
  9.         _.each(result, function(replay_id, id) {
  10.           redisdb.hgetall("replay:" + replay_id, function(err, replay) {
  11.             if (err || !replay) {
  12.               if (err) {
  13.                 log.info("cloud replay getall error: " + err);
  14.               }
  15.               return;
  16.             }
  17.             ygopro.stoc_send_chat(client, "<" + (id - 0 + 1) + "> R#" + replay_id + " " + replay.player_names + " " + replay.date_time, ygopro.constants.COLORS.BABYBLUE);
  18.           });
  19.         });
  20.       });
  21.       setTimeout((function() {
  22.         ygopro.stoc_send(client, 'ERROR_MSG', {
  23.           msg: 1,
  24.           code: 9
  25.         });
  26.         client.destroy();
  27.       }), 500);
  28.     } else if (info.pass.slice(0, 2).toUpperCase() === "R#" && settings.modules.cloud_replay.enabled) {
  29.       replay_id = info.pass.split("#")[1];
  30.       if (replay_id > 0 && replay_id <= 9) {
  31.         redisdb.lindex(client.ip + ":replays", replay_id - 1, function(err, replay_id) {
  32.           if (err || !replay_id) {
  33.             if (err) {
  34.               log.info("cloud replay replayid error: " + err);
  35.             }
  36.             ygopro.stoc_die(client, "${cloud_replay_no}");
  37.             return;
  38.           }
  39.           redisdb.hgetall("replay:" + replay_id, client.open_cloud_replay);
  40.         });
  41.       } else if (replay_id) {
  42.         redisdb.hgetall("replay:" + replay_id, client.open_cloud_replay);
  43.       } else {
  44.         ygopro.stoc_die(client, "${cloud_replay_no}");
  45.       }
  46.     } else if (info.pass.toUpperCase() === "W" && settings.modules.cloud_replay.enabled) {
  47.       replay_id = Cloud_replay_ids[Math.floor(Math.random() * Cloud_replay_ids.length)];
  48.       redisdb.hgetall("replay:" + replay_id, client.open_cloud_replay);
  49.       client.destroy();
  50.     } else if (!info.pass.length && !settings.modules.random_duel.enabled && !settings.modules.windbot.enabled) {
  51.       ygopro.stoc_die(client, "${blank_room_name}");
  52.     } else if (info.pass.length && settings.modules.mycard.enabled && info.pass.slice(0, 3) !== 'AI#') {
  53.       ygopro.stoc_send_chat(client, '${loading_user_info}', ygopro.constants.COLORS.BABYBLUE);
  54.       if (info.pass.length <= 8) {
  55.         ygopro.stoc_die(client, '${invalid_password_length}');
  56.         return;
  57.       }
  58.       buffer = new Buffer(info.pass.slice(0, 8), 'base64');
  59.       if (buffer.length !== 6) {
  60.         ygopro.stoc_die(client, '${invalid_password_payload}');
  61.         return;
  62.       }
  63.       check = function(buf) {
  64.         var checksum, i, j, ref;
  65.         checksum = 0;
  66.         for (i = j = 0, ref = buf.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
  67.           checksum += buf.readUInt8(i);
  68.         }
  69.         return (checksum & 0xFF) === 0;
  70.       };
  71.       finish = function(buffer) {
  72.         var action, j, len, name, opt1, opt2, opt3, options, ref, room, title;
  73.         action = buffer.readUInt8(1) >> 4;
  74.         if (buffer !== decrypted_buffer && (action === 1 || action === 2 || action === 4)) {
  75.           ygopro.stoc_die(client, '${invalid_password_unauthorized}');
  76.           return;
  77.         }
  78.         switch (action) {
  79.           case 1:
  80.           case 2:
  81.             name = crypto.createHash('md5').update(info.pass + client.name).digest('base64').slice(0, 10).replace('+', '-').replace('/', '_');
  82.             if (ROOM_find_by_name(name)) {
  83.               ygopro.stoc_die(client, '${invalid_password_existed}');
  84.               return;
  85.             }
  86.             opt1 = buffer.readUInt8(2);
  87.             opt2 = buffer.readUInt16LE(3);
  88.             opt3 = buffer.readUInt8(5);
  89.             options = {
  90.               lflist: 0,
  91.               time_limit: 180,
  92.               rule: (opt1 >> 5) & 3,
  93.               mode: (opt1 >> 3) & 3,
  94.               enable_priority: !!((opt1 >> 2) & 1),
  95.               no_check_deck: !!((opt1 >> 1) & 1),
  96.               no_shuffle_deck: !!(opt1 & 1),
  97.               start_lp: opt2,
  98.               start_hand: opt3 >> 4,
  99.               draw_count: opt3 & 0xF
  100.             };
  101.             options.lflist = _.findIndex(settings.lflist, function(list) {
  102.               return ((options.rule === 1) === list.tcg) && list.date.isBefore();
  103.             });
  104.             room = new Room(name, options);
  105.             room.title = info.pass.slice(8).replace(String.fromCharCode(0xFEFF), ' ');
  106.             room["private"] = action === 2;
  107.             break;
  108.           case 3:
  109.             name = info.pass.slice(8);
  110.             room = ROOM_find_by_name(name);
  111.             if (!room) {
  112.               ygopro.stoc_die(client, '${invalid_password_not_found}');
  113.               return;
  114.             }
  115.             break;
  116.           case 4:
  117.             room = ROOM_find_or_create_by_name('M#' + info.pass.slice(8));
  118.             room["private"] = true;
  119.             room.arena = settings.modules.arena_mode.mode;
  120.             if (room.arena === "athletic") {
  121.               room.max_player = 2;
  122.               room.welcome = "${athletic_arena_tip}";
  123.             }
  124.             break;
  125.           case 5:
  126.             title = info.pass.slice(8).replace(String.fromCharCode(0xFEFF), ' ');
  127.             room = ROOM_find_by_title(title);
  128.             if (!room) {
  129.               ygopro.stoc_die(client, '${invalid_password_not_found}');
  130.               return;
  131.             }
  132.             break;
  133.           default:
  134.             ygopro.stoc_die(client, '${invalid_password_action}');
  135.             return;
  136.         }
  137.         if (!room) {
  138.           ygopro.stoc_die(client, "${server_full}");
  139.         } else if (room.error) {
  140.           ygopro.stoc_die(client, room.error);
  141.         } else if (room.started) {
  142.           if (settings.modules.cloud_replay.enable_halfway_watch) {
  143.             client.setTimeout(300000);
  144.             client.rid = _.indexOf(ROOM_all, room);
  145.             client.is_post_watcher = true;
  146.             ygopro.stoc_send_chat_to_room(room, client.name + " ${watch_join}");
  147.             room.watchers.push(client);
  148.             ygopro.stoc_send_chat(client, "${watch_watching}", ygopro.constants.COLORS.BABYBLUE);
  149.             ref = room.watcher_buffers;
  150.             for (j = 0, len = ref.length; j < len; j++) {
  151.               buffer = ref[j];
  152.               client.write(buffer);
  153.             }
  154.           } else {
  155.             ygopro.stoc_die(client, "${watch_denied}");
  156.           }
  157.         } else {
  158.           client.setTimeout(300000);
  159.           client.rid = _.indexOf(ROOM_all, room);
  160.           room.connect(client);
  161.         }
  162.       };
  163.       if (id = users_cache[client.name]) {
  164.         secret = id % 65535 + 1;
  165.         decrypted_buffer = new Buffer(6);
  166.         ref = [0, 2, 4];
  167.         for (j = 0, len = ref.length; j < len; j++) {
  168.           i = ref[j];
  169.           decrypted_buffer.writeUInt16LE(buffer.readUInt16LE(i) ^ secret, i);
  170.         }
  171.         if (check(decrypted_buffer)) {
  172.           return finish(decrypted_buffer);
  173.         }
  174.       }
  175.       request({
  176.         baseUrl: settings.modules.mycard.auth_base_url,
  177.         url: '/users/' + encodeURIComponent(client.name) + '.json',
  178.         qs: {
  179.           api_key: settings.modules.mycard.auth_key,
  180.           api_username: client.name,
  181.           skip_track_visit: true
  182.         },
  183.         json: true
  184.       }, function(error, response, body) {
  185.         var k, len1, ref1;
  186.         if (body && body.user) {
  187.           users_cache[client.name] = body.user.id;
  188.           secret = body.user.id % 65535 + 1;
  189.           decrypted_buffer = new Buffer(6);
  190.           ref1 = [0, 2, 4];
  191.           for (k = 0, len1 = ref1.length; k < len1; k++) {
  192.             i = ref1[k];
  193.             decrypted_buffer.writeUInt16LE(buffer.readUInt16LE(i) ^ secret, i);
  194.           }
  195.           if (check(decrypted_buffer)) {
  196.             buffer = decrypted_buffer;
  197.           }
  198.         }
  199.         if (!check(buffer)) {
  200.           ygopro.stoc_die(client, '${invalid_password_checksum}');
  201.           return;
  202.         }
  203.         return finish(buffer);
  204.       });
  205.     } else if (!client.name || client.name === "") {
  206.       ygopro.stoc_die(client, "${bad_user_name}");
  207.     } else if (ROOM_connected_ip[client.ip] > 5) {
  208.       log.warn("MULTI LOGIN", client.name, client.ip);
  209.       ygopro.stoc_die(client, "${too_much_connection}" + client.ip);
  210.     } else if (_.indexOf(settings.ban.banned_user, client.name) > -1) {
  211.       settings.ban.banned_ip.push(client.ip);
  212.       log.warn("BANNED USER LOGIN", client.name, client.ip);
  213.       ygopro.stoc_die(client, "${banned_user_login}");
  214.     } else if (_.indexOf(settings.ban.banned_ip, client.ip) > -1) {
  215.       log.warn("BANNED IP LOGIN", client.name, client.ip);
  216.       ygopro.stoc_die(client, "${banned_ip_login}");
  217.     } else if (_.any(settings.ban.badword_level3, function(badword) {
  218.       var regexp;
  219.       regexp = new RegExp(badword, 'i');
  220.       return name.match(regexp);
  221.     }, name = client.name)) {
  222.       log.warn("BAD NAME LEVEL 3", client.name, client.ip);
  223.       ygopro.stoc_die(client, "${bad_name_level3}");
  224.     } else if (_.any(settings.ban.badword_level2, function(badword) {
  225.       var regexp;
  226.       regexp = new RegExp(badword, 'i');
  227.       return name.match(regexp);
  228.     }, name = client.name)) {
  229.       log.warn("BAD NAME LEVEL 2", client.name, client.ip);
  230.       ygopro.stoc_die(client, "${bad_name_level2}");
  231.     } else if (_.any(settings.ban.badword_level1, function(badword) {
  232.       var regexp;
  233.       regexp = new RegExp(badword, 'i');
  234.       return name.match(regexp);
  235.     }, name = client.name)) {
  236.       log.warn("BAD NAME LEVEL 1", client.name, client.ip);
  237.       ygopro.stoc_die(client, "${bad_name_level1}");
  238.     } else if (info.pass.length && !ROOM_validate(info.pass)) {
  239.       ygopro.stoc_die(client, "${invalid_password_room}");
  240.     } else {
  241.       room = ROOM_find_or_create_by_name(info.pass, client.ip);
  242.       if (!room) {
  243.         ygopro.stoc_die(client, "${server_full}");
  244.       } else if (room.error) {
  245.         ygopro.stoc_die(client, room.error);
  246.       } else if (room.started) {
  247.         if (settings.modules.cloud_replay.enable_halfway_watch) {
  248.           client.setTimeout(300000);
  249.           client.rid = _.indexOf(ROOM_all, room);
  250.           client.is_post_watcher = true;
  251.           ygopro.stoc_send_chat_to_room(room, client.name + " ${watch_join}");
  252.           room.watchers.push(client);
  253.           ygopro.stoc_send_chat(client, "${watch_watching}", ygopro.constants.COLORS.BABYBLUE);
  254.           ref1 = room.watcher_buffers;
  255.           for (k = 0, len1 = ref1.length; k < len1; k++) {
  256.             buffer = ref1[k];
  257.             client.write(buffer);
  258.           }
  259.         } else {
  260.           ygopro.stoc_die(client, "${watch_denied}");
  261.         }
  262.       } else {
  263.         client.setTimeout(300000);
  264.         client.rid = _.indexOf(ROOM_all, room);
  265.         room.connect(client);
  266.       }
  267.     }
  268.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement