Compare commits
16
Commits
b335c2c511
..
master
+1
-1
@@ -31,7 +31,7 @@ out/
|
||||
.userlibraries
|
||||
|
||||
# Maven
|
||||
targetId/
|
||||
target/
|
||||
pom.xml.tag
|
||||
pom.xml.releaseBackup
|
||||
pom.xml.versionsBackup
|
||||
|
||||
@@ -36,8 +36,8 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk15on</artifactId>
|
||||
<version>1.67</version>
|
||||
<artifactId>bcprov-jdk18on</artifactId>
|
||||
<version>1.86</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@@ -12,4 +12,27 @@ public interface Microphone {
|
||||
CodecType getCodec();
|
||||
|
||||
byte[] provide();
|
||||
|
||||
/**
|
||||
* Milliseconds between a voice packet and each copy of it sent afterwards, or 0 to send it
|
||||
* once. A copy is the same packet, so receivers keep whichever arrives first; a jitter buffer
|
||||
* that sees copies arrive late grows to wait for them, trading latency for riding out loss.
|
||||
*/
|
||||
default int getRedundancyDelay() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@link #provide} hands out frames captured on a clock of their own, such as a sound
|
||||
* card's. That clock drifts against the send timer, so each tick then sends every frame that is
|
||||
* ready instead of one, keeping the drift from piling up as latency or overflowing a queue.
|
||||
*/
|
||||
default boolean isLiveCapture() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** How many copies follow each voice packet, {@link #getRedundancyDelay} apart. */
|
||||
default int getRedundancyCopies() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.github.manevolent.ts3j.protocol;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* SRV lookups through JNDI's DNS provider, which desktop JVMs ship. Kept apart from
|
||||
* {@link TS3DNS} so runtimes without JNDI (Android) never load it.
|
||||
*/
|
||||
final class JndiSrvLookup implements TS3DNS.SrvLookup {
|
||||
|
||||
@Override
|
||||
public List<String> lookup(String name) throws IOException {
|
||||
Attribute attribute;
|
||||
try {
|
||||
Attributes attributes = new InitialDirContext().getAttributes("dns:/" + name, new String[] { "SRV" });
|
||||
attribute = attributes.get("SRV");
|
||||
} catch (NamingException e) {
|
||||
throw new UnknownHostException("Host not found: " + name);
|
||||
}
|
||||
|
||||
List<String> records = new ArrayList<>();
|
||||
if (attribute == null) return records;
|
||||
try {
|
||||
for (int i = 0; i < attribute.size(); i++)
|
||||
records.add((String) attribute.get(i));
|
||||
} catch (NamingException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
return records;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
package com.github.manevolent.ts3j.protocol;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
@@ -18,8 +14,28 @@ public class TS3DNS {
|
||||
private static final String DNS_PREFIX_TCP = "_tsdns._tcp.";
|
||||
private static final String DNS_PREFIX_UDP = "_ts3._udp.";
|
||||
|
||||
/** Looks up DNS SRV records; see {@link #setSrvLookup}. */
|
||||
public interface SrvLookup {
|
||||
/**
|
||||
* SRV record, as returned by JNDI: "priority weight port target."
|
||||
* @return each record as "priority weight port target", empty if there are none
|
||||
* @throws UnknownHostException if the name does not exist
|
||||
*/
|
||||
List<String> lookup(String name) throws IOException;
|
||||
}
|
||||
|
||||
/** Null until first use, when JNDI's DNS provider becomes the default. */
|
||||
private static volatile SrvLookup srvLookup;
|
||||
|
||||
/**
|
||||
* Replaces how SRV records are looked up. The default goes through JNDI, which not every
|
||||
* runtime has (Android doesn't); without it, servers are reached by address only.
|
||||
*/
|
||||
public static void setSrvLookup(SrvLookup lookup) {
|
||||
srvLookup = lookup;
|
||||
}
|
||||
|
||||
/**
|
||||
* SRV record, as {@link SrvLookup} returns it: "priority weight port target."
|
||||
*/
|
||||
private static final class SrvRecord {
|
||||
private final int priority, weight, port;
|
||||
@@ -40,25 +56,12 @@ public class TS3DNS {
|
||||
}
|
||||
|
||||
private static List<InetSocketAddress> lookupSRV(String domain) throws IOException {
|
||||
Attribute attribute;
|
||||
List<String> found = srvLookup().lookup(domain);
|
||||
if (found.isEmpty()) throw new UnknownHostException("Host not found");
|
||||
|
||||
try {
|
||||
Attributes attributes = new InitialDirContext().getAttributes("dns:/" + domain, new String[] { "SRV" });
|
||||
attribute = attributes.get("SRV");
|
||||
} catch (NamingException e) {
|
||||
throw new UnknownHostException("Host not found: " + domain);
|
||||
}
|
||||
|
||||
if (attribute == null || attribute.size() <= 0) throw new UnknownHostException("Host not found");
|
||||
|
||||
List<SrvRecord> records = new ArrayList<>(attribute.size());
|
||||
|
||||
try {
|
||||
for (int i = 0; i < attribute.size(); i++)
|
||||
records.add(new SrvRecord((String) attribute.get(i)));
|
||||
} catch (NamingException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
List<SrvRecord> records = new ArrayList<>(found.size());
|
||||
for (String record : found)
|
||||
records.add(new SrvRecord(record));
|
||||
|
||||
// Loose sorting
|
||||
records.sort(Comparator.comparingInt((SrvRecord x) -> x.priority).thenComparingInt(x -> x.weight));
|
||||
@@ -70,6 +73,22 @@ public class TS3DNS {
|
||||
return addresses;
|
||||
}
|
||||
|
||||
private static SrvLookup srvLookup() {
|
||||
SrvLookup lookup = srvLookup;
|
||||
if (lookup != null) return lookup;
|
||||
try {
|
||||
Class.forName("javax.naming.directory.InitialDirContext");
|
||||
lookup = new JndiSrvLookup();
|
||||
} catch (ClassNotFoundException e) {
|
||||
// No JNDI on this runtime: behave as if no server published an SRV record.
|
||||
lookup = name -> {
|
||||
throw new UnknownHostException("No SRV lookup available for " + name);
|
||||
};
|
||||
}
|
||||
srvLookup = lookup;
|
||||
return lookup;
|
||||
}
|
||||
|
||||
public static List<InetSocketAddress> lookup(String domain) throws IOException {
|
||||
return lookupSRV(DNS_PREFIX_UDP + domain);
|
||||
}
|
||||
|
||||
+2
-2
@@ -239,13 +239,13 @@ public class PacketBody1VoiceWhisper extends PacketBody {
|
||||
channelIds[n] = (int) (buffer.getLong());
|
||||
|
||||
clientIds = new int[clientIdCount];
|
||||
for (int n = 0; n < channelIdCount; n ++)
|
||||
for (int n = 0; n < clientIdCount; n ++)
|
||||
clientIds[n] = (buffer.getShort() & 0xFFFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return 2 + 2 +
|
||||
return 1 + 1 +
|
||||
(channelIds == null ? 0 : channelIds.length * 8) +
|
||||
(clientIds == null ? 0 : clientIds.length * 2);
|
||||
}
|
||||
|
||||
+95
-14
@@ -1,6 +1,7 @@
|
||||
package com.github.manevolent.ts3j.protocol.packet.handler.client;
|
||||
|
||||
import com.github.manevolent.ts3j.command.Command;
|
||||
import com.github.manevolent.ts3j.command.CommandException;
|
||||
import com.github.manevolent.ts3j.command.SingleCommand;
|
||||
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
|
||||
import com.github.manevolent.ts3j.protocol.Packet;
|
||||
@@ -22,6 +23,9 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Base64;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
@@ -30,12 +34,19 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
private byte[] randomBytes;
|
||||
private byte[] alphaBytes;
|
||||
|
||||
/** The last Init1 we sent; resent until the server answers it. */
|
||||
private PacketBody8Init1 pendingInit1;
|
||||
/** The highest Init1 step the server has answered with; anything up to it again is a duplicate. */
|
||||
private int serverStep;
|
||||
private ScheduledExecutorService init1Resender;
|
||||
|
||||
public LocalClientHandlerConnecting(LocalTeamspeakClientSocket client) {
|
||||
super(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAssigned() throws IOException, TimeoutException {
|
||||
serverStep = 0;
|
||||
PacketBody8Init1 packet = new PacketBody8Init1(ProtocolRole.CLIENT);
|
||||
|
||||
// Initialize connection
|
||||
@@ -53,10 +64,53 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
//getClient().writePacket(new PacketBody2Command(ProtocolRole.CLIENT, createInitIv()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnassigning() {
|
||||
stopResendingInit1();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an Init1 and keeps resending the same packet until the server answers: Init1 has no
|
||||
* acknowledgements, so without this one lost datagram stalls the handshake for good.
|
||||
*/
|
||||
private void sendInit1(PacketBody8Init1 packet) throws IOException, TimeoutException {
|
||||
packet.setVersion(INIT1_VERSION);
|
||||
|
||||
synchronized (this) {
|
||||
pendingInit1 = packet;
|
||||
if (init1Resender == null) {
|
||||
init1Resender = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
Thread thread = new Thread(r, "TS3J/Init1Resend");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
}
|
||||
}
|
||||
getClient().writePacket(packet);
|
||||
scheduleInit1Resend(packet, 0);
|
||||
}
|
||||
|
||||
private synchronized void scheduleInit1Resend(PacketBody8Init1 packet, int resends) {
|
||||
if (pendingInit1 != packet || init1Resender == null) return;
|
||||
init1Resender.schedule(() -> {
|
||||
synchronized (this) {
|
||||
if (pendingInit1 != packet) return;
|
||||
}
|
||||
try {
|
||||
getClient().writePacket(packet);
|
||||
} catch (Exception e) {
|
||||
Ts3Debugging.debug("Problem resending Init1", e);
|
||||
}
|
||||
scheduleInit1Resend(packet, resends + 1);
|
||||
}, getClient().getResendTimeout(resends), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private synchronized void stopResendingInit1() {
|
||||
pendingInit1 = null;
|
||||
if (init1Resender != null) {
|
||||
init1Resender.shutdownNow();
|
||||
init1Resender = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,6 +119,10 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
PacketBody8Init1 init1 = (PacketBody8Init1) packet.getBody();
|
||||
|
||||
Ts3Debugging.debug("Handle Init1 step " + init1.getStep().getNumber());
|
||||
// Answering a duplicate would redo the handshake step, and a second clientinitiv would
|
||||
// come with a new alpha, leaving us with a different key than the server.
|
||||
if (init1.getStep().getNumber() <= serverStep) return;
|
||||
serverStep = init1.getStep().getNumber();
|
||||
PacketBody8Init1.Step step;
|
||||
|
||||
switch (init1.getStep().getNumber()) {
|
||||
@@ -101,16 +159,7 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
BigInteger x = new BigInteger(1, serverReplyStep3.getX());
|
||||
BigInteger n = new BigInteger(1, serverReplyStep3.getN());
|
||||
|
||||
byte[] y = new byte[64];
|
||||
|
||||
byte[] solution =
|
||||
x.modPow(BigInteger.valueOf(2L).pow(serverReplyStep3.getLevel()), n).toByteArray();
|
||||
|
||||
System.arraycopy(
|
||||
solution, Math.abs(solution.length - 64),
|
||||
y, solution.length < 64 ? 64 - solution.length : 0,
|
||||
solution.length < 64 ? solution.length : 64
|
||||
);
|
||||
byte[] y = unsigned64(x.modPow(BigInteger.valueOf(2L).pow(serverReplyStep3.getLevel()), n));
|
||||
|
||||
// Build response
|
||||
PacketBody8Init1.Step4 step4 = new PacketBody8Init1.Step4();
|
||||
@@ -138,6 +187,7 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
|
||||
sendInit1(new PacketBody8Init1(getClient().getRole().getOut(), step));
|
||||
} else if (packet.getBody() instanceof PacketBody2Command) {
|
||||
stopResendingInit1();
|
||||
SingleCommand command = ((PacketBody2Command) packet.getBody()).parse().simplifyOne();
|
||||
|
||||
Ts3Debugging.debug(command.build());
|
||||
@@ -146,6 +196,7 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
byte[] alpha = Base64.getDecoder().decode(command.get("alpha").getValue()); // alpha
|
||||
byte[] beta = Base64.getDecoder().decode(command.get("beta").getValue()); // beta
|
||||
byte[] omega = Base64.getDecoder().decode(command.get("omega").getValue()); // omega
|
||||
getClient().setServerPublicKey(command.get("omega").getValue());
|
||||
|
||||
getClient().setSecureParameters(Ts3Crypt.cryptoInit(alpha, beta, omega, getClient().getIdentity()));
|
||||
|
||||
@@ -166,6 +217,7 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
|
||||
byte[] beta = Base64.getDecoder().decode(command.get("beta").getValue()); // beta
|
||||
byte[] omega = Base64.getDecoder().decode(command.get("omega").getValue()); // omega
|
||||
getClient().setServerPublicKey(command.get("omega").getValue());
|
||||
byte[] proof = Base64.getDecoder().decode(command.get("proof").getValue());
|
||||
|
||||
Pair<byte[], byte[]> keyPair = Ts3Crypt.generateKeypair();
|
||||
@@ -185,7 +237,9 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
getClient().getIdentity()
|
||||
);
|
||||
|
||||
getClient().writePacket(new PacketBody2Command(
|
||||
// Don't wait for the ACK: the server ignores a resent clientek, so a lost ACK would stall us, and
|
||||
// it can't decrypt clientinit before processing clientek anyway, so the two stay in order.
|
||||
getClient().writePacketAsync(new PacketBody2Command(
|
||||
ProtocolRole.CLIENT,
|
||||
new SingleCommand(
|
||||
"clientek", ProtocolRole.CLIENT,
|
||||
@@ -203,15 +257,28 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
getClient().setCommandProcessor(getClient());
|
||||
getClient().setState(ClientConnectionState.RETRIEVING_DATA);
|
||||
} else if (command.getName().equals("error")) {
|
||||
getClient().setState(ClientConnectionState.DISCONNECTED);
|
||||
CommandException refusal = refusal(command);
|
||||
getClient().refuse(refusal);
|
||||
|
||||
throw new IOException(command.get("msg").getValue());
|
||||
throw new IOException(refusal.getMessage(), refusal);
|
||||
} else {
|
||||
throw new IOException("Unknown Init command: " + command.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The server's answer to a handshake command it refused, as an exception carrying its error id. */
|
||||
static CommandException refusal(SingleCommand error) {
|
||||
String message = error.has("msg") ? error.get("msg").getValue() : "connection refused";
|
||||
int id;
|
||||
try {
|
||||
id = error.has("id") ? Integer.parseInt(error.get("id").getValue()) : -1;
|
||||
} catch (NumberFormatException e) {
|
||||
id = -1;
|
||||
}
|
||||
return new CommandException(message, id);
|
||||
}
|
||||
|
||||
private SingleCommand createInitIv() {
|
||||
alphaBytes = new byte[10];
|
||||
new Random().nextBytes(alphaBytes);
|
||||
@@ -279,8 +346,22 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
|
||||
)
|
||||
);
|
||||
|
||||
Ts3Debugging.debug(clientinit.build());
|
||||
getClient().addMyTeamSpeakParameters(clientinit);
|
||||
Ts3Debugging.debug("Sending clientinit");
|
||||
|
||||
getClient().writePacket(new PacketBody2Command(ProtocolRole.CLIENT, clientinit));
|
||||
}
|
||||
|
||||
/**
|
||||
* The puzzle's answer as 64 unsigned big-endian bytes. {@link BigInteger#toByteArray()}
|
||||
* adds a sign byte when the top bit is set and drops leading zero bytes, so it can be
|
||||
* 65 bytes or fewer than 64.
|
||||
*/
|
||||
static byte[] unsigned64(BigInteger value) {
|
||||
byte[] bytes = value.toByteArray();
|
||||
byte[] y = new byte[64];
|
||||
int length = Math.min(bytes.length, 64);
|
||||
System.arraycopy(bytes, bytes.length - length, y, 64 - length, length);
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-4
@@ -1,9 +1,9 @@
|
||||
package com.github.manevolent.ts3j.protocol.packet.handler.client;
|
||||
|
||||
import com.github.manevolent.ts3j.command.CommandException;
|
||||
import com.github.manevolent.ts3j.command.CommandProcessException;
|
||||
import com.github.manevolent.ts3j.command.CommandProcessor;
|
||||
import com.github.manevolent.ts3j.command.MultiCommand;
|
||||
import com.github.manevolent.ts3j.protocol.client.ClientConnectionState;
|
||||
import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket;
|
||||
|
||||
|
||||
@@ -15,13 +15,14 @@ public class LocalClientHandlerRetrievingData extends LocalClientHandlerFull {
|
||||
@Override
|
||||
protected void handleCommand(MultiCommand command) throws CommandProcessException {
|
||||
if (command.getName().equalsIgnoreCase("error")) {
|
||||
// clientinit was refused, e.g. for a nickname in use or a wrong server password.
|
||||
CommandException refusal = LocalClientHandlerConnecting.refusal(command.simplifyOne());
|
||||
try {
|
||||
getClient().setState(ClientConnectionState.DISCONNECTED);
|
||||
getClient().refuse(refusal);
|
||||
} catch (Exception e) {
|
||||
getClient().getExceptionHandler().accept(e);
|
||||
}
|
||||
|
||||
throw new CommandProcessException(command.simplifyOne().get("msg").getValue());
|
||||
throw new CommandProcessException(refusal.getMessage());
|
||||
}
|
||||
|
||||
CommandProcessor processor = getClient().getCommandProcessor();
|
||||
|
||||
+11
-1
@@ -61,7 +61,7 @@ public class PacketStatistics {
|
||||
}
|
||||
|
||||
public int getSentPacketsLastMinute() {
|
||||
long now = System.nanoTime() - 1_000_000_000L;
|
||||
long now = System.nanoTime() - 60_000_000_000L;
|
||||
|
||||
synchronized (sentPacketList) {
|
||||
return (int) sentPacketList.stream()
|
||||
@@ -124,6 +124,16 @@ public class PacketStatistics {
|
||||
}
|
||||
}
|
||||
|
||||
/** Average bytes per second sent over the last minute, as TeamSpeak reports a minute's bandwidth. */
|
||||
public int getSentBandwidthLastMinute() {
|
||||
return getSentBytesLastMinute() / 60;
|
||||
}
|
||||
|
||||
/** Average bytes per second received over the last minute. */
|
||||
public int getReceivedBandwidthLastMinute() {
|
||||
return getReceivedBytesLastMinute() / 60;
|
||||
}
|
||||
|
||||
public int getReceivedBytesLastMinute() {
|
||||
long now = System.nanoTime() - 60_000_000_000L;
|
||||
|
||||
|
||||
+12
-8
@@ -24,7 +24,7 @@ public class PacketTransformation {
|
||||
|
||||
public PacketTransformation(byte[] ivStruct, byte[] fakeMac) {
|
||||
this.ivStruct = ivStruct;
|
||||
this.cipher = new EAXBlockCipher(new AESEngine());
|
||||
this.cipher = new EAXBlockCipher(AESEngine.newInstance());
|
||||
this.fakeMac = fakeMac;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,12 @@ public class PacketTransformation {
|
||||
return fakeMac;
|
||||
}
|
||||
|
||||
/** Connection binding used by myTeamSpeak, in the AES-128/SHA-256 protocol mode. */
|
||||
public byte[] getMyTeamSpeakChallenge() {
|
||||
return Ts3Crypt.hash256(ByteBuffer.allocate(6 + ivStruct.length)
|
||||
.put(new byte[] {0x30, 8, 0, 0, 0, 0}).put(ivStruct).array());
|
||||
}
|
||||
|
||||
public Pair<byte[], byte[]> computeParameters(PacketHeader header) {
|
||||
ByteBuffer temporaryByteBuffer = ByteBuffer
|
||||
.allocate(ivStruct.length == 20 ? 26 : 70)
|
||||
@@ -86,21 +92,19 @@ public class PacketTransformation {
|
||||
int dataLen = packet.getBody().getSize();
|
||||
ByteBuffer packetBuffer = packet.writeBody(ByteBuffer.allocate(dataLen).order(ByteOrder.BIG_ENDIAN));
|
||||
|
||||
byte[] result;
|
||||
int len;
|
||||
|
||||
synchronized (cipher) {
|
||||
// TS3 derives key and nonce from the packet id, so a resend encrypts under the same pair
|
||||
// again. BouncyCastle refuses that on a cipher that has seen it, hence one per packet.
|
||||
EAXBlockCipher cipher = new EAXBlockCipher(AESEngine.newInstance());
|
||||
cipher.init(true, cipherParameters);
|
||||
|
||||
result = new byte[cipher.getOutputSize(dataLen)];
|
||||
len = cipher.processBytes(packetBuffer.array(), 0, dataLen, result, 0);
|
||||
byte[] result = new byte[cipher.getOutputSize(dataLen)];
|
||||
int len = cipher.processBytes(packetBuffer.array(), 0, dataLen, result, 0);
|
||||
|
||||
try {
|
||||
len += cipher.doFinal(result, len);
|
||||
} catch (InvalidCipherTextException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
ByteBuffer outputBuffer = ByteBuffer.allocate(MAC_LEN + headerWithoutMac.length + (len - MAC_LEN));
|
||||
|
||||
|
||||
+189
-93
@@ -1,6 +1,9 @@
|
||||
package com.github.manevolent.ts3j.protocol.socket.client;
|
||||
|
||||
import com.github.manevolent.ts3j.command.CommandException;
|
||||
|
||||
import com.github.manevolent.ts3j.command.*;
|
||||
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
|
||||
import com.github.manevolent.ts3j.identity.Identity;
|
||||
import com.github.manevolent.ts3j.protocol.NetworkPacket;
|
||||
import com.github.manevolent.ts3j.protocol.Packet;
|
||||
@@ -34,6 +37,7 @@ import java.nio.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Server/client common abstract client socket, used to interact with the UDP connection. Provides most of the protocol
|
||||
@@ -52,6 +56,8 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
private final Map<String, Object> clientOptions = new ConcurrentHashMap<>();
|
||||
|
||||
private final Object connectionStateLock = new Object();
|
||||
/** Why the server turned the connection down while it was being set up; null otherwise. */
|
||||
private volatile CommandException refusal;
|
||||
|
||||
private final LinkedBlockingDeque<Packet> readQueue = new LinkedBlockingDeque<>(65536);
|
||||
|
||||
@@ -75,7 +81,8 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
|
||||
private Identity identity = null;
|
||||
private ClientConnectionState connectionState = null;
|
||||
private PacketTransformation transformation = new InitPacketTransformation();
|
||||
private final PacketTransformation initTransformation = new InitPacketTransformation();
|
||||
private PacketTransformation transformation = initTransformation;
|
||||
private PacketHandler handler;
|
||||
|
||||
private int clientId = 0;
|
||||
@@ -83,13 +90,7 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
private long lastResponse;
|
||||
private long lastPing;
|
||||
|
||||
// RTT
|
||||
private static final double alphaSmooth = 0.125D;
|
||||
private static final double betaSmooth = 0.25D;
|
||||
private double smoothedRtt;
|
||||
private double smoothedRttVar;
|
||||
private double currentRto;
|
||||
//
|
||||
private final ResendTiming resendTiming = new ResendTiming();
|
||||
|
||||
// Ping
|
||||
private List<Double> pings = new LinkedList<>();
|
||||
@@ -166,6 +167,21 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
setTransformation(new PacketTransformation(parameters.getIvStruct(), parameters.getFakeSignature()));
|
||||
}
|
||||
|
||||
private volatile Function<byte[], Map<String, String>> myTeamSpeakAuthenticator;
|
||||
|
||||
/** Set before connecting; invoked after the handshake with this connection's challenge. */
|
||||
public void setMyTeamSpeakAuthenticator(Function<byte[], Map<String, String>> authenticator) {
|
||||
myTeamSpeakAuthenticator = authenticator;
|
||||
}
|
||||
|
||||
public void addMyTeamSpeakParameters(Command command) {
|
||||
Function<byte[], Map<String, String>> authenticator = myTeamSpeakAuthenticator;
|
||||
if (authenticator == null) return;
|
||||
Map<String, String> parameters = authenticator.apply(transformation.getMyTeamSpeakChallenge());
|
||||
for (Map.Entry<String, String> entry : parameters.entrySet())
|
||||
command.add(new CommandSingleParameter(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
protected abstract NetworkPacket readNetworkPacket(int timeout) throws IOException;
|
||||
|
||||
protected abstract void writeNetworkPacket(NetworkPacket packet) throws IOException;
|
||||
@@ -177,8 +193,10 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
|
||||
boolean initialState = this.connectionState == null;
|
||||
|
||||
if (state == ClientConnectionState.CONNECTING)
|
||||
if (state == ClientConnectionState.CONNECTING) {
|
||||
refusal = null;
|
||||
setReading(true);
|
||||
}
|
||||
|
||||
this.connectionState = state;
|
||||
|
||||
@@ -250,20 +268,40 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
return connectionState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the connection to reach a state.
|
||||
*
|
||||
* @throws IOException if the connection is closed before it gets there
|
||||
*/
|
||||
public void waitForState(ClientConnectionState state, long wait)
|
||||
throws InterruptedException, TimeoutException {
|
||||
throws InterruptedException, TimeoutException, IOException {
|
||||
synchronized (connectionStateLock) {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
while (connectionState != state && System.currentTimeMillis() - start < wait) {
|
||||
while (connectionState != state && connectionState != ClientConnectionState.DISCONNECTED
|
||||
&& System.currentTimeMillis() - start < wait) {
|
||||
connectionStateLock.wait(Math.max(0L, wait - (System.currentTimeMillis() - start)));
|
||||
}
|
||||
|
||||
if (connectionState != state && connectionState == ClientConnectionState.DISCONNECTED) {
|
||||
CommandException refused = refusal;
|
||||
if (refused != null) throw new IOException(refused.getMessage(), refused);
|
||||
throw new IOException("disconnected while waiting for " + state.name() + " state");
|
||||
}
|
||||
if (connectionState != state)
|
||||
throw new TimeoutException("timeout waiting for " + state.name() + " state");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the error the server answered a handshake command with, and leaves the connection:
|
||||
* {@link #waitForState} then fails with it rather than with a bare disconnect.
|
||||
*/
|
||||
public void refuse(CommandException error) throws IOException, TimeoutException {
|
||||
refusal = error;
|
||||
setState(ClientConnectionState.DISCONNECTED);
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return connectionState == ClientConnectionState.CONNECTED;
|
||||
}
|
||||
@@ -282,6 +320,41 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
|
||||
@Override
|
||||
public void writePacket(PacketBody body) throws IOException, TimeoutException {
|
||||
writePacket(newOutgoingPacket(body));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a packet body without waiting for its acknowledgement. It is numbered and encrypted before this
|
||||
* returns, so packets written afterwards follow it; resends until it is acknowledged continue in the background.
|
||||
*
|
||||
* @return completes once the packet is acknowledged, or exceptionally if it never is
|
||||
*/
|
||||
public CompletableFuture<Void> writePacketAsync(PacketBody body) throws IOException, TimeoutException {
|
||||
Packet packet = newOutgoingPacket(body);
|
||||
List<PacketResponse> responses = new ArrayList<>();
|
||||
|
||||
synchronized (sendLocks.get(packet.getHeader().getType())) {
|
||||
for (Packet piece : split(packet))
|
||||
responses.add(sendPacketIntl(piece));
|
||||
}
|
||||
|
||||
CompletableFuture<Void> acknowledged = new CompletableFuture<>();
|
||||
Thread thread = new Thread(() -> {
|
||||
try {
|
||||
for (PacketResponse response : responses)
|
||||
awaitAcknowledgement(response);
|
||||
acknowledged.complete(null);
|
||||
} catch (Throwable e) {
|
||||
acknowledged.completeExceptionally(e);
|
||||
}
|
||||
}, "TS3J/Resend");
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
|
||||
return acknowledged;
|
||||
}
|
||||
|
||||
private Packet newOutgoingPacket(PacketBody body) {
|
||||
// Construct a packet object
|
||||
Packet packet = new Packet(body.getRole());
|
||||
|
||||
@@ -317,12 +390,32 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
// Assign header pointer
|
||||
packet.setHeader(header);
|
||||
|
||||
// Lower-level write; helper is done
|
||||
writePacket(packet);
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writePacket(Packet packet) throws IOException, TimeoutException {
|
||||
List<Packet> pieces = split(packet);
|
||||
|
||||
if (packet.getHeader().getType().isSplittable()) {
|
||||
// Note, it's very important we send these packets synchronously.
|
||||
// we allow the split to happen multi-thread, but we block until all
|
||||
// chunks are sent synchronously. So, all chunks of packets are synchronous.
|
||||
// this happens because we care that the packet IDs are contiguous.
|
||||
synchronized (sendLocks.get(packet.getHeader().getType())) {
|
||||
for (Packet piece : pieces)
|
||||
awaitAcknowledgement(sendPacketIntl(piece)); // Write the individual piece, incrementing its packet
|
||||
}
|
||||
} else {
|
||||
// Don't lock here, for the slight performance benefit.
|
||||
awaitAcknowledgement(sendPacketIntl(packet));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks an outgoing packet and splits it into the pieces to send.
|
||||
*/
|
||||
private List<Packet> split(Packet packet) throws IOException {
|
||||
// Sanity checks. People are insane!
|
||||
if (packet.getHeader() == null)
|
||||
throw new NullPointerException("header");
|
||||
@@ -338,28 +431,23 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
// it's better to do this all at once instead of sending them as they are split,
|
||||
// because we might get halfway through splitting them then throw an exception and
|
||||
// we can't write the rest.
|
||||
List<Packet> packets = Fragments.split(packet);
|
||||
|
||||
// Note, it's very important we send these packets synchronously.
|
||||
// we allow the split to happen multi-thread, but we block until all
|
||||
// chunks are sent synchronously. So, all chunks of packets are synchronous.
|
||||
// this happens because we care that the packet IDs are contiguous.
|
||||
synchronized (sendLocks.get(packet.getHeader().getType())) {
|
||||
for (Packet piece : packets)
|
||||
writePacketIntl(piece); // Write the individual piece, incrementing its packet
|
||||
return Fragments.split(packet);
|
||||
}
|
||||
} else {
|
||||
|
||||
// Ensure the outbound packet's maximum size isn't too large if it can't be split (which it can't here)
|
||||
if (packet.getSize() > Fragments.MAXIMUM_PACKET_SIZE)
|
||||
throw new IllegalArgumentException("packet too large: " + packet.getSize() + "; " +
|
||||
packet.getHeader().getType().name() + " cannot split");
|
||||
|
||||
// Don't lock here, for the slight performance benefit.
|
||||
writePacketIntl(packet);
|
||||
}
|
||||
return Collections.singletonList(packet);
|
||||
}
|
||||
|
||||
private void writePacketIntl(Packet packet) throws IOException, TimeoutException {
|
||||
/**
|
||||
* Numbers, encrypts and sends a single packet.
|
||||
*
|
||||
* @return the pending acknowledgement to wait for, or null if none is expected
|
||||
*/
|
||||
private PacketResponse sendPacketIntl(Packet packet) throws IOException {
|
||||
PacketHeader header = packet.getHeader();
|
||||
|
||||
// Count packet
|
||||
@@ -407,6 +495,8 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
// > X is a ushort in H2N order of an own audio packet counter
|
||||
// it seems it can be the same as the packet counter so we will let the packethandler do it.
|
||||
((PacketBody0Voice)packet.getBody()).setPacketId(header.getPacketId());
|
||||
} else if (header.getType() == PacketBodyType.VOICE_WHISPER) {
|
||||
((PacketBody1VoiceWhisper)packet.getBody()).setPacketId(header.getPacketId());
|
||||
}
|
||||
|
||||
// Flush to a buffer
|
||||
@@ -467,7 +557,7 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
response = new PacketResponse(
|
||||
networkPacket,
|
||||
responsibleQueue,
|
||||
networkPacket.getHeader().getType().canResend() ? 30 : 0
|
||||
networkPacket.getHeader().getType().canResend()
|
||||
)
|
||||
);
|
||||
} else
|
||||
@@ -485,31 +575,40 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
.get(kind)
|
||||
.processOutgoing(packet);
|
||||
|
||||
// If necessary, fulfill promise for the packet acknowledgement or throw TimeoutException
|
||||
if (response != null && willWaitForAck) {
|
||||
while (response.getRetries() < response.getMaxTries() &&
|
||||
getState() != ClientConnectionState.DISCONNECTED) {
|
||||
try {
|
||||
if (getState() == ClientConnectionState.DISCONNECTED)
|
||||
throw new IllegalStateException("no longer connected");
|
||||
return willWaitForAck ? response : null;
|
||||
}
|
||||
|
||||
// Wait one cycle for an ACK
|
||||
response.waitForAcknowledgement(1000L);
|
||||
/** How long to wait for an answer to a packet that has been resent the given number of times. */
|
||||
public long getResendTimeout(int resends) {
|
||||
return resendTiming.timeoutMs(resends);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for a packet's acknowledgement, resending it until it arrives.
|
||||
*/
|
||||
private void awaitAcknowledgement(PacketResponse response) throws IOException, TimeoutException {
|
||||
if (response != null) {
|
||||
while (getState() != ClientConnectionState.DISCONNECTED) {
|
||||
try {
|
||||
response.waitForAcknowledgement(resendTiming.timeoutMs(response.getResends()));
|
||||
break;
|
||||
} catch (TimeoutException e) {
|
||||
if (response.willResend())
|
||||
if (!response.willResend() || response.getResends() >= ResendTiming.MAX_RESENDS
|
||||
|| System.nanoTime() - response.getFirstSent()
|
||||
>= TimeUnit.MILLISECONDS.toNanos(ResendTiming.GIVE_UP_MS)) {
|
||||
// Commands are handled in order: the server holds back everything sent after this one
|
||||
// until it arrives, so giving up on it loses the connection, as in the official client.
|
||||
if (getState() == ClientConnectionState.CONNECTED)
|
||||
setState(ClientConnectionState.DISCONNECTED);
|
||||
throw new TimeoutException("send");
|
||||
}
|
||||
response.resend();
|
||||
else
|
||||
throw e;
|
||||
} catch (CancellationException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
if (response.getRetries() >= response.getMaxTries())
|
||||
throw new TimeoutException("send");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -543,24 +642,18 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
+ " generation=" + networkPacket.getHeader().getGeneration());
|
||||
|
||||
PacketTransformation transformation = getTransformation();
|
||||
int dataLen = networkPacket.getDatagram().getLength() - networkPacket.getHeader().getSize();
|
||||
|
||||
try {
|
||||
packetBuffer = ByteBuffer.wrap(
|
||||
transformation.decrypt(
|
||||
networkPacket.getHeader(),
|
||||
packetBuffer,
|
||||
networkPacket.getDatagram().getLength() -
|
||||
networkPacket.getHeader().getSize()
|
||||
)
|
||||
decrypt(transformation, networkPacket.getHeader(), packetBuffer, dataLen)
|
||||
).order(ByteOrder.BIG_ENDIAN);
|
||||
} catch (InvalidCipherTextException e) {
|
||||
throw new IOException(
|
||||
"failed to decrypt " + networkPacket.getHeader().getType().name()
|
||||
+ " (" +
|
||||
"transformation=" + transformation.getClass() +
|
||||
", state=" + getState() +
|
||||
", id=" + packet.getHeader().getPacketId() +
|
||||
", generation=" + networkPacket.getHeader().getGeneration(),
|
||||
+ " id=" + packet.getHeader().getPacketId()
|
||||
+ " generation=" + networkPacket.getHeader().getGeneration()
|
||||
+ " (" + transformation.getClass().getSimpleName() + ", " + getState() + ")",
|
||||
e
|
||||
);
|
||||
}
|
||||
@@ -604,6 +697,20 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
return packet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts with the session key, falling back to the handshake key: the server encrypts initivexpand2 with
|
||||
* the latter, so its retransmits (sent when our ACK was lost) still arrive under it after the key switch.
|
||||
*/
|
||||
private byte[] decrypt(PacketTransformation transformation, PacketHeader header, ByteBuffer buffer, int dataLen)
|
||||
throws InvalidCipherTextException {
|
||||
try {
|
||||
return transformation.decrypt(header, buffer, dataLen);
|
||||
} catch (InvalidCipherTextException e) {
|
||||
if (transformation instanceof InitPacketTransformation || header.getGeneration() != 0) throw e;
|
||||
return initTransformation.decrypt(header, buffer, dataLen);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readPacket() throws
|
||||
IOException,
|
||||
@@ -746,13 +853,9 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
}
|
||||
}
|
||||
|
||||
if (smoothedRtt < 0)
|
||||
smoothedRtt = sampleRtt;
|
||||
else
|
||||
smoothedRtt = (long)((1 - alphaSmooth) * smoothedRtt + alphaSmooth * sampleRtt);
|
||||
|
||||
smoothedRttVar = (long)((1 - betaSmooth) * smoothedRttVar + betaSmooth * Math.abs(sampleRtt - smoothedRtt));
|
||||
currentRto = smoothedRtt + Math.max(0D, 4 * smoothedRttVar);
|
||||
// An acknowledgement of a resent packet can't tell which copy it answers.
|
||||
if (response.getResends() == 0)
|
||||
resendTiming.addSample(TimeUnit.NANOSECONDS.toMillis(rttNano));
|
||||
}
|
||||
|
||||
if (response != null)
|
||||
@@ -923,22 +1026,25 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
private class NetworkHandler implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (isReading()) {
|
||||
Packet packet;
|
||||
try {
|
||||
Packet packet = readQueue.take();
|
||||
|
||||
// separate these so we getHandler at proper runtime instant rather than get it and let it change
|
||||
// on us
|
||||
|
||||
getHandler().handlePacket(packet);
|
||||
packet = readQueue.take();
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// One bad packet must not end this thread: everything received after it would queue up unhandled
|
||||
// while ACKs and pings, answered by the reader, keep the connection looking alive.
|
||||
try {
|
||||
// separate these so we getHandler at proper runtime instant rather than get it and let it change
|
||||
// on us
|
||||
getHandler().handlePacket(packet);
|
||||
} catch (Throwable e) {
|
||||
Ts3Debugging.debug("Problem reading packet", e);
|
||||
getExceptionHandler().accept(e);
|
||||
Ts3Debugging.debug("Problem handling packet", e);
|
||||
// A disconnect interrupts whatever the handler was waiting on; that is not an error.
|
||||
if (isReading()) getExceptionHandler().accept(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -970,18 +1076,17 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
private final NetworkPacket sentPacket;
|
||||
private final CompletableFuture<Packet> future = new CompletableFuture<>();
|
||||
private final Map<Integer, PacketResponse> responsibleQueue;
|
||||
private long lastSent = 0L;
|
||||
private int tries = 1, maxTries;
|
||||
private boolean willResend;
|
||||
private final long firstSent = System.nanoTime();
|
||||
private long lastSent = firstSent;
|
||||
private int resends;
|
||||
private final boolean willResend;
|
||||
|
||||
public PacketResponse(NetworkPacket sentPacket,
|
||||
Map<Integer, PacketResponse> responsibleQueue,
|
||||
int maxTries) {
|
||||
boolean willResend) {
|
||||
this.sentPacket = sentPacket;
|
||||
this.responsibleQueue = responsibleQueue;
|
||||
this.maxTries = maxTries;
|
||||
this.lastSent = System.nanoTime();
|
||||
this.willResend = maxTries > 0;
|
||||
this.willResend = willResend;
|
||||
}
|
||||
|
||||
public NetworkPacket getPacket() {
|
||||
@@ -990,17 +1095,8 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
|
||||
public void resend() throws IOException {
|
||||
AbstractTeamspeakClientSocket.this.writeNetworkPacket(sentPacket);
|
||||
|
||||
long t = System.nanoTime();
|
||||
long before = lastSent;
|
||||
long elapsed = t - before;
|
||||
|
||||
setLastSent(t);
|
||||
|
||||
double elapsedSeconds = elapsed / 1_000_000_000D;
|
||||
currentRto += elapsedSeconds;
|
||||
|
||||
tries ++;
|
||||
setLastSent(System.nanoTime());
|
||||
resends++;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1033,12 +1129,12 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
return willResend;
|
||||
}
|
||||
|
||||
public int getMaxTries() {
|
||||
return maxTries;
|
||||
public int getResends() {
|
||||
return resends;
|
||||
}
|
||||
|
||||
public int getRetries() {
|
||||
return tries;
|
||||
public long getFirstSent() {
|
||||
return firstSent;
|
||||
}
|
||||
|
||||
public long getLastSent() {
|
||||
|
||||
+212
-45
@@ -16,8 +16,12 @@ import com.github.manevolent.ts3j.identity.Uid;
|
||||
import com.github.manevolent.ts3j.protocol.*;
|
||||
import com.github.manevolent.ts3j.protocol.client.ClientConnectionState;
|
||||
import com.github.manevolent.ts3j.protocol.header.PacketHeader;
|
||||
import com.github.manevolent.ts3j.enums.CodecType;
|
||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody;
|
||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice;
|
||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody1VoiceWhisper;
|
||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody2Command;
|
||||
import com.github.manevolent.ts3j.protocol.packet.PacketBodyType;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnected;
|
||||
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnecting;
|
||||
@@ -32,6 +36,9 @@ import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.function.Function;
|
||||
@@ -50,7 +57,13 @@ public class LocalTeamspeakClientSocket
|
||||
private volatile InetSocketAddress remote;
|
||||
|
||||
private Microphone microphone;
|
||||
private volatile PacketBody1VoiceWhisper.WhisperTarget whisperTarget;
|
||||
private HighPrecisionTimer microphoneThread;
|
||||
/** Voice packets still to be sent again, soonest first; see {@link Microphone#getRedundancyDelay}. */
|
||||
private final Queue<VoiceCopy> voiceCopies =
|
||||
new PriorityBlockingQueue<>(64, Comparator.comparingLong((VoiceCopy copy) -> copy.due));
|
||||
private volatile long voiceCopyDelayNanos;
|
||||
private volatile int voiceCopyCount;
|
||||
|
||||
private List<TS3Listener> listeners = new LinkedList<>();
|
||||
|
||||
@@ -59,6 +72,9 @@ public class LocalTeamspeakClientSocket
|
||||
/** The virtual server's properties as announced by <b>initserver</b>. */
|
||||
private volatile Map<String, String> serverProperties = Collections.emptyMap();
|
||||
|
||||
/** The public key the server identified itself with in the handshake, base64 as sent. */
|
||||
private volatile String serverPublicKey;
|
||||
|
||||
/**
|
||||
* The internal command buffer
|
||||
* <p>
|
||||
@@ -66,6 +82,9 @@ public class LocalTeamspeakClientSocket
|
||||
* <p>
|
||||
* get
|
||||
*/
|
||||
/** Whether losing the connection now would go unreported: connected, and neither side has said goodbye. */
|
||||
private volatile boolean dropUnannounced;
|
||||
|
||||
private Map<Integer, ClientCommandResponse> awaitingCommands = new ConcurrentHashMap<>();
|
||||
|
||||
public LocalTeamspeakClientSocket() {
|
||||
@@ -90,6 +109,7 @@ public class LocalTeamspeakClientSocket
|
||||
throws CommandProcessException {
|
||||
int clientId = Integer.parseInt(command.get("clid").getValue());
|
||||
if (clientId == getClientId()) {
|
||||
dropUnannounced = false; // this is the announcement
|
||||
try {
|
||||
setState(ClientConnectionState.DISCONNECTED);
|
||||
} catch (Exception e) {
|
||||
@@ -134,17 +154,17 @@ public class LocalTeamspeakClientSocket
|
||||
response.add(new CommandSingleParameter("connection_bandwidth_sent_last_second_" + name,
|
||||
Integer.toString(stats.getSentBytesLastSecond())));
|
||||
response.add(new CommandSingleParameter("connection_bandwidth_sent_last_minute_" + name,
|
||||
Integer.toString(stats.getSentBytesLastMinute())));
|
||||
Integer.toString(stats.getSentBandwidthLastMinute())));
|
||||
response.add(new CommandSingleParameter("connection_bandwidth_received_last_second_" + name,
|
||||
Integer.toString(stats.getReceivedBytesLastSecond())));
|
||||
response.add(new CommandSingleParameter("connection_bandwidth_received_last_minute_" + name,
|
||||
Integer.toString(stats.getReceivedBytesLastMinute())));
|
||||
Integer.toString(stats.getReceivedBandwidthLastMinute())));
|
||||
}
|
||||
|
||||
response.add(new CommandSingleParameter("connection_server2client_packetloss_total", Integer.toString(0)));
|
||||
|
||||
try {
|
||||
writePacket(new PacketBody2Command(getRole().getOut(), response));
|
||||
writePacketAsync(new PacketBody2Command(getRole().getOut(), response));
|
||||
} catch (Exception e) {
|
||||
Ts3Debugging.debug("Problem sending setconnectioninfo", e);
|
||||
}
|
||||
@@ -247,7 +267,7 @@ public class LocalTeamspeakClientSocket
|
||||
);
|
||||
}
|
||||
|
||||
throw new IOException("disconnected");
|
||||
throw new EOFException("disconnected");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -268,6 +288,40 @@ public class LocalTeamspeakClientSocket
|
||||
packet.getDatagram().setSocketAddress(remote);
|
||||
|
||||
socket.send(packet.getDatagram());
|
||||
|
||||
long copyDelay = voiceCopyDelayNanos;
|
||||
PacketBodyType type = packet.getHeader().getType();
|
||||
if (copyDelay > 0 && (type == PacketBodyType.VOICE || type == PacketBodyType.VOICE_WHISPER)) {
|
||||
// Aim half a tick early, so each copy goes out on the tick nearest its time.
|
||||
long sent = System.nanoTime() - TICK_MS * 1_000_000L / 2;
|
||||
for (int i = 1, n = voiceCopyCount; i <= n; i++)
|
||||
voiceCopies.add(new VoiceCopy(sent + i * copyDelay, packet.getDatagram()));
|
||||
}
|
||||
}
|
||||
|
||||
/** Sends the voice copies that are due; runs on the microphone timer. */
|
||||
private void sendDueVoiceCopies() {
|
||||
long now = System.nanoTime();
|
||||
VoiceCopy copy;
|
||||
while ((copy = voiceCopies.peek()) != null && now - copy.due >= 0) {
|
||||
// Whatever poll() hands out is due no later than the one peeked at.
|
||||
copy = voiceCopies.poll();
|
||||
try {
|
||||
socket.send(copy.datagram);
|
||||
} catch (IOException e) {
|
||||
getExceptionHandler().accept(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class VoiceCopy {
|
||||
final long due;
|
||||
final DatagramPacket datagram;
|
||||
|
||||
VoiceCopy(long due, DatagramPacket datagram) {
|
||||
this.due = due;
|
||||
this.datagram = datagram;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -283,6 +337,7 @@ public class LocalTeamspeakClientSocket
|
||||
@Override
|
||||
protected void onConnect() {
|
||||
super.onConnect();
|
||||
dropUnannounced = true;
|
||||
|
||||
if (microphoneThread != null && !microphoneThread.isAlive())
|
||||
microphoneThread.start();
|
||||
@@ -304,8 +359,23 @@ public class LocalTeamspeakClientSocket
|
||||
awaitingCommands.clear();
|
||||
}
|
||||
|
||||
if (dropUnannounced) {
|
||||
dropUnannounced = false;
|
||||
// Timed out, or gave up on a command: nobody else tells the listeners.
|
||||
Map<String, String> reason = new HashMap<>();
|
||||
reason.put("reasonid", "3");
|
||||
reason.put("reasonmsg", "connection lost");
|
||||
DisconnectedEvent e = new DisconnectedEvent(reason);
|
||||
try {
|
||||
commandExecutionService.submit(() -> listeners.forEach(e::fire));
|
||||
} catch (RejectedExecutionException ignored) {
|
||||
// the listeners are gone already
|
||||
}
|
||||
}
|
||||
|
||||
serverId = null;
|
||||
serverProperties = Collections.emptyMap();
|
||||
serverPublicKey = null;
|
||||
|
||||
remote = null;
|
||||
}
|
||||
@@ -380,6 +450,7 @@ public class LocalTeamspeakClientSocket
|
||||
// Free stuff
|
||||
serverId = null;
|
||||
serverProperties = Collections.emptyMap();
|
||||
serverPublicKey = null;
|
||||
|
||||
|
||||
synchronized (commandSendLock) {
|
||||
@@ -519,17 +590,31 @@ public class LocalTeamspeakClientSocket
|
||||
return microphone;
|
||||
}
|
||||
|
||||
/** Where the microphone's voice goes: null for our channel, else whisper packets to the target. */
|
||||
public PacketBody1VoiceWhisper.WhisperTarget getWhisperTarget() {
|
||||
return whisperTarget;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the microphone's voice to {@code target} as whispers, or to our channel again with null.
|
||||
* A voice session under way ends on its old route first.
|
||||
*/
|
||||
public void setWhisperTarget(PacketBody1VoiceWhisper.WhisperTarget target) {
|
||||
this.whisperTarget = target;
|
||||
}
|
||||
|
||||
public void setMicrophone(Microphone microphone) {
|
||||
if (this.microphone != microphone) {
|
||||
this.microphone = microphone;
|
||||
|
||||
if (microphone != null && microphoneThread == null) {
|
||||
microphoneThread = new HighPrecisionTimer(
|
||||
20,
|
||||
TICK_MS,
|
||||
5F, // up to 100ms of drift/catchup
|
||||
new MicrophoneTask()
|
||||
);
|
||||
microphoneThread.setDaemon(true);
|
||||
microphoneThread.setPriority(Thread.MAX_PRIORITY);
|
||||
|
||||
if (getState() != ClientConnectionState.DISCONNECTED)
|
||||
microphoneThread.start();
|
||||
@@ -593,14 +678,24 @@ public class LocalTeamspeakClientSocket
|
||||
}
|
||||
}
|
||||
|
||||
private static final int TICK_MS = 20;
|
||||
|
||||
private class MicrophoneTask implements Runnable {
|
||||
private boolean wasSendingAudio = false;
|
||||
/** Where the voice session under way goes; null for the channel. */
|
||||
private PacketBody1VoiceWhisper.WhisperTarget sendingTarget;
|
||||
private byte sessionId = 1;
|
||||
private int flaggedPackets = 5;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!isConnected()) return;
|
||||
if (!isConnected()) {
|
||||
// Copies from a connection that is gone must not reach the next one.
|
||||
voiceCopies.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
sendDueVoiceCopies();
|
||||
|
||||
Microphone microphone;
|
||||
|
||||
@@ -609,38 +704,51 @@ public class LocalTeamspeakClientSocket
|
||||
if ((microphone = LocalTeamspeakClientSocket.this.microphone) == null)
|
||||
return;
|
||||
|
||||
// Copies are sent from this tick, so the delay is best a whole number of ticks.
|
||||
int copyDelay = microphone.getRedundancyDelay();
|
||||
int copies = microphone.getRedundancyCopies();
|
||||
voiceCopyCount = copies;
|
||||
voiceCopyDelayNanos = copyDelay > 0 && copies > 0 ? copyDelay * 1_000_000L : 0;
|
||||
|
||||
PacketBody1VoiceWhisper.WhisperTarget target = whisperTarget;
|
||||
if (wasSendingAudio && target != sendingTarget) {
|
||||
// The listeners of the old route hear the end of what they were hearing.
|
||||
if (!sendEnd(microphone)) return;
|
||||
}
|
||||
|
||||
if (microphone.isMuted()) {
|
||||
// TODO
|
||||
// Set a state and send a muted event
|
||||
} else if (microphone.isReady()) {
|
||||
byte[] data = microphone.provide();
|
||||
if (data.length <= 0)
|
||||
return;
|
||||
|
||||
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut());
|
||||
|
||||
voice.setCodecType(microphone.getCodec());
|
||||
voice.setCodecData(data);
|
||||
if (flaggedPackets > 0) voice.setServerFlag0(sessionId);
|
||||
boolean live = microphone.isLiveCapture();
|
||||
byte[] data;
|
||||
do {
|
||||
data = microphone.provide();
|
||||
if (data.length <= 0) break;
|
||||
|
||||
try {
|
||||
writePacket(voice);
|
||||
writePacket(voicePacket(target, microphone.getCodec(), data,
|
||||
flaggedPackets > 0 && target == null ? sessionId : null));
|
||||
|
||||
flaggedPackets--;
|
||||
wasSendingAudio = true;
|
||||
sendingTarget = target;
|
||||
} catch (Exception e) {
|
||||
// All we are really concerned about here is a disconnection event, in which case the loop
|
||||
// will exit.
|
||||
getExceptionHandler().accept(e);
|
||||
return;
|
||||
}
|
||||
} while (live && microphone.isReady());
|
||||
} else if (wasSendingAudio) {
|
||||
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut());
|
||||
|
||||
voice.setCodecType(microphone.getCodec());
|
||||
voice.setCodecData(new byte[0]);
|
||||
sendEnd(microphone);
|
||||
}
|
||||
}
|
||||
|
||||
/** Ends the voice session on the route it went, which an empty packet does; false once disconnected. */
|
||||
private boolean sendEnd(Microphone microphone) {
|
||||
try {
|
||||
writePacket(voice);
|
||||
writePacket(voicePacket(sendingTarget, microphone.getCodec(), new byte[0], null));
|
||||
|
||||
// reset decoder flag
|
||||
flaggedPackets = 5;
|
||||
@@ -650,12 +758,29 @@ public class LocalTeamspeakClientSocket
|
||||
sessionId = 1;
|
||||
|
||||
wasSendingAudio = false;
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
// All we are really concerned about here is a disconnection event, in which case the loop
|
||||
// will exit.
|
||||
getExceptionHandler().accept(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private PacketBody voicePacket(PacketBody1VoiceWhisper.WhisperTarget target, CodecType codec, byte[] data,
|
||||
Byte flag) {
|
||||
if (target == null) {
|
||||
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut());
|
||||
voice.setCodecType(codec);
|
||||
voice.setCodecData(data);
|
||||
if (flag != null) voice.setServerFlag0(flag);
|
||||
return voice;
|
||||
}
|
||||
PacketBody1VoiceWhisper whisper = new PacketBody1VoiceWhisper(getRole().getOut());
|
||||
whisper.setCodecType(codec);
|
||||
whisper.setTarget(target);
|
||||
whisper.setCodecData(data);
|
||||
return whisper;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -740,16 +865,39 @@ public class LocalTeamspeakClientSocket
|
||||
return command;
|
||||
}
|
||||
|
||||
public boolean ensureSent()
|
||||
/**
|
||||
* Sends the command unless it already was. Its acknowledgement is awaited in the background: the reply
|
||||
* proves delivery anyway, and this is often called from the packet handler thread, which must keep
|
||||
* handling while resends go on. A command that never gets through fails and makes way for the next one.
|
||||
*/
|
||||
public void ensureSent()
|
||||
throws IOException, TimeoutException {
|
||||
synchronized (sendLock) {
|
||||
if (!sent) {
|
||||
LocalTeamspeakClientSocket.this.writePacket(new PacketBody2Command(ProtocolRole.CLIENT, command));
|
||||
if (sent) return;
|
||||
sent = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
CompletableFuture<Void> acknowledged;
|
||||
try {
|
||||
acknowledged = writePacketAsync(new PacketBody2Command(ProtocolRole.CLIENT, command));
|
||||
} catch (IOException | TimeoutException | RuntimeException e) {
|
||||
abandon(e);
|
||||
throw e;
|
||||
}
|
||||
acknowledged.whenComplete((ignored, e) -> {
|
||||
if (e != null) abandon(e);
|
||||
});
|
||||
}
|
||||
|
||||
private void abandon(Throwable cause) {
|
||||
completeFailure(cause);
|
||||
synchronized (commandSendLock) {
|
||||
if (!awaitingCommands.remove(returnCode, this)) return; // answered after all
|
||||
try {
|
||||
sendNextCommand();
|
||||
} catch (IOException | TimeoutException e) {
|
||||
// that command failed as well, and made way in turn
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -798,6 +946,25 @@ public class LocalTeamspeakClientSocket
|
||||
return serverProperties;
|
||||
}
|
||||
|
||||
public void setServerPublicKey(String serverPublicKey) {
|
||||
this.serverPublicKey = serverPublicKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* The virtual server's unique identifier, or null before the handshake. It is derived from
|
||||
* the server's public key as a client's is from its own; the server tells a client no other
|
||||
* way, as guests may not read it with <b>serverinfo</b>.
|
||||
*/
|
||||
public Uid getServerUid() {
|
||||
String key = serverPublicKey;
|
||||
if (key == null) return null;
|
||||
try {
|
||||
return new Uid(MessageDigest.getInstance("SHA-1").digest(key.getBytes(StandardCharsets.US_ASCII)));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/** The virtual server's name, or null if it was not announced. */
|
||||
public String getServerName() {
|
||||
return serverProperties.get("virtualserver_name");
|
||||
@@ -1291,34 +1458,34 @@ public class LocalTeamspeakClientSocket
|
||||
disconnect(null);
|
||||
}
|
||||
|
||||
private static PacketBody2Command clientDisconnect(String reason) {
|
||||
SingleCommand command = new SingleCommand("clientdisconnect", ProtocolRole.CLIENT);
|
||||
if (reason != null) {
|
||||
command.add(new CommandSingleParameter("reasonid", "8"));
|
||||
command.add(new CommandSingleParameter("reasonmsg", reason));
|
||||
}
|
||||
return new PacketBody2Command(ProtocolRole.CLIENT, command);
|
||||
}
|
||||
|
||||
public void disconnect(String reason)
|
||||
throws
|
||||
IOException, TimeoutException,
|
||||
ExecutionException, InterruptedException {
|
||||
dropUnannounced = false;
|
||||
try {
|
||||
switch (getState()) {
|
||||
case CONNECTING:
|
||||
case RETRIEVING_DATA:
|
||||
waitForState(ClientConnectionState.CONNECTED, 10000L);
|
||||
// Still loading, possibly stuck: say goodbye, but don't wait on a server that may not answer.
|
||||
writePacketAsync(clientDisconnect(reason));
|
||||
break;
|
||||
case CONNECTED:
|
||||
if (reason == null) {
|
||||
writePacket(new PacketBody2Command(
|
||||
ProtocolRole.CLIENT,
|
||||
new SingleCommand("clientdisconnect", ProtocolRole.CLIENT))
|
||||
);
|
||||
} else {
|
||||
writePacket(new PacketBody2Command(
|
||||
ProtocolRole.CLIENT,
|
||||
new SingleCommand(
|
||||
"clientdisconnect", ProtocolRole.CLIENT,
|
||||
new CommandSingleParameter("reasonid", "8"),
|
||||
new CommandSingleParameter("reasonmsg", reason)
|
||||
))
|
||||
);
|
||||
// The server confirms by letting us go, and after that acknowledges nothing: if the
|
||||
// confirmation is lost, waiting for an ACK would only keep us resending until the give-up.
|
||||
writePacketAsync(clientDisconnect(reason));
|
||||
try {
|
||||
waitForState(ClientConnectionState.DISCONNECTED, 5000L);
|
||||
} catch (TimeoutException ignored) {
|
||||
}
|
||||
|
||||
waitForState(ClientConnectionState.DISCONNECTED, 30000L);
|
||||
|
||||
break;
|
||||
}
|
||||
} finally {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.github.manevolent.ts3j.protocol.socket.client;
|
||||
|
||||
/**
|
||||
* When to resend an unacknowledged packet and when to give up on it, as the official client
|
||||
* (3.6.2) does it: the timeout is the mean round trip plus three standard deviations plus 50 ms
|
||||
* over the last 20 samples (a flat 500 ms until there are 6), growing by 1.4x per resend.
|
||||
*/
|
||||
final class ResendTiming {
|
||||
/** How long a packet may go unacknowledged before the connection counts as lost. */
|
||||
static final long GIVE_UP_MS = 18_000L;
|
||||
/** Resends after which the connection is dropped regardless of time. */
|
||||
static final int MAX_RESENDS = 255;
|
||||
|
||||
private static final int WINDOW = 20;
|
||||
private static final int MIN_SAMPLES = 6;
|
||||
private static final int INITIAL_MS = 500;
|
||||
|
||||
private final long[] samples = new long[WINDOW];
|
||||
private int count;
|
||||
private int next;
|
||||
|
||||
/** Adds the round trip of a packet that was acknowledged without having been resent. */
|
||||
synchronized void addSample(long rttMs) {
|
||||
samples[next] = rttMs;
|
||||
next = (next + 1) % WINDOW;
|
||||
if (count < WINDOW) count++;
|
||||
}
|
||||
|
||||
/** How long to wait for an acknowledgement after the given number of resends. */
|
||||
synchronized long timeoutMs(int resends) {
|
||||
double base = INITIAL_MS;
|
||||
if (count >= MIN_SAMPLES) {
|
||||
double sum = 0, sumSquares = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
sum += samples[i];
|
||||
sumSquares += (double) samples[i] * samples[i];
|
||||
}
|
||||
double mean = sum / count;
|
||||
double deviation = Math.sqrt(Math.max(0, (sumSquares - mean * sum) / count));
|
||||
base = (int) (mean + 3 * deviation + 50);
|
||||
}
|
||||
int timeout = (int) (base * Math.pow(1.4, resends));
|
||||
return timeout < 15_001 ? timeout : 19_000;
|
||||
}
|
||||
}
|
||||
@@ -64,8 +64,8 @@ public class HighPrecisionTimer extends Thread {
|
||||
|
||||
now = System.nanoTime();
|
||||
|
||||
// Clamp the next target to the maximum "drift" allowed
|
||||
if (nextTarget - now > maximumDriftInNanos)
|
||||
// After a stall, catch up at most the allowed number of ticks rather than all of them
|
||||
if (now - nextTarget > maximumDriftInNanos)
|
||||
nextTarget = now - maximumDriftInNanos;
|
||||
|
||||
if (sleepFor(nextTarget - now)) {
|
||||
|
||||
@@ -53,6 +53,10 @@ public class EncryptionTest extends TestCase {
|
||||
|
||||
byte[] packetBytes = transformation.encrypt(packet).array();
|
||||
transformation.decrypt(header, ByteBuffer.wrap(packetBytes), packet.getBody().getSize());
|
||||
|
||||
// A resend encrypts the same packet again, under the same key and nonce.
|
||||
byte[] resent = transformation.encrypt(packet).array();
|
||||
assertTrue(java.util.Arrays.equals(packetBytes, resent));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.github.manevolent.ts3j;
|
||||
|
||||
import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ServerUidTest extends TestCase {
|
||||
/** A real server's handshake key, and the unique id the official client files its chat logs under. */
|
||||
public void testDerivedFromTheHandshakeKey() {
|
||||
LocalTeamspeakClientSocket client = new LocalTeamspeakClientSocket();
|
||||
assertNull(client.getServerUid());
|
||||
|
||||
client.setServerPublicKey("MEwDAgcAAgEgAiEAtg1UQ4UpfiyTjrYPQcWV7Pa04/HTWr/viRhqEOg7+MQCIH/FqUKJDpBJDn/v01KOR+snSWlcoxOsfb2R8C02T3/z");
|
||||
assertEquals("+Tyg2JtxE8vRNZp+JiUBnmBh0MY=", client.getServerUid().toBase64());
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,47 @@
|
||||
package com.github.manevolent.ts3j;
|
||||
|
||||
import com.github.manevolent.ts3j.protocol.TS3DNS;
|
||||
import com.github.manevolent.ts3j.util.Ts3Debugging;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/** SRV answers are stubbed: the test must not depend on what the network resolves today. */
|
||||
public class TS3DNSTest extends TestCase {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new TS3DNSTest().testParser();
|
||||
@Override
|
||||
protected void tearDown() {
|
||||
TS3DNS.setSrvLookup(null);
|
||||
}
|
||||
|
||||
public void testParser() throws Exception {
|
||||
Ts3Debugging.setEnabled(true);
|
||||
public void testOrdersRecordsByPriorityThenWeight() throws Exception {
|
||||
TS3DNS.setSrvLookup(name -> {
|
||||
assertEquals("_ts3._udp.voice.example.com", name);
|
||||
return Arrays.asList("20 0 9989 backup.example.com.", "10 5 9988 b.example.com.",
|
||||
"10 1 9987 a.example.com.");
|
||||
});
|
||||
|
||||
List<InetSocketAddress> soaddr = TS3DNS.lookup("voice.teamspeak.com");
|
||||
List<InetSocketAddress> addresses = TS3DNS.lookup("voice.example.com");
|
||||
|
||||
assertTrue(soaddr.size() > 0);
|
||||
assertEquals(3, addresses.size());
|
||||
assertEquals("a.example.com", addresses.get(0).getHostString());
|
||||
assertEquals(9987, addresses.get(0).getPort());
|
||||
assertEquals("b.example.com", addresses.get(1).getHostString());
|
||||
assertEquals("backup.example.com", addresses.get(2).getHostString());
|
||||
}
|
||||
|
||||
for (SocketAddress socketAddress : soaddr)
|
||||
Ts3Debugging.debug(socketAddress);
|
||||
public void testNoRecordMeansTheHostIsNotFound() {
|
||||
TS3DNS.setSrvLookup(name -> Collections.emptyList());
|
||||
try {
|
||||
TS3DNS.lookup("voice.example.com");
|
||||
fail("expected UnknownHostException");
|
||||
} catch (UnknownHostException expected) {
|
||||
// the caller then connects to the address itself
|
||||
} catch (Exception e) {
|
||||
fail("unexpected " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.github.manevolent.ts3j;
|
||||
|
||||
import com.github.manevolent.ts3j.enums.CodecType;
|
||||
import com.github.manevolent.ts3j.protocol.ProtocolRole;
|
||||
import com.github.manevolent.ts3j.protocol.header.ClientPacketHeader;
|
||||
import com.github.manevolent.ts3j.protocol.packet.PacketBody1VoiceWhisper;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
|
||||
/** A whisper a client sends: codec, then the channel and client counts and ids, then the voice. */
|
||||
public class WhisperPacketTest extends TestCase {
|
||||
|
||||
public void testClientWhisperRoundTrip() {
|
||||
PacketBody1VoiceWhisper.WhisperTargetMultiple target = new PacketBody1VoiceWhisper.WhisperTargetMultiple();
|
||||
target.setChannelIds(new int[]{7, 9});
|
||||
target.setClientIds(new int[]{3});
|
||||
PacketBody1VoiceWhisper sent = new PacketBody1VoiceWhisper(ProtocolRole.CLIENT);
|
||||
sent.setPacketId(42);
|
||||
sent.setCodecType(CodecType.OPUS_VOICE);
|
||||
sent.setTarget(target);
|
||||
sent.setCodecData(new byte[]{1, 2, 3});
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(sent.getSize());
|
||||
sent.write(buffer);
|
||||
assertEquals("the size covers exactly what is written", buffer.capacity(), buffer.position());
|
||||
assertEquals(2 + 1 + 1 + 1 + 2 * 8 + 2 + 3, buffer.capacity());
|
||||
|
||||
buffer.flip();
|
||||
PacketBody1VoiceWhisper read = new PacketBody1VoiceWhisper(ProtocolRole.CLIENT);
|
||||
read.read(new ClientPacketHeader(), buffer);
|
||||
PacketBody1VoiceWhisper.WhisperTargetMultiple readTarget =
|
||||
(PacketBody1VoiceWhisper.WhisperTargetMultiple) read.getTarget();
|
||||
assertEquals(42, read.getPacketId());
|
||||
assertEquals(CodecType.OPUS_VOICE, read.getCodecType());
|
||||
assertTrue(Arrays.equals(new int[]{7, 9}, readTarget.getChannelIds()));
|
||||
assertTrue(Arrays.equals(new int[]{3}, readTarget.getClientIds()));
|
||||
assertTrue(Arrays.equals(new byte[]{1, 2, 3}, read.getCodecData()));
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.github.manevolent.ts3j.protocol.packet.handler.client;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PuzzleAnswerTest extends TestCase {
|
||||
private static byte[] bytes(int length, int first) {
|
||||
byte[] b = new byte[length];
|
||||
Arrays.fill(b, (byte) 0x11);
|
||||
b[0] = (byte) first;
|
||||
return b;
|
||||
}
|
||||
|
||||
/** Top bit set: toByteArray() gives 65 bytes, the first a sign byte. */
|
||||
public void testDropsTheSignByte() {
|
||||
byte[] answer = bytes(64, 0x80);
|
||||
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
|
||||
}
|
||||
|
||||
public void testKeepsSixtyFourBytes() {
|
||||
byte[] answer = bytes(64, 0x7f);
|
||||
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
|
||||
}
|
||||
|
||||
/** A leading zero byte: toByteArray() gives 63 bytes, which used to fail the connect. */
|
||||
public void testPadsAShortAnswerWithZeros() {
|
||||
byte[] answer = bytes(64, 0x00);
|
||||
answer[1] = 0x42;
|
||||
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user