11package com .mageddo .dnsserver ;
22
3+ import java .net .InetSocketAddress ;
34import java .net .SocketAddress ;
45import java .util .ArrayList ;
5- import java .util .Collections ;
66import java .util .List ;
77import java .util .stream .Collectors ;
88
99import javax .inject .Inject ;
1010import javax .inject .Singleton ;
1111
12+ import com .mageddo .commons .Collections ;
13+ import com .mageddo .dnsproxyserver .utils .InetAddresses ;
1214import com .mageddo .dnsproxyserver .utils .Ips ;
15+ import com .mageddo .net .IP ;
1316
1417import lombok .RequiredArgsConstructor ;
1518import lombok .extern .slf4j .Slf4j ;
@@ -23,15 +26,31 @@ public class UDPServerPool {
2326 private List <UDPServer > servers = new ArrayList <>();
2427
2528 public void start (int port ) {
26- this .servers = Collections .singletonList (
27- new UDPServer (Ips .getAnyLocalAddress (port ), this .requestHandler ));
29+ final var addresses = this .buildAddressesToBind (port );
30+ this .servers = Collections .map (
31+ addresses ,
32+ address -> new UDPServer (address , this .requestHandler )
33+ );
2834 this .servers .forEach (UDPServer ::start );
29- final var addresses = this .servers
30- .stream ()
31- .map (UDPServer ::getAddress )
35+ log .info ("Starting UDP server, addresses={}" , this .toString (addresses ));
36+ }
37+
38+ private List <InetSocketAddress > buildAddressesToBind (int port ) {
39+ final var bindIp = Ips .from (Ips .getAnyLocalIpv6Address ());
40+ return this .buildAddressesToBind (bindIp , port );
41+ }
42+
43+ private List <InetSocketAddress > buildAddressesToBind (IP ip , int port ) {
44+ return Collections .map (
45+ Addresses .findBindAddresses (ip ),
46+ it -> InetAddresses .toSocketAddress (it , port )
47+ );
48+ }
49+
50+ private String toString (List <InetSocketAddress > addresses ) {
51+ return addresses .stream ()
3252 .map (SocketAddress ::toString )
3353 .collect (Collectors .joining (", " ));
34- log .info ("Starting UDP server, addresses={}" , addresses );
3554 }
3655
3756 public void stop () {
0 commit comments