ื”ืขืœื, ื”ืื‘ืจ. ื“ืึธืก ืื™ื– ื“ืขืจ 2ื˜ืขืจ ืึทืจื˜ื™ืงืœ ืื™ืŸ ื“ืขืจ ssh-chat ืกืขืจื™ืข.

ื•ื•ืึธืก ืžื™ืจ ื•ื•ืขืœืŸ ื˜ืึธืŸ:

  • ืœืึธืžื™ืจ ืœื™ื™ื’ืŸ ื“ื™ ืคื™ื™ื™ืงื™ื™ื˜ ืฆื• ืฉืึทืคึฟืŸ ื“ื™ื™ืŸ ืื™ื™ื’ืขื ืข ืคึผืœืึทืŸ ืคืึทื ื’ืงืฉืึทื ื–
  • ื–ืืœ ืก ืœื™ื™ื’ืŸ ืžืึทืจืงื“ืึทื•ืŸ ืฉื˜ื™ืฆืŸ
  • ื–ืืœ ืก ืœื™ื™ื’ืŸ ื‘ืึธื˜ ืฉื˜ื™ืฆืŸ
  • ืคืึทืจื’ืจืขืกืขืจืŸ ืคึผืึทืจืึธืœ ื–ื™ื›ืขืจื”ื™ื™ื˜ (ื”ืึทืฉ ืื•ืŸ ื–ืึทืœืฅ)
    ืื ื˜ืฉื•ืœื“ื™ื’ื˜, ืึธื‘ืขืจ ืขืก ื•ื•ืขื˜ ื–ื™ื™ืŸ ืงื™ื™ืŸ ืฉื™ืงื˜ ืคื•ืŸ ื˜ืขืงืขืก.

ืžื ื”ื’ ืคึผืœืึทืŸ ืคึฟืขื™ึดืงื™ื™ื˜ืŸ

ื“ืขืจื•ื•ื™ื™ึทืœ, ื“ื™ ืคืืœื’ืขื ื“ืข ืคึผืœืึทืŸ ืคืึทื ื’ืงืฉืึทื ื– ื–ืขื ืขืŸ ื’ืขืฉื˜ื™ืฆื˜:

  • @color
  • @bold
  • @underline
  • @hex
  • @box
    ืึธื‘ืขืจ ืขืก ืื™ื– ื•ื•ืขืจื˜ ืฆื• ืœื™ื™ื’ืŸ ื“ื™ ืคื™ื™ื™ืงื™ื™ื˜ ืฆื• ืฉืึทืคึฟืŸ ื“ื™ื™ืŸ ืื™ื™ื’ืขื ืข ืคืึทื ื’ืงืฉืึทื ื–:
    ืึทืœืข ืคืึทื ื’ืงืฉืึทื ื– ื–ืขื ืขืŸ ืกื˜ืึธืจื“ ืื™ืŸ ะพะฑัŠะตะบั‚ะต ะฟะพะด ะฝะฐะทะฒะฐะฝะธะตะผ methods
    ืึทื–ื•ื™ ืขืก ื•ื•ืขื˜ ื–ื™ื™ืŸ ื’ืขื ื•ื’ ืฆื• ืฉืึทืคึฟืŸ ืึท ืคึฟื•ื ืงืฆื™ืข registerMethod:

// parserExec.js at end
module.exports.registerMethod  =  function(name, func) {
  methods[name] =  func
}

ืื™ืจ ืื•ื™ืš ื“ืึทืจืคึฟืŸ ืฆื• ืฆื•ืจื™ืงืงื•ืžืขืŸ ื“ืขื ืื•ืคึฟืŸ ื ืึธืš ืงืจื™ื™ื™ื˜ื™ื ื’ ื“ื™ ืกืขืจื•ื•ืขืจ

// index.js at require part
const { registerMethod } = require('./parserExec')

// index.js at end
module.exports.registerMethod  =  registerMethod

ืื™ืฆื˜, ื•ื•ืขืŸ ืงืจื™ื™ื™ื˜ื™ื ื’ ืึท ืกืขืจื•ื•ืขืจ, ืžื™ืจ ืงืขื ืขืŸ ืจืขื’ื™ืกื˜ืจื™ืจืŸ ืคืึธืจืžืึทื˜ื˜ื™ื ื’ ืžืขื˜ื”ืึธื“ืก. ื‘ื™ื™ึทืฉืคึผื™ืœ:

