Skip to content

Commit 6e4f766

Browse files
auto detect ip address if not provided
1 parent 07bec6c commit 6e4f766

7 files changed

Lines changed: 47 additions & 7 deletions

File tree

node/mesh_peer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function main() {
1212
heartbeat_threshold: 3
1313
},
1414
pubsub: {},
15-
host: '127.0.0.1',
15+
// host: '127.0.0.1',
1616
port: 0
1717
};
1818

node/src/demo_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async function main() {
88
heartbeat_threshold: 3
99
},
1010
pubsub: {},
11-
host: '127.0.0.1',
11+
// host: '127.0.0.1', // Auto-detected
1212
port: 0
1313
};
1414

node/src/mesh.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { v4 as uuidv4 } from 'uuid';
2+
import * as os from 'os';
23
import { MeshConfig, Node, Message } from './shared/types';
34
import { RedisServiceDiscovery } from './discovery/redis-discovery';
45
import { H2Communication } from './communication/h2-communication';
@@ -28,6 +29,12 @@ export class Mesh implements IMesh {
2829

2930
constructor(config: MeshConfig) {
3031
this.config = config;
32+
33+
if (!this.config.host) {
34+
this.config.host = this.getLocalIP();
35+
console.log(`[Mesh] Auto-detected host IP: ${this.config.host}`);
36+
}
37+
3138
this.discovery = new RedisServiceDiscovery(config.redis, config.service_discovery);
3239
this.communication = new H2Communication(config);
3340
this.pubsub = new RedisPubSub(config.redis);
@@ -51,7 +58,7 @@ export class Mesh implements IMesh {
5158
// Register with SD
5259
const node: Node = {
5360
id: this.nodeId,
54-
host: this.config.host,
61+
host: this.config.host!, // Detected in constructor
5562
port: this.config.port,
5663
service_name: serviceName
5764
};
@@ -60,6 +67,20 @@ export class Mesh implements IMesh {
6067
console.log(`[Mesh] Registered service '${serviceName}' on ${node.host}:${node.port}`);
6168
}
6269

70+
private getLocalIP(): string {
71+
const interfaces = os.networkInterfaces();
72+
for (const name of Object.keys(interfaces)) {
73+
for (const iface of interfaces[name]!) {
74+
// Skip internal and non-IPv4 addresses
75+
if ('IPv4' !== iface.family || iface.internal) {
76+
continue;
77+
}
78+
return iface.address;
79+
}
80+
}
81+
return '127.0.0.1'; // Fallback
82+
}
83+
6384
public service(name: string): IServiceClient {
6485
return new ServiceClient(name, this.discovery, this.communication);
6586
}

node/src/shared/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface MeshConfig {
3131
redis: RedisConfig; // Shared Redis Configuration
3232
service_discovery: ServiceDiscoveryConfig;
3333
pubsub?: PubSubConfig;
34-
host: string;
34+
host?: string;
3535
port: number; // 0 for dynamic
3636
debug?: boolean;
3737
}

python/mesh_peer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async def main():
1818
heartbeat_interval=2,
1919
heartbeat_threshold=3
2020
),
21-
host="127.0.0.1",
21+
# host="127.0.0.1", # Auto-detected
2222
port=0
2323
)
2424

python/src/mesh_lib/mesh.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import uuid
3+
import socket
34
from typing import Optional, List, Callable, Awaitable
45
from mesh_lib.shared.types import MeshConfig, Node, Message
56
from mesh_lib.service_discovery import RedisServiceDiscovery
@@ -9,9 +10,27 @@
910
from mesh_lib.abstract import IMesh, IChannel, ITopic, IServiceHandlers
1011
from mesh_lib.service_client import ServiceClient
1112

12-
class Mesh(IMesh):
13+
class Mesh(IMesh):
14+
@staticmethod
15+
def _get_local_ip() -> str:
16+
try:
17+
# Trick to finding local IP by connecting to a public DNS
18+
# (doesn't actually establish connection)
19+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
20+
s.connect(("8.8.8.8", 80))
21+
ip = s.getsockname()[0]
22+
s.close()
23+
return ip
24+
except Exception:
25+
return "127.0.0.1"
26+
1327
def __init__(self, config: MeshConfig):
1428
self.config = config
29+
30+
if not self.config.host:
31+
self.config.host = self._get_local_ip()
32+
print(f"[Mesh] Auto-detected host IP: {self.config.host}")
33+
1534
self.service_discovery = RedisServiceDiscovery(config.redis, config.service_discovery)
1635
self.communication = H2Communication(config, self.service_discovery)
1736
self.pubsub = RedisPubSub(config.redis)

python/src/mesh_lib/shared/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PubSubConfig:
2121
class MeshConfig:
2222
service_discovery: ServiceDiscoveryConfig
2323
pubsub: PubSubConfig = field(default_factory=PubSubConfig)
24-
host: str = "127.0.0.1"
24+
host: Optional[str] = None
2525
port: int = 8000
2626
debug: bool = False
2727
redis: Dict[str, Any] = field(default_factory=dict) # Shared Redis Config

0 commit comments

Comments
 (0)