හෙලෝ, හබ්ර්. මෙය ssh-chat මාලාවේ 2 වන ලිපියයි.
අපි මොකද කරන්නේ:
- ඔබේම නිර්මාණ කාර්යයන් නිර්මාණය කිරීමේ හැකියාව අපි එකතු කරමු
- අපි markdown සහාය එකතු කරමු
- අපි බොට් සහාය එකතු කරමු
- මුරපද ආරක්ෂාව වැඩි කරන්න (හැෂ් සහ ලුණු)
කණගාටුයි, නමුත් ගොනු යැවීමක් සිදු නොවනු ඇත.
අභිරුචි නිර්මාණ විශේෂාංග
දැනට, පහත සැලසුම් කාර්යයන් සඳහා සහය දක්වයි:
@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) + '!'
})
මාර්ක්ඩවුන් සහාය
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)
බොට්ස්
කොහොමද වැඩේ වෙන්නේ
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 යතුරු නැත්තේ ඇයි? විවිධ උපාංගවල 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 භාවිතා කරමු
ප්රතිඵලය
එහි ප්රතිඵලයක් වශයෙන්, අපට නිර්මාණ හැකියාවන් සහ bots සමඟ ssh හරහා කතාබහක් ඇත.
මූලාශ්රය: www.habr.com