Skip to content

Commit

Permalink
fix(issue #239): resolve server crash caused by missing 'numbers' res…
Browse files Browse the repository at this point in the history
…ource
  • Loading branch information
psanders committed Dec 26, 2023
1 parent 8e8d3f5 commit 6df5aab
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 208 deletions.
3 changes: 1 addition & 2 deletions mods/connect/src/handlers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Helper as H } from "@routr/location"
import { Helper as H, ILocationService } from "@routr/location"
import {
Extensions as E,
MessageRequest,
Response,
Target as T
} from "@routr/processor"
import { ILocationService } from "@routr/location"
import {
Auth,
CommonConnect as CC,
Expand Down
65 changes: 36 additions & 29 deletions mods/connect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as grpc from "@grpc/grpc-js"
import * as jwt from "jsonwebtoken"
import {
CONNECT_VERIFIER_ADDR,
CONNECT_VERIFIER_OPTIONS,
CONNECT_VERIFIER_PUBLIC_KEY_PATH
} from "./envs"
import fs from "fs"
import {
HeaderModifier,
Expand All @@ -28,11 +34,6 @@ import {
} from "@routr/common"
import { Extensions as E } from "@routr/processor"
import { RoutingDirection } from "./types"
import {
CONNECT_VERIFIER_ADDR,
CONNECT_VERIFIER_OPTIONS,
CONNECT_VERIFIER_PUBLIC_KEY_PATH
} from "./envs"

// OMG, this is so ugly and hacky
export const isKind = (res: CC.RoutableResourceUnion, kind: CC.Kind) => {
Expand Down Expand Up @@ -78,35 +79,41 @@ export const findResource = async (
domainUri: string,
userpart: string
): Promise<CC.RoutableResourceUnion> => {
const domain = await findDomain(apiClient, domainUri)
try {
const domain = await findDomain(apiClient, domainUri)

// First, try to find a number
const number = await findNumberByTelUrl(apiClient, `tel:${userpart}`)
// First, try to find a number
const number = await findNumberByTelUrl(apiClient, `tel:${userpart}`)

if (number != null) return number
if (number != null) return number

// Next, try to find an agent
const agent = (
await apiClient.agents.findBy({
fieldName: "username",
fieldValue: userpart
})
).items[0]
// Next, try to find an agent
const agent = (
await apiClient.agents.findBy({
fieldName: "username",
fieldValue: userpart
})
).items[0]

if (agent && agent.domain.ref != domain?.ref) {
// Not in the same domain
return null
}

if (agent != null) return agent
if (agent && agent.domain.ref != domain?.ref) {
// Not in the same domain
return null
}

// Next, try to find a peer
return (
await apiClient.peers.findBy({
fieldName: "username",
fieldValue: userpart
})
).items[0]
if (agent != null) return agent

// Next, try to find a peer
return (
await apiClient.peers.findBy({
fieldName: "username",
fieldValue: userpart
})
).items[0]
} catch (err) {
if (err.code === grpc.status.NOT_FOUND) {
return null
}
}
}

export const getRoutingDirection = (
Expand Down
13 changes: 6 additions & 7 deletions mods/simpledata/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
* limitations under the License.
*/
import { JsonObject, struct } from "pb-util"
import { CommonTypes as CT } from "@routr/common"
import { CommonConnect as CC, CommonErrors as CE } from "@routr/common"
import { Helper as H } from "@routr/common"
import {
CommonTypes as CT,
CommonConnect as CC,
CommonErrors as CE,
Helper as H
} from "@routr/common"

/**
* Enclosure with method to obtain a resource by reference.
Expand Down Expand Up @@ -60,10 +63,6 @@ export function get(resources: CC.ConnectModel[]) {
*/
export function findBy(resources: CC.ConnectModel[]) {
return (call: CT.GrpcCall, callback: CT.GrpcCallback) => {
if (resources.length === 0) {
return callback(new CE.ResourceNotFoundError(""), null)
}

const { request } = call
const queryResult = resources.filter(
(r) =>
Expand Down
168 changes: 0 additions & 168 deletions mods/simpledata/src/loaders.ts

This file was deleted.

27 changes: 27 additions & 0 deletions mods/simpledata/src/loaders/acl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable require-jsdoc */
/*
* Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
* http://github.com/fonoster/routr
*
* This file is part of Routr.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CommonConnect as CC } from "@routr/common"

export function aclLoader(config: CC.UserConfig): CC.AccessControlList {
if (config.kind !== CC.Kind.ACL)
throw new Error("invalid resource type `AccessControlList`")

return CC.mapToACL(config)
}
38 changes: 38 additions & 0 deletions mods/simpledata/src/loaders/agents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable require-jsdoc */
/*
* Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
* http://github.com/fonoster/routr
*
* This file is part of Routr.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CommonConnect as CC } from "@routr/common"
import { findByRef } from "./find"

export function agentsLoader(
config: CC.UserConfig,
list: CC.UserConfig[]
): CC.Agent {
if (config.kind !== CC.Kind.AGENT)
throw new Error("invalid resource type `Agent`")

const agent = CC.mapToAgent(config)
agent.domain = findByRef(config.spec.domainRef, list) as CC.Domain
agent.credentials = findByRef(
config.spec.credentialsRef,
list
) as CC.Credentials

return agent
}
27 changes: 27 additions & 0 deletions mods/simpledata/src/loaders/credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable require-jsdoc */
/*
* Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
* http://github.com/fonoster/routr
*
* This file is part of Routr.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CommonConnect as CC } from "@routr/common"

export function credentialsLoader(config: CC.UserConfig): CC.Credentials {
if (config.kind !== CC.Kind.CREDENTIALS)
throw new Error("invalid resource type `Credentials`")

return CC.mapToCredentials(config)
}
Loading

0 comments on commit 6df5aab

Please sign in to comment.