2018-09-18 15:36:38 +00:00
|
|
|
//
|
|
|
|
// SwiftMainDelegate.swift
|
|
|
|
// I2PLauncher
|
|
|
|
//
|
|
|
|
// Created by Mikal Villa on 17/09/2018.
|
|
|
|
// Copyright © 2018 The I2P Project. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
@objc class SwiftMainDelegate : NSObject {
|
|
|
|
|
|
|
|
let statusBarController = StatusBarController()
|
2018-09-22 22:13:40 +00:00
|
|
|
let sharedRouterMgmr = RouterManager.shared()
|
2018-09-20 02:38:44 +00:00
|
|
|
static let javaDetector = DetectJava()
|
2018-09-18 15:36:38 +00:00
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
2018-09-20 02:38:44 +00:00
|
|
|
if (!SwiftMainDelegate.javaDetector.isJavaFound()) {
|
|
|
|
SwiftMainDelegate.javaDetector.findIt()
|
|
|
|
if (!SwiftMainDelegate.javaDetector.isJavaFound()) {
|
|
|
|
print("Could not find java....")
|
|
|
|
terminate("No java..")
|
|
|
|
}
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
2018-09-20 02:38:44 +00:00
|
|
|
let javaBinPath = SwiftMainDelegate.javaDetector.javaHome
|
2018-09-19 00:37:16 +00:00
|
|
|
RouterProcessStatus.knownJavaBinPath = javaBinPath
|
2018-09-18 15:36:38 +00:00
|
|
|
print("Found java home = ", javaBinPath)
|
|
|
|
|
2018-09-19 00:37:16 +00:00
|
|
|
let (portIsNotTaken, _) = RouterProcessStatus.checkTcpPortForListen(port: 7657)
|
2018-09-18 15:36:38 +00:00
|
|
|
if (!portIsNotTaken) {
|
|
|
|
RouterProcessStatus.isRouterRunning = true
|
|
|
|
RouterProcessStatus.isRouterChildProcess = false
|
|
|
|
print("I2P Router seems to be running")
|
|
|
|
} else {
|
|
|
|
RouterProcessStatus.isRouterRunning = false
|
|
|
|
print("I2P Router seems to NOT be running")
|
|
|
|
}
|
2018-09-19 00:37:16 +00:00
|
|
|
} // End of init()
|
2018-09-18 15:36:38 +00:00
|
|
|
|
2018-09-19 00:37:16 +00:00
|
|
|
@objc func findInstalledI2PVersion() {
|
2018-09-18 15:36:38 +00:00
|
|
|
var i2pPath = NSHomeDirectory()
|
|
|
|
i2pPath += "/Library/I2P"
|
2018-09-19 00:37:16 +00:00
|
|
|
var jExecPath:String = RouterProcessStatus.knownJavaBinPath!
|
2018-09-18 15:36:38 +00:00
|
|
|
|
|
|
|
// Sometimes, home will return the binary, sometimes the actual home dir. This fixes the diverge.
|
|
|
|
// If JRE is detected, binary follows - if it's JDK, home follows.
|
|
|
|
if (jExecPath.hasSuffix("Home")) {
|
|
|
|
jExecPath += "/jre/bin/java"
|
|
|
|
}
|
|
|
|
|
2018-09-19 00:37:16 +00:00
|
|
|
let jarPath = i2pPath + "/lib/i2p.jar"
|
|
|
|
|
2018-09-18 15:36:38 +00:00
|
|
|
let subCmd = jExecPath + " -cp " + jarPath + " net.i2p.CoreVersion"
|
|
|
|
|
2018-09-19 00:37:16 +00:00
|
|
|
let cmdArgs:[String] = ["-c", subCmd]
|
2018-09-18 15:36:38 +00:00
|
|
|
print(cmdArgs)
|
|
|
|
let sub:Subprocess = Subprocess.init(executablePath: "/bin/sh", arguments: cmdArgs)
|
|
|
|
let results:ExecutionResult = sub.execute(captureOutput: true)!
|
|
|
|
if (results.didCaptureOutput) {
|
|
|
|
let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "")
|
2018-09-19 00:37:16 +00:00
|
|
|
NSLog("I2P version detected: %@",i2pVersion ?? "Unknown")
|
2018-09-18 15:36:38 +00:00
|
|
|
RouterProcessStatus.routerVersion = i2pVersion
|
2018-09-22 22:13:40 +00:00
|
|
|
RouterManager.shared().eventManager.trigger(eventName: "router_version", information: i2pVersion)
|
2018-09-18 15:36:38 +00:00
|
|
|
} else {
|
2018-09-22 22:13:40 +00:00
|
|
|
print("Warning: Version Detection did NOT captured output")
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func applicationDidFinishLaunching() {
|
|
|
|
print("Hello from swift!")
|
|
|
|
var i2pPath = NSHomeDirectory()
|
|
|
|
i2pPath += "/Library/I2P"
|
|
|
|
|
2018-09-19 00:37:16 +00:00
|
|
|
findInstalledI2PVersion()
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc static func openLink(url: String) {
|
2018-09-22 22:13:40 +00:00
|
|
|
SBridge.sharedInstance().openUrl(url)
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func applicationWillTerminate() {
|
|
|
|
// Shutdown stuff
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func terminate(_ why: Any?) {
|
|
|
|
print("Stopping cause of ", why!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|