Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions StikJIT/JSSupport/RunJSView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ class RunJSViewModel: ObservableObject {
self.semaphore = semaphore
}

func runScript(path: URL) throws {
let scriptContent = try String(contentsOf: path, encoding: .utf8)
scriptName = path.lastPathComponent
func runScript(path: URL, scriptName: String? = nil) throws {
try runScript(data: Data(contentsOf: path), name: scriptName)
}

func runScript(data: Data, name: String? = nil) throws {
let scriptContent = String(data: data, encoding: .utf8)
scriptName = name ?? "Script"

let getPidFunction: @convention(block) () -> Int = {
return self.pid
Expand Down
99 changes: 79 additions & 20 deletions StikJIT/Views/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,40 @@ struct HomeView: View {
)
.onOpenURL { url in
print(url.path)
if url.host != "enable-jit" {
return
}

let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if let bundleId = components?.queryItems?.first(where: { $0.name == "bundle-id" })?.value {
if viewDidAppeared {
startJITInBackground(with: bundleId)
} else {
pendingBundleIdToEnableJIT = bundleId
switch url.host {
case "enable-jit":
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if let bundleId = components?.queryItems?.first(where: { $0.name == "bundle-id" })?.value {
if viewDidAppeared {
startJITInBackground(with: bundleId)
} else {
pendingBundleIdToEnableJIT = bundleId
}
} else if let pidStr = components?.queryItems?.first(where: { $0.name == "pid" })?.value, let pid = Int(pidStr) {
if viewDidAppeared {
startJITInBackground(with: pid)
} else {
pendingPIDToEnableJIT = pid
}
}
} else if let pidStr = components?.queryItems?.first(where: { $0.name == "pid" })?.value, let pid = Int(pidStr) {
if viewDidAppeared {
startJITInBackground(with: pid)
} else {
pendingPIDToEnableJIT = pid
case "script":
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if let scriptBase64URL = components?.queryItems?.first(where: { $0.name == "data" })?.value?.removingPercentEncoding,
let bundleId = components?.queryItems?.first(where: { $0.name == "bundle-id" })?.value {

let base64 = base64URLToBase64(scriptBase64URL)
if let scriptData = Data(base64Encoded: base64) {
startJITInBackground(with: bundleId, script: scriptData)
}
} else if let scriptBase64URL = components?.queryItems?.first(where: { $0.name == "data" })?.value?.removingPercentEncoding,
let pidStr = components?.queryItems?.first(where: { $0.name == "pid" })?.value {
let base64 = base64URLToBase64(scriptBase64URL)
if let scriptData = Data(base64Encoded: base64) {
startJITInBackground(with: Int(pidStr) ?? 0, script: scriptData)
}
}
default:
break
}

}
Expand Down Expand Up @@ -433,22 +450,42 @@ struct HomeView: View {
}
}

private func startJITInBackground(with bundleID: String) {
private func getJsCallback(_ script: Data, name: String? = nil) -> DebugAppCallback? {
return { pid, debugProxyHandle, semaphore in
jsModel = RunJSViewModel(pid: Int(pid), debugProxy: debugProxyHandle, semaphore: semaphore)
scriptViewShow = true

DispatchQueue.global(qos: .background).async {
do {
try jsModel?.runScript(data: script, name: name)
isProcessing = false
} catch {
showAlert(title: "Error Occurred While Executing the Default Script.".localized, message: error.localizedDescription, showOk: true)
}
}
}
}

private func startJITInBackground(with bundleID: String, script: Data? = nil) {
isProcessing = true

// Add log message
LogManager.shared.addInfoLog("Starting Debug for \(bundleID)")

DispatchQueue.global(qos: .background).async {
var callback: DebugAppCallback? = nil
if enableAdvancedOptions {

if let script {
callback = getJsCallback(script, name: bundleID + ".js")
} else if enableAdvancedOptions {
let mapping = UserDefaults.standard.dictionary(forKey: "BundleScriptMap") as? [String: String]
if let script = mapping?[bundleID] {
callback = getJsCallback(for: script)
} else if useDefaultScript {
callback = getJsCallback()
}
}


let success = JITEnableContext.shared.debugApp(withBundleID: bundleID, logger: { message in

if let message = message {
Expand All @@ -464,15 +501,22 @@ struct HomeView: View {
}
}

private func startJITInBackground(with pid: Int) {
private func startJITInBackground(with pid: Int, script: Data? = nil) {
isProcessing = true

// Add log message
LogManager.shared.addInfoLog("Starting JIT for pid \(pid)")

DispatchQueue.global(qos: .background).async {

let jsCallback: DebugAppCallback? = (enableAdvancedOptions && useDefaultScript) ? getJsCallback() : nil
var jsCallback: DebugAppCallback? = nil

if let script {
jsCallback = getJsCallback(script, name: "Script from PID \(pid)")
} else if (enableAdvancedOptions && useDefaultScript) {
jsCallback = getJsCallback()
}

let success = JITEnableContext.shared.debugApp(withPID: Int32(pid), logger: { message in

if let message = message {
Expand All @@ -488,6 +532,21 @@ struct HomeView: View {
}
}
}

func base64URLToBase64(_ base64url: String) -> String {
var base64 = base64url
.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")

// Pad with "=" to make length a multiple of 4
let paddingLength = 4 - (base64.count % 4)
if paddingLength < 4 {
base64 += String(repeating: "=", count: paddingLength)
}

return base64
}

}

class InstalledAppsViewModel: ObservableObject {
Expand Down