public final class SourceRconClient extends NettySocketClient<SourceRconRequest,SourceRconResponse>
An RCON client based on Valve's Source RCON Protocol.
try (SourceRconClient rconClient = new SourceRconClient()) { //status command final String command = "status"; //authenticate + execute CompletableFuture<SourceRconCmdResponse> responseFuture = rconClient.authenticate(serverAddress, password.getBytes()) .thenCompose(authResponse -> { if (!authResponse.isAuthenticated()) throw new CompletionException(String.format("Failed to authenticate address '%s' (Reason: %s)", authResponse.getAddress(), authResponse.getReason()), authResponse.getAddress()); return rconClient.execute(authResponse.getAddress(), command); }); //Check future if completed if (responseFuture.isDone()) { SourceRconCmdResponse response = responseFuture.getNow(null); System.out.println("RESPONSE: " + response.getResult()); } //Register callback if not yet complete else { responseFuture.whenComplete((response, error) -> { if (error != null) { error.printStackTrace(System.err); return; } assert response != null; System.out.println(response.getResult()); }); } }
try (SourceRconClient rconClient = new SourceRconClient()) { //status command final String command = "status"; //authenticate + execute SourceRconCmdResponse response = rconClient.authenticate(serverAddress, password.getBytes()) .thenCompose(authResponse -> { if (!authResponse.isAuthenticated()) throw new CompletionException(String.format("Failed to authenticate address '%s' (Reason: %s)", authResponse.getAddress(), authResponse.getReason()), authResponse.getAddress()); return rconClient.execute(authResponse.getAddress(), command); }).join(); System.out.println("RESPONSE: " + response.getResult()); }
SourceRconOptions
Constructor and Description |
---|
SourceRconClient()
Create a new instance using the pre-defined configuration
Options for this client |
SourceRconClient(SourceRconOptions options)
Create a new instance using the provided configuration
SourceRconOptions |
Modifier and Type | Method and Description |
---|---|
CompletableFuture<SourceRconAuthResponse> |
authenticate(InetSocketAddress address)
Re-authenticate a previously registered address.
|
CompletableFuture<SourceRconAuthResponse> |
authenticate(InetSocketAddress address,
byte[] passphrase)
Sends an authentication request to the specified address.
|
void |
cleanup()
Request to release/close inactive connections.
|
void |
cleanup(boolean force)
Request to release/close connections
|
protected NettyMessenger<SourceRconRequest,SourceRconResponse> |
createMessenger(Options options)
Factory method for
Messenger . |
CompletableFuture<SourceRconCmdResponse> |
execute(InetSocketAddress address,
String command)
Sends a command request to the server
|
protected SourceRconMessenger |
getMessenger()
Getter for the field
messenger . |
com.google.common.collect.SetMultimap<InetSocketAddress,SourceRconMessenger.ConnectionStats> |
getStatistics()
Rcon connection statistics
|
void |
invalidate()
Invalidates only the connections of all registered address (registered via
authenticate(InetSocketAddress, byte[]) ). |
void |
invalidate(boolean onlyConnections)
Invalidates only the connections of all registered address (registered via
authenticate(InetSocketAddress, byte[]) ). |
void |
invalidate(InetSocketAddress address)
Invalidates the specified address along with the previously authenticated connections.
|
boolean |
isAuthenticated(InetSocketAddress address)
Checks if the specified address is authenticated
|
void |
printConnectionStats(Consumer<String> output) |
void |
printExecutorStats(Consumer<String> output) |
protected <V extends SourceRconResponse> |
send(InetSocketAddress address,
SourceRconRequest request,
Class<V> expectedResponse)
Send request.
|
close, equals, getExecutor, hashCode
getOptions, id, send
public SourceRconClient()
Options
for this clientpublic SourceRconClient(SourceRconOptions options)
SourceRconOptions
options
- The Options
instance containing the user-defined configuration options.Options
,
SourceRconOptions
,
OptionBuilder
public CompletableFuture<SourceRconAuthResponse> authenticate(InetSocketAddress address, byte[] passphrase)
Sends an authentication request to the specified address. If successful, the credentials is then registered and the underlying connection is now managed internally. You will only need to call this once unless it has been invalidated. Use isAuthenticated(InetSocketAddress)
to check if the credentials of the address is still valid.
WARNING: By default, the credentials stored in-memory are not encrypted, however you can implement and provide your own custom CredentialsStore
which can be set via configuration.
address
- The address of the source serverpassphrase
- The rcon passphrase in byte array formCompletableFuture
when completed, returns a SourceRconAuthResponse
that holds the status of the
authentication request.IllegalArgumentException
- If the address or password supplied is empty or nullSourceRconOptions.CREDENTIALS_STORE
,
CredentialsStore
,
Credentials
,
isAuthenticated(InetSocketAddress)
protected <V extends SourceRconResponse> CompletableFuture<V> send(InetSocketAddress address, SourceRconRequest request, Class<V> expectedResponse)
send
in class AbstractClient<SourceRconRequest,SourceRconResponse>
V
- a V classaddress
- a InetSocketAddress
objectrequest
- a R objectexpectedResponse
- a Class
objectCompletableFuture
objectpublic CompletableFuture<SourceRconAuthResponse> authenticate(InetSocketAddress address)
Re-authenticate a previously registered address. The address should be authenticated (via authenticate(InetSocketAddress, byte[])
) and the credentials should still be valid, or the returned future will fail. Use isAuthenticated(InetSocketAddress)
to check if the address is authenticated and registered.
address
- The address of the source serverCompletableFuture
when completed, returns a SourceRconAuthResponse
which holds the status of the authentication request.RconNotYetAuthException
- If the address has not yet been authenticated by the serverauthenticate(InetSocketAddress, byte[])
public boolean isAuthenticated(InetSocketAddress address)
address
- An InetSocketAddress
representing the servertrue
if the address has been successfully been authenticated by the remote serverauthenticate(InetSocketAddress, byte[])
public CompletableFuture<SourceRconCmdResponse> execute(InetSocketAddress address, String command) throws RconAuthException
Sends a command request to the server
address
- The InetSocketAddress
of the source servercommand
- The String
containing the command to be issued on the serverCompletableFuture
which contains a response String
returned by the serverRconAuthException
- If the address is not yet authenticated by the server.authenticate(InetSocketAddress, byte[])
public void invalidate()
Invalidates only the connections of all registered address (registered via authenticate(InetSocketAddress, byte[])
). The credentials registered with the address will remain valid. You do not need to call authenticate(InetSocketAddress, byte[])
unless the Credentials
have been invalidated.
authenticate(InetSocketAddress, byte[])
public void invalidate(boolean onlyConnections)
Invalidates only the connections of all registered address (registered via authenticate(InetSocketAddress, byte[])
). The credentials registered with the address will remain valid. You do not need to call authenticate(InetSocketAddress, byte[])
unless the Credentials
have been invalidated.
onlyConnections
- true
if we should only invalidate the Channel
's for the specified address.authenticate(InetSocketAddress, byte[])
public void invalidate(InetSocketAddress address)
authenticate(InetSocketAddress, byte[])
again.address
- The InetSocketAddress
to invalidate.authenticate(InetSocketAddress, byte[])
public void cleanup()
Request to release/close inactive connections. This is similar to calling cleanup(false)
cleanup(boolean)
public void cleanup(boolean force)
Request to release/close connections
force
- true
to force release/close all connections, otherwise false
to only release/close inactive connections.cleanup()
@ApiStatus.Experimental public com.google.common.collect.SetMultimap<InetSocketAddress,SourceRconMessenger.ConnectionStats> getStatistics()
Rcon connection statistics
Multimap
containing statistical information about the active connections for each registered address@ApiStatus.Experimental public void printConnectionStats(Consumer<String> output)
protected NettyMessenger<SourceRconRequest,SourceRconResponse> createMessenger(Options options)
Messenger
.createMessenger
in class NettySocketClient<SourceRconRequest,SourceRconResponse>
options
- a Options
objectMessenger
objectprotected SourceRconMessenger getMessenger()
messenger
.getMessenger
in class NettySocketClient<SourceRconRequest,SourceRconResponse>
Messenger
objectCopyright © 2016–2024. All rights reserved.