Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for new mitmdump version and server and proxy not deactivating when exiting the app #16

Merged
merged 16 commits into from
Apr 11, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/Mockingbird/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
AppStore.task.dispatch(TaskAction.initialize)
ServerManager.shared.initialize()

AppStore.task.dispatch(TaskAction.stopAll)
AppStore.task.dispatch(TaskAction.stopAll(forceSync: false))
ServerManager.shared.stop()
}

Expand All @@ -41,7 +41,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) {

ServerManager.shared.stop()
AppStore.task.dispatch(TaskAction.stopAll)
AppStore.task.dispatch(TaskAction.stopAll(forceSync: true))

NSApp.terminate(self)
}
Expand Down
4 changes: 2 additions & 2 deletions src/Mockingbird/Resources/Scripts/mitm_on.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
if [ -z "$2" ]
then
~/.mockingbird/mitmproxy/mitmdump_mb
~/.mockingbird/mitmproxy/mitmdump_mbv2
else
~/.mockingbird/mitmproxy/mitmdump_mb --allow-hosts "$2"
~/.mockingbird/mitmproxy/mitmdump_mbv2 --allow-hosts "$2"
fi
Binary file modified src/Mockingbird/Resources/mitmdump_mb.zip
Binary file not shown.
20 changes: 13 additions & 7 deletions src/Mockingbird/Store/Task/Service/ProcessManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@ final class ProcessManager {

private var mitmProcess: Process?

func execute(process: ProcessType) {
func execute(process: ProcessType, forceSync: Bool = false) {

switch process {
if forceSync {

case .ipInfo:
ProcessTask.launch(process: process, callback: self)

ProcessTask.launchSync(process: process, callback: self)
} else {

default:
switch process {

case .ipInfo:

ProcessTask.launchBackgroundSync(process: process, callback: self)

default:

ProcessTask.launchAsync(process: process,
callback: self)
ProcessTask.launchBackgroundAsync(process: process, callback: self)
}
}
}
}
Expand Down
50 changes: 27 additions & 23 deletions src/Mockingbird/Store/Task/Service/ProcessTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,48 @@ enum ProcessType {

class ProcessTask {

static func launchSync(process: ProcessType, callback: ProcessTaskCallback?) {
static func launch(process: ProcessType, callback: ProcessTaskCallback?) {

guard let script = Bundle.main.path(forResource: process.script, ofType: "sh") else { return }

DispatchQueue.global(qos: .background).async {
callback?.processStarted(process)

callback?.processStarted(process)
let stdoutPipe = Pipe()
let stderrPipe = Pipe()

let stdoutPipe = Pipe()
let stderrPipe = Pipe()
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = [script, (script as NSString).deletingLastPathComponent]
task.standardOutput = stdoutPipe
task.standardError = stderrPipe

let task = Process()
task.launchPath = "/bin/sh"
task.arguments = [script, (script as NSString).deletingLastPathComponent]
task.standardOutput = stdoutPipe
task.standardError = stderrPipe
task.launch()
task.waitUntilExit()

task.launch()
task.waitUntilExit()
if let stdoutText = String(data: stdoutPipe.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8),
stdoutText.count > 0 {

if let stdoutText = String(data: stdoutPipe.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8),
stdoutText.count > 0 {
callback?.stdoutUpdated(process, text: stdoutText)
}

callback?.stdoutUpdated(process, text: stdoutText)
}
if let stderrText = String(data: stderrPipe.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8),
stderrText.count > 0 {

if let stderrText = String(data: stderrPipe.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8),
stderrText.count > 0 {
callback?.stderrUpdated(process, text: stderrText)
}

callback?.stderrUpdated(process, text: stderrText)
}
callback?.processEnded(process)
}

callback?.processEnded(process)
static func launchBackgroundSync(process: ProcessType, callback: ProcessTaskCallback?) {

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

Self.launch(process: process, callback: callback)
}
}

static func launchAsync(process: ProcessType,
callback: ProcessTaskCallback?) {
static func launchBackgroundAsync(process: ProcessType, callback: ProcessTaskCallback?) {

guard let script = Bundle.main.path(forResource: process.script, ofType: "sh") else { return }

Expand Down
2 changes: 1 addition & 1 deletion src/Mockingbird/Store/Task/TaskActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum TaskAction: Action {

case initialize
case startAll
case stopAll
case stopAll(forceSync: Bool)
case startProxy(isWifi: Bool)
case stopProxy
case startMitm
Expand Down
18 changes: 9 additions & 9 deletions src/Mockingbird/Store/Task/TaskReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class TaskReducer {

Self.startAll(with: &state)

case .stopAll:
case .stopAll(let forceSync):

Self.stopAll(with: &state)
Self.stopAll(with: &state, forceSync: forceSync)

case .startProxy(let isWifi):

Expand Down Expand Up @@ -91,10 +91,10 @@ private extension TaskReducer {
Self.startProxy(with: &state)
}

static func stopAll(with state: inout TaskState) {
static func stopAll(with state: inout TaskState, forceSync: Bool = false) {

Self.stopProxy(with: &state)
Self.stopMitm(with: &state)
Self.stopProxy(with: &state, forceSync: forceSync)
Self.stopMitm(with: &state, forceSync: forceSync)
}

static func startProxy(with state: inout TaskState, isWifi: Bool = false) {
Expand All @@ -106,12 +106,12 @@ private extension TaskReducer {
ProcessManager.shared.execute(process: .ipInfo)
}

static func stopProxy(with state: inout TaskState) {
static func stopProxy(with state: inout TaskState, forceSync: Bool = false) {

state.isProxyEnabled = false
state.ipInfo = ""

ProcessManager.shared.execute(process: .proxyOff)
ProcessManager.shared.execute(process: .proxyOff, forceSync: forceSync)
}

static func startMitm(with state: inout TaskState) {
Expand All @@ -121,10 +121,10 @@ private extension TaskReducer {
ProcessManager.shared.execute(process: .mitmOn(currentContext: ContextManager.shared.currentContext))
}

static func stopMitm(with state: inout TaskState) {
static func stopMitm(with state: inout TaskState, forceSync: Bool = false) {

state.isMitmEnabled = false

ProcessManager.shared.execute(process: .mitmOff)
ProcessManager.shared.execute(process: .mitmOff, forceSync: forceSync)
}
}
2 changes: 1 addition & 1 deletion src/Mockingbird/Utils/Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Default {
public enum Folder {

static var main = "/Users/" + NSUserName() + "/.mockingbird"
static var mitm: String { main + "/mitmproxy-v2" }
static var mitm: String { main + "/mitmproxy" }
static var capture: String { workingDirectory + "/capture" }
static var record: String { workingDirectory + "/record" }
static var data: String { workingDirectory + "/data" }
Expand Down