Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Url: https://repl.it/@jimboy3100/agario-bots
  2.  Websocket: wss://agario-bots--jimboy3100.repl.co
  3.  
  4. Make your own repl.it Websocket in the same way after testing
  5.  
  6. If you get captcha here, it will be because of the many testers
  7. You will not get captcha on your own VPS if you STOP bots properly
  8.  
  9. REMEMBER: BEFORE CHANGING SERVER, First stop BOTS and wait 30 seconds, then change server, run this server and connect to bots
  10.  
  11. IF YOU GET RECAPTCHA, NO PROBLEM...
  12. Fork this script, and use names agario-bots1, agarbots2 etc...
  13. Example https://repl.it/@jimboy3100/agario-bots1
  14. Websocket: wss://agario-bots1--jimboy3100.repl.co
  15.  
  16. Captcha's go away after some time. Use 10 websockets for your daily bot needs :D.
  17.  
  18. */
  19.  
  20. const WebSocket = require('ws'),
  21.     { murmur2 } = require('murmurhash-js'),
  22.     buffers = require('./buffers'),
  23.     algorithm = require('./algorithm'),
  24.     Reader = require('./reader'),
  25.     Entity = require('./entity'),
  26.     requester = require("request-promise"),
  27.     logger = require("./logger.js"),
  28.     config = require('./config.json');
  29.  
  30. const userBots = [];
  31. let userWS = null,
  32.     stoppingBots = false,
  33.     connectedBots = 0,
  34.     spawnedBots = 0,
  35.     serverPlayers = 0;
  36.  
  37. if (config.server.update) {
  38.     requester(config.server.link, (err, req, data) => {
  39.         const requesterData = Buffer.from(data).toString()
  40.         requesterConfig = JSON.parse(requesterData)
  41.  
  42.         if (config.server.version < requesterConfig.server.version) {
  43.             logger.warn(`[SERVER] A new update was found!`)
  44.             logger.warn(`[SERVER] Download -> https://jimboy3100.github.io/ExampleScripts/agario-bots2/`)
  45.         } else {
  46.             logger.good(`[SERVER] No updates found!`)
  47.         }
  48.     })
  49. } else {
  50.     logger.error('[SERVER] Update is false!')
  51. }
  52.  
  53. logger.good(`[SERVER] Running version ${config.server.version} on port ${config.server.port}`)
  54.  
  55. const game = {
  56.     url: '',
  57.     protocolVersion: 0,
  58.     clientVersion: 0
  59. }
  60.  
  61. const user = {
  62.     ws: null,
  63.     bots: [],
  64.     startedBots: false,
  65.     stoppingBots: false,
  66.     isAlive: false,
  67.     mouseX: 0,
  68.     mouseY: 0
  69. }
  70.  
  71. const bots = {
  72.     name: '',
  73.     amount: 0,
  74.     ai: false
  75. }
  76. const dataBot = {
  77.     ws: null,
  78.     buffersKey: 0,
  79.     isConnected: false,
  80.     playersAmount: 0,
  81.     lastPlayersAmount: 0,
  82.     connect() {
  83.         this.buffersKey = 0
  84.         this.isConnected = false
  85.         this.playersAmount = 0
  86.         this.lastPlayersAmount = 0
  87.         this.ws = new WebSocket(game.url)
  88.         this.ws.onopen = this.onopen.bind(this)
  89.         this.ws.onmessage = this.onmessage.bind(this)
  90.         this.ws.onclose = this.onclose.bind(this)
  91.     },
  92.     send(buffer) {
  93.         if (this.ws && this.ws.readyState === WebSocket.OPEN) this.ws.send(buffer)
  94.     },
  95.     onopen() {
  96.         this.send(buffers.protocolVersion(game.protocolVersion))
  97.         this.send(buffers.clientVersion(game.clientVersion))
  98.  
  99.     },
  100.     onmessage(message) {
  101.         if (this.buffersKey) message.data = algorithm.rotateBufferBytes(message.data, this.buffersKey)
  102.         this.handleBuffer(message.data)
  103.     },
  104.     onclose() {
  105.         if (this.isConnected) {
  106.             this.isConnected = false
  107.             this.connect()
  108.             logger.error('[SERVER] DataBot disconnected!')
  109.         }
  110.     },
  111.     handleBuffer(buffer) {
  112.         const reader = new Reader(buffer)
  113.         switch (reader.readUint8()) {
  114.             case 54:
  115.                 this.playersAmount = 0
  116.                 serverPlayers = 0;
  117.                 reader.byteOffset += 2
  118.                 while (reader.byteOffset < reader.buffer.byteLength) {
  119.                     const flags = reader.readUint8()
  120.                     if (flags & 2) reader.readString()
  121.                     if (flags & 4) reader.byteOffset += 4
  122.                     this.playersAmount++
  123.                         serverPlayers++
  124.                 }
  125.                 this.lastPlayersAmount = this.playersAmount
  126.  
  127.                 break
  128.             case 241:
  129.                 this.buffersKey = reader.readInt32() ^ game.clientVersion
  130.                 this.isConnected = true
  131.                 logger.good('[SERVER] DataBot connected!')
  132.                 break
  133.         }
  134.     }
  135. }
  136.  
  137. function calculateDistance(botX, botY, targetX, targetY) {
  138.     return Math.hypot(targetX - botX, targetY - botY)
  139. }
  140.  
  141. class Bot {
  142.     constructor() {
  143.         this.ws = null
  144.         this.encryptionKey = 0
  145.         this.decryptionKey = 0
  146.         this.isConnected = false
  147.         this.cellsIDs = []
  148.         this.isAlive = false
  149.         this.followMouseTimeout = null
  150.         this.followMouse = false
  151.         this.gotCaptcha = false
  152.         this.viewportEntities = {}
  153.         this.offsetX = 0
  154.         this.offsetY = 0
  155.         this.connect()
  156.     }
  157.     reset() {
  158.         this.encryptionKey = 0
  159.         this.decryptionKey = 0
  160.         this.isConnected = false
  161.         this.cellsIDs = []
  162.         this.isAlive = false
  163.         this.followMouseTimeout = null
  164.         this.followMouse = false
  165.         this.viewportEntities = {}
  166.         this.offsetX = 0
  167.         this.offsetY = 0
  168.     }
  169.     connect() {
  170.         this.reset()
  171.         this.ws = new WebSocket(game.url)
  172.         this.ws.onopen = this.onopen.bind(this)
  173.         this.ws.onmessage = this.onmessage.bind(this)
  174.         this.ws.onerror = this.onerror.bind(this)
  175.         this.ws.onclose = this.onclose.bind(this)
  176.     }
  177.     send(buffer) {
  178.         if (this.ws && this.ws.readyState === WebSocket.OPEN) {
  179.             if (this.encryptionKey) {
  180.                 buffer = algorithm.rotateBufferBytes(buffer, this.encryptionKey)
  181.                 this.encryptionKey = algorithm.rotateEncryptionKey(this.encryptionKey)
  182.             }
  183.             this.ws.send(buffer)
  184.         }
  185.     }
  186.     onopen() {
  187.         this.send(buffers.protocolVersion(game.protocolVersion))
  188.         this.send(buffers.clientVersion(game.clientVersion))
  189.         this.isConnected = true
  190.         connectedBots++
  191.     }
  192.     onmessage(message) {
  193.         if (this.decryptionKey) message.data = algorithm.rotateBufferBytes(message.data, this.decryptionKey ^ game.clientVersion)
  194.         this.handleBuffer(message.data)
  195.     }
  196.     onerror() {
  197.         setTimeout(() => {
  198.             if (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN) this.ws.close()
  199.         }, 1000)
  200.     }
  201.     onclose() {
  202.         if (this.isConnected) {
  203.             this.isConnected = false
  204.             connectedBots--
  205.             //userWS.send(Buffer.from([4, connectedBots, spawnedBots, serverPlayers]))
  206.             //if(!this.gotCaptcha) setTimeout(this.connect.bind(this), 1000)
  207.         }
  208.     }
  209.     handleBuffer(buffer) {
  210.         const reader = new Reader(buffer)
  211.         switch (reader.readUint8()) {
  212.             case 32:
  213.                 this.cellsIDs.push(reader.readUint32())
  214.                 if (!this.isAlive) {
  215.                     this.isAlive = true
  216.                     spawnedBots++
  217.                     //userWS.send(Buffer.from([6, spawnedBots]))
  218.                     if (!user.startedBots) {
  219.                         setInterval(() => {
  220.                             for (const bot of userBots) {
  221.                                 if (bot.isAlive) bot.move()
  222.                             }
  223.                         }, 40)
  224.                         userWS.send(Buffer.from([0]))
  225.                         user.startedBots = true
  226.                         logger.good('[SERVER] Bots started!')
  227.                     }
  228.                     if (!this.followMouseTimeout) {
  229.                         this.followMouseTimeout = setTimeout(() => {
  230.                             if (this.isAlive) this.followMouse = true
  231.                         }, 18000)
  232.                     }
  233.                 }
  234.                 break
  235.             case 85:
  236.                 if (!user.startedBots) {
  237.                     userWS.send(Buffer.from([3]))
  238.                     setTimeout(process.exit, 1000)
  239.                 }
  240.                 this.gotCaptcha = true
  241.                 this.ws.onmessage = null
  242.                 this.reset()
  243.                 setTimeout(() => {
  244.                     userBots.push(new Bot())
  245.                     if (userBots.includes(this)) userBots.splice(userBots.indexOf(this), 1)
  246.                 }, 2000)
  247.                 break
  248.             case 241:
  249.                 this.decryptionKey = reader.readInt32()
  250.                 this.encryptionKey = murmur2(`${game.url.match(/(live-arena-\w+\.agar\.io)/)[1]}${reader.readString()}`, 255)
  251.                 this.isConnected = true
  252.                 break
  253.             case 242:
  254.                 this.send(buffers.spawn(bots.name))
  255.                 break
  256.             case 255:
  257.                 this.handleCompressedBuffer(algorithm.uncompressBuffer(reader.buffer.slice(5), Buffer.allocUnsafe(reader.readUint32())))
  258.                 break
  259.         }
  260.     }
  261.     handleCompressedBuffer(buffer) {
  262.         const reader = new Reader(buffer)
  263.         switch (reader.readUint8()) {
  264.             case 16:
  265.                 this.updateViewportEntities(reader)
  266.                 break
  267.             case 64:
  268.                 this.updateOffset(reader)
  269.                 break
  270.         }
  271.     }
  272.     updateViewportEntities(reader) {
  273.         const eatRecordLength = reader.readUint16()
  274.         for (let i = 0; i < eatRecordLength; i++) reader.byteOffset += 8
  275.         while (true) {
  276.             const id = reader.readUint32()
  277.             if (id === 0) break
  278.             const entity = new Entity()
  279.             entity.id = id
  280.             entity.x = reader.readInt32()
  281.             entity.y = reader.readInt32()
  282.             entity.size = reader.readUint16()
  283.             const flags = reader.readUint8()
  284.             const extendedFlags = flags & 128 ? reader.readUint8() : 0
  285.             if (flags & 1) entity.isVirus = true
  286.             if (flags & 2) reader.byteOffset += 3
  287.             if (flags & 4) reader.readString()
  288.             if (flags & 8) entity.name = decodeURIComponent(escape(reader.readString()))
  289.             if (extendedFlags & 1) entity.isPellet = true
  290.             if (extendedFlags & 4) reader.byteOffset += 4
  291.             if (this.viewportEntities[entity.id] && this.viewportEntities[entity.id].name && entity.name) entity.name = this.viewportEntities[entity.id].name
  292.             this.viewportEntities[entity.id] = entity
  293.         }
  294.         const removeRecordLength = reader.readUint16()
  295.         for (let i = 0; i < removeRecordLength; i++) {
  296.             const removedEntityID = reader.readUint32()
  297.             if (this.cellsIDs.includes(removedEntityID)) this.cellsIDs.splice(this.cellsIDs.indexOf(removedEntityID), 1)
  298.             delete this.viewportEntities[removedEntityID]
  299.         }
  300.         if (this.isAlive && this.cellsIDs.length === 0) {
  301.             this.isAlive = false
  302.             spawnedBots--
  303.             //  userWS.send(Buffer.from([6, spawnedBots]))
  304.             if (this.followMouseTimeout) {
  305.                 clearTimeout(this.followMouseTimeout)
  306.                 this.followMouseTimeout = null
  307.             }
  308.             this.followMouse = false
  309.             this.send(buffers.spawn(bots.name))
  310.         }
  311.     }
  312.     updateOffset(reader) {
  313.         const left = reader.readDouble()
  314.         const top = reader.readDouble()
  315.         const right = reader.readDouble()
  316.         const bottom = reader.readDouble()
  317.         if (~~(right - left) === 14142 && ~~(bottom - top) === 14142) {
  318.             this.offsetX = (left + right) / 2
  319.             this.offsetY = (top + bottom) / 2
  320.         }
  321.     }
  322.     getClosestEntity(type, botX, botY, botSize) {
  323.         let closestDistance = Infinity
  324.         let closestEntity = null
  325.         for (const entity of Object.values(this.viewportEntities)) {
  326.             let isConditionMet = false
  327.             switch (type) {
  328.                 case 'biggerPlayer':
  329.                     isConditionMet = !entity.isVirus && !entity.isPellet && entity.size > botSize * 1.15 && entity.name !== bots.name
  330.                     break
  331.                 case 'pellet':
  332.                     isConditionMet = !entity.isVirus && entity.isPellet
  333.                     break
  334.             }
  335.             if (isConditionMet) {
  336.                 const distance = calculateDistance(botX, botY, entity.x, entity.y)
  337.                 if (distance < closestDistance) {
  338.                     closestDistance = distance
  339.                     closestEntity = entity
  340.                 }
  341.             }
  342.         }
  343.         return {
  344.             distance: closestDistance,
  345.             entity: closestEntity
  346.         }
  347.     }
  348.     move() {
  349.         const bot = {
  350.             x: 0,
  351.             y: 0,
  352.             size: 0
  353.         }
  354.         for (const id of this.cellsIDs) {
  355.             const cell = this.viewportEntities[id]
  356.             if (cell) {
  357.                 bot.x += cell.x / this.cellsIDs.length
  358.                 bot.y += cell.y / this.cellsIDs.length
  359.                 bot.size += cell.size
  360.             }
  361.         }
  362.         const closestBiggerPlayer = this.getClosestEntity('biggerPlayer', bot.x, bot.y, bot.size)
  363.         const closestPellet = this.getClosestEntity('pellet', bot.x, bot.y, bot.size)
  364.         if (user.isAlive) {
  365.             if (this.followMouse && !stoppingBots && !bots.ai) this.send(buffers.move(user.mouseX + this.offsetX, user.mouseY + this.offsetY, this.decryptionKey))
  366.             else {
  367.                 if (closestBiggerPlayer.entity && closestBiggerPlayer.distance < 420) {
  368.                     const angle = (Math.atan2(closestBiggerPlayer.entity.y - bot.y, closestBiggerPlayer.entity.x - bot.x) + Math.PI) % (2 * Math.PI)
  369.                     this.send(buffers.move(14142 * Math.cos(angle), 14142 * Math.sin(angle), this.decryptionKey))
  370.                 } else if (closestPellet.entity) this.send(buffers.move(closestPellet.entity.x, closestPellet.entity.y, this.decryptionKey))
  371.                 else if (!closestBiggerPlayer.entity && !closestPellet.entity) {
  372.                     const random = Math.random()
  373.                     const randomX = ~~(1337 * Math.random())
  374.                     const randomY = ~~(1337 * Math.random())
  375.                     if (random > 0.5) this.send(buffers.move(bot.x + randomX, bot.y - randomY, this.decryptionKey))
  376.                     else if (random < 0.5) this.send(buffers.move(bot.x - randomX, bot.y + randomY, this.decryptionKey))
  377.                 }
  378.             }
  379.         } else {
  380.             if (closestBiggerPlayer.entity && closestBiggerPlayer.distance < 420) {
  381.                 const angle = (Math.atan2(closestBiggerPlayer.entity.y - bot.y, closestBiggerPlayer.entity.x - bot.x) + Math.PI) % (2 * Math.PI)
  382.                 this.send(buffers.move(14142 * Math.cos(angle), 14142 * Math.sin(angle), this.decryptionKey))
  383.             } else if (closestPellet.entity) this.send(buffers.move(closestPellet.entity.x, closestPellet.entity.y, this.decryptionKey))
  384.             else if (!closestBiggerPlayer.entity && !closestPellet.entity) {
  385.                 const random = Math.random()
  386.                 const randomX = ~~(1337 * Math.random())
  387.                 const randomY = ~~(1337 * Math.random())
  388.                 if (random > 0.5) this.send(buffers.move(bot.x + randomX, bot.y - randomY, this.decryptionKey))
  389.                 else if (random < 0.5) this.send(buffers.move(bot.x - randomX, bot.y + randomY, this.decryptionKey))
  390.             }
  391.         }
  392.     }
  393. }
  394.  
  395. new WebSocket.Server({
  396.     port: config.server.port
  397. }).on('connection', ws => {
  398.     setInterval(() => {
  399.         userWS.send(Buffer.from([4, connectedBots, spawnedBots, serverPlayers]))
  400.     }, 1000);
  401.     userWS = ws
  402.     logger.good('[SERVER] User connected!')
  403.     ws.on('message', buffer => {
  404.         const reader = new Reader(buffer)
  405.         switch (reader.readUint8()) {
  406.             case 0:
  407.                 if (!user.startedBots) {
  408.                     game.url = reader.readString()
  409.                     game.protocolVersion = reader.readUint32()
  410.                     game.clientVersion = reader.readUint32()
  411.                     user.isAlive = !!reader.readUint8()
  412.                     bots.name = reader.readString()
  413.                     bots.amount = reader.readUint8()
  414.                     dataBot.connect()
  415.                     let index = 0
  416.                     startBotsInterval = setInterval(() => {
  417.                         if (dataBot.lastPlayersAmount < 195 && connectedBots < bots.amount && !stoppingBots) userBots.push(new Bot())
  418.                     }, 150)
  419.                     logger.good('[SERVER] Starting bots...')
  420.                 }
  421.                 break
  422.             case 1:
  423.                 if (user.startedBots && !stoppingBots) {
  424.                     stoppingBots = true
  425.                     ws.send(Buffer.from([1]))
  426.                     let seconds = 0
  427.                     setInterval(() => {
  428.                         if (seconds === 30) {
  429.                             ws.send(Buffer.from([2]))
  430.                             setTimeout(process.exit, 1000)
  431.                         } else {
  432.                             logger.warn(`[SERVER] Stopping bots in ${30 - seconds} seconds`)
  433.                             seconds++
  434.                         }
  435.                     }, 1000)
  436.                 }
  437.                 break
  438.             case 2:
  439.                 for (const bot of userBots) {
  440.                     if (bot.isAlive && bot.followMouse && !stoppingBots && !bots.ai) bot.send(Buffer.from([17]))
  441.                 }
  442.                 break
  443.             case 3:
  444.                 for (const bot of userBots) {
  445.                     if (bot.isAlive && bot.followMouse && !stoppingBots && !bots.ai) bot.send(Buffer.from([21]))
  446.                 }
  447.                 break
  448.             case 4:
  449.                 bots.ai = !!reader.readUint8()
  450.                 break
  451.             case 5:
  452.                 user.isAlive = !!reader.readUint8()
  453.                 break
  454.             case 6:
  455.                 user.mouseX = reader.readInt32()
  456.                 user.mouseY = reader.readInt32()
  457.                 break
  458.         }
  459.     })
  460.     ws.on('close', () => {
  461.         if (user.startedBots && !stoppingBots) {
  462.             stoppingBots = true
  463.             let seconds = 0
  464.             setInterval(() => {
  465.                 if (seconds === 30) process.exit()
  466.                 else {
  467.                     logger.warn(`[SERVER] Stopping bots in ${30 - seconds} seconds`)
  468.                     seconds++
  469.                 }
  470.             }, 1000)
  471.         }
  472.         logger.error('[SERVER] User disconnected!')
  473.     })
  474. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement