DiscordService: swap to eris and refactor the entire service while we're at it.

This commit is contained in:
41666 2019-03-22 07:01:08 -05:00
parent 27fb06a197
commit 03ad4202b8
No known key found for this signature in database
GPG key ID: BC51D07640DC10AF
14 changed files with 4075 additions and 410 deletions

View file

@ -7,6 +7,7 @@
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-optional-chaining",
["@babel/plugin-transform-runtime",
{ "helpers": false }]
],

View file

@ -19,7 +19,7 @@ export default (R: Router, $: AppContext) => {
console.log(ctx.session.expiresAt >= new Date(), ctx.session.expiresAt, new Date())
if (ctx.session.accessToken === undefined || ctx.session.expiresAt >= new Date()) {
const data = await $.discord.getAuthToken(token)
const data = await $.discord.initializeOAuth(token)
ctx.session.accessToken = data.access_token
ctx.session.refreshToken = data.refresh_token
ctx.session.expiresAt = new Date() + ctx.expires_in
@ -105,7 +105,7 @@ export default (R: Router, $: AppContext) => {
}
try {
const tokens = await $.discord.getAuthToken(code)
const tokens = await $.discord.initializeOAuth(code)
const user = await $.discord.getUserFromToken(tokens.access_token)
$.auth.injectSessionFromOAuth(ctx, tokens, user.id)
return ctx.redirect(r || '/')

13
bot/index.js Normal file
View file

@ -0,0 +1,13 @@
// @flow
import type DiscordService from '../services/discord'
import logger from '../logger'
const log = logger(__filename)
export default class Bot {
svc: DiscordService
log: typeof log
constructor (DS: DiscordService) {
this.svc = DS
this.log = log
}
}

2229
flow-typed/eris.js vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -8,17 +8,19 @@
"build": "npm-run-all build:*",
"build:ui": "NODE_ENV=production next build ui",
"build:server": "NODE_ENV=production babel --delete-dir-on-start -d dist .",
"build:move": "mkdir dist/ui; cp -R ui/.next dist/ui/.next",
"remotedebug": "remotedebug_ios_webkit_adapter --port=9000 > /dev/null &"
"remotedebug": "remotedebug_ios_webkit_adapter --port=9000 > /dev/null",
"test": "jest"
},
"dependencies": {
"@discordjs/uws": "^11.149.1",
"@primer/components": "^11.0.0",
"@primer/components": "^12.0.0",
"bufferutil": "^4.0.1",
"chalk": "^2.4.2",
"color": "^3.1.0",
"discord.js": "^11.4.2",
"dotenv": "^7.0.0",
"erlpack": "hammerandchisel/erlpack",
"eris": "^0.9.0",
"erlpack": "discordapp/erlpack",
"eventemitter3": "^3.1.0",
"fast-redux": "^0.7.1",
"fnv-plus": "^1.2.12",
"glob": "^7.1.3",
@ -51,15 +53,17 @@
"socket.io": "^2.2.0",
"styled-components": "^4.1.3",
"superagent": "^4.1.0",
"uuid": "^3.3.2"
"zlib-sync": "^0.1.4"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/node": "^7.2.2",
"@babel/core": "^7.4.0",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-optional-chaining": "^7.2.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.4.0",
"@babel/preset-env": "^7.4.1",
"@babel/preset-env": "^7.4.2",
"@babel/preset-flow": "^7.0.0",
"babel-eslint": "^10.0.1",
"babel-plugin-styled-components": "^1.10.0",
@ -68,6 +72,7 @@
"eslint-plugin-flowtype": "^3.4.2",
"flow-bin": "^0.95.1",
"flow-typed": "^2.5.1",
"jest": "^24.5.0",
"npm-run-all": "^4.1.5",
"standard": "12.0.1"
},

View file

@ -18,6 +18,9 @@ export default (ctx: AppContext, forceClear: ?boolean = false): {
const filename = path.basename(a)
const dirname = path.dirname(a)
const pathname = a.replace('rpc/', '')
delete require.cache[require.resolve(pathname)]
// internal stuff
if (filename.startsWith('_')) {
log.debug(`skipping ${a}`)
@ -37,10 +40,6 @@ export default (ctx: AppContext, forceClear: ?boolean = false): {
log.debug(`mounting ${a}`)
try {
const pathname = a.replace('rpc/', '')
delete require.cache[require.resolve(pathname)]
const r = require(pathname)
let o = r
if (o.default) {

42
rpc/_security.js Normal file
View file

@ -0,0 +1,42 @@
// @flow
import { type AppContext } from '../Roleypoly'
import { type Context } from 'koa'
import RPCError from './_error'
// import logger from '../logger'
// const log = logger(__filename)
const PermissionError = new RPCError('User does not have permission to call this RPC.', 403)
// export const bot = (fn: Function) => (secret: string, ...args: any[]) => {
// if (secret !== process.env.SHARED_SECRET) {
// log.error('unauthenticated bot request', { secret })
// return { err: 'unauthenticated' }
// }
// return fn(...args)
// }
export const root = ($: AppContext, fn: Function) => (ctx: Context, ...args: any[]) => {
if ($.discord.isRoot(ctx.session.userId)) {
return fn(ctx, ...args)
}
throw PermissionError
}
export const manager = ($: AppContext, fn: Function) => (ctx: Context, server: string, ...args: any[]) => {
if ($.discord.canManageRoles(server, ctx.session.userId)) {
return fn(ctx, server, ...args)
}
throw PermissionError
}
export const member = ($: AppContext, fn: Function) => (ctx: Context, server: string, ...args: any[]) => {
if ($.discord.isMember(server, ctx.session.userId)) {
return fn(ctx, server, ...args)
}
throw PermissionError
}

View file

@ -86,6 +86,18 @@ export default class RPCServer {
}
}
/**
* For internally called stuff, such as from a bot shard.
*/
async call (fn: string, ...args: any[]) {
if (!(fn in this.rpcMap)) {
throw new RPCError(`RPC call ${fn}(...) not found.`, 404)
}
const call = this.rpcMap[fn]
return call(...args)
}
rpcError (ctx: Context & {body: any}, msg: ?string, err: ?Error = null, code: ?number = null) {
log.error('rpc error', { msg, err })

View file

@ -1,17 +1,19 @@
// @flow
import { type AppContext } from '../Roleypoly'
import { type Context } from 'koa'
import { type Guild } from 'discord.js'
import { type Guild } from 'eris'
import { root } from './_security'
export default ($: AppContext) => ({
listRelevantServers (ctx: Context) {
rootGetAllServers: root($, (ctx: Context) => {
return $.discord.client.guilds.map<{
url: string,
name: string,
members: number,
roles: number
}>((g: Guild) => ({ url: `${$.config.appUrl}/s/${g.id}`, name: g.name, members: g.members.array().length, roles: g.roles.array().length }))
},
}>((g: Guild) => ({ url: `${$.config.appUrl}/s/${g.id}`, name: g.name, members: g.memberCount, roles: g.roles.size }))
}),
getServerSlug (ctx: Context, id: string) {
const srv = $.discord.client.guilds.get(id)

View file

@ -1,18 +1,45 @@
// @flow
import Service from './Service'
import type { AppContext } from '../Roleypoly'
import Bot from '../bot'
import Eris, { type Member, Role, type Guild, type Permission as ErisPermission } from 'eris'
import LRU from 'lru-cache'
// $FlowFixMe
import { OrderedSet } from 'immutable'
import superagent from 'superagent'
// import invariant from 'invariant'
import { type AppContext } from '../Roleypoly'
import {
type Message,
type Guild,
type Role,
type GuildMember,
type Collection,
Client
} from 'discord.js'
import type { AuthTokens } from './auth'
type DiscordServiceConfig = {
token: string,
clientId: string,
clientSecret: string,
rootUsers: Set<string>,
isBot: boolean
}
export type Permissions = {
canManageRoles: boolean,
isAdmin: boolean,
faked?: boolean,
__faked?: Permissions
}
type CachedRole = {
id: string,
position: number
}
type OAuthRequestData = {
grant_type: 'authorization_code',
code: string
} | {
grant_type: 'refresh_token',
refresh_token: string
} | {
grant_type: 'access_token',
token: string
}
export type UserPartial = {
id: string,
username: string,
@ -20,153 +47,201 @@ export type UserPartial = {
avatar: string
}
export type Permissions = {
isAdmin: boolean,
canManageRoles: boolean
}
export default class DiscordService extends Service {
ctx: AppContext
bot: Bot
client: Eris
type ChatCommandHandler = (message: Message, matches: string[], reply: (text: string) => Promise<Message>) => Promise<void>|void
type ChatCommand = {
regex: RegExp,
handler: ChatCommandHandler
}
cfg: DiscordServiceConfig
class DiscordService extends Service {
botToken: string = process.env.DISCORD_BOT_TOKEN || ''
clientId: string = process.env.DISCORD_CLIENT_ID || ''
clientSecret: string = process.env.DISCORD_CLIENT_SECRET || ''
oauthCallback: string = process.env.OAUTH_AUTH_CALLBACK || ''
isBot: boolean = process.env.IS_BOT === 'true'
// a small cache of role data for checking viability
ownRoleCache: LRU<string, CachedRole>
oauthCallback: string
appUrl: string
botCallback: string
rootUsers: Set<string>
client: Client
cmds: ChatCommand[]
constructor (ctx: AppContext) {
super(ctx)
this.appUrl = ctx.config.appUrl
this.ctx = ctx
this.oauthCallback = `${this.appUrl}/api/oauth/callback`
this.botCallback = `${this.appUrl}/api/oauth/bot/callback`
this.rootUsers = new Set((process.env.ROOT_USERS || '').split(','))
this.cfg = {
rootUsers: new Set((process.env.ROOT_USERS || '').split(',')),
token: process.env.DISCORD_BOT_TOKEN || '',
clientId: process.env.DISCORD_CLIENT_ID || '',
clientSecret: process.env.DISCORD_CLIENT_SECRET || '',
isBot: process.env.IS_BOT === 'true'
}
this.client = new Client()
this.client.options.disableEveryone = true
this.oauthCallback = `${ctx.config.appUrl}/api/oauth/callback`
this._cmds()
this.ownRoleCache = new LRU()
this.startBot()
}
ownGm (server: string) {
return this.gm(server, this.client.user.id)
}
fakeGm ({ id = '0', nickname = '[none]', displayHexColor = '#ffffff' }: $Shape<{ id: string, nickname: string, displayHexColor: string }>) { // eslint-disable-line no-undef
return { id, nickname, displayHexColor, __faked: true, roles: { has () { return false } } }
if (this.cfg.isBot) {
this.client = new Eris(this.cfg.token, {
disableEveryone: true,
maxShards: 'auto',
messageLimit: 10,
disableEvents: {
CHANNEL_PINS_UPDATE: true,
USER_SETTINGS_UPDATE: true,
USER_NOTE_UPDATE: true,
RELATIONSHIP_ADD: true,
RELATIONSHIP_REMOVE: true,
GUILD_BAN_ADD: true,
GUILD_BAN_REMOVE: true,
TYPING_START: true,
MESSAGE_UPDATE: true,
MESSAGE_DELETE: true,
MESSAGE_DELETE_BULK: true,
VOICE_STATE_UPDATE: true
}
})
this.bot = new Bot(this)
} else {
this.client = new Eris(`Bot ${this.cfg.token}`, {
restMode: true
})
}
}
isRoot (id: string): boolean {
return this.rootUsers.has(id)
return this.cfg.rootUsers.has(id)
}
async startBot () {
await this.client.login(this.botToken)
// not all roleypolys are bots.
if (this.isBot) {
this.log.info('this roleypoly is a bot')
this.client.on('message', this.handleMessage.bind(this))
this.client.on('guildCreate', this.handleJoin.bind(this))
}
for (let server of this.client.guilds.array()) {
await this.ctx.server.ensure(server)
}
getRelevantServers (user: string) {
return this.client.guilds.filter(guild => guild.members.has(user))
}
getRelevantServers (userId: string): Collection<string, Guild> {
return this.client.guilds.filter((g) => g.members.has(userId))
gm (serverId: string, userId: string): ?Member {
return this.client.guilds.get(serverId)?.members.get(userId)
}
gm (serverId: string, userId: string): ?GuildMember {
const s = this.client.guilds.get(serverId)
if (s == null) {
return null
}
return s.members.get(userId)
ownGm (serverId: string) {
return this.gm(serverId, this.client.user.id)
}
fakeGm ({ id = '0', nickname = '[none]', displayHexColor = '#ffffff' }: $Shape<Member>): $Shape<Member> {
return { id, nickname, displayHexColor, __faked: true, roles: { has () { return false } } }
}
getRoles (server: string) {
const s = this.client.guilds.get(server)
if (s == null) {
return null
}
return s.roles
return this.client.guilds.get(server)?.roles
}
getPermissions (gm: GuildMember): Permissions {
getOwnPermHeight (server: Guild): number {
if (this.ownRoleCache.has(server)) {
return this.ownRoleCache.get(server).position
}
const gm = this.ownGm(server.id)
const g = gm?.guild
const r: Role = OrderedSet(gm?.roles).map(id => g?.roles.get(id)).sortBy(r => r.position).last({ position: 0, id: '0' })
this.ownRoleCache.set(server, {
id: r.id,
position: r.position
})
return r.position
}
calcPerms (permable: Role | Member): Permissions {
const p: ErisPermission = (permable instanceof Role) ? permable.permissions : permable.permission
return {
isAdmin: p.has('administrator'),
canManageRoles: p.has('manageRoles') || p.has('administrator')
}
}
getPermissions (gm: Member): Permissions {
const real = this.calcPerms(gm)
if (this.isRoot(gm.id)) {
return {
isAdmin: true,
canManageRoles: true
canManageRoles: true,
faked: true,
__faked: real
}
}
return {
isAdmin: gm.permissions.has('ADMINISTRATOR'),
canManageRoles: gm.permissions.has('MANAGE_ROLES', true)
}
return real
}
safeRole (server: string, role: string) {
const rl = this.getRoles(server)
if (rl == null) {
throw new Error(`safeRole can't see ${server}`)
}
const r: ?Role = rl.get(role)
const r = this.getRoles(server)?.get(role)
if (r == null) {
throw new Error(`safeRole can't find ${role} in ${server}`)
}
return r.editable && !r.hasPermission('MANAGE_ROLES', false, true)
return this.roleIsEditable(r) && !this.calcPerms(r).canManageRoles
}
// oauth step 2 flow, grab the auth token via code
async getAuthToken (code: string): Promise<AuthTokens> {
const url = 'https://discordapp.com/api/oauth2/token'
try {
const rsp =
await superagent
.post(url)
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({
client_id: this.clientId,
client_secret: this.clientSecret,
grant_type: 'authorization_code',
code: code,
redirect_uri: this.oauthCallback
})
roleIsEditable (role: Role): boolean {
// role will be LOWER than my own
return this.getOwnPermHeight(role.guild) > role.position
}
return rsp.body
async oauthRequest (path: string, data: OAuthRequestData) {
const url = `https://discordapp.com/api/oauth2/${path}`
try {
const rsp = await superagent.post(url)
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('User-Agent', 'DiscordBot (https://roleypoly.com, 2.x.x) OAuthHandler/1.0')
.send({
client_id: this.cfg.clientId,
client_secret: this.cfg.clientSecret,
redirect_uri: this.oauthCallback,
...data
})
return (rsp.body: AuthTokens)
} catch (e) {
this.log.error('getAuthToken failed', e)
this.log.error('oauthRequest failed', { e, path })
throw e
}
}
async getUserFromToken (authToken?: string): Promise<UserPartial> {
initializeOAuth (code: string) {
return this.oauthRequest('token', {
grant_type: 'authorization_code',
code
})
}
refreshOAuth ({ refreshToken }: { refreshToken: string }) {
return this.oauthRequest('token', {
grant_type: 'refresh_token',
refresh_token: refreshToken
})
}
revokeOAuth ({ accessToken }: { accessToken: string }) {
return this.oauthRequest('token/revoke', {
grant_type: 'access_token',
token: accessToken
})
}
getUserPartial (userId: string): ?UserPartial {
const u = this.client.users.get(userId)
if (u == null) {
return null
}
return {
username: u.username,
discriminator: u.discriminator,
avatar: u.avatarURL,
id: u.id
}
}
async getUserFromToken (authToken: string): Promise<UserPartial> {
const url = 'https://discordapp.com/api/v6/users/@me'
try {
if (authToken == null || authToken === '') {
throw new Error('not logged in')
}
const rsp = await superagent.get(url)
.set('User-Agent', 'DiscordBot (https://roleypoly.com, 2.x.x) OAuthHandler/1.0')
.set('Authorization', `Bearer ${authToken}`)
const rsp =
await superagent
.get(url)
.set('Authorization', `Bearer ${authToken}`)
return rsp.body
} catch (e) {
this.log.error('getUser error', e)
@ -174,142 +249,19 @@ class DiscordService extends Service {
}
}
getUserPartial (userId: string): ?UserPartial {
const U = this.client.users.get(userId)
if (U == null) {
return null
}
return {
username: U.username,
discriminator: U.discriminator,
avatar: U.displayAvatarURL,
id: U.id
}
}
async refreshOAuth ({ refreshToken }: { refreshToken: string }): Promise<AuthTokens> {
const url = 'https://discordapp.com/api/oauth2/token'
try {
const rsp =
await superagent
.post(url)
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({
client_id: this.clientId,
client_secret: this.clientSecret,
grant_type: 'refresh_token',
refresh_token: refreshToken,
redirect_uri: this.oauthCallback
})
return rsp.body
} catch (e) {
this.log.error('refreshOAuth failed', e)
throw e
}
}
async revokeOAuth ({ accessToken }: { accessToken: string }) {
const url = 'https://discordapp.com/api/oauth2/token/revoke'
try {
const rsp =
await superagent
.post(url)
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({
client_id: this.clientId,
client_secret: this.clientSecret,
grant_type: 'access_token',
token: accessToken,
redirect_uri: this.oauthCallback
})
return rsp.body
} catch (e) {
this.log.error('revokeOAuth failed', e)
throw e
}
}
// returns oauth authorize url with IDENTIFY permission
// we only need IDENTIFY because we only use it for matching IDs from the bot
getAuthUrl (state: string): string {
return `https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&redirect_uri=${this.oauthCallback}&response_type=code&scope=identify&state=${state}`
return `https://discordapp.com/oauth2/authorize?client_id=${this.cfg.clientId}&redirect_uri=${this.oauthCallback}&response_type=code&scope=identify&state=${state}`
}
// returns the bot join url with MANAGE_ROLES permission
// MANAGE_ROLES is the only permission we really need.
getBotJoinUrl (): string {
return `https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&scope=bot&permissions=268435456`
return `https://discordapp.com/oauth2/authorize?client_id=${this.cfg.clientId}&scope=bot&permissions=268435456`
}
mentionResponse (message: Message) {
message.channel.send(`🔰 Assign your roles here! ${this.appUrl}/s/${message.guild.id}`, { disableEveryone: true })
}
_cmds () {
const cmds = [
{
regex: /say (.*)/,
handler (message, matches, r) {
r(matches[0])
}
},
{
regex: /set username (.*)/,
async handler (message, matches) {
const { username } = this.client.user
await this.client.user.setUsername(matches[0])
message.channel.send(`Username changed from ${username} to ${matches[0]}`)
}
},
{
regex: /stats/,
async handler (message, matches) {
const t = [
`**Stats** 📈`,
'',
`👩‍❤️‍👩 **Users Served:** ${this.client.guilds.reduce((acc, g) => acc + g.memberCount, 0)}`,
`🔰 **Servers:** ${this.client.guilds.size}`,
`💮 **Roles Seen:** ${this.client.guilds.reduce((acc, g) => acc + g.roles.size, 0)}`
]
message.channel.send(t.join('\n'))
}
}
]
// prefix regex with ^ for ease of code
.map(({ regex, ...rest }) => ({ regex: new RegExp(`^${regex.source}`, regex.flags), ...rest }))
this.cmds = cmds
return cmds
}
async handleCommand (message: Message) {
const cmd = message.content.replace(`<@${this.client.user.id}> `, '')
this.log.debug(`got command from ${message.author.username}`, cmd)
for (let { regex, handler } of this.cmds) {
const match = regex.exec(cmd)
if (match !== null) {
this.log.debug('command accepted', { cmd, match })
try {
await handler.call(this, message, match.slice(1))
return
} catch (e) {
this.log.error('command errored', { e, cmd, message })
message.channel.send(`❌ **An error occured.** ${e}`)
return
}
}
}
// nothing matched?
this.mentionResponse(message)
}
async issueChallenge (message: Message) {
async issueChallenge (author: string) {
// Create a challenge
const chall = await this.ctx.auth.createDMChallenge(message.author.id)
const chall = await this.ctx.auth.createDMChallenge(author)
const randomLines = [
'🐄 A yellow cow is only as bright as it lets itself be. ✨',
@ -324,7 +276,7 @@ class DiscordService extends Service {
'🛠 I am completely open source, check me out!~ <https://github.com/kayteh/roleypoly>'
]
message.channel.send([
return ([
'**Hey there!** <a:StockKyaa:435353158462603266>',
'',
`Use this secret code: **${chall.human}**`,
@ -336,37 +288,11 @@ class DiscordService extends Service {
].join('\n'))
}
handleDM (message: Message) {
switch (message.content.toLowerCase()) {
case 'login':
case 'auth':
case 'log in':
this.issueChallenge(message)
}
canManageRoles (server: string, user: string) {
return this.getPermissions(this.gm(server, user)).canManageRoles
}
handleMessage (message: Message) {
if (message.author.bot) { // drop bot messages
return
}
if (message.channel.type === 'dm') {
// handle dm
return this.handleDM(message)
}
if (message.mentions.users.has(this.client.user.id)) {
if (this.rootUsers.has(message.author.id)) {
this.handleCommand(message)
} else {
this.mentionResponse(message)
}
}
}
async handleJoin (guild: Guild) {
await this.ctx.server.ensure(guild)
isMember (server: string, user: string) {
return this.gm(server, user) != null
}
}
module.exports = DiscordService

View file

@ -7,9 +7,9 @@ import { type ServerModel } from '../models/Server'
import type DiscordService, { Permissions } from './discord'
import {
type Guild,
type GuildMember,
type Member,
type Collection
} from 'discord.js'
} from 'eris'
import areduce from '../util/areduce'
export type ServerSlug = {
@ -60,21 +60,6 @@ class PresentationService extends Service {
}
}
async oldPresentableServers (collection: Collection<string, Guild>, userId: string) {
this.log.deprecated('use presentableServers instead of oldPresentableServers')
let servers = []
for (let server of collection.array()) {
const gm = server.members.get(userId)
// $FlowFixMe this is deprecated, forget adding more check code.
servers.push(await this.presentableServer(server, gm))
}
return servers
}
presentableServers (collection: Collection<string, Guild>, userId: string) {
return areduce(collection.array(), async (acc, server) => {
const gm = server.members.get(userId)
@ -87,7 +72,7 @@ class PresentationService extends Service {
}, [])
}
async presentableServer (server: Guild, gm: GuildMember, { incRoles = true }: { incRoles: boolean } = {}): Promise<PresentableServer> {
async presentableServer (server: Guild, gm: Member, { incRoles = true }: { incRoles: boolean } = {}): Promise<PresentableServer> {
const sd = await this.ctx.server.get(server.id)
return {

View file

@ -4,7 +4,7 @@ import { type AppContext } from '../Roleypoly'
import type PresentationService from './presentation'
import {
type Guild
} from 'discord.js'
} from 'eris'
import { type ServerModel, type Category } from '../models/Server'
export default class ServerService extends Service {

View file

@ -3,6 +3,7 @@
"next/babel", "@babel/preset-flow"
],
"plugins": [
[ "styled-components", { "ssr": true } ]
[ "styled-components", { "ssr": true } ],
"@babel/plugin-proposal-optional-chaining"
]
}

1660
yarn.lock

File diff suppressed because it is too large Load diff