こんにちは、ハブル。 これは 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) + '!'
})
マークダウンのサポート
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 経由でチャットできるようになりました。
出所: habr.com