From 25d5fa9646b33286e56c5f32e71c88bfe81f82ce Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Fri, 16 Nov 2018 12:42:55 +0100 Subject: [PATCH] Add HMAC as request param, if authentication is enabled --- .../ctbrec/recorder/download/MergedHlsDownload.java | 12 ++++++++++++ src/main/java/ctbrec/ui/Player.java | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/ctbrec/recorder/download/MergedHlsDownload.java b/src/main/java/ctbrec/recorder/download/MergedHlsDownload.java index 97cc6bf1..55cfdcf3 100644 --- a/src/main/java/ctbrec/recorder/download/MergedHlsDownload.java +++ b/src/main/java/ctbrec/recorder/download/MergedHlsDownload.java @@ -14,6 +14,8 @@ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.time.Duration; @@ -34,6 +36,7 @@ import com.iheartradio.m3u8.ParseException; import com.iheartradio.m3u8.PlaylistException; import ctbrec.Config; +import ctbrec.Hmac; import ctbrec.Model; import ctbrec.Recording; import ctbrec.io.HttpClient; @@ -71,6 +74,13 @@ public class MergedHlsDownload extends AbstractHlsDownload { mergeThread = createMergeThread(targetFile, progressListener, false); LOG.debug("Merge thread started"); mergeThread.start(); + if(Config.getInstance().getSettings().requireAuthentication) { + URL u = new URL(segmentPlaylistUri); + String path = u.getPath(); + byte[] key = Config.getInstance().getSettings().key; + String hmac = Hmac.calculate(path, key); + segmentPlaylistUri = segmentPlaylistUri + "?hmac=" + hmac; + } LOG.debug("Downloading segments"); downloadSegments(segmentPlaylistUri, false); LOG.debug("Waiting for merge thread to finish"); @@ -82,6 +92,8 @@ public class MergedHlsDownload extends AbstractHlsDownload { throw new IOException("Couldn't parse HLS playlist", e); } catch (InterruptedException e) { throw new IOException("Couldn't wait for write thread to finish. Recording might be cut off", e); + } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException e) { + throw new IOException("Couldn't add HMAC to playlist url", e); } finally { alive = false; streamer.stop(); diff --git a/src/main/java/ctbrec/ui/Player.java b/src/main/java/ctbrec/ui/Player.java index 1a38b9f4..185ddc33 100644 --- a/src/main/java/ctbrec/ui/Player.java +++ b/src/main/java/ctbrec/ui/Player.java @@ -1,11 +1,13 @@ package ctbrec.ui; import java.io.File; +import java.net.URL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ctbrec.Config; +import ctbrec.Hmac; import ctbrec.OS; import ctbrec.Recording; import ctbrec.io.DevNull; @@ -81,6 +83,14 @@ public class Player { } playerProcess = rt.exec(Config.getInstance().getSettings().mediaPlayer + " " + file, OS.getEnvironment(), dir); } else { + if(Config.getInstance().getSettings().requireAuthentication) { + URL u = new URL(url); + String path = u.getPath(); + byte[] key = Config.getInstance().getSettings().key; + String hmac = Hmac.calculate(path, key); + url = url + "?hmac=" + hmac; + } + LOG.debug("Playing {}", url); playerProcess = rt.exec(Config.getInstance().getSettings().mediaPlayer + " " + url); }