Ssh-chat, பகுதி 2

வணக்கம், ஹப்ர். இது ssh-chat தொடரின் 2வது கட்டுரை.

நாம் என்ன செய்ய போகிறோம்:

  • உங்கள் சொந்த வடிவமைப்பு செயல்பாடுகளை உருவாக்கும் திறனைச் சேர்ப்போம்
  • மார்க் டவுன் ஆதரவைச் சேர்ப்போம்
  • போட் ஆதரவைச் சேர்ப்போம்
  • கடவுச்சொல் பாதுகாப்பை அதிகரிக்கவும் (ஹாஷ் மற்றும் உப்பு)
    மன்னிக்கவும், ஆனால் கோப்புகளை அனுப்புவது இருக்காது.

விருப்ப வடிவமைப்பு அம்சங்கள்

தற்போது, ​​பின்வரும் வடிவமைப்பு செயல்பாடுகள் ஆதரிக்கப்படுகின்றன:

  • @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 இல் ஒப்பிடுவதற்குப் பதிலாக checkPassword ஐப் பயன்படுத்துகிறோம்

இதன் விளைவாக

இதன் விளைவாக, வடிவமைப்பு திறன்கள் மற்றும் போட்களுடன் ssh வழியாக அரட்டையடிக்கிறோம்.
இறுதி களஞ்சியம்

ஆதாரம்: www.habr.com

கருத்தைச் சேர்