-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ipv6 bootnodes #138
Merged
Merged
Ipv6 bootnodes #138
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package co.rsk.util; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Created by mario on 13/07/17. | ||
*/ | ||
public class IpUtils { | ||
private static final Logger logger = LoggerFactory.getLogger(IpUtils.class); | ||
|
||
private static final String IPV6_INPUT_FORMAT = "^\\[(.*)\\]:([0-9]{1,})"; | ||
private static final String IPV4_INPUT_FORMAT = "^(.*):([0-9]{1,})"; | ||
private static final Pattern ipv6Pattern = Pattern.compile(IPV6_INPUT_FORMAT); | ||
private static final Pattern ipv4Pattern = Pattern.compile(IPV4_INPUT_FORMAT); | ||
|
||
|
||
public static InetSocketAddress parseAddress(String address) { | ||
Matcher matcher = ipv6Pattern.matcher(address); | ||
if(matcher.matches()) | ||
return parseMatch(matcher); | ||
|
||
matcher = ipv4Pattern.matcher(address); | ||
if(StringUtils.countMatches(address, ":") == 1 && matcher.matches()) | ||
return parseMatch(matcher); | ||
|
||
logger.debug("Invalid address: {}. For ipv6 use de convention [address]:port. For ipv4 address:port", address); | ||
return null; | ||
} | ||
|
||
public static List<InetSocketAddress> parseAddresses(List<String> addresses) { | ||
List<InetSocketAddress> result = new ArrayList<>(); | ||
if(CollectionUtils.isNotEmpty(addresses)) { | ||
for(String a : addresses) { | ||
InetSocketAddress res = parseAddress(a); | ||
if (res != null) | ||
result.add(res); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private static InetSocketAddress parseMatch(Matcher matcher) { | ||
return new InetSocketAddress(matcher.group(1), Integer.valueOf(matcher.group(2))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package co.rsk.util; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by mario on 13/07/17. | ||
*/ | ||
public class IpUtilsTest { | ||
|
||
private static final String IPV6_WITH_PORT = "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"; | ||
private static final String IPV6_NO_PORT = "[2001:db8:85a3:8d3:1319:8a2e:370:7348]"; | ||
private static final String IPV6_INVALID = "2001:db8:85a3:8d3:1319:8a2e:370:7348"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This IPv6 address is valid if square brackets are not mandatory. |
||
private static final String IPV4_WITH_PORT = "172.217.28.228:80"; | ||
private static final String IPV4_NO_PORT = "172.217.28.228"; | ||
|
||
@Test | ||
public void parseIPv6() { | ||
InetSocketAddress result = IpUtils.parseAddress(IPV6_WITH_PORT); | ||
Assert.assertNotNull(result); | ||
} | ||
|
||
@Test | ||
public void parseIPv6NoPort() { | ||
InetSocketAddress result = IpUtils.parseAddress(IPV6_NO_PORT); | ||
Assert.assertNull(result); | ||
} | ||
|
||
@Test | ||
public void parseIPv6InvalidFormat() { | ||
InetSocketAddress result = IpUtils.parseAddress(IPV6_INVALID); | ||
Assert.assertNull(result); | ||
} | ||
|
||
@Test | ||
public void parseIPv4() { | ||
InetSocketAddress result = IpUtils.parseAddress(IPV4_WITH_PORT); | ||
Assert.assertNotNull(result); | ||
} | ||
|
||
@Test | ||
public void parseIPv4NoPort() { | ||
InetSocketAddress result = IpUtils.parseAddress(IPV4_NO_PORT); | ||
Assert.assertNull(result); | ||
} | ||
|
||
@Test | ||
public void parseAddresses() { | ||
List<String> addresses = new ArrayList<>(); | ||
addresses.add(IPV6_WITH_PORT); | ||
addresses.add(IPV6_NO_PORT); | ||
addresses.add(IPV6_INVALID); | ||
addresses.add(IPV4_WITH_PORT); | ||
addresses.add(IPV4_NO_PORT); | ||
List<InetSocketAddress> result = IpUtils.parseAddresses(addresses); | ||
Assert.assertTrue(CollectionUtils.isNotEmpty(result)); | ||
Assert.assertEquals(2, CollectionUtils.size(result)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.