Ssh-แชท ตอนที่ 2

สวัสดีฮับ นี่เป็นบทความที่ 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-แชท ตอนที่ 2

การสนับสนุนมาร์กดาวน์

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-แชท ตอนที่ 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-แชท ตอนที่ 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 พร้อมความสามารถในการออกแบบและบอท
ที่เก็บข้อมูลสุดท้าย

ที่มา: will.com

เพิ่มความคิดเห็น