-
Notifications
You must be signed in to change notification settings - Fork 137
Add header auth and access restrictions to reverse proxy #583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
src/modules/reverse-proxy/AccessRestrictionsSection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| import Button from "@components/Button"; | ||
| import HelpText from "@components/HelpText"; | ||
| import { Label } from "@components/Label"; | ||
| import { CountrySelector } from "@components/ui/CountrySelector"; | ||
| import { cn } from "@utils/helpers"; | ||
| import cidr from "ip-cidr"; | ||
| import { Globe, MinusCircleIcon, PlusCircle, ShieldCheck } from "lucide-react"; | ||
| import React, { useState } from "react"; | ||
| import { AccessRestrictions } from "@/interfaces/ReverseProxy"; | ||
|
|
||
| let nextRowId = 0; | ||
|
|
||
| type Props = { | ||
| value: AccessRestrictions; | ||
| onChange: (value: AccessRestrictions) => void; | ||
| }; | ||
|
|
||
| type CidrRow = { id: number; value: string }; | ||
|
|
||
| type RowListProps = { | ||
| values: string[]; | ||
| onChange: (values: string[]) => void; | ||
| }; | ||
|
|
||
| function CidrList({ values, onChange }: Readonly<RowListProps>) { | ||
| const [rows, setRows] = useState<CidrRow[]>(() => | ||
| values.map((v) => ({ id: nextRowId++, value: v })), | ||
| ); | ||
| const [errors, setErrors] = useState<Record<number, string>>({}); | ||
|
|
||
| const addRow = () => { | ||
| const next = [...rows, { id: nextRowId++, value: "" }]; | ||
| setRows(next); | ||
| onChange(next.map((r) => r.value)); | ||
| }; | ||
|
|
||
| const updateRow = (id: number, raw: string) => { | ||
| const next = rows.map((r) => (r.id === id ? { ...r, value: raw } : r)); | ||
| setRows(next); | ||
| onChange(next.map((r) => r.value)); | ||
|
|
||
| if (raw && !cidr.isValidCIDR(raw)) { | ||
| setErrors((prev) => ({ ...prev, [id]: "Invalid CIDR format" })); | ||
| } else { | ||
| setErrors((prev) => { | ||
| const copy = { ...prev }; | ||
| delete copy[id]; | ||
| return copy; | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const removeRow = (id: number) => { | ||
| const next = rows.filter((r) => r.id !== id); | ||
| setRows(next); | ||
| onChange(next.map((r) => r.value)); | ||
| setErrors((prev) => { | ||
| const copy = { ...prev }; | ||
| delete copy[id]; | ||
| return copy; | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-2"> | ||
| {rows.map((row) => ( | ||
| <div key={row.id}> | ||
| <div className="flex gap-2"> | ||
| <input | ||
| type="text" | ||
| value={row.value} | ||
| onChange={(e) => updateRow(row.id, e.target.value.trim())} | ||
| placeholder="e.g. 10.0.0.0/8" | ||
| className={cn( | ||
| "flex-1 h-[42px] px-3 text-sm font-mono bg-nb-gray-900/40 rounded-md outline-none", | ||
| "border placeholder:text-neutral-400/70", | ||
| errors[row.id] | ||
| ? "border-red-400" | ||
| : "border-nb-gray-700", | ||
| )} | ||
| /> | ||
| <Button | ||
| className="h-[42px]" | ||
| variant="default-outline" | ||
| onClick={() => removeRow(row.id)} | ||
| > | ||
| <MinusCircleIcon size={15} /> | ||
| </Button> | ||
| </div> | ||
| {errors[row.id] && ( | ||
| <p className="text-xs text-red-400 mt-1">{errors[row.id]}</p> | ||
| )} | ||
| </div> | ||
| ))} | ||
| <Button variant="dotted" size="sm" onClick={addRow}> | ||
| <PlusCircle size={16} /> | ||
| Add CIDR | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| type CountryRow = { id: number; value: string }; | ||
|
|
||
| function CountryList({ values, onChange }: Readonly<RowListProps>) { | ||
| const [rows, setRows] = useState<CountryRow[]>(() => | ||
| values.map((v) => ({ id: nextRowId++, value: v })), | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const addCountry = () => { | ||
| const next = [...rows, { id: nextRowId++, value: "" }]; | ||
| setRows(next); | ||
| onChange(next.map((r) => r.value)); | ||
| }; | ||
|
|
||
| const updateCountry = (id: number, code: string) => { | ||
| if (rows.some((r) => r.value === code)) return; | ||
| const next = rows.map((r) => (r.id === id ? { ...r, value: code } : r)); | ||
| setRows(next); | ||
| onChange(next.map((r) => r.value)); | ||
| }; | ||
|
|
||
| const removeCountry = (id: number) => { | ||
| const next = rows.filter((r) => r.id !== id); | ||
| setRows(next); | ||
| onChange(next.map((r) => r.value)); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-2"> | ||
| {rows.map((row) => ( | ||
| <div key={row.id} className="flex gap-2"> | ||
| <div className="flex-1"> | ||
| <CountrySelector | ||
| value={row.value} | ||
| onChange={(v) => updateCountry(row.id, v)} | ||
| /> | ||
| </div> | ||
| <Button | ||
| className="h-[42px]" | ||
| variant="default-outline" | ||
| onClick={() => removeCountry(row.id)} | ||
| > | ||
| <MinusCircleIcon size={15} /> | ||
| </Button> | ||
| </div> | ||
| ))} | ||
| <Button variant="dotted" size="sm" onClick={addCountry}> | ||
| <PlusCircle size={16} /> | ||
| Add Country | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default function AccessRestrictionsSection({ | ||
| value, | ||
| onChange, | ||
| }: Readonly<Props>) { | ||
| const update = (patch: Partial<AccessRestrictions>) => { | ||
| onChange({ ...value, ...patch }); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="px-8 flex-col flex gap-6"> | ||
| <div> | ||
| <Label> | ||
| <ShieldCheck size={14} /> | ||
| CIDR Allowlist | ||
| </Label> | ||
| <HelpText> | ||
| Only connections from these IP ranges will be allowed. All other | ||
| IPs will be blocked. Leave empty to allow all. | ||
| </HelpText> | ||
| <CidrList | ||
| values={value.allowed_cidrs ?? []} | ||
| onChange={(v) => update({ allowed_cidrs: v })} | ||
| /> | ||
| </div> | ||
|
|
||
| <div> | ||
| <Label> | ||
| <ShieldCheck size={14} /> | ||
| CIDR Blocklist | ||
| </Label> | ||
| <HelpText> | ||
| Block connections from these IP ranges. Takes priority over the | ||
| allowlist. | ||
| </HelpText> | ||
| <CidrList | ||
| values={value.blocked_cidrs ?? []} | ||
| onChange={(v) => update({ blocked_cidrs: v })} | ||
| /> | ||
| </div> | ||
|
|
||
| <div> | ||
| <Label> | ||
| <Globe size={14} /> | ||
| Country Allowlist | ||
| </Label> | ||
| <HelpText> | ||
| Only connections from these countries will be allowed. All other | ||
| countries will be blocked. Leave empty to allow all. | ||
| </HelpText> | ||
| <CountryList | ||
| values={value.allowed_countries ?? []} | ||
| onChange={(v) => update({ allowed_countries: v })} | ||
| /> | ||
| </div> | ||
|
|
||
| <div> | ||
| <Label> | ||
| <Globe size={14} /> | ||
| Country Blocklist | ||
| </Label> | ||
| <HelpText> | ||
| Block connections from these countries. Takes priority over the | ||
| allowlist. | ||
| </HelpText> | ||
| <CountryList | ||
| values={value.blocked_countries ?? []} | ||
| onChange={(v) => update({ blocked_countries: v })} | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.