geesawra.industries/hydraulophone
An unofficial YouTube Music app for iPhone, iPad, and Mac.
0
0
import Foundation
public enum InnerTubeMediaURLPolicy {
public static func isAllowedMediaURL(_ url: URL) -> Bool {
guard
url.scheme?.caseInsensitiveCompare("https") == .orderedSame,
let host = url.host?.lowercased(),
url.port == nil,
url.user == nil,
url.password == nil
else {
return false
}
if host == "googlevideo.com" || host.hasSuffix(".googlevideo.com") {
return true
}
guard
host == "youtube.com"
|| host == "www.youtube.com"
|| host == "music.youtube.com"
else {
return false
}
return url.path == "/videoplayback" || url.path.hasPrefix("/videoplayback/")
}
public static func isAllowedAPIOrigin(_ url: URL) -> Bool {
url.scheme?.caseInsensitiveCompare("https") == .orderedSame
&& url.host?.caseInsensitiveCompare("music.youtube.com") == .orderedSame
&& url.port == nil
&& url.user == nil
&& url.password == nil
}
public static func isAllowedPlayerJavaScriptURL(_ url: URL) -> Bool {
guard
url.scheme?.caseInsensitiveCompare("https") == .orderedSame,
isAllowedYouTubeHost(url),
url.path.hasPrefix("/s/player/") || url.path.hasPrefix("/yts/jsbin/")
else {
return false
}
return true
}
public static func isAllowedYouTubePageURL(_ url: URL) -> Bool {
guard
url.scheme?.caseInsensitiveCompare("https") == .orderedSame,
isAllowedYouTubeHost(url)
else {
return false
}
return true
}
public static func isAllowedRedirectTarget(_ url: URL) -> Bool {
isAllowedMediaURL(url)
|| isAllowedAPIOrigin(url)
|| isAllowedPlayerJavaScriptURL(url)
|| isAllowedYouTubePageURL(url)
}
public static func isAllowedRedirect(from source: URL, to target: URL) -> Bool {
if isAllowedMediaURL(source) {
return isAllowedMediaURL(target)
}
if isAllowedAPIOrigin(source) {
return isAllowedAPIOrigin(target)
}
if isAllowedPlayerJavaScriptURL(source) {
return isAllowedPlayerJavaScriptURL(target)
}
if isAllowedYouTubePageURL(source) {
return isAllowedYouTubePageURL(target)
}
return false
}
private static func isAllowedYouTubeHost(_ url: URL) -> Bool {
guard
let host = url.host?.lowercased(),
url.port == nil,
url.user == nil,
url.password == nil
else {
return false
}
return host == "youtube.com" || host == "www.youtube.com" || host == "music.youtube.com"
}
}
final class InnerTubeRedirectDelegate: NSObject, URLSessionTaskDelegate {
func urlSession(
_ session: URLSession,
task: URLSessionTask,
willPerformHTTPRedirection response: HTTPURLResponse,
newRequest request: URLRequest,
completionHandler: @escaping (URLRequest?) -> Void
) {
guard
let sourceURL = task.currentRequest?.url ?? response.url,
let targetURL = request.url,
InnerTubeMediaURLPolicy.isAllowedRedirect(from: sourceURL, to: targetURL)
else {
completionHandler(nil)
return
}
completionHandler(request)
}
}