Skip to content

Commit 07cbb32

Browse files
committed
chore: added https flag
1 parent 8c85947 commit 07cbb32

File tree

19 files changed

+492
-29
lines changed

19 files changed

+492
-29
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@hookform/resolvers": "^4.1.3",
2424
"@prisma/client": "^6.5.0",
2525
"@radix-ui/react-avatar": "^1.1.3",
26+
"@radix-ui/react-checkbox": "^1.1.4",
2627
"@radix-ui/react-dialog": "^1.1.6",
2728
"@radix-ui/react-dropdown-menu": "^2.1.6",
2829
"@radix-ui/react-label": "^2.1.2",

pnpm-lock.yaml

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- RedefineTables
2+
PRAGMA defer_foreign_keys=ON;
3+
PRAGMA foreign_keys=OFF;
4+
CREATE TABLE "new_Domains" (
5+
"id" TEXT NOT NULL PRIMARY KEY,
6+
"incomingAddress" TEXT NOT NULL,
7+
"destinationAddress" TEXT NOT NULL,
8+
"port" INTEGER NOT NULL,
9+
"isLocked" BOOLEAN NOT NULL DEFAULT false,
10+
"disabledHttps" BOOLEAN NOT NULL DEFAULT false,
11+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
12+
);
13+
INSERT INTO "new_Domains" ("createdAt", "destinationAddress", "id", "incomingAddress", "isLocked", "port") SELECT "createdAt", "destinationAddress", "id", "incomingAddress", "isLocked", "port" FROM "Domains";
14+
DROP TABLE "Domains";
15+
ALTER TABLE "new_Domains" RENAME TO "Domains";
16+
CREATE UNIQUE INDEX "Domains_incomingAddress_key" ON "Domains"("incomingAddress");
17+
PRAGMA foreign_keys=ON;
18+
PRAGMA defer_foreign_keys=OFF;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `disabledHttps` on the `Domains` table. All the data in the column will be lost.
5+
6+
*/
7+
-- RedefineTables
8+
PRAGMA defer_foreign_keys=ON;
9+
PRAGMA foreign_keys=OFF;
10+
CREATE TABLE "new_Domains" (
11+
"id" TEXT NOT NULL PRIMARY KEY,
12+
"incomingAddress" TEXT NOT NULL,
13+
"destinationAddress" TEXT NOT NULL,
14+
"port" INTEGER NOT NULL,
15+
"isLocked" BOOLEAN NOT NULL DEFAULT false,
16+
"enableHttps" BOOLEAN NOT NULL DEFAULT true,
17+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
18+
);
19+
INSERT INTO "new_Domains" ("createdAt", "destinationAddress", "id", "incomingAddress", "isLocked", "port") SELECT "createdAt", "destinationAddress", "id", "incomingAddress", "isLocked", "port" FROM "Domains";
20+
DROP TABLE "Domains";
21+
ALTER TABLE "new_Domains" RENAME TO "Domains";
22+
CREATE UNIQUE INDEX "Domains_incomingAddress_key" ON "Domains"("incomingAddress");
23+
PRAGMA foreign_keys=ON;
24+
PRAGMA defer_foreign_keys=OFF;

prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ model Domains {
2020
destinationAddress String
2121
port Int
2222
isLocked Boolean @default(false)
23+
enableHttps Boolean @default(true)
2324
createdAt DateTime @default(now())
2425
}
2526

src/app/api/_services/caddy/caddy-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const constructInitialCaddyConfiguration = () => {
4444
apiHost,
4545
apiService,
4646
Number(apiPort),
47-
true
47+
false
4848
);
4949
const caddyConfig = getCaddyConfigTemplate([backendRoute]);
5050
return caddyConfig;

