์๋ ํ์ธ์, ํ๋ธ๋ฅด๋. ์ด๊ฒ์ ssh-chat ์๋ฆฌ์ฆ์ ๋ ๋ฒ์งธ ๊ธฐ์ฌ์ ๋๋ค.
์ฐ๋ฆฌ๊ฐ ํ ์ผ:
- ๋๋ง์ ๋์์ธ ๊ธฐ๋ฅ์ ๋ง๋ค ์ ์๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํ์
- ๋งํฌ๋ค์ด ์ง์์ ์ถ๊ฐํ์
- ๋ด ์ง์์ ์ถ๊ฐํ์
- ๋น๋ฐ๋ฒํธ ๋ณด์ ๊ฐํ(ํด์ ๋ฐ ์ํธ)
์ฃ์กํฉ๋๋ค. ํ์ผ์ ์ ์ก๋์ง ์์ต๋๋ค.
๋ง์ถคํ ๋์์ธ ๊ธฐ๋ฅ
ํ์ฌ ๋ค์๊ณผ ๊ฐ์ ๋์์ธ ๊ธฐ๋ฅ์ด ์ง์๋ฉ๋๋ค.
@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