Skip to content
Snippets Groups Projects
show-signin-history.ts 1.34 KiB
Newer Older
import { Users, Signins } from '@/models/index';
syuilo's avatar
syuilo committed
// node built/tools/show-signin-history username
//  => {Success} {Date} {IPAddrsss}

syuilo's avatar
syuilo committed
// node built/tools/show-signin-history username user-agent,x-forwarded-for
//  with user-agent and x-forwarded-for

syuilo's avatar
syuilo committed
// node built/tools/show-signin-history username all
syuilo's avatar
syuilo committed
async function main(username: string, headers?: string[]) {
	const user = await Users.findOne({
		host: null,
		usernameLower: username.toLowerCase(),
	});

syuilo's avatar
syuilo committed
	if (user == null) throw new Error('User not found');
	const history = await Signins.find({
		userId: user.id
	});

	for (const signin of history) {
		console.log(`${signin.success ? 'OK' : 'NG'} ${signin.createdAt ? signin.createdAt.toISOString() : 'Unknown'} ${signin.ip}`);

		// headers
		if (headers != null) {
			for (const key of Object.keys(signin.headers)) {
				if (headers.includes('all') || headers.includes(key)) {
					console.log(`   ${key}: ${signin.headers[key]}`);
				}
			}
		}
	}
}

syuilo's avatar
syuilo committed
// get args
const args = process.argv.slice(2);
syuilo's avatar
syuilo committed
let username = args[0];
let headers: string[] | undefined;
syuilo's avatar
syuilo committed
if (args[1] != null) {
	headers = args[1].split(/,/).map(header => header.toLowerCase());
tamaina's avatar
tamaina committed
}
syuilo's avatar
syuilo committed
// normalize args
username = username.replace(/^@/, '');

main(username, headers).then(() => {
	process.exit(0);
}).catch(e => {
	console.warn(e);
	process.exit(1);
});