yattee/Shared/URLTester.swift
Toni Förster 6eba2a45c8
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-16 19:35:31 +02:00

104 lines
4.0 KiB
Swift

import Foundation
import Logging
enum URLTester {
private static let hlsMediaPrefix = "#EXT-X-MEDIA:"
private static let hlsInfPrefix = "#EXTINF:"
private static let uriRegex = "(?<=URI=\")(.*?)(?=\")"
private static let HTTPStatusForbidden = 403
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")
var dataTask: URLSessionDataTask?
dataTask = URLSession.shared.dataTask(with: request) { _, response, _ in
let statusCode = (response as? HTTPURLResponse)?.statusCode ?? HTTPStatusForbidden
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)
}
}
}
}
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
guard let data = data,
let manifest = String(data: data, encoding: .utf8),
!manifest.isEmpty
else {
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()
}
}