mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
Add User-Agent to request
We generate a User-Agent for the platform we are running on and add it to the URLTester requests and also to MPV and AVPlayer
This commit is contained in:
@@ -103,9 +103,8 @@ enum Constants {
|
||||
#elseif os(iOS)
|
||||
if isIPad {
|
||||
return .sidebar
|
||||
} else {
|
||||
return .tab
|
||||
}
|
||||
return .tab
|
||||
#else
|
||||
return .tab
|
||||
#endif
|
||||
|
@@ -20,6 +20,7 @@ enum URLTester {
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "HEAD"
|
||||
request.setValue("bytes=\(range)", forHTTPHeaderField: "Range")
|
||||
request.setValue(UserAgentManager.shared.userAgent, forHTTPHeaderField: "User-Agent")
|
||||
|
||||
var dataTask: URLSessionDataTask?
|
||||
dataTask = URLSession.shared.dataTask(with: request) { _, response, _ in
|
||||
|
28
Shared/UserAgentManager.swift
Normal file
28
Shared/UserAgentManager.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
import Logging
|
||||
import WebKit
|
||||
|
||||
final class UserAgentManager {
|
||||
static let shared = UserAgentManager()
|
||||
|
||||
private(set) var userAgent: String
|
||||
private var webView: WKWebView?
|
||||
|
||||
private init() {
|
||||
// In case an error occurs while retrieving the actual User-Agent,
|
||||
// we set a default User-Agent value that represents a commonly used User-Agent.
|
||||
userAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
|
||||
|
||||
webView = WKWebView()
|
||||
webView?.evaluateJavaScript("navigator.userAgent") { [weak self] result, _ in
|
||||
if let userAgent = result as? String {
|
||||
DispatchQueue.main.async {
|
||||
self?.userAgent = userAgent
|
||||
Logger(label: "stream.yattee.userAgentManager").info("User-Agent: \(userAgent)")
|
||||
print("User-Agent updated: \(userAgent)")
|
||||
}
|
||||
} else {
|
||||
Logger(label: "stream.yattee.userAgentManager").warning("Failed to update User-Agent.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -206,6 +206,9 @@ struct YatteeApp: App {
|
||||
}
|
||||
#endif
|
||||
|
||||
// Initialize UserAgentManager
|
||||
_ = UserAgentManager.shared
|
||||
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
URLBookmarkModel.shared.refreshAll()
|
||||
}
|
||||
|
Reference in New Issue
Block a user