-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·93 lines (90 loc) · 3.77 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
require('dotenv').config()
const os = require("node:os")
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
const { Command } = require('commander');
const program = new Command();
const utils = require("./utils")
const validator = require('validator');
const os = require("os")
var username = os.userInfo().username
const isAdmin = !process.getuid() || os.userInfo().username == "nest-internal"
program
.name('yatcm')
.description('Yet another tilde caddy manager')
.version(require("./package.json").version);
program
.command('list')
.description('lists all domains you have configured in caddy')
.option('--user', 'allows you to add a domain on behalf of a user (requires sudo)')
.action(async (options) => {
if (options?.user && isAdmin) username = options.user
var domains = await utils.getDomains(username)
domains = domains.map(domain => `- ${domain.domain} (${domain.proxy})`).join("\n")
console.log(domains)
});
program
.command('add <domain>')
.description('adds a domain to caddy')
.option('--proxy', 'changes where the domain should be proxied to (advanced)')
.option('--user', 'allows you to add a domain on behalf of a user (requires sudo)')
.action(async (domain, options) => {
if (options?.user && isAdmin) username = options.user
if (!validator.isFQDN(domain)) {
console.error("This domain is not a valid domain name. Please choose a valid domain name.")
process.exit(1)
}
if (await utils.domainExists(domain)) {
console.error("This domain already has already been taken by you or someone else. Pick another one!")
process.exit(1)
}
if (utils.checkWhitelist(domain, username)) {
await prisma.domain.create({
data: {
domain, username, proxy: options?.proxy || `unix//home/${username}/.${domain}.webserver.sock`
}
})
await utils.reloadCaddy()
return console.log(`${domain} added. (${options?.proxy || `unix//home/${username}/.${domain}.webserver.sock`})`)
}
// Proceed as a regular domain
if (!await utils.checkVerification(domain, username)) {
console.error(`Please set the TXT record for domain-verification to your username (${username}). You can remove it after it is added.`)
process.exit(1)
}
await prisma.domain.create({
data: {
domain, username, proxy: options?.proxy || `unix//home/${username}/.${domain}.webserver.sock`
}
})
await utils.reloadCaddy()
return console.log(`${domain} added. (${options?.proxy || `unix//home/${username}/.${domain}.webserver.sock`})`)
});
program
.command('rm <domain>')
.description('removes a domain from caddy')
.option('--user', 'allows you to add a domain on behalf of a user (requires sudo)')
.action(async (domain, options) => {
if (options?.user && isAdmin) username = options.user
if (!validator.isFQDN(domain)) {
console.error("This domain is not a valid domain name. Please choose a valid domain name.")
process.exit(1)
}
if (!await utils.domainExists(domain)) {
console.error("This domain is not in Caddy.")
process.exit(1)
}
if (!await utils.domainOwnership(domain, username)) {
console.error("You do not own the domain, so you cannot remove it.")
process.exit(1)
}
await prisma.domain.delete({
where: {
domain, username
}
})
await utils.reloadCaddy()
console.log(`${domain} removed.`)
});
program.parse();