Conditional proxying
I added a new feature. When instances are not proxied, Yattee first checks the URL to make sure it is not a restricted video. Usually, music videos and sports content can only be played back by the same IP address that requested the URL in the first place. That is why some videos do not play when the proxy is disabled.
This approach has multiple advantages. First and foremost, It reduced the load on Invidious/Piped instances, since users can now directly access the videos without going through the instance, which might be severely bandwidth limited. Secondly, users don't need to manually turn on the proxy when they want to watch IP address bound content, since Yattee automatically proxies such content.
Furthermore, adding the proxy option allows mitigating some severe playback issues with invidious instances. Invidious by default returns proxied URLs for videos, and due to some bug in the Invidious proxy, scrubbing or continuing playback at a random timestamp can lead to severe wait times for the users.
This should fix numerous playback issues: #666, #626, #590, #585, #498, #457, #400
2024-05-09 18:07:55 +00:00
|
|
|
import Foundation
|
|
|
|
import Logging
|
|
|
|
|
|
|
|
enum URLTester {
|
|
|
|
private static let hlsMediaPrefix = "#EXT-X-MEDIA:"
|
|
|
|
private static let hlsInfPrefix = "#EXTINF:"
|
|
|
|
private static let uriRegex = "(?<=URI=\")(.*?)(?=\")"
|
|
|
|
|
|
|
|
static func testURLResponse(url: URL, range: String, isHLS: Bool, completion: @escaping (Int) -> Void) {
|
|
|
|
if isHLS {
|
|
|
|
parseAndTestHLSManifest(manifestUrl: url, range: range, completion: completion)
|
|
|
|
} else {
|
|
|
|
httpRequest(url: url, range: range) { statusCode, _ in
|
|
|
|
completion(statusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func httpRequest(url: URL, range: String, completion: @escaping (Int, URLSessionDataTask?) -> Void) {
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "HEAD"
|
|
|
|
request.setValue("bytes=\(range)", forHTTPHeaderField: "Range")
|
2024-05-19 11:37:50 +00:00
|
|
|
request.setValue(UserAgentManager.shared.userAgent, forHTTPHeaderField: "User-Agent")
|
Conditional proxying
I added a new feature. When instances are not proxied, Yattee first checks the URL to make sure it is not a restricted video. Usually, music videos and sports content can only be played back by the same IP address that requested the URL in the first place. That is why some videos do not play when the proxy is disabled.
This approach has multiple advantages. First and foremost, It reduced the load on Invidious/Piped instances, since users can now directly access the videos without going through the instance, which might be severely bandwidth limited. Secondly, users don't need to manually turn on the proxy when they want to watch IP address bound content, since Yattee automatically proxies such content.
Furthermore, adding the proxy option allows mitigating some severe playback issues with invidious instances. Invidious by default returns proxied URLs for videos, and due to some bug in the Invidious proxy, scrubbing or continuing playback at a random timestamp can lead to severe wait times for the users.
This should fix numerous playback issues: #666, #626, #590, #585, #498, #457, #400
2024-05-09 18:07:55 +00:00
|
|
|
|
|
|
|
var dataTask: URLSessionDataTask?
|
|
|
|
dataTask = URLSession.shared.dataTask(with: request) { _, response, _ in
|
2024-05-17 13:29:42 +00:00
|
|
|
let statusCode = (response as? HTTPURLResponse)?.statusCode ?? HTTPStatus.Forbidden
|
Conditional proxying
I added a new feature. When instances are not proxied, Yattee first checks the URL to make sure it is not a restricted video. Usually, music videos and sports content can only be played back by the same IP address that requested the URL in the first place. That is why some videos do not play when the proxy is disabled.
This approach has multiple advantages. First and foremost, It reduced the load on Invidious/Piped instances, since users can now directly access the videos without going through the instance, which might be severely bandwidth limited. Secondly, users don't need to manually turn on the proxy when they want to watch IP address bound content, since Yattee automatically proxies such content.
Furthermore, adding the proxy option allows mitigating some severe playback issues with invidious instances. Invidious by default returns proxied URLs for videos, and due to some bug in the Invidious proxy, scrubbing or continuing playback at a random timestamp can lead to severe wait times for the users.
This should fix numerous playback issues: #666, #626, #590, #585, #498, #457, #400
2024-05-09 18:07:55 +00:00
|
|
|
Logger(label: "stream.yattee.httpRequest").info("URL: \(url) | Status Code: \(statusCode)")
|
|
|
|
completion(statusCode, dataTask)
|
|
|
|
}
|
|
|
|
dataTask?.resume()
|
|
|
|
}
|
|
|
|
|
|
|
|
static func parseAndTestHLSManifest(manifestUrl: URL, range: String, completion: @escaping (Int) -> Void) {
|
|
|
|
recursivelyParseManifest(manifestUrl: manifestUrl) { allURLs in
|
|
|
|
if let url = allURLs.randomElement() {
|
|
|
|
httpRequest(url: url, range: range) { statusCode, _ in
|
|
|
|
completion(statusCode)
|
|
|
|
}
|
2024-05-17 13:29:42 +00:00
|
|
|
} else {
|
|
|
|
completion(HTTPStatus.NotFound)
|
Conditional proxying
I added a new feature. When instances are not proxied, Yattee first checks the URL to make sure it is not a restricted video. Usually, music videos and sports content can only be played back by the same IP address that requested the URL in the first place. That is why some videos do not play when the proxy is disabled.
This approach has multiple advantages. First and foremost, It reduced the load on Invidious/Piped instances, since users can now directly access the videos without going through the instance, which might be severely bandwidth limited. Secondly, users don't need to manually turn on the proxy when they want to watch IP address bound content, since Yattee automatically proxies such content.
Furthermore, adding the proxy option allows mitigating some severe playback issues with invidious instances. Invidious by default returns proxied URLs for videos, and due to some bug in the Invidious proxy, scrubbing or continuing playback at a random timestamp can lead to severe wait times for the users.
This should fix numerous playback issues: #666, #626, #590, #585, #498, #457, #400
2024-05-09 18:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func recursivelyParseManifest(manifestUrl: URL, fullyParsed: @escaping ([URL]) -> Void) {
|
|
|
|
parseHLSManifest(manifestUrl: manifestUrl) { urls in
|
|
|
|
var allURLs = [URL]()
|
|
|
|
let group = DispatchGroup()
|
|
|
|
for url in urls {
|
|
|
|
if url.pathExtension == "m3u8" {
|
|
|
|
group.enter()
|
|
|
|
recursivelyParseManifest(manifestUrl: url) { subUrls in
|
|
|
|
allURLs += subUrls
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allURLs.append(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
group.notify(queue: .main) {
|
|
|
|
fullyParsed(allURLs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func parseHLSManifest(manifestUrl: URL, completion: @escaping ([URL]) -> Void) {
|
|
|
|
URLSession.shared.dataTask(with: manifestUrl) { data, _, _ in
|
2024-05-17 13:29:42 +00:00
|
|
|
// swiftlint:disable:next shorthand_optional_binding
|
|
|
|
guard let data = data else {
|
|
|
|
Logger(label: "stream.yattee.httpRequest").error("Data is nil")
|
|
|
|
completion([])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// swiftlint:disable:next non_optional_string_data_conversion
|
|
|
|
guard let manifest = String(data: data, encoding: .utf8), !manifest.isEmpty else {
|
Conditional proxying
I added a new feature. When instances are not proxied, Yattee first checks the URL to make sure it is not a restricted video. Usually, music videos and sports content can only be played back by the same IP address that requested the URL in the first place. That is why some videos do not play when the proxy is disabled.
This approach has multiple advantages. First and foremost, It reduced the load on Invidious/Piped instances, since users can now directly access the videos without going through the instance, which might be severely bandwidth limited. Secondly, users don't need to manually turn on the proxy when they want to watch IP address bound content, since Yattee automatically proxies such content.
Furthermore, adding the proxy option allows mitigating some severe playback issues with invidious instances. Invidious by default returns proxied URLs for videos, and due to some bug in the Invidious proxy, scrubbing or continuing playback at a random timestamp can lead to severe wait times for the users.
This should fix numerous playback issues: #666, #626, #590, #585, #498, #457, #400
2024-05-09 18:07:55 +00:00
|
|
|
Logger(label: "stream.yattee.httpRequest").error("Cannot read or empty HLS manifest")
|
|
|
|
completion([])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let lines = manifest.split(separator: "\n")
|
|
|
|
var mediaURLs: [URL] = []
|
|
|
|
|
|
|
|
for index in 0 ..< lines.count {
|
|
|
|
let lineString = String(lines[index])
|
|
|
|
|
|
|
|
if lineString.hasPrefix(hlsMediaPrefix),
|
|
|
|
let uriRange = lineString.range(of: uriRegex, options: .regularExpression)
|
|
|
|
{
|
|
|
|
let uri = lineString[uriRange]
|
|
|
|
if let url = URL(string: String(uri)) {
|
|
|
|
mediaURLs.append(url)
|
|
|
|
}
|
|
|
|
} else if lineString.hasPrefix(hlsInfPrefix), index < lines.count - 1 {
|
|
|
|
let possibleURL = String(lines[index + 1])
|
|
|
|
let baseURL = manifestUrl.deletingLastPathComponent()
|
|
|
|
if let relativeURL = URL(string: possibleURL, relativeTo: baseURL),
|
|
|
|
relativeURL.scheme != nil
|
|
|
|
{
|
|
|
|
mediaURLs.append(relativeURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
completion(mediaURLs)
|
|
|
|
}
|
|
|
|
.resume()
|
|
|
|
}
|
|
|
|
}
|