ಹಲೋ, ಹಬ್ರ್. ಇದು ssh-chat ಸರಣಿಯಲ್ಲಿ 2 ನೇ ಲೇಖನವಾಗಿದೆ.
ನಾವು ಏನು ಮಾಡುತ್ತೇವೆ:
- ನಿಮ್ಮ ಸ್ವಂತ ವಿನ್ಯಾಸ ಕಾರ್ಯಗಳನ್ನು ರಚಿಸುವ ಸಾಮರ್ಥ್ಯವನ್ನು ಸೇರಿಸೋಣ
- ಮಾರ್ಕ್ಡೌನ್ ಬೆಂಬಲವನ್ನು ಸೇರಿಸೋಣ
- ಬಾಟ್ ಬೆಂಬಲವನ್ನು ಸೇರಿಸೋಣ
- ಪಾಸ್ವರ್ಡ್ ಭದ್ರತೆಯನ್ನು ಹೆಚ್ಚಿಸಿ (ಹ್ಯಾಶ್ ಮತ್ತು ಉಪ್ಪು)
ಕ್ಷಮಿಸಿ, ಆದರೆ ಯಾವುದೇ ಫೈಲ್ಗಳನ್ನು ಕಳುಹಿಸಲಾಗುವುದಿಲ್ಲ.
ಕಸ್ಟಮ್ ವಿನ್ಯಾಸ ವೈಶಿಷ್ಟ್ಯಗಳು
ಪ್ರಸ್ತುತ, ಕೆಳಗಿನ ವಿನ್ಯಾಸ ಕಾರ್ಯಗಳನ್ನು ಬೆಂಬಲಿಸಲಾಗುತ್ತದೆ:
@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) + '!'
})
ಮಾರ್ಕ್ಡೌನ್ ಬೆಂಬಲ
ಮಾರ್ಕ್ಡೌನ್ ತುಂಬಾ ಅನುಕೂಲಕರವಾಗಿದೆ, ಆದ್ದರಿಂದ ಅದನ್ನು ಬಳಸಿಕೊಂಡು ಸೇರಿಸೋಣ
// 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 ನಲ್ಲಿ ಹೋಲಿಸುವ ಬದಲು ನಾವು ಚೆಕ್ಪಾಸ್ವರ್ಡ್ ಅನ್ನು ಬಳಸುತ್ತೇವೆ
ಫಲಿತಾಂಶ
ಪರಿಣಾಮವಾಗಿ, ನಾವು ವಿನ್ಯಾಸ ಸಾಮರ್ಥ್ಯಗಳು ಮತ್ತು ಬಾಟ್ಗಳೊಂದಿಗೆ ssh ಮೂಲಕ ಚಾಟ್ ಹೊಂದಿದ್ದೇವೆ.
ಮೂಲ: www.habr.com