Skip to content

Commit

Permalink
physicalAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Mar 11, 2024
1 parent 1bc117a commit ee8f574
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
7 changes: 5 additions & 2 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ services:
# zt container
zerotier:
image: zyclonite/zerotier:1.12.2


ports:
- "9993:9993"
environment:
- ZT_OVERRIDE_LOCAL_CONF=true
- ZT_ALLOW_MANAGEMENT_FROM=0.0.0.0/0
# ztnet
ztnet:
build:
Expand Down
19 changes: 9 additions & 10 deletions src/server/api/services/memberService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@ export const syncMemberPeersAndStatus = async (
ztMembers: MemberEntity[],
) => {
if (ztMembers.length === 0) return [];

const updatedMembers = await Promise.all(
ztMembers.map(async (ztMember) => {
const peers = await ztController.peer(ctx, ztMember.address).catch(() => null);
const dbMember = await retrieveActiveMemberFromDatabase(nwid, ztMember.id);
const activePreferredPath = findActivePreferredPeerPath(peers);

const flattenPeers = activePreferredPath
? {
physicalAddress: activePreferredPath.address || dbMember?.physicalAddress,
...peers,
}
: {};
const { physicalAddress, ...restOfDbMembers } = dbMember || {};

const flattenPeers = {
physicalAddress: activePreferredPath?.address || physicalAddress,
...peers,
};

// Merge the data from the database with the data from Controller
const updatedMember = {
...dbMember,
...restOfDbMembers,
...ztMember,
peers: activePreferredPath ? flattenPeers : {},
peers: flattenPeers,
} as MemberEntity;

// Update the connection status
Expand Down Expand Up @@ -85,7 +84,7 @@ export const syncMemberPeersAndStatus = async (
return updatedMember;
}),
);
// console.log(updatedMembers);
// console.log(updatedMembers[0].peers?.paths);
return updatedMembers.filter(Boolean); // Filter out any null values
};

Expand Down
32 changes: 18 additions & 14 deletions src/server/api/utils/memberUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,36 @@ export enum ConnectionStatus {
}

export function determineConnectionStatus(member: MemberEntity): ConnectionStatus {
// Check for Controller status
const regex = new RegExp(`^${member.id}`);
if (regex.test(member.nwid)) {
return ConnectionStatus.Controller;
}
// fix for zt version 1.12. Return type of peer is object!.
if (!member?.peers || Object.keys(member?.peers).length === 0) {
return ConnectionStatus.Offline;
}

if (Array.isArray(member?.peers) && member?.peers.length === 0) {
// Determine if Offline
const peerKeys = Object.keys(member?.peers || {});
if (!peerKeys || (peerKeys.length === 1 && peerKeys.includes("physicalAddress"))) {
return ConnectionStatus.Offline;
}

if (member?.peers?.latency === -1 || member?.peers?.versionMajor === -1) {
// At this point, it is ensured that the peers object exists and has more properties
const { latency, paths } = member.peers;

// Determine if Relayed
if (latency === -1) {
return ConnectionStatus.Relayed;
}

// Check if at least one path has a private IP
if (member?.peers?.paths && member?.peers.paths.length > 0) {
for (const path of member.peers.paths) {
// Determine Direct connection type (DirectLAN or DirectWAN)
if (Array.isArray(paths) && paths.some((path) => path.active && !path.expired)) {
const hasDirectLAN = paths.some((path) => {
const ip = path.address.split("/")[0];
if (isPrivateIP(ip)) {
return ConnectionStatus.DirectLAN;
}
}
return isPrivateIP(ip);
});

return hasDirectLAN ? ConnectionStatus.DirectLAN : ConnectionStatus.DirectWAN;
}

return ConnectionStatus.DirectWAN;
// If none of the above conditions are met, default to Offline as a fallback
return ConnectionStatus.Offline;
}
1 change: 0 additions & 1 deletion src/utils/ztApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ export const peers = async (ctx: UserContext): Promise<ZTControllerGetPeer> => {
const { localControllerUrl } = await getOptions(ctx, false);

const addr = `${localControllerUrl}/peer`;

// get headers based on local or central api
const { headers } = await getOptions(ctx, false);

Expand Down

0 comments on commit ee8f574

Please sign in to comment.