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 statusItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength )
|
|
|
|
let statusBarController = StatusBarController()
|
|
|
|
let javaDetector = DetectJava()
|
|
|
|
static let objCBridge = SBridge()
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
self.javaDetector.findIt()
|
|
|
|
if (!javaDetector.isJavaFound()) {
|
|
|
|
print("Could not find java....")
|
|
|
|
terminate("No java..")
|
|
|
|
}
|
|
|
|
let javaBinPath = self.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) {
|
|
|
|
print("captured output")
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
print("did NOT captured output")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func applicationDidFinishLaunching() {
|
|
|
|
print("Hello from swift!")
|
|
|
|
var i2pPath = NSHomeDirectory()
|
|
|
|
i2pPath += "/Library/I2P"
|
|
|
|
|
2018-09-19 00:37:16 +00:00
|
|
|
//let javaBinPath = self.javaDetector.javaHome.replace(target: " ", withString: "\\ ")
|
2018-09-18 15:36:38 +00:00
|
|
|
|
|
|
|
let fileManager = FileManager()
|
|
|
|
var ok = ObjCBool(true)
|
|
|
|
let doesI2PDirExists = fileManager.fileExists(atPath: i2pPath, isDirectory: &ok)
|
|
|
|
|
|
|
|
if (!doesI2PDirExists) {
|
|
|
|
// Deploy
|
|
|
|
}
|
|
|
|
|
2018-09-19 00:37:16 +00:00
|
|
|
//let i2pJarPath = i2pPath + "/lib/i2p.jar"
|
2018-09-18 15:36:38 +00:00
|
|
|
|
2018-09-19 00:37:16 +00:00
|
|
|
findInstalledI2PVersion()
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc static func openLink(url: String) {
|
|
|
|
objCBridge.openUrl(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func applicationWillTerminate() {
|
|
|
|
// Shutdown stuff
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func terminate(_ why: Any?) {
|
|
|
|
print("Stopping cause of ", why!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|