diff --git a/common/src/main/java/ctbrec/io/UrlUtil.java b/common/src/main/java/ctbrec/io/UrlUtil.java index 5db200cc..558b591e 100644 --- a/common/src/main/java/ctbrec/io/UrlUtil.java +++ b/common/src/main/java/ctbrec/io/UrlUtil.java @@ -2,6 +2,8 @@ package ctbrec.io; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; @@ -14,16 +16,20 @@ public class UrlUtil { private UrlUtil() {} public static String addHmac(String url, Config config) throws MalformedURLException, InvalidKeyException, NoSuchAlgorithmException, IllegalStateException, UnsupportedEncodingException { - URL u = new URL(url); - String path = u.getPath(); - if (!config.getContextPath().isEmpty()) { - path = path.substring(config.getContextPath().length()); + try { + URI uri = new URI(url); + URL u = uri.toURL(); + String path = u.getPath(); + + if (!config.getContextPath().isEmpty()) { + path = path.substring(config.getContextPath().length()); + } + byte[] key = config.getSettings().key; + String hmac = Hmac.calculate(path, key); + url = url + "?hmac=" + hmac; + return url; + } catch (URISyntaxException e) { + throw new IllegalArgumentException("Invalid URI format: " + url, e); } - byte[] key = config.getSettings().key; - String hmac = Hmac.calculate(path, key); - url = url + "?hmac=" + hmac; - return url; } - - } diff --git a/common/src/main/java/ctbrec/recorder/download/hls/HlsDownload.java b/common/src/main/java/ctbrec/recorder/download/hls/HlsDownload.java index 0b1412a1..77c7431f 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/HlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/HlsDownload.java @@ -13,6 +13,8 @@ import org.slf4j.LoggerFactory; import java.io.*; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -199,18 +201,24 @@ public class HlsDownload extends AbstractHlsDownload { @Override protected OutputStream getSegmentOutputStream(Segment segment) throws FileNotFoundException, MalformedURLException { - URL segmentUrl = new URL(segment.url); - File tmp = new File(segmentUrl.getFile()); - String prefixedFileName = segment.prefix + '_' + tmp.getName(); - int questionMarkPosition = prefixedFileName.indexOf('?'); - if (questionMarkPosition > 0) { - prefixedFileName = prefixedFileName.substring(0, questionMarkPosition); - } - if (!prefixedFileName.endsWith(".ts")) { - prefixedFileName += ".ts"; - } - segment.targetFile = FileSystems.getDefault().getPath(downloadDir.toAbsolutePath().toString(), prefixedFileName).toFile(); - return new FileOutputStream(segment.targetFile); + try { + URI segmentUri = new URI(segment.url); + URL segmentUrl = segmentUri.toURL(); + + File tmp = new File(segmentUrl.getFile()); + String prefixedFileName = segment.prefix + '_' + tmp.getName(); + int questionMarkPosition = prefixedFileName.indexOf('?'); + if (questionMarkPosition > 0) { + prefixedFileName = prefixedFileName.substring(0, questionMarkPosition); + } + if (!prefixedFileName.endsWith(".ts")) { + prefixedFileName += ".ts"; + } + segment.targetFile = FileSystems.getDefault().getPath(downloadDir.toAbsolutePath().toString(), prefixedFileName).toFile(); + return new FileOutputStream(segment.targetFile); + } catch (URISyntaxException e) { + throw new IllegalArgumentException("Invalid URI format: " + segment.url, e); + } } @Override diff --git a/common/src/main/java/ctbrec/recorder/download/hls/SegmentDownload.java b/common/src/main/java/ctbrec/recorder/download/hls/SegmentDownload.java index 74d0bb93..55790ecc 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/SegmentDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/SegmentDownload.java @@ -15,6 +15,8 @@ import org.slf4j.LoggerFactory; import javax.crypto.NoSuchPaddingException; import java.io.*; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; @@ -48,7 +50,13 @@ public class SegmentDownload implements Callable { this.segment = segment; this.client = client; this.out = out; - this.url = new URL(segment.url); + + try { + URI uri = new URI(segment.url); + url = uri.toURL(); + } catch (URISyntaxException e) { + throw new IllegalArgumentException("Invalid URI format: " + segment.url, e); + } } @Override diff --git a/common/src/main/java/ctbrec/recorder/postprocessing/Remux.java b/common/src/main/java/ctbrec/recorder/postprocessing/Remux.java index 5e661a81..c772dcb0 100644 --- a/common/src/main/java/ctbrec/recorder/postprocessing/Remux.java +++ b/common/src/main/java/ctbrec/recorder/postprocessing/Remux.java @@ -81,8 +81,8 @@ public class Remux extends AbstractPostProcessor { private String[] prepareCommandline(File inputFile, File remuxedFile) throws IOException { String[] argsPreFfmpeg = getConfig().get(PRE_FFMPEG_ARGS).isEmpty() - ? new String[] {} // If empty, create an empty array - : getConfig().get(PRE_FFMPEG_ARGS).split(" "); + ? new String[] {} // If empty, create an empty array + : getConfig().get(PRE_FFMPEG_ARGS).split(" "); String[] args = getConfig().get(FFMPEG_ARGS).split(" "); String[] argsPlusFile = new String[argsPreFfmpeg.length + args.length + 3]; int i = 0; diff --git a/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java b/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java index c5c66024..8378377c 100644 --- a/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java +++ b/common/src/main/java/ctbrec/sites/bonga/BongaCamsModel.java @@ -24,8 +24,6 @@ import java.time.Duration; import java.time.Instant; import java.util.*; import java.util.concurrent.ExecutionException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL; import static ctbrec.Model.State.*; diff --git a/common/src/main/java/ctbrec/sites/chaturbate/Chaturbate.java b/common/src/main/java/ctbrec/sites/chaturbate/Chaturbate.java index cf2af4bf..25c90f91 100644 --- a/common/src/main/java/ctbrec/sites/chaturbate/Chaturbate.java +++ b/common/src/main/java/ctbrec/sites/chaturbate/Chaturbate.java @@ -13,7 +13,6 @@ import org.json.JSONObject; import java.io.IOException; import java.net.URLEncoder; import java.time.Duration; -import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher;