Skip to content

Commit

Permalink
Update IP resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
C-D-Lewis committed Oct 27, 2024
1 parent b8d6cb3 commit 60308f8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dashboard2/src/components/DeviceMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const DeviceMetrics = () => fabricate('Row')
return;
}

if (keys.includes('metricNames')) {
if (keys.includes('metricNames') && metricNames.length) {
el.setChildren(
metricNames
.filter((name) => !!METRIC_NAME_MAP[name])
Expand Down
1 change: 1 addition & 0 deletions dashboard2/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const CONDUIT_PORT = 5959;
export const INITIAL_STATE: AppState = {
// App data
token: '',
publicIp: '',

// Loaded data
selectedDeviceApps: [],
Expand Down
18 changes: 13 additions & 5 deletions dashboard2/src/services/conduitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const sendConduitPacket = async (
deviceNameOverride?: string,
tokenOverride?: string,
) => {
const { token, selectedDevice, devices } = state;
const { token, selectedDevice, devices, publicIp: currentPublicIp } = state;
// const reqStateKey = appRequestStateKey(packet.to);
// fabricate.update(reqStateKey, 'pending');

Expand All @@ -38,18 +38,22 @@ export const sendConduitPacket = async (
: selectedDevice;
if (!finalDevice) throw new Error('Unable to identify device to send message to.');

// If we're on the same local detwork, use local IP, else try conduit forwarding
const { localIp, publicIp } = finalDevice;
const isLocalDevice = publicIp === currentPublicIp;
const finalIp = isLocalDevice ? localIp : publicIp;
const finalHost = isLocalDevice ? undefined : localIp;

// Destination is local if reachable, else forward local via public
console.log(`${finalDevice?.deviceName} >>> ${JSON.stringify(packet)}`);

const res = await fetch(`http://${publicIp}:${CONDUIT_PORT}/conduit`, {
const res = await fetch(`http://${finalIp}:${CONDUIT_PORT}/conduit`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
...packet,
auth: tokenOverride || token || '',
host: localIp,
host: finalHost,
device: finalDevice && finalDevice.deviceName,
}),
});
Expand All @@ -74,6 +78,10 @@ export const fetchFleetList = async (state: AppState) => {
const { token } = state;
fabricate.update({ devices: [] });

// First get this machine's public IP to know if we can use local IPs
const { ip: publicIp } = await fetch('https://api.ipify.org?format=json')
.then((r) => r.json());

try {
// Can't use sendConduitPacket, not a device by name
const res = await fetch(`http://${FLEET_HOST}:${CONDUIT_PORT}/conduit`, {
Expand All @@ -87,7 +95,7 @@ export const fetchFleetList = async (state: AppState) => {
}),
});
const { message } = await res.json();
fabricate.update({ devices: message.value });
fabricate.update({ devices: message.value, publicIp });
} catch (err) {
console.error(err);
alert(err);
Expand Down Expand Up @@ -222,7 +230,7 @@ export const fetchMetric = async (state: AppState, name: string) => {
*/
export const fetchMetricNames = async (state: AppState) => {
const {
message: metricNames,
message: metricNames = [],
} = await sendConduitPacket(state, { to: 'monitor', topic: 'getMetricNames' });
fabricate.update({ metricNames });
};
1 change: 1 addition & 0 deletions dashboard2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type AppState = {

// App data
token: string;
publicIp: string;

// Loaded data
selectedDeviceApps: DeviceApp[];
Expand Down

0 comments on commit 60308f8

Please sign in to comment.