Skip to content

Commit

Permalink
clean-up reverse proxy URL detection
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjl-mux committed Apr 26, 2024
1 parent 3940672 commit 428b02a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
38 changes: 17 additions & 21 deletions Sources/MuxPlayerSwift/GlobalLifecycle/PlayerSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,29 @@ class PlayerSDK {
guard let components = URLComponents(
url: urlAsset.url,
resolvingAgainstBaseURL: false
) else {
), urlAsset.url.isReverseProxyable else {
return
}

if let port = components.port,
components.scheme == "http",
port == reverseProxyServer.port {
guard let originURLQueryComponentValue = components.queryItems?.first(
where: { $0.name == self.reverseProxyServer.originURLKey }
)?.value else {
// TODO: Handle more gracefully
fatalError("Invalid origin URL")
}
guard let originURLQueryComponentValue = components.queryItems?.first(
where: { $0.name == self.reverseProxyServer.originURLKey }
)?.value else {
// TODO: Handle more gracefully
fatalError("Invalid origin URL")
}

guard let originURL = URL(string: originURLQueryComponentValue) else {
// TODO: Handle more gracefully
fatalError("Invalid origin URL")
}
guard let originURL = URL(string: originURLQueryComponentValue) else {
// TODO: Handle more gracefully
fatalError("Invalid origin URL")
}

let playerItem = AVPlayerItem(
url: originURL
)
let playerItem = AVPlayerItem(
url: originURL
)

player.replaceCurrentItem(
with: playerItem
)
}
player.replaceCurrentItem(
with: playerItem
)
}

class KeyValueObservation {
Expand Down
22 changes: 22 additions & 0 deletions Sources/MuxPlayerSwift/InternalExtensions/URL+Mux.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// URL+Mux.swift
//

import Foundation

internal extension URL {

var isReverseProxyable: Bool {
guard let components = URLComponents(
url: self,
resolvingAgainstBaseURL: false
) else {
return false
}

return components.scheme == PlaybackURLConstants.reverseProxyScheme &&
components.port == PlaybackURLConstants.reverseProxyPort &&
components.host == PlaybackURLConstants.reverseProxyHost
}

}
14 changes: 11 additions & 3 deletions Sources/MuxPlayerSwift/PublicAPI/Extensions/AVPlayerItem+Mux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
import AVFoundation
import Foundation

internal enum PlaybackURLConstants {
static let reverseProxyScheme = "http"

static let reverseProxyHost = "127.0.0.1"

static let reverseProxyPort = Int(1234)
}

fileprivate func makePlaybackURL(
playbackID: String,
playbackOptions: PlaybackOptions
Expand Down Expand Up @@ -89,9 +97,9 @@ fileprivate func makePlaybackURL(
]

// TODO: currently enables reverse proxying unless caching is disabled
components.scheme = "http"
components.host = "127.0.0.1"
components.port = Int(1234)
components.scheme = PlaybackURLConstants.reverseProxyScheme
components.host = PlaybackURLConstants.reverseProxyHost
components.port = PlaybackURLConstants.reverseProxyPort
}

guard let playbackURL = components.url else {
Expand Down

0 comments on commit 428b02a

Please sign in to comment.