Skip to content

Commit

Permalink
add pattern and add sync db on rpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bonez committed Jan 16, 2025
1 parent 60e5ac8 commit 51feba1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 39 deletions.
45 changes: 38 additions & 7 deletions core/startos/src/net/acme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,23 @@ impl<'a> async_acme::cache::AcmeCache for AcmeCertCache<'a> {
}

pub fn acme<C: Context>() -> ParentHandler<C> {
ParentHandler::new().subcommand(
"init",
from_fn_async(init)
.no_display()
.with_about("Setup ACME certificate acquisition")
.with_call_remote::<CliContext>(),
)
ParentHandler::new()
.subcommand(
"init",
from_fn_async(init)
.with_metadata("sync_db", Value::Bool(true))
.no_display()
.with_about("Setup ACME certificate acquisition")
.with_call_remote::<CliContext>(),
)
.subcommand(
"remove",
from_fn_async(remove)
.with_metadata("sync_db", Value::Bool(true))
.no_display()
.with_about("Setup ACME certificate acquisition")
.with_call_remote::<CliContext>(),
)
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, TS)]
Expand Down Expand Up @@ -230,3 +240,24 @@ pub async fn init(
.await?;
Ok(())
}

#[derive(Deserialize, Serialize, Parser)]
pub struct RemoveAcmeParams {
#[arg(long)]
pub provider: AcmeProvider,
}

