Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.google.protobuf:protobuf-java:3.19.3'
implementation 'com.squareup.okhttp3:okhttp:3.14.9'
implementation 'commons-validator:commons-validator:1.7'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'org.java-websocket:Java-WebSocket:1.5.2'
testCompile 'com.squareup.okhttp3:mockwebserver:3.14.9'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package in.dragonbra.javasteam.steam.discovery;

import in.dragonbra.javasteam.networking.steam3.ProtocolTypes;
import in.dragonbra.javasteam.util.NetHelpers;

import java.net.InetSocketAddress;
import java.util.EnumSet;
Expand Down Expand Up @@ -55,6 +56,24 @@ public static ServerRecord createSocketServer(InetSocketAddress endpoint) {
return new ServerRecord(endpoint, EnumSet.of(ProtocolTypes.TCP, ProtocolTypes.UDP));
}

/**
* Creates a Socket server given an IP endpoint.
*
* @param address The IP address and port of the server, as a string.
* @return A new [ServerRecord], if the address was able to be parsed. **null** otherwise.
*/
public static ServerRecord tryCreateSocketServer(String address) {
InetSocketAddress endpoint;

endpoint = NetHelpers.tryParseIPEndPoint(address);

if (endpoint == null) {
return null;
}

return new ServerRecord(endpoint, EnumSet.of(ProtocolTypes.TCP, ProtocolTypes.UDP));
}

public static ServerRecord createWebSocketServer(String address) {
if (address == null) {
throw new IllegalArgumentException("address is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import in.dragonbra.javasteam.steam.discovery.ServerRecord;
import in.dragonbra.javasteam.steam.steamclient.configuration.SteamConfiguration;
import in.dragonbra.javasteam.types.KeyValue;
import in.dragonbra.javasteam.util.NetHelpers;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -65,8 +65,10 @@ public static List<ServerRecord> load(SteamConfiguration configuration, int maxS
List<ServerRecord> records = new ArrayList<>();

for (KeyValue socket : socketList.getChildren()) {
String[] split = socket.getValue().split(":");
records.add(ServerRecord.createSocketServer(new InetSocketAddress(split[0], Integer.parseInt(split[1]))));
ServerRecord record = ServerRecord.tryCreateSocketServer(socket.getValue());
if (record != null) {
records.add(record);
}
}

for (KeyValue socket : webSocketList.getChildren()) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/in/dragonbra/javasteam/util/NetHelpers.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package in.dragonbra.javasteam.util;

import org.apache.commons.validator.routines.InetAddressValidator;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

Expand All @@ -27,4 +30,27 @@ public static int getIPAddress(InetAddress ip) {
final ByteBuffer buff = ByteBuffer.wrap(ip.getAddress());
return (int) (buff.getInt() & 0xFFFFFFFFL);
}


public static InetSocketAddress tryParseIPEndPoint(String address) {
if (address == null) {
return null;
}

String[] split = address.split(":");

if (!InetAddressValidator.getInstance().isValidInet4Address(split[0])) {
return null;
}

try {
if (split.length > 1) {
return new InetSocketAddress(split[0], Integer.parseInt(split[1]));
}
} catch (IllegalArgumentException exception) {
// no-op
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import in.dragonbra.javasteam.networking.steam3.ProtocolTypes;
import org.junit.Test;

import java.net.InetSocketAddress;
import java.util.EnumSet;

import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -55,4 +58,39 @@ public void SameEndPointsAndProtocolsAreEqual() {

assertEquals(l.hashCode(), r.hashCode());
}

@Test
public void canTryCreateSocketServer() {
ServerRecord record = ServerRecord.tryCreateSocketServer("127.0.0.1:1234");

assertNotNull(record);
assertEquals(new InetSocketAddress("127.0.0.1", 1234), record.getEndpoint());
assertEquals(EnumSet.of(ProtocolTypes.TCP, ProtocolTypes.UDP), record.getProtocolTypes());

record = ServerRecord.tryCreateSocketServer("192.168.0.1:5678");

assertNotNull(record);
assertEquals(new InetSocketAddress("192.168.0.1", 5678), record.getEndpoint());
assertEquals(EnumSet.of(ProtocolTypes.TCP, ProtocolTypes.UDP), record.getProtocolTypes());
}

@Test
public void cannotTryCreateSocketServer() {
ServerRecord record;

record = ServerRecord.tryCreateSocketServer("127.0.0.1");
assertNull(record);

record = ServerRecord.tryCreateSocketServer("127.0.0.1:123456789");
assertNull(record);

record = ServerRecord.tryCreateSocketServer("127.0.0.1:-1234");
assertNull(record);

record = ServerRecord.tryCreateSocketServer("127.0.0.1:notanint");
assertNull(record);

record = ServerRecord.tryCreateSocketServer("volvopls.valvesoftware.com:1234");
assertNull(record);
}
}