import { Datagram } from "../Datagram/datagram-types.js" import { DatagramShort } from "../Datagram/datagram.js"; import { RandomString } from "../RememberableNames/random-host-name.js"; import { processType } from "../util/misc.js"; import { HostName } from "./HostName.js"; export type TBurpastatePersistent = { ssl: boolean, nonBurpahttpHosts: string[] | undefined, processId: string, processType: string, localGateway: string, localHost: string | null, rootGateway: string | null, rootHost: string | null, aliases: { [key: string]: string }, hosts: { [key: string]: THost }, log: string[] } export type THostType = "gateway" | "vagrant"; export type THostBase = { type: THostType, nextReqno: number, owned: { [key: string]: THostOwnershipRecord }, burpaport: string[]; } export type TVagrant = { type: "vagrant" } & THostBase export type TGateway = { type: "gateway", upstream: string[] } & THostBase export type THost = TGateway | TVagrant; export type THostOwnershipRecord = { type: THostType, registred: boolean, name: string, secret: string, gateway?: string, } export type RouteAction = { action: "error" | "route" | "inbox" | "connect" | "arrived", transit?: string, datagram: Datagram, message?: string, destination?: string, } export function newBurpaPersistent(): TBurpastatePersistent { return { ssl: false, nonBurpahttpHosts: undefined, processId: RandomString(), processType: processType(), localGateway: null as unknown as string, localHost: null as unknown as string, aliases: {}, hosts: {}, rootGateway: null, rootHost: null, log: [] } } // /** // * // * @param datagram // * @returns RouteAction // */ // export function GetRouteAction(state: TBurpastatePersistent, datagram: Datagram): RouteAction { // let to = datagram.to; // let ton = new HostName(datagram.to); // console.log(datagram.to); // if (datagram.trail.indexOf(state.localGateway) !== -1) { // datagram.trail.push(state.localGateway); // return { action: "error", message: `Detected routing circularity for datagram ${DatagramShort(datagram)}`, datagram } // } // let found = findLocalHost(state, to); // if (found) { // return { action: "arrived", datagram } // } // // TODO! Find closest parent // let parent = findLocalHost(state, ton.parent); // if (parent) { // let rec = parent.owned[to]; // if (rec) { // if (rec.registred) { // return { action: "route", transit: rec.gateway!, datagram } // } // } else { // return { action: "inbox", datagram } // } // } // if (state.localGateway == state.rootGateway) { // return { action: "error", datagram }; // } // else { // let gw = getLocalGateway(state); // if (gw.upstream.length == 0) { // return { action: "connect", datagram } // } else { // let transit = gw.upstream[0]; // return { action: "route", datagram, transit }; // } // } // return { action: "error", datagram }; // } export function getLocalGateway(state: TBurpastatePersistent): TGateway { return state.hosts[state.localGateway] as TGateway; } export function findLocalHost(state: TBurpastatePersistent, host: string): THost | null { host = state.aliases[host]; if (host) { if (state.hosts[host]) { return state.hosts[host]; } } return null; }