pub async fn remove(
ctx: RpcContext,
RemoveAcmeParams { provider }: RemoveAcmeParams,
) -> Result<(), Error> {
ctx.db
.mutate(|db| {
db.as_public_mut()
.as_server_info_mut()
.as_acme_mut()
.remove(&provider)
})
.await?;
Ok(())
}
4 changes: 4 additions & 0 deletions core/startos/src/net/host/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn address<C: Context>() -> ParentHandler<C, AddressApiParams, PackageId> {
.subcommand(
"add",
from_fn_async(add_domain)
.with_metadata("sync_db", Value::Bool(true))
.with_inherited(|_, a| a)
.no_display()
.with_about("Add an address to this host")
Expand All @@ -56,6 +57,7 @@ pub fn address<C: Context>() -> ParentHandler<C, AddressApiParams, PackageId> {
.subcommand(
"remove",
from_fn_async(remove_domain)
.with_metadata("sync_db", Value::Bool(true))
.with_inherited(|_, a| a)
.no_display()
.with_about("Remove an address from this host")
Expand All @@ -69,6 +71,7 @@ pub fn address<C: Context>() -> ParentHandler<C, AddressApiParams, PackageId> {
.subcommand(
"add",
from_fn_async(add_onion)
.with_metadata("sync_db", Value::Bool(true))
.with_inherited(|_, a| a)
.no_display()
.with_about("Add an address to this host")
Expand All @@ -77,6 +80,7 @@ pub fn address<C: Context>() -> ParentHandler<C, AddressApiParams, PackageId> {
.subcommand(
"remove",
from_fn_async(remove_onion)
.with_metadata("sync_db", Value::Bool(true))
.with_inherited(|_, a| a)
.no_display()
.with_about("Remove an address from this host")
Expand Down
1 change: 1 addition & 0 deletions core/startos/src/net/host/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub fn binding<C: Context>() -> ParentHandler<C, BindingApiParams, PackageId> {
.subcommand(
"set-public",
from_fn_async(set_public)
.with_metadata("sync_db", Value::Bool(true))
.with_inherited(|BindingApiParams { host }, package| (package, host))
.no_display()
.with_about("Add an binding to this host")
Expand Down
29 changes: 17 additions & 12 deletions sdk/base/lib/util/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,68 @@ import { Pattern } from "../actions/input/inputSpecTypes"
import * as regexes from "./regexes"

export const ipv6: Pattern = {
regex: regexes.ipv6.source,
regex: regexes.ipv6.matches(),
description: "Must be a valid IPv6 address",
}

export const ipv4: Pattern = {
regex: regexes.ipv4.source,
regex: regexes.ipv4.matches(),
description: "Must be a valid IPv4 address",
}

export const hostname: Pattern = {
regex: regexes.hostname.source,
regex: regexes.hostname.matches(),
description: "Must be a valid hostname",
}

export const localHostname: Pattern = {
regex: regexes.localHostname.source,
regex: regexes.localHostname.matches(),
description: 'Must be a valid ".local" hostname',
}

export const torHostname: Pattern = {
regex: regexes.torHostname.source,
regex: regexes.torHostname.matches(),
description: 'Must be a valid Tor (".onion") hostname',
}

export const url: Pattern = {
regex: regexes.url.source,
regex: regexes.url.matches(),
description: "Must be a valid URL",
}

export const localUrl: Pattern = {
regex: regexes.localUrl.source,
regex: regexes.localUrl.matches(),
description: 'Must be a valid ".local" URL',
}

export const torUrl: Pattern = {
regex: regexes.torUrl.source,
regex: regexes.torUrl.matches(),
description: 'Must be a valid Tor (".onion") URL',
}

export const ascii: Pattern = {
regex: regexes.ascii.source,
regex: regexes.ascii.matches(),
description:
"May only contain ASCII characters. See https://www.w3schools.com/charsets/ref_html_ascii.asp",
}

export const domain: Pattern = {
regex: regexes.domain.matches(),
description: "Must be a valid Fully Qualified Domain Name",
}

export const email: Pattern = {
regex: regexes.email.source,
regex: regexes.email.matches(),
description: "Must be a valid email address",
}

export const emailWithName: Pattern = {
regex: regexes.emailWithName.source,
regex: regexes.emailWithName.matches(),
description: "Must be a valid email address, optionally with a name",
}

export const base64: Pattern = {
regex: regexes.base64.source,
regex: regexes.base64.matches(),
description:
"May only contain base64 characters. See https://base64.guru/learn/base64-characters",
}
73 changes: 53 additions & 20 deletions sdk/base/lib/util/regexes.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,71 @@
export class ComposableRegex {
readonly regex: RegExp
constructor(regex: RegExp | string) {
if (regex instanceof RegExp) {
this.regex = regex
} else {
this.regex = new RegExp(regex)
}
}
asExpr(): string {
return `(${this.regex.source})`
}
matches(): string {
return `^${this.regex.source}$`
}
contains(): string {
return this.regex.source
}
}

// https://ihateregex.io/expr/ipv6/
export const ipv6 =
/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){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,1}[0-9]))/,
)

// https://ihateregex.io/expr/ipv4/
export const ipv4 =
/(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/
export const ipv4 = new ComposableRegex(
/(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/,
)

export const hostname =
/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/
export const hostname = new ComposableRegex(
/(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])/,
)

export const localHostname = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.local/
export const localHostname = new ComposableRegex(
/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.local/,
)

export const torHostname = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.onion/
export const torHostname = new ComposableRegex(
/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.onion/,
)

// https://ihateregex.io/expr/url/
export const url =
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/
export const url = new ComposableRegex(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/,
)

export const localUrl =
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.local\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/
export const localUrl = new ComposableRegex(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.local\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/,
)

export const torUrl =
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.onion\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/
export const torUrl = new ComposableRegex(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.onion\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/,
)

// https://ihateregex.io/expr/ascii/
export const ascii = /^[ -~]*$/
export const ascii = new ComposableRegex(/[ -~]*/)

export const domain = new ComposableRegex(/[A-Za-z0-9.-]+\.[A-Za-z]{2,}/)

// https://www.regular-expressions.info/email.html
export const email = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/
export const email = new ComposableRegex(`[A-Za-z0-9._%+-]+@${domain.asExpr()}`)

export const emailWithName = new RegExp(
`(${email.source})|([^<]*<(${email.source})>)`,
export const emailWithName = new ComposableRegex(
`${email.asExpr()}|([^<]*<${email.asExpr()}>)`,
)

//https://rgxdb.com/r/1NUN74O6
export const base64 =
/^(?:[a-zA-Z0-9+\/]{4})*(?:|(?:[a-zA-Z0-9+\/]{3}=)|(?:[a-zA-Z0-9+\/]{2}==)|(?:[a-zA-Z0-9+\/]{1}===))$/
export const base64 = new ComposableRegex(
/(?:[a-zA-Z0-9+\/]{4})*(?:|(?:[a-zA-Z0-9+\/]{3}=)|(?:[a-zA-Z0-9+\/]{2}==)|(?:[a-zA-Z0-9+\/]{1}===))/,
)

0 comments on commit 51feba1

Please sign in to comment.