Sveiki, Habr. Tas ir 2. raksts no sÄrijas par ssh-chat.
Ko mÄs darÄ«sim:
- Pievienosim iespÄju izveidot savas noformÄÅ”anas funkcijas
- Pievienosim markdown atbalstu
- Pievienosim botu atbalstu
- PalielinÄsim paroles droŔību (hash un sÄls)
DiemžÄl failu sÅ«tīŔana nebÅ«s iespÄjama
LietotÄju noformÄÅ”anas funkcijas
PaÅ”laik ir ieviests atbalsts sekojoÅ”Äm noformÄÅ”anas funkcijÄm:
@color@bold@underline@hex@box
Bet ir vÄrts pievienot iespÄju izveidot savas funkcijas:
Visas funkcijas tiek glabÄtas
TÄtad bÅ«s pietiekami izveidot funkcijuregisterMethod:
// parserExec.js at end
module.exports.registerMethod = function(name, func) {
methods[name] = func
}TÄpat ir jÄdod atpakaļ Ŕī metode pÄc servera izveides
// index.js at require part
const { registerMethod } = require('./parserExec')
// index.js at end
module.exports.registerMethod = registerMethodTagad, veidojot serveri, mÄs varam reÄ£istrÄt noformÄÅ”anas metodes. PiemÄrs:
const chat = require('.')
const { formatNick } = require('./format')
chat({})
chat.registerMethod('hello', function(p, name){
return 'Sveiks, ' + formatNick(name) + '!'
})
Markdown atbalsts
Markdown ir ļoti Ärts, tÄpÄc pievienosim to ar
// 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)
Boti
KÄ tas darbosies
let writeBotBob = chat.registerBot({
name: 'botBob',
onConnect(nick, write){
write('@hello{' + nick + '}')
},
onDisconnect(nick, write){},
onMessage(nick, message, write) {
if(message == 'botBob!') write('Es esmu Ŕeit')
},
onCommand(command, write) {
write('Darbu veicu ' + command)
}
})onCommand var iespÄjams izsaukt, izmantojot @bot(botBob){Command}
Viss, kas saistÄ«ts ar botu darbu, ir aprakstÄ«ts failÄ:
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;
};

Ko var izdarīt ar botiem:
- Slodzes uzraudzība
- Izvietot
- Uzdevumu plate
Hash un sÄls
KÄpÄc ne ssh atslÄgas? Jo ssh atslÄgas dažÄdos ierÄ«cÄs bÅ«s atŔķirÄ«gas
Izveidosim failu, kas atbildÄs par parolÄm un to izveidi
// 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 = checkPassTÄpat skripts par paroles sÄls un hashÄÅ”anu
// To generate password run node ./encryptPassword password
const { encodePass } =require('./crypto')
console.log(encodePass(process.argv[2]))Atjaunojam users.json un vietÄ, lai salÄ«dzinÄtu lobby.js, izmantojam checkPassword
RezultÄts
KÄ rezultÄtÄ mums ir ssh Äats ar noformÄÅ”anas iespÄjÄm un botu atbalstu.
Avots: habr.com
