ssh-chat ื—ืœืง 2

ืฉืœื•ื, ื”ื‘ืจ. ื–ื”ื• ื”ืžืืžืจ ื”ืฉื ื™ ื‘ืกื“ืจืช ssh-chat.

ืžื” ืฉื ืขืฉื”:

  • ื‘ื•ืื• ื ื•ืกื™ืฃ ืืช ื”ื™ื›ื•ืœืช ืœื™ืฆื•ืจ ืคื•ื ืงืฆื™ื•ืช ืขื™ืฆื•ื‘ ืžืฉืœื›ื
  • ื‘ื•ืื• ื ื•ืกื™ืฃ ืชืžื™ื›ืช ืกื™ืžื•ืŸ
  • ื‘ื•ืื• ื ื•ืกื™ืฃ ืชืžื™ื›ื” ื‘ื‘ื•ื˜
  • ื”ื’ื‘ืจ ืืช ืื‘ื˜ื—ืช ื”ืกื™ืกืžืื•ืช (hash ื•ืžืœื—)
    ืžืฆื˜ืขืจื™ื, ืื‘ืœ ืœื ืชื”ื™ื” ืฉืœื™ื—ืช ืงื‘ืฆื™ื.

ืชื›ื•ื ื•ืช ืขื™ืฆื•ื‘ ื‘ื”ืชืืžื” ืื™ืฉื™ืช

ื ื›ื•ืŸ ืœืขื›ืฉื™ื•, ืคื•ื ืงืฆื™ื•ืช ื”ืขื™ืฆื•ื‘ ื”ื‘ืื•ืช ื ืชืžื›ื•ืช:

  • @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

ืชืžื™ื›ืช Markdown

Markdown ืžืื•ื“ ื ื•ื—, ืื– ื‘ื•ืื• ื ื•ืกื™ืฃ ืื•ืชื• ื‘ืืžืฆืขื•ืช ืžืกื•ืฃ ืžืกื•ืžืŸ

// 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 ืื ื• ืžืฉืชืžืฉื™ื ื‘-checkPassword

ืกืš ื”ื›ืœ

ื›ืชื•ืฆืื” ืžื›ืš, ื™ืฉ ืœื ื• ืฆ'ืื˜ ื‘ืืžืฆืขื•ืช ssh ืขื ื™ื›ื•ืœื•ืช ืขื™ืฆื•ื‘ ื•ื‘ื•ื˜ื™ื.
ืžืื’ืจ ืกื•ืคื™

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

ื”ื•ืกืคืช ืชื’ื•ื‘ื”