Lumela, Habr. Ena ke sengoloa sa 2 letotong la ssh-chat.
Seo re tla se etsa:
- Ha re eketse bokhoni ba ho iketsetsa mesebetsi ea moralo
- Ha re kenye ts'ehetso ea markdown
- Ha re kenye tšehetso ea bot
- Eketsa ts'ireletso ea password (hash le letsoai)
Tšoarelo, empa lifaele ha li na ho romelloa.
Likarolo tsa moralo o ikhethileng
Hona joale, mesebetsi e latelang ea moralo e tšehetsoa:
@color
@bold
@underline
@hex
@box
Empa ho bohlokoa ho eketsa bokhoni ba ho iketsetsa mesebetsi ea hau:
Mesebetsi eohle e bolokiloe hoобъекте под названием methods
Kahoo ho tla ba ho lekaneng ho etsa mosebetsiregisterMethod
:
// parserExec.js at end
module.exports.registerMethod = function(name, func) {
methods[name] = func
}
U boetse u hloka ho khutlisa mokhoa ona ka mor'a ho theha seva
// index.js at require part
const { registerMethod } = require('./parserExec')
// index.js at end
module.exports.registerMethod = registerMethod
Hona joale, ha re theha seva, re ka ngolisa mekhoa ea ho fomata. Mohlala:
const chat = require('.')
const { formatNick } = require('./format')
chat({})
chat.registerMethod('hello', function(p, name){
return 'Hi, ' + formatNick(name) + '!'
})
Tšehetso ea Markdown
Markdown e bonolo haholo, ka hona, ha re e eketse re sebelisa
// 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)
Botsana
E tla sebetsa joang
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
e ka bitsoa ho sebelisa @bot(botBob){Command}
Ntho e ngoe le e ngoe ea ho sebetsa le bots e hlalositsoe faeleng:
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;
};
Seo u ka se etsang ka bots:
- Laela leihlo
- Sebelisa
- Boto ea mosebetsi
Hash le letsoai
Hobaneng o sa ssh linotlolo? Hobane linotlolo tsa ssh li tla fapana ho lisebelisoa tse fapaneng
Ha re theheng faele e tla ba le boikarabello ba ho hlahloba le ho etsa li-password
// 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
Hape ke script bakeng sa salting le hashing password
// To generate password run node ./encryptPassword password
const { encodePass } =require('./crypto')
console.log(encodePass(process.argv[2]))
Re ntlafatsa ho users.json mme sebakeng sa ho bapisa ho lobby.js re sebelisa checkPassword
Phello
Ka lebaka leo, re na le moqoqo ka ssh ka bokhoni ba ho qapa le bots.
Source: www.habr.com