summaryrefslogtreecommitdiff
path: root/src/tools/show-signin-history.ts
blob: fd7cd39e38e3e157e98ff4ca163c82f5b714e8fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Users, Signins } from '../models';

// node built/tools/show-signin-history username
//  => {Success} {Date} {IPAddrsss}

// node built/tools/show-signin-history username user-agent,x-forwarded-for
//  with user-agent and x-forwarded-for

// node built/tools/show-signin-history username all
//  with full request headers

async function main(username: string, headers?: string[]) {
	const user = await Users.findOne({
		host: null,
		usernameLower: username.toLowerCase(),
	});

	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]}`);
				}
			}
		}
	}
}

// get args
const args = process.argv.slice(2);

let username = args[0];
let headers: string[] | undefined;

if (args[1] != null) {
	headers = args[1].split(/,/).map(header => header.toLowerCase());
}

// normalize args
username = username.replace(/^@/, '');

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