Skip to content

Commit

Permalink
added original stuff + root checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dispherical committed Sep 8, 2024
1 parent 39dc2aa commit 54a066f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cli/commands/caddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const os = require("node:os")
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
const validator = require('validator');

const isAdmin = !process.getuid() || os.userInfo().username == "nest-internal"
module.exports = function ({ program, run }) {
const caddy = program.command("caddy")
var username = os.userInfo().username
Expand All @@ -12,7 +12,9 @@ module.exports = function ({ program, run }) {
.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) username = options.user
username = options?.user && !isAdmin
? (console.error("To change/see another user's domains, you'll need to use sudo or be root."), process.exit(1))
: options?.user;
var domains = await utils.getDomains(username)
domains = domains.map(domain => `- ${domain.domain} (${domain.proxy})`).join("\n")
console.log(domains)
Expand All @@ -21,9 +23,11 @@ module.exports = function ({ program, run }) {
.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)')
.option('--user', "allows you to list a different user's domains (requires sudo)")
.action(async (domain, options) => {
if (options?.user) username = options.user
username = options?.user && !isAdmin
? (console.error("To change/see another user's domains, you'll need to use sudo or be root."), process.exit(1))
: 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)
Expand Down Expand Up @@ -60,7 +64,9 @@ module.exports = function ({ program, run }) {
.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) username = options.user
username = options?.user && !isAdmin
? (console.error("To change/see another user's domains, you'll need to use sudo or be root."), process.exit(1))
: 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)
Expand Down
18 changes: 18 additions & 0 deletions cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ module.exports = {
}
})
var caddy = {
admin: {
listen: "unix//run/caddy/caddy-admin.sock"
},
logging: {
logs: {
default: {
encoder: {
format: "console"
}
}
}
},
apps: {
http: {
servers: {
Expand All @@ -88,6 +100,12 @@ module.exports = {
tls: {
automation: {
policies: []
},
on_demand: {
permission: {
endpoint: "https://my.hackclub.app/ok",
module: "http"
}
}
}
}
Expand Down

0 comments on commit 54a066f

Please sign in to comment.