Aloha, Habr. ʻO kēia ka ʻatikala ʻelua ma ka moʻo ssh-chat.
He aha kā mākou e hana ai:
- E hoʻohui i ka hiki ke hana i kāu mau hana hoʻolālā ponoʻī
- E hoʻohui i ke kākoʻo markdown
- E hoʻohui kākou i ke kākoʻo bot
- Hoʻonui i ka palekana ʻōlelo huna (hash a me ka paʻakai)
E kala mai, ʻaʻole e hoʻouna ʻia nā faila
Nā hiʻohiʻona hoʻolālā maʻamau
I kēia manawa, kākoʻo ʻia nā hana hoʻolālā penei:
@color
@bold
@underline
@hex
@box
Akā, pono e hoʻohui i ka hiki ke hana i kāu mau hana ponoʻī:
Mālama ʻia nā hana a pau i lokoобъекте под названием methods
No laila e lawa ia e hana i kahi hanaregisterMethod
:
// parserExec.js at end
module.exports.registerMethod = function(name, func) {
methods[name] = func
}
Pono ʻoe e hoʻihoʻi i kēia ʻano hana ma hope o ka hana ʻana i ke kikowaena
// index.js at require part
const { registerMethod } = require('./parserExec')
// index.js at end
module.exports.registerMethod = registerMethod
I kēia manawa, i ka hana ʻana i kahi kikowaena, hiki iā mākou ke hoʻopaʻa inoa i nā ʻano formatting. Laʻana:
const chat = require('.')
const { formatNick } = require('./format')
chat({})
chat.registerMethod('hello', function(p, name){
return 'Hi, ' + formatNick(name) + '!'
})
Kākoʻo Markdown
Maikaʻi loa ʻo Markdown, no laila e hoʻohui mākou me ka hoʻohana ʻana
// 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)
ʻO Boti
Pehea e hana ai
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
hiki ke kapa ʻia me ka hoʻohana ʻana @bot(botBob){Command}
ʻO nā mea a pau no ka hana ʻana me nā bots i wehewehe ʻia ma ka faila:
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;
};
He aha kāu e hana ai me nā bots:
- Hoʻouka ka nānā
- Deploy
- Papa hana
Hash a me ka paʻakai
No ke aha ʻaʻole ssh kī? No ka mea e ʻokoʻa nā kī ssh ma nā ʻaoʻao like ʻole
E hana kākou i faila nāna e nānā a hana i nā ʻōlelo huna
// 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
He palapala no ka paʻakai a me ka hashing i ka ʻōlelo huna
// To generate password run node ./encryptPassword password
const { encodePass } =require('./crypto')
console.log(encodePass(process.argv[2]))
Hoʻonui mākou i nā users.json a ma kahi o ka hoʻohālikelike ʻana ma lobby.js hoʻohana mākou i ka checkPassword
ʻO ka hopena
ʻO ka hopena, loaʻa iā mākou kahi kamaʻilio ma ssh me nā mana hoʻolālā a me nā bots.
Source: www.habr.com