11package io .quarkus .stork ;
22
33import java .io .IOException ;
4+ import java .net .Inet4Address ;
45import java .net .InetAddress ;
56import java .net .NetworkInterface ;
7+ import java .net .SocketException ;
68import java .net .UnknownHostException ;
79import java .util .ArrayList ;
810import java .util .Enumeration ;
@@ -55,27 +57,30 @@ public static List<ServiceConfig> toStorkServiceConfig(StorkConfiguration storkC
5557 public static ServiceConfiguration buildDefaultRegistrarConfiguration (String serviceRegistrarType , String healthCheckPath ) {
5658 Map <String , String > parameters = new HashMap <>();
5759 Config quarkusConfig = ConfigProvider .getConfig ();
58- String defaultHost = quarkusConfig .getValue (QUARKUS_HTTP_HOST , String .class );
5960 if (healthCheckPath != null && !healthCheckPath .isBlank ()) {
6061 healthCheckPath = HTTPS + getOrDefaultHost (parameters , quarkusConfig ) + ":"
6162 + getOrDefaultPort (parameters , quarkusConfig ) + healthCheckPath ;
6263 parameters .put ("health-check-url" , healthCheckPath );
6364 }
64- return buildServiceConfigurationWithRegistrar (serviceRegistrarType , parameters );
65+ return buildServiceConfigurationWithRegistrar (serviceRegistrarType , true , parameters );
6566 }
6667
6768 public static ServiceConfiguration addRegistrarTypeIfAbsent (String serviceRegistrarType ,
6869 ServiceConfiguration serviceConfiguration , String healthCheckUrl ) {
69- Map <String , String > parameters = serviceConfiguration .serviceRegistrar ()
70+ Optional <StorkServiceRegistrarConfiguration > storkServiceRegistrarConfiguration = serviceConfiguration
71+ .serviceRegistrar ();
72+ Map <String , String > parameters = storkServiceRegistrarConfiguration
7073 .map (StorkServiceRegistrarConfiguration ::parameters )
7174 .orElse (new HashMap <>());
7275 if (healthCheckUrl != null && !healthCheckUrl .isBlank ()) {
7376 parameters .put ("health-check-url" , healthCheckUrl );
7477 }
75- return buildServiceConfigurationWithRegistrar (serviceRegistrarType , parameters );
78+ boolean enabled = storkServiceRegistrarConfiguration .map (StorkServiceRegistrarConfiguration ::enabled ).orElse (true );
79+ return buildServiceConfigurationWithRegistrar (serviceRegistrarType , enabled , parameters );
7680 }
7781
78- private static ServiceConfiguration buildServiceConfigurationWithRegistrar (String type , Map <String , String > parameters ) {
82+ private static ServiceConfiguration buildServiceConfigurationWithRegistrar (String type , boolean enabled ,
83+ Map <String , String > parameters ) {
7984 return new ServiceConfiguration () {
8085 @ Override
8186 public Optional <StorkServiceDiscoveryConfiguration > serviceDiscovery () {
@@ -89,17 +94,17 @@ public StorkLoadBalancerConfiguration loadBalancer() {
8994
9095 @ Override
9196 public Optional <StorkServiceRegistrarConfiguration > serviceRegistrar () {
92- return Optional .of (buildServiceRegistrarConfiguration (type , parameters ));
97+ return Optional .of (buildServiceRegistrarConfiguration (type , enabled , parameters ));
9398 }
9499 };
95100 }
96101
97- private static StorkServiceRegistrarConfiguration buildServiceRegistrarConfiguration (String type ,
102+ private static StorkServiceRegistrarConfiguration buildServiceRegistrarConfiguration (String type , boolean enabled ,
98103 Map <String , String > parameters ) {
99104 return new StorkServiceRegistrarConfiguration () {
100105 @ Override
101106 public boolean enabled () {
102- return true ;
107+ return enabled ;
103108 }
104109
105110 @ Override
@@ -117,7 +122,7 @@ public Map<String, String> parameters() {
117122 public static String getOrDefaultHost (Map <String , String > parameters , Config quarkusConfig ) {
118123 String customHost = parameters .containsKey ("ip-address" ) ? parameters .get ("ip-address" )
119124 : null ;
120- String defaultHost = quarkusConfig .getValue ("quarkus.http.host" , String .class );
125+ String defaultHost = quarkusConfig .getValue (QUARKUS_HTTP_HOST , String .class );
121126 if (customHost == null || customHost .isEmpty ()) {
122127 InetAddress inetAddress = StorkConfigUtil .detectAddress ();
123128 customHost = inetAddress != null ? inetAddress .getHostAddress () : defaultHost ;
@@ -130,29 +135,143 @@ public static int getOrDefaultPort(Map<String, String> parameters, Config quarku
130135 return Integer .parseInt (customPort );
131136 }
132137
138+ // public static InetAddress detectAddress() {
139+ // InetAddress result = null;
140+ // try {
141+ // int lowest = Integer.MAX_VALUE;
142+ // Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
143+ // while (networkInterfaces.hasMoreElements()) {
144+ // NetworkInterface networkInterface = networkInterfaces.nextElement();
145+ // if (networkInterface.isUp()) {
146+ // LOGGER.debug("Testing interface: {}" + networkInterface.getDisplayName());
147+ // if (networkInterface.getIndex() < lowest || result == null) {
148+ // lowest = networkInterface.getIndex();
149+ // } else if (result != null) {
150+ // continue;
151+ // }
152+ // }
153+ // }
154+ // } catch (IOException ex) {
155+ // LOGGER.error("Unable to get first non-loopback address", ex);
156+ // }
157+ // try {
158+ // return InetAddress.getLocalHost();
159+ // } catch (UnknownHostException e) {
160+ // LOGGER.error("Unable to detect address", e);
161+ // }
162+ //
163+ // return null;
164+ // }
165+
166+ // public static InetAddress detectAddress() {
167+ // try {
168+ // Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
169+ // while (interfaces.hasMoreElements()) {
170+ // NetworkInterface iface = interfaces.nextElement();
171+ // if (!iface.isUp() || iface.isLoopback())
172+ // continue;
173+ //
174+ // Enumeration<InetAddress> addresses = iface.getInetAddresses();
175+ // while (addresses.hasMoreElements()) {
176+ // InetAddress addr = addresses.nextElement();
177+ // if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
178+ // return addr;
179+ // }
180+ // }
181+ // }
182+ // } catch (IOException e) {
183+ // LOGGER.error("Failed to detect IP address", e);
184+ // }
185+ //
186+ // try {
187+ // return InetAddress.getLocalHost();
188+ // } catch (UnknownHostException e) {
189+ // LOGGER.error("Fallback to localhost failed", e);
190+ // return null;
191+ // }
192+ // }
193+ //
194+ // public static InetAddress detectAddress() {
195+ // try {
196+ // Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
197+ // return findFirstValidAddress(interfaces);
198+ // } catch (IOException e) {
199+ // LOGGER.error("Failed to detect IP address", e);
200+ // }
201+ //
202+ // try {
203+ // return InetAddress.getLocalHost();
204+ // } catch (UnknownHostException e) {
205+ // LOGGER.error("Fallback to localhost failed", e);
206+ // return null;
207+ // }
208+ // }
209+
210+ // static InetAddress findFirstValidAddress(Enumeration<NetworkInterface> interfaces) {
211+ // try {
212+ // while (interfaces.hasMoreElements()) {
213+ // NetworkInterface iface = interfaces.nextElement();
214+ // if (!iface.isUp() || iface.isLoopback()) {
215+ // continue;
216+ // }
217+ //
218+ // Enumeration<InetAddress> addresses = iface.getInetAddresses();
219+ // while (addresses.hasMoreElements()) {
220+ // InetAddress addr = addresses.nextElement();
221+ // if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
222+ // return addr;
223+ // }
224+ // }
225+ // }
226+ // } catch (IOException e) {
227+ // LOGGER.error("Error processing network interfaces", e);
228+ // }
229+ //
230+ // return null;
231+ // }
232+
133233 public static InetAddress detectAddress () {
134- InetAddress result = null ;
135234 try {
136- int lowest = Integer .MAX_VALUE ;
137- Enumeration <NetworkInterface > networkInterfaces = NetworkInterface .getNetworkInterfaces ();
138- while (networkInterfaces .hasMoreElements ()) {
139- NetworkInterface networkInterface = networkInterfaces .nextElement ();
140- if (networkInterface .isUp ()) {
141- LOGGER .debug ("Testing interface: {}" + networkInterface .getDisplayName ());
142- if (networkInterface .getIndex () < lowest || result == null ) {
143- lowest = networkInterface .getIndex ();
144- } else if (result != null ) {
145- continue ;
146- }
147- }
235+ Enumeration <NetworkInterface > interfaces = NetworkInterface .getNetworkInterfaces ();
236+ List <NetworkInterfaceWrapper > wrappedInterfaces = new ArrayList <>();
237+
238+ while (interfaces .hasMoreElements ()) {
239+ NetworkInterface ni = interfaces .nextElement ();
240+ wrappedInterfaces .add (new StorkNetworkInterfaceWrapper (ni ));
241+ }
242+
243+ InetAddress addr = findFirstValidAddress (wrappedInterfaces );
244+ if (addr != null ) {
245+ return addr ;
148246 }
149- } catch (IOException ex ) {
150- LOGGER .error ("Unable to get first non-loopback address" , ex );
247+ } catch (IOException e ) {
248+ LOGGER .error ("Failed to detect IP address" , e );
151249 }
250+
152251 try {
153252 return InetAddress .getLocalHost ();
154253 } catch (UnknownHostException e ) {
155- LOGGER .error ("Unable to detect address" , e );
254+ LOGGER .error ("Fallback to localhost failed" , e );
255+ return null ;
256+ }
257+ }
258+
259+ static InetAddress findFirstValidAddress (List <NetworkInterfaceWrapper > interfaces ) {
260+ try {
261+ for (NetworkInterfaceWrapper iface : interfaces ) {
262+ if (!iface .isUp () || iface .isLoopback ())
263+ continue ;
264+
265+ Enumeration <InetAddress > addresses = iface .getInetAddresses ();
266+ while (addresses .hasMoreElements ()) {
267+ InetAddress addr = addresses .nextElement ();
268+ if (!addr .isLoopbackAddress () && addr instanceof Inet4Address ) {
269+ return addr ;
270+ }
271+ }
272+ }
273+ } catch (SocketException e ) {
274+ LOGGER .error ("Error checking network interfaces" , e );
156275 }
157276
158277 return null ;
0 commit comments