fix sort functions for public and private domains

This commit is contained in:
Matt Hill
2025-08-13 14:28:53 -06:00
parent d5bb537368
commit bfc88a2225

View File

@@ -1,7 +1,6 @@
import { inject, Injectable } from '@angular/core' import { inject, Injectable } from '@angular/core'
import { T, utils } from '@start9labs/start-sdk' import { T, utils } from '@start9labs/start-sdk'
import { ConfigService } from 'src/app/services/config.service' import { ConfigService } from 'src/app/services/config.service'
import { toAuthorityName } from 'src/app/utils/acme'
import { GatewayPlus } from 'src/app/services/gateway.service' import { GatewayPlus } from 'src/app/services/gateway.service'
import { PublicDomain } from './public-domains/pd.service' import { PublicDomain } from './public-domains/pd.service'
import { i18nKey } from '@start9labs/shared' import { i18nKey } from '@start9labs/shared'
@@ -43,7 +42,7 @@ function cmpLan(host: T.Host, a: LanAddress, b: LanAddress): -1 | 0 | 1 {
return cmpWithRankedPredicates(a, b, [ return cmpWithRankedPredicates(a, b, [
x => x =>
x.info.hostname.kind === 'domain' && x.info.hostname.kind === 'domain' &&
!host.domains[x.info.hostname.value]?.public, // private domain !!host.privateDomains[x.info.hostname.value], // private domain
x => x.info.hostname.kind === 'local', // .local x => x.info.hostname.kind === 'local', // .local
x => x.info.hostname.kind === 'ipv4', // ipv4 x => x.info.hostname.kind === 'ipv4', // ipv4
x => x.info.hostname.kind === 'ipv6', // ipv6 x => x.info.hostname.kind === 'ipv6', // ipv6
@@ -67,7 +66,7 @@ function cmpVpn(host: T.Host, a: VpnAddress, b: VpnAddress): -1 | 0 | 1 {
return cmpWithRankedPredicates(a, b, [ return cmpWithRankedPredicates(a, b, [
x => x =>
x.info.hostname.kind === 'domain' && x.info.hostname.kind === 'domain' &&
!host.domains[x.info.hostname.value]?.public, // private domain !!host.privateDomains[x.info.hostname.value], // private domain
x => x.info.hostname.kind === 'ipv4', // ipv4 x => x.info.hostname.kind === 'ipv4', // ipv4
x => x.info.hostname.kind === 'ipv6', // ipv6 x => x.info.hostname.kind === 'ipv6', // ipv6
// remainder: public domains accessible privately // remainder: public domains accessible privately
@@ -85,7 +84,6 @@ function filterClearnet(a: AddressWithInfo): a is ClearnetAddress {
return a.info.kind === 'ip' && a.info.public return a.info.kind === 'ip' && a.info.public
} }
function cmpClearnet( function cmpClearnet(
domains: Record<string, T.DomainSettings>,
host: T.Host, host: T.Host,
a: ClearnetAddress, a: ClearnetAddress,
b: ClearnetAddress, b: ClearnetAddress,
@@ -93,8 +91,7 @@ function cmpClearnet(
return cmpWithRankedPredicates(a, b, [ return cmpWithRankedPredicates(a, b, [
x => x =>
x.info.hostname.kind === 'domain' && x.info.hostname.kind === 'domain' &&
x.info.gatewayId === x.info.gatewayId === host.publicDomains[x.info.hostname.value]?.gateway, // public domain for this gateway
domains[host.domains[x.info.hostname.value]?.root!]?.gateway, // public domain for this gateway
x => x.info.hostname.kind === 'ipv4', // ipv4 x => x.info.hostname.kind === 'ipv4', // ipv4
x => x.info.hostname.kind === 'ipv6', // ipv6 x => x.info.hostname.kind === 'ipv6', // ipv6
// remainder: private domains / domains public on other gateways // remainder: private domains / domains public on other gateways
@@ -106,7 +103,6 @@ function toDisplayAddress(
{ info, url }: AddressWithInfo, { info, url }: AddressWithInfo,
gateways: GatewayPlus[], gateways: GatewayPlus[],
publicDomains: Record<string, T.DomainConfig>, publicDomains: Record<string, T.DomainConfig>,
privateDomains: string[],
): DisplayAddress { ): DisplayAddress {
let access: DisplayAddress['access'] let access: DisplayAddress['access']
let gatewayName: DisplayAddress['gatewayName'] let gatewayName: DisplayAddress['gatewayName']
@@ -209,7 +205,7 @@ function toDisplayAddress(
`Requires a DNS record for ${info.hostname.value} that resolves to ${gateway.ipInfo.wanIp}`, `Requires a DNS record for ${info.hostname.value} that resolves to ${gateway.ipInfo.wanIp}`,
`Requires port forwarding in gateway "${gatewayName}": ${port} -> ${info.hostname.value}:${port === 443 ? 5443 : port}`, `Requires port forwarding in gateway "${gatewayName}": ${port} -> ${info.hostname.value}:${port === 443 ? 5443 : port}`,
] ]
if (publicDomains[info.hostname.value]!) { if (publicDomains[info.hostname.value]?.acme) {
bullets.unshift('Ideal for public access via the Internet') bullets.unshift('Ideal for public access via the Internet')
} else { } else {
bullets = [ bullets = [
@@ -251,7 +247,9 @@ function toDisplayAddress(
} }
} }
export function getPublicDomains(publicDomains: any): PublicDomain[] { export function getPublicDomains(
publicDomains: Record<string, T.DomainConfig>,
): PublicDomain[] {
return Object.entries(publicDomains).map(([fqdn, info]) => ({ return Object.entries(publicDomains).map(([fqdn, info]) => ({
fqdn, fqdn,
...info, ...info,
@@ -267,7 +265,6 @@ export class InterfaceService {
getAddresses( getAddresses(
serviceInterface: T.ServiceInterface, serviceInterface: T.ServiceInterface,
host: T.Host, host: T.Host,
serverDomains: Record<string, T.DomainSettings>,
gateways: GatewayPlus[], gateways: GatewayPlus[],
): MappedServiceInterface['addresses'] { ): MappedServiceInterface['addresses'] {
const hostnamesInfos = this.hostnameInfo(serviceInterface, host) const hostnamesInfos = this.hostnameInfo(serviceInterface, host)
@@ -294,7 +291,7 @@ export class InterfaceService {
.sort((a, b) => cmpVpn(host, a, b)) .sort((a, b) => cmpVpn(host, a, b))
const clearnetAddrs = allAddressesWithInfo const clearnetAddrs = allAddressesWithInfo
.filter(filterClearnet) .filter(filterClearnet)
.sort((a, b) => cmpClearnet(serverDomains, host, a, b)) .sort((a, b) => cmpClearnet(host, a, b))
let bestAddrs = [clearnetAddrs[0], lanAddrs[0], vpnAddrs[0], torAddrs[0]] let bestAddrs = [clearnetAddrs[0], lanAddrs[0], vpnAddrs[0], torAddrs[0]]
.filter(a => !!a) .filter(a => !!a)