ืฉืืื, ืืืจ. ืืื ืืืืืจ ืืฉื ื ืืกืืจืช ssh-chat.
ืื ืฉื ืขืฉื:
- ืืืื ื ืืกืืฃ ืืช ืืืืืืช ืืืฆืืจ ืคืื ืงืฆืืืช ืขืืฆืื ืืฉืืื
- ืืืื ื ืืกืืฃ ืชืืืืช ืกืืืื
- ืืืื ื ืืกืืฃ ืชืืืื ืืืื
- ืืืืจ ืืช ืืืืืช ืืกืืกืืืืช (hash ืืืื)
ืืฆืืขืจืื, ืืื ืื ืชืืื ืฉืืืืช ืงืืฆืื.
ืชืืื ืืช ืขืืฆืื ืืืชืืื ืืืฉืืช
ื ืืื ืืขืืฉืื, ืคืื ืงืฆืืืช ืืขืืฆืื ืืืืืช ื ืชืืืืช:
@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
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
ืกื ืืื
ืืชืืฆืื ืืื, ืืฉ ืื ื ืฆ'ืื ืืืืฆืขืืช ssh ืขื ืืืืืืช ืขืืฆืื ืืืืืื.
ืืงืืจ: www.habr.com