const  chat  =  require('.')
const { formatNick } =  require('./format')

chat({})

chat.registerMethod('hello', function(p, name){
  return  'Hi, '  +  formatNick(name) +  '!'
})

Ssh-Chat, ื˜ื™ื™ืœ 2

ืžืึทืจืงื“ืึทื•ืŸ ืฉื˜ื™ืฆืŸ

ืžืึทืจืงื“ืึทื•ืŸ ืื™ื– ื–ื™ื™ืขืจ ื‘ืึทืงื•ื•ืขื, ืึทื–ื•ื™ ืœืึธื–ืŸ ืื•ื ื“ื– ืœื™ื™ื’ืŸ ืขืก ืžื™ื˜ ื•ื•ืึธืงื–ืึทืœ ืื ื’ืขืฆื™ื™ื›ื ื˜

// format.js near require
const marked = require('marked');
const TerminalRenderer = require('marked-terminal');

marked.setOptions({
  renderer: new TerminalRenderer()
});

// format.js line 23
message = marked(message)

Ssh-Chat, ื˜ื™ื™ืœ 2

ื‘ืึธืฅ

ื•ื•ื™ ื•ื•ืขื˜ ืขืก ืึทืจื‘ืขื˜

let writeBotBob = chat.registerBot({
  name: 'botBob',

  onConnect(nick, write){
    write('@hello{' + nick + '}')
  },

  onDisconnect(nick, write){},

  onMessage(nick, message, write) {
    if(message == 'botBob!') write('I'm here')
  },

  onCommand(command, write) {
    write('Doing ' + command)
  }
})

onCommand ืงืขื ืขืŸ ื–ื™ื™ืŸ ื’ืขืจื•ืคืŸ ื ื™ืฆืŸ @bot(botBob){Command}

ืึทืœืฅ ืคึฟืึทืจ ืืจื‘ืขื˜ืŸ ืžื™ื˜ ื‘ืึธืฅ ืื™ื– ื“ื™ืกืงืจื™ื™ื‘ื“ ืื™ืŸ ื“ืขืจ ื˜ืขืงืข:

let bots = []; // ะ’ัะต ะฑะพั‚ั‹

let onWrite = () => {}; 

function getWrite(bot) { // ะ“ะตะฝะตั€ะธั€ัƒะตั‚ ะผะตั‚ะพะด ะพั‚ะฟั€ะฐะฒะบะธ ัะพะพะฑั‰ะตะฝะธั ะดะปั ะฑะพั‚ะฐ
  return msg => {
    onWrite(bot.name, msg);
  };
}

module.exports.message = function message(nick, message) { // index.js ะฒั‹ะฟะพะปะฝะธั‚ ัั‚ัƒ ั„ัƒะฝะบั†ะธัŽ ะฟะพัะปะต ะพั‚ะฟั€ะฐะฒะบะธ ัะพะพะฑั‰ะตะฝะธั
  bots.forEach(bot => {
    try {
      bot.onMessage(nick, message, getWrite(bot));
    } catch (e) {
      console.error(e);
    }
  });
};

module.exports.connect = function message(nick) { // ะŸั€ะธ ัะพะตะดะธะฝะตะฝะธะธ
  bots.forEach(bot => {
    try {
      bot.onConnect(nick, getWrite(bot));
    } catch (e) {
      console.error(e);
    }
  });
};

module.exports.disConnect = function message(nick) { // ะŸั€ะธ ะพั‚ัะพะตะดะธะฝะตะฝะธะธ
  bots.forEach(bot => {
    try {
      bot.onDisconnect(nick, message, getWrite(bot));
    } catch (e) {
      console.error(e);
    }
  });
};

module.exports.command = function message(name, message) { // ะŸั€ะธ ะฒั‹ะฟะพะปะฝะตะฝะธะธ ะบะพะผะฐะฝะดั‹
  bots.forEach(bot => {
    if (bot.name == name) {
      try {
        bot.onCommand(message, getWrite(bot));
      } catch (e) {
        console.error(e);
      }
    }
  });
};

module.exports.registerBot = function(bot) {
  bots.push(bot);
  return  getWrite(bot)
};

module.exports.onMessage = func => {
  onWrite = func;
};

Ssh-Chat, ื˜ื™ื™ืœ 2

ื•ื•ืึธืก ืื™ืจ ืงืขื ืขืŸ ื˜ืึธืŸ ืžื™ื˜ ื‘ืึธืฅ:

  • ืžืึทืกืข ืžืึธื ื™ื˜ืึธืจ
  • ืฆืขื•ื•ื™ืงืœืขืŸ
  • ื˜ืึทืกืง ื‘ืจืขื˜

ื”ืึทืฉ ืื•ืŸ ื–ืึทืœืฅ

ืคืืจื•ื•ืืก ื ื™ืฉื˜ ssh ืฉืœื™ืกืœืขืŸ? ื•ื•ื™ื™ึทืœ ssh ืฉืœื™ืกืœืขืŸ ื•ื•ืขื˜ ื–ื™ื™ืŸ ืึทื ื“ืขืจืฉ ืื•ื™ืฃ ืคืึทืจืฉื™ื“ืขื ืข ื“ืขื•ื•ื™ืกืขืก
ืœืึธืžื™ืจ ืžืึทื›ืŸ ืึท ื˜ืขืงืข ื•ื•ืึธืก ื•ื•ืขื˜ ื–ื™ื™ืŸ ืคืึทืจืึทื ื˜ื•ื•ืึธืจื˜ืœืขืš ืคึฟืึทืจ ืงืึธื ื˜ืจืึธืœื™ืจื•ื ื’ ืื•ืŸ ืงืจื™ื™ื™ื˜ื™ื ื’ ืคึผืึทืกื•ื•ืขืจื“ื–

// crypto.js
const crypto = require('crypto');

function genRandomString(length) {
  return crypto
    .randomBytes(Math.ceil(length / 2))
    .toString('hex')
    .slice(0, length);
}

function sha512(password, salt){
  const hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */
  hash.update(password);
  const value = hash.digest('hex');
  return value
};

function checkPass(pass, obj){
  return obj.password == sha512(pass, obj.salt)
}

function encodePass(pass){
  const salt = genRandomString(16)
  return JSON.stringify({
    salt,
    password: sha512(pass, salt)
  })
}

module.exports.encodePass = encodePass
module.exports.checkPass = checkPass

ืื•ื™ืš ืึท ืฉืจื™ืคื˜ ืคึฟืึทืจ ืกืึธืœื˜ื™ื ื’ ืื•ืŸ ื›ืึทืฉื™ื ื’ ื“ื™ ืคึผืึทืจืึธืœ

// To generate password run node ./encryptPassword password
const { encodePass } =require('./crypto')
console.log(encodePass(process.argv[2]))

ืžื™ืจ ื“ืขืจื”ื™ื™ึทื ื˜ื™ืงืŸ ืื™ืŸ users.json ืื•ืŸ ืึทื ืฉื˜ืึธื˜ ืคื•ืŸ ืงืึทืžืคึผืขืจื™ื ื’ ืื™ืŸ lobby.js ืžื™ืจ ื ื•ืฆืŸ ื˜ืฉืขืงืคึผืึทืกืกื•ื•ืึธืจื“

ื’ืึทื ืฅ

ื•ื•ื™ ืึท ืจืขื–ื•ืœื˜ืึทื˜, ืžื™ืจ ื”ืึธื‘ืŸ ืึท ืฉืžื•ืขืก ื“ื•ืจืš ssh ืžื™ื˜ ืคึผืœืึทืŸ ืงื™ื™ืคึผืึทื‘ื™ืœืึทื˜ื™ื– ืื•ืŸ ื‘ืึธืฅ.
ืœืขืฆื˜ ืจื™ืคึผืึทื–ืึทื˜ืึธืจื™

ืžืงื•ืจ: www.habr.com

ืœื™ื™ื’ืŸ ืึท ื‘ืึทืžืขืจืงื•ื ื’