add padding to writes when connected over tls

This commit is contained in:
Craig Raw 2025-07-31 10:34:56 +02:00
parent f48fa7e23c
commit 2ff7a15d1e
2 changed files with 21 additions and 2 deletions

View file

@ -16,6 +16,7 @@ import java.security.cert.Certificate;
public class TcpOverTlsTransport extends TcpTransport {
private static final Logger log = LoggerFactory.getLogger(TcpOverTlsTransport.class);
public static final int PAD_TO_MULTIPLE_OF_BYTES = 96;
protected final SSLSocketFactory sslSocketFactory;
@ -41,6 +42,24 @@ public class TcpOverTlsTransport extends TcpTransport {
sslSocketFactory = sslContext.getSocketFactory();
}
@Override
protected void writeRequest(String request) throws IOException {
int currentLength = request.length();
int targetLength;
if(currentLength % PAD_TO_MULTIPLE_OF_BYTES == 0) {
targetLength = currentLength;
} else {
targetLength = ((currentLength / PAD_TO_MULTIPLE_OF_BYTES) + 1) * PAD_TO_MULTIPLE_OF_BYTES;
}
int paddingNeeded = targetLength - currentLength;
if(paddingNeeded > 0) {
super.writeRequest(request + " ".repeat(paddingNeeded));
} else {
super.writeRequest(request);
}
}
private TrustManager[] getTrustManagers(File crtFile) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException {
if(crtFile == null) {
return new TrustManager[] {

View file

@ -97,7 +97,7 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter {
}
}
private void writeRequest(String request) throws IOException {
protected void writeRequest(String request) throws IOException {
if(log.isTraceEnabled()) {
log.trace("Sending to electrum server at " + server + ": " + request);
}
@ -106,7 +106,7 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter {
throw new IllegalStateException("Socket connection has not been established.");
}
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8)));
out.println(request);
out.flush();
}