src/app/api/_services/caddy/caddy-templates.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ export const getRouteTemplate = (
2626
incomingAddress: string,
2727
targetAddress: string,
2828
upstreamPort = 443,
29-
disableHttps = false,
29+
enableHttps = true,
3030
): RouteConfig => {
31-
const handler = getHandlerTemplate(targetAddress, upstreamPort, disableHttps);
31+
const handler = getHandlerTemplate(targetAddress, upstreamPort, enableHttps);
3232
const routeConfig: RouteConfig = {
3333
match: [
3434
{
@@ -44,7 +44,7 @@ export const getRouteTemplate = (
4444
export const getHandlerTemplate = (
4545
targetAddress: string,
4646
upstreamPort = 443,
47-
disableHttps = false,
47+
enableHttps = true,
4848
): HandlerConfig => {
4949
const handlerConfig: HandlerConfig = {
5050
handler: "reverse_proxy",
@@ -64,7 +64,7 @@ export const getHandlerTemplate = (
6464
},
6565
};
6666

67-
if (disableHttps) {
67+
if (!enableHttps) {
6868
delete handlerConfig?.transport?.tls;
6969
}
7070

src/app/api/domain/add/route.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ export async function POST(request: NextRequest) {
2424
);
2525
}
2626

27+
const parsedPort = Number(reqPayload.port);
28+
2729
const routeConfig = getRouteTemplate(
2830
reqPayload.incomingAddress,
2931
reqPayload.destinationAddress,
30-
reqPayload.port
32+
parsedPort,
33+
reqPayload.enableHttps
3134
);
3235

3336
const newConfigPayload = { ...currentConfig };
3437
newConfigPayload.apps.http.servers.main.routes.push(routeConfig);
3538

36-
await loadCaddyConfig(newConfigPayload);
3739

3840
await prisma.$transaction(async (tx) => {
3941
await tx.caddyConfiguration.create({
@@ -45,11 +47,14 @@ export async function POST(request: NextRequest) {
4547
data: {
4648
incomingAddress: reqPayload.incomingAddress,
4749
destinationAddress: reqPayload.destinationAddress,
48-
port: reqPayload.port,
50+
port: parsedPort,
51+
enableHttps: reqPayload.enableHttps
4952
},
5053
});
5154
});
5255

56+
await loadCaddyConfig(newConfigPayload);
57+
5358
return NextResponse.json(
5459
{
5560
message: "Domain added successfully!",

src/app/api/domain/domain-schema.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export const addDomainSchema = z.object({
1717
message:
1818
"Invalid domain format (must be a plain domain, e.g., example.com)",
1919
}),
20-
port: z.number().min(1, "Port is required"),
20+
port: z.string().min(1, "Port is required"),
21+
enableHttps: z.boolean().default(true)
2122
});
23+
export type AddDomainValues = z.infer<typeof addDomainSchema>
2224

2325
export const deleteDomainSchema = z.object({
2426
incomingAddress: z
@@ -31,3 +33,4 @@ export const deleteDomainSchema = z.object({
3133
{ message: "Invalid domain format" }
3234
),
3335
});
36+
export type DeleteDomainValues = z.infer<typeof deleteDomainSchema>

src/app/api/domain/domain-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ export type DomainWithCheckResults = {
66
destinationAddress: string;
77
port: number;
88
createdAt: Date;
9+
isLocked: boolean;
10+
enableHttps: boolean;
911
checkResults: DomainCheckResults;
1012
};

src/app/api/domain/registered/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { DomainWithCheckResults } from "../domain-types";
55

66
export async function GET() {
77
try {
8-
const registeredDomains = await prisma.domains.findMany({});
8+
const registeredDomains = await prisma.domains.findMany({
9+
orderBy: {
10+
createdAt: 'desc'
11+
}
12+
});
913

1014
const domainsWithCheckResults: DomainWithCheckResults[] = [];
1115

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
'use client'
2+
3+
import { useForm } from 'react-hook-form'
4+
import { zodResolver } from '@hookform/resolvers/zod'
5+
import { Button } from '@/components/ui/button'
6+
import {
7+
Dialog,
8+
DialogContent,
9+
DialogDescription,
10+
DialogHeader,
11+
DialogTitle,
12+
} from '@/components/ui/dialog'
13+
import { addDomainSchema, AddDomainValues } from '@/app/api/domain/domain-schema'
14+
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '../ui/form'
15+
import { Input } from '../ui/input'
16+
import { useAddDomain } from '@/hooks/domains/domain.hooks'
17+
import { Checkbox } from '../ui/checkbox'
18+
19+
interface Props {
20+
open: boolean
21+
onClose: VoidFunction
22+
}
23+
24+
export function AddProxyDialog({ open, onClose }: Props) {
25+
const addDomainMutation = useAddDomain()
26+
27+
const form = useForm<AddDomainValues>({
28+
resolver: zodResolver(addDomainSchema),
29+
defaultValues: {
30+
enableHttps: true
31+
}
32+
})
33+
34+
const onSubmit = async (values: AddDomainValues) => {
35+
await addDomainMutation.mutateAsync(values);
36+
form.reset()
37+
onClose()
38+
}
39+
40+
return (
41+
<Dialog
42+
open={open}
43+
onOpenChange={onClose}
44+
>
45+
<DialogContent className='sm:max-w-lg'>
46+
<DialogHeader className='text-left'>
47+
<DialogTitle>{'Add Proxy'}</DialogTitle>
48+
<DialogDescription>
49+
{'Enter the details below.'}{' '}
50+
Click save when you&apos;re done.
51+
</DialogDescription>
52+
</DialogHeader>
53+
<div>
54+
<Form {...form}>
55+
<form onSubmit={form.handleSubmit(onSubmit)}>
56+
<div className='grid gap-6'>
57+
<FormField
58+
control={form.control}
59+
name='incomingAddress'
60+
render={({ field }) => (
61+
<FormItem className='space-y-1'>
62+
<FormLabel>Incoming Address</FormLabel>
63+
<FormControl>
64+
<Input placeholder="Enter incoming address." {...field} />
65+
</FormControl>
66+
<FormMessage />
67+
</FormItem>
68+
)}
69+
/>
70+
<FormField
71+
control={form.control}
72+
name='destinationAddress'
73+
render={({ field }) => (
74+
<FormItem className='space-y-1'>
75+
<FormLabel>Destination Address</FormLabel>
76+
<FormControl>
77+
<Input placeholder="Enter destination address" {...field} />
78+
</FormControl>
79+
<FormMessage />
80+
</FormItem>
81+
)}
82+
/>
83+
<FormField
84+
control={form.control}
85+
name='port'
86+
render={({ field }) => (
87+
<FormItem className='space-y-1'>
88+
<FormLabel>Desination Port</FormLabel>
89+
<FormControl>
90+
<Input type='number' placeholder="Enter destination port" {...field} />
91+
</FormControl>
92+
<FormMessage />
93+
</FormItem>
94+
)}
95+
/>
96+
{/* <FormField
97+
control={form.control}
98+
name='enableHttps'
99+
render={({ field: {
100+
value,
101+
onChange
102+
...restFieldValues
103+
} }) => (
104+
<FormItem className='space-y-1 flex items-center justify-start'>
105+
<FormLabel>Enable HTTPS</FormLabel>
106+
<FormControl>
107+
<Checkbox checked={value} {...restFieldValues} />
108+
</FormControl>
109+
<FormMessage />
110+
</FormItem>
111+
)}
112+
/> */}<FormField
113+
control={form.control}
114+
name="enableHttps"
115+
render={({ field }) => (
116+
<FormItem className="space-y-1 flex items-center justify-start">
117+
<FormLabel>Enable HTTPS</FormLabel>
118+
<FormControl>
119+
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
120+
</FormControl>
121+
<FormMessage />
122+
</FormItem>
123+
)}
124+
/>
125+
126+
<Button loading={false} type='submit' className='mt-2 cursor-pointer' disabled={false}>
127+
Save
128+
</Button>
129+
</div>
130+
</form>
131+
</Form>
132+
</div>
133+
</DialogContent>
134+
</Dialog>
135+
)
136+
}

0 commit comments

Comments
 (0)