Merge pull request #848 from michael-loki/ui-macos_rework

Improvements to macOS app workflow
pull/851/head
Jeff 5 years ago committed by GitHub
commit a50c80070c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,29 @@
//
// WindowsManager.swift
// lokinet
//
// Copyright © 2019 Loki. All rights reserved.
//
import AppKit
import Foundation
struct WindowsManager {
static func getVC<T: NSViewController>(withIdentifier identifier: String,
ofType: T.Type?,
storyboard: String = "Main",
bundle: Bundle? = nil) -> T? {
let storyboard = NSStoryboard(name: storyboard, bundle: bundle)
guard let vc: T = storyboard.instantiateController(withIdentifier: identifier) as? T else {
let alert = NSAlert()
alert.alertStyle = .critical
alert.messageText = "Error initiating the viewcontroller"
alert.runModal()
return nil
}
return vc
}
}

@ -9,8 +9,12 @@
/* Begin PBXBuildFile section */
7B28BD1A232EA8B40073B955 /* DNSManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B28BD19232EA8B40073B955 /* DNSManager.swift */; };
7B28BD1C232EB6EF0073B955 /* LokinetRunner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B28BD1B232EB6EF0073B955 /* LokinetRunner.swift */; };
7BA4FB642340D5940098E20A /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BA4FB632340D5940098E20A /* Preferences.swift */; };
7BA4FB662340DA820098E20A /* StatusMenuExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BA4FB652340DA820098E20A /* StatusMenuExt.swift */; };
7BA4FB6C2340F2270098E20A /* WindowsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BA4FB6B2340F2270098E20A /* WindowsManager.swift */; };
7BA4FB7023411FF60098E20A /* PrefsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BA4FB6E23411FF60098E20A /* PrefsViewController.swift */; };
7BA4FB7323412D700098E20A /* Interfaces.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BA4FB7223412D700098E20A /* Interfaces.swift */; };
7BED5B7A232D78D900DF603F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BED5B79232D78D900DF603F /* AppDelegate.swift */; };
7BED5B7C232D78D900DF603F /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BED5B7B232D78D900DF603F /* ViewController.swift */; };
7BED5B7E232D78DB00DF603F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7BED5B7D232D78DB00DF603F /* Assets.xcassets */; };
7BED5B81232D78DB00DF603F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7BED5B7F232D78DB00DF603F /* Main.storyboard */; };
7BED5B8D232D78DB00DF603F /* lokinetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BED5B8C232D78DB00DF603F /* lokinetTests.swift */; };
@ -39,9 +43,13 @@
/* Begin PBXFileReference section */
7B28BD19232EA8B40073B955 /* DNSManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DNSManager.swift; sourceTree = "<group>"; };
7B28BD1B232EB6EF0073B955 /* LokinetRunner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LokinetRunner.swift; sourceTree = "<group>"; };
7BA4FB632340D5940098E20A /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
7BA4FB652340DA820098E20A /* StatusMenuExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusMenuExt.swift; sourceTree = "<group>"; };
7BA4FB6B2340F2270098E20A /* WindowsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowsManager.swift; sourceTree = "<group>"; };
7BA4FB6E23411FF60098E20A /* PrefsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefsViewController.swift; sourceTree = "<group>"; };
7BA4FB7223412D700098E20A /* Interfaces.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Interfaces.swift; sourceTree = "<group>"; };
7BED5B76232D78D900DF603F /* lokinet.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = lokinet.app; sourceTree = BUILT_PRODUCTS_DIR; };
7BED5B79232D78D900DF603F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7BED5B7B232D78D900DF603F /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
7BED5B7D232D78DB00DF603F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
7BED5B80232D78DB00DF603F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
7BED5B82232D78DB00DF603F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@ -84,6 +92,7 @@
7BED5B6D232D78D900DF603F = {
isa = PBXGroup;
children = (
7BA4FB6B2340F2270098E20A /* WindowsManager.swift */,
7BED5B78232D78D900DF603F /* lokinet */,
7BED5B8B232D78DB00DF603F /* lokinetTests */,
7BED5B96232D78DB00DF603F /* lokinetUITests */,
@ -106,7 +115,9 @@
isa = PBXGroup;
children = (
7BED5B79232D78D900DF603F /* AppDelegate.swift */,
7BED5B7B232D78D900DF603F /* ViewController.swift */,
7BA4FB652340DA820098E20A /* StatusMenuExt.swift */,
7BA4FB632340D5940098E20A /* Preferences.swift */,
7BA4FB6E23411FF60098E20A /* PrefsViewController.swift */,
7BED5B7D232D78DB00DF603F /* Assets.xcassets */,
7BED5B7F232D78DB00DF603F /* Main.storyboard */,
7BED5B82232D78DB00DF603F /* Info.plist */,
@ -115,6 +126,7 @@
7B28BD19232EA8B40073B955 /* DNSManager.swift */,
7BED5BA7232E831B00DF603F /* StreamReader.swift */,
7B28BD1B232EB6EF0073B955 /* LokinetRunner.swift */,
7BA4FB7223412D700098E20A /* Interfaces.swift */,
);
path = lokinet;
sourceTree = "<group>";
@ -286,10 +298,14 @@
buildActionMask = 2147483647;
files = (
7BED5BA8232E831B00DF603F /* StreamReader.swift in Sources */,
7BED5B7C232D78D900DF603F /* ViewController.swift in Sources */,
7BA4FB7023411FF60098E20A /* PrefsViewController.swift in Sources */,
7BED5BA6232E7E6600DF603F /* LokinetLog.swift in Sources */,
7BA4FB662340DA820098E20A /* StatusMenuExt.swift in Sources */,
7B28BD1A232EA8B40073B955 /* DNSManager.swift in Sources */,
7B28BD1C232EB6EF0073B955 /* LokinetRunner.swift in Sources */,
7BA4FB6C2340F2270098E20A /* WindowsManager.swift in Sources */,
7BA4FB7323412D700098E20A /* Interfaces.swift in Sources */,
7BA4FB642340D5940098E20A /* Preferences.swift in Sources */,
7BED5B7A232D78D900DF603F /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -373,6 +389,7 @@
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = 23TKR8Q2XE;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
@ -435,6 +452,7 @@
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = 23TKR8Q2XE;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;

@ -7,17 +7,149 @@
import Cocoa
let LOG_WINDOW_CONTROLLER: NSWindowController = NSWindowController(window: nil)
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var lokinet: LokinetRunner? = nil
var appender: Appendable? = nil
var statusBarItem: NSStatusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
func applicationWillFinishLaunching(_ notification: Notification) {
if (!Preferences.firstRunDone) {
Preferences.firstRunDone = true
Preferences.restore()
}
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
guard let statusButton = statusBarItem.button else { return }
statusButton.title = "LokiNet"
let statusMenu: NSMenu = NSMenu()
statusMenu.autoenablesItems = false
statusMenu.addItem(withTitle: "LokiNet", action: nil, keyEquivalent: "")
let runItem: NSMenuItem = {
let item = NSMenuItem(
title: "Run",
action: #selector(runLokinet),
keyEquivalent: "r"
)
item.target = self
return item
}()
let stopItem: NSMenuItem = {
let item = NSMenuItem(
title: "Stop",
action: #selector(stopLokinet),
keyEquivalent: "s"
)
item.isEnabled = false
item.target = self
return item
}()
let showWindowItem: NSMenuItem = {
let item = NSMenuItem(
title: "Show Window",
action: #selector(showWindow),
keyEquivalent: "w"
)
item.target = self
return item
}()
let quitApplicationItem: NSMenuItem = {
let item = NSMenuItem(
title: "Quit",
action: #selector(terminate),
keyEquivalent: "q")
item.target = self
return item
}()
statusMenu.addItems(
.separator(),
runItem,
stopItem,
.separator(),
showWindowItem,
.separator(),
quitApplicationItem
)
statusBarItem.menu = statusMenu
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
lokinet?.stop()
}
}
extension AppDelegate {
@objc
func showWindow(sender: NSMenuItem) {
if let vc = WindowsManager.getVC(withIdentifier: "LokinetLogController", ofType: LokinetLogController.self) {
appender = vc.log
let window: NSWindow = {
let w = NSWindow(contentViewController: vc)
w.styleMask.remove(.fullScreen)
w.styleMask.remove(.resizable)
w.styleMask.remove(.miniaturizable)
w.level = .floating
return w
}()
lokinet?.logAppender = vc.log
if LOG_WINDOW_CONTROLLER.window == nil {
LOG_WINDOW_CONTROLLER.window = window
}
LOG_WINDOW_CONTROLLER.showWindow(window)
}
}
@objc
func runLokinet(sender: NSMenuItem) {
if lokinet == nil {
lokinet = LokinetRunner(interface: Preferences.interfaceName, path: Preferences.lokinetPath)
lokinet?.logAppender = appender
lokinet?.start()
}
sender.isEnabled = false;
if let menu = statusBarItem.menu, let stop = menu.item(withTitle: "Stop") {
stop.isEnabled = true
}
}
@objc
func stopLokinet(_ sender: NSMenuItem) {
lokinet?.stop()
lokinet = nil
sender.isEnabled = false;
if let menu = statusBarItem.menu, let start = menu.item(withTitle: "Run") {
start.isEnabled = true
}
}
@objc
func terminate(_ sender: NSMenuItem) {
NSApp.terminate(sender)
}
}

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@ -22,7 +22,11 @@
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
<menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW"/>
<menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW">
<connections>
<segue destination="I5t-b2-LMF" kind="show" id="DG8-M4-AZh"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="wFC-TO-SCJ"/>
<menuItem title="Services" id="NMo-om-nkz">
<modifierMask key="keyEquivalentModifierMask"/>
@ -59,25 +63,6 @@
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="View" id="HyV-fh-RgO">
<items>
<menuItem title="Show Toolbar" keyEquivalent="t" id="snW-S8-Cw5">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<connections>
<action selector="toggleToolbarShown:" target="Ady-hI-5gd" id="BXY-wc-z0C"/>
</connections>
</menuItem>
<menuItem title="Customize Toolbar…" id="1UK-8n-QPP">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="runToolbarCustomizationPalette:" target="Ady-hI-5gd" id="pQI-g3-MTW"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="hB3-LF-h0Y"/>
<menuItem title="Show Sidebar" keyEquivalent="s" id="kIP-vf-haE">
<modifierMask key="keyEquivalentModifierMask" control="YES" command="YES"/>
<connections>
<action selector="toggleSidebar:" target="Ady-hI-5gd" id="iwa-gc-5KM"/>
</connections>
</menuItem>
<menuItem title="Enter Full Screen" keyEquivalent="f" id="4J7-dP-txa">
<modifierMask key="keyEquivalentModifierMask" control="YES" command="YES"/>
<connections>
@ -134,93 +119,120 @@
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="0.0"/>
<point key="canvasLocation" x="123" y="-201"/>
</scene>
<!--Window Controller-->
<scene sceneID="R2V-B0-nI4">
<!--lokinet-->
<scene sceneID="KyO-ZK-znh">
<objects>
<windowController id="B8D-0N-5wS" sceneMemberID="viewController">
<window key="window" title="lokinet" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA" userLabel="lokinet">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="480" height="270"/>
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<toolbar key="toolbar" implicitIdentifier="15556861-12B2-41A2-94F3-EC481E1D2BCA" autosavesConfiguration="NO" displayMode="iconAndLabel" sizeMode="regular" id="m54-Gk-RXu">
<allowedToolbarItems>
<toolbarItem implicitItemIdentifier="NSToolbarFlexibleSpaceItem" id="YLq-u5-F2C"/>
<toolbarItem implicitItemIdentifier="D19F1556-B143-4AE3-8753-40097BAF14EB" label="Start" paletteLabel="Start Item" tag="-1" image="NSGoForwardTemplate" id="RL6-BS-Pmv">
<size key="minSize" width="4" height="15"/>
<size key="maxSize" width="4" height="15"/>
</toolbarItem>
<toolbarItem implicitItemIdentifier="59F36847-628F-4613-A690-9E9C3CA18DC3" label="Stop" paletteLabel="Stop Item" tag="-1" image="NSStopProgressTemplate" id="KxD-EA-GZ9">
<size key="minSize" width="11" height="11"/>
<size key="maxSize" width="11" height="11"/>
</toolbarItem>
</allowedToolbarItems>
<defaultToolbarItems>
<toolbarItem reference="RL6-BS-Pmv"/>
<toolbarItem reference="YLq-u5-F2C"/>
<toolbarItem reference="KxD-EA-GZ9"/>
</defaultToolbarItems>
</toolbar>
<connections>
<outlet property="delegate" destination="B8D-0N-5wS" id="98r-iN-zZc"/>
</connections>
</window>
<connections>
<segue destination="XfG-lQ-9wD" kind="relationship" relationship="window.shadowedContentViewController" id="cq2-FE-JQM"/>
</connections>
</windowController>
<customObject id="Oky-zY-oP4" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="250"/>
</scene>
<!--View Controller-->
<scene sceneID="hIz-AP-VOD">
<objects>
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModule="lokinet" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="m2S-Jp-Qdl">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<viewController title="lokinet" storyboardIdentifier="LokinetLogController" id="Bom-14-aTM" customClass="LokinetLogController" customModule="lokinet" sceneMemberID="viewController">
<view key="view" id="vMu-xc-OqT">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<scrollView fixedFrame="YES" borderType="none" autohidesScrollers="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" horizontalScrollElasticity="allowed" verticalScrollElasticity="allowed" translatesAutoresizingMaskIntoConstraints="NO" id="h6o-Zr-Gxr">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<scrollView fixedFrame="YES" borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" translatesAutoresizingMaskIntoConstraints="NO" id="pjx-TF-AEF">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<clipView key="contentView" ambiguous="YES" drawsBackground="NO" copiesOnScroll="NO" id="rbl-Lx-Aom">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<clipView key="contentView" ambiguous="YES" drawsBackground="NO" copiesOnScroll="NO" id="LWK-yZ-W61">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView identifier="logView" ambiguous="YES" editable="NO" importsGraphics="NO" verticallyResizable="YES" findStyle="bar" incrementalSearchingEnabled="YES" smartInsertDelete="YES" id="L9a-we-bXA" customClass="LokinetLog" customModule="lokinet">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<textView ambiguous="YES" editable="NO" importsGraphics="NO" richText="NO" verticallyResizable="YES" id="Iv9-j0-Ji1" customClass="LokinetLog" customModule="lokinet" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="textColor" name="systemGreenColor" catalog="System" colorSpace="catalog"/>
<color key="textColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="systemGrayColor" catalog="System" colorSpace="catalog"/>
<size key="minSize" width="480" height="270"/>
<size key="maxSize" width="797" height="10000000"/>
<size key="minSize" width="450" height="300"/>
<size key="maxSize" width="498" height="10000000"/>
<color key="insertionPointColor" name="textColor" catalog="System" colorSpace="catalog"/>
</textView>
</subviews>
<color key="backgroundColor" name="textColor" catalog="System" colorSpace="catalog"/>
</clipView>
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="3eM-L0-C72">
<rect key="frame" x="0.0" y="262" width="480" height="16"/>
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="2H0-hl-PSz">
<rect key="frame" x="-100" y="-100" width="240" height="16"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="9e5-C6-MIV">
<rect key="frame" x="464" y="0.0" width="16" height="270"/>
<scroller key="verticalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="0vF-TP-EGb">
<rect key="frame" x="434" y="0.0" width="16" height="300"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
</subviews>
</view>
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<customObject id="4KZ-DK-tjg" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
</scene>
<!--Preferences-->
<scene sceneID="DWQ-D6-m8l">
<objects>
<viewController title="Preferences" id="I5t-b2-LMF" customClass="PrefsViewController" customModule="lokinet" sceneMemberID="viewController">
<view key="view" id="KZz-Tr-Jig">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<box fixedFrame="YES" title="Options" translatesAutoresizingMaskIntoConstraints="NO" id="Mbe-YJ-q14">
<rect key="frame" x="-3" y="-4" width="456" height="304"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<view key="contentView" id="tbu-Pf-Pbq">
<rect key="frame" x="3" y="3" width="450" height="286"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<box verticalHuggingPriority="750" fixedFrame="YES" boxType="separator" translatesAutoresizingMaskIntoConstraints="NO" id="keI-0V-OFa">
<rect key="frame" x="0.0" y="226" width="450" height="5"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
</box>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="NuC-QX-tDB">
<rect key="frame" x="18" y="269" width="414" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Lokinet Path" id="J0z-ru-wAJ">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Faq-SR-HLK">
<rect key="frame" x="18" y="201" width="414" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Network Interface" id="EAM-Cf-Ujw">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<comboBox identifier="lokinetInterface" verticalHuggingPriority="750" fixedFrame="YES" tag="2" translatesAutoresizingMaskIntoConstraints="NO" id="S5U-xg-7pX">
<rect key="frame" x="20" y="169" width="413" height="26"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<comboBoxCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" numberOfVisibleItems="7" id="f1D-GI-o0g">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</comboBoxCell>
<connections>
<action selector="comboAction:" target="I5t-b2-LMF" id="Bji-8L-xCR"/>
</connections>
</comboBox>
<textField verticalHuggingPriority="750" fixedFrame="YES" tag="1" translatesAutoresizingMaskIntoConstraints="NO" id="esl-Vn-YX2">
<rect key="frame" x="20" y="239" width="410" height="22"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="WCQ-qF-r0o">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
</view>
</box>
</subviews>
</view>
<connections>
<outlet property="interfaceEntry" destination="S5U-xg-7pX" id="OaG-RT-28K"/>
<outlet property="pathEntry" destination="WCQ-qF-r0o" id="aVf-iB-WbW"/>
</connections>
</viewController>
<customObject id="ncN-3l-Dyn" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="655"/>
<point key="canvasLocation" x="845" y="227"/>
</scene>
</scenes>
<resources>
<image name="NSGoForwardTemplate" width="9" height="12"/>
<image name="NSStopProgressTemplate" width="11" height="11"/>
</resources>
</document>

@ -15,10 +15,10 @@ func split(str: String?) -> [String] {
class DNSManager {
static let netSetup = URL(fileURLWithPath: "/usr/sbin/networksetup")
let oldDNSSettings: [String]
let interface: String
var oldDNSSettings: [String] = []
static func getOldSettings(interface: String) -> [String] {
func getOldSettings() -> [String] {
let netprocess = Process()
netprocess.executableURL = DNSManager.netSetup
netprocess.arguments = ["-getdnsservers", interface]
@ -30,13 +30,20 @@ class DNSManager {
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let asStr = String(data: data, encoding: .ascii)
return split(str: asStr).filter({$0 != "127.0.0.1"})
if asStr?.contains("There aren't any DNS Servers") ?? true {
return []
} else {
return split(str: asStr).filter({$0 != "127.0.0.1"})
}
} catch {
return []
}
}
func setNewSettings() throws {
self.oldDNSSettings = getOldSettings()
print("Overriding DNS Settings of \(self.oldDNSSettings)")
let netprocess = Process()
netprocess.executableURL = DNSManager.netSetup
@ -50,11 +57,17 @@ class DNSManager {
netprocess.executableURL = DNSManager.netSetup
netprocess.arguments = ["-setdnsservers", self.interface]
netprocess.arguments?.append(contentsOf: oldDNSSettings)
if oldDNSSettings.isEmpty {
// networkmsetup uses "networksetup -setdnsservers <interface> Empty" to reset
netprocess.arguments?.append("Empty")
} else {
netprocess.arguments?.append(contentsOf: oldDNSSettings)
}
do {
try netprocess.run()
print("Overriding DNS Settings of \(self.oldDNSSettings)")
print("Resetting DNS Settings to \(self.oldDNSSettings)")
} catch {
// do nothing
}
@ -62,11 +75,5 @@ class DNSManager {
init(interface: String) {
self.interface = interface
self.oldDNSSettings = DNSManager.getOldSettings(interface: interface)
print("Overriding DNS Settings of \(self.oldDNSSettings)")
}
deinit {
restoreOldSettings()
}
}

@ -7,24 +7,29 @@
import AppKit
final class LokinetLog : NSTextView {
var runner: LokinetRunner?
override init(frame: NSRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
self.runner = LokinetRunner(window: self, interface: "Wi-Fi")
class LokinetLogController : NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
self.runner?.start()
var log: LokinetLog {
get {
// this is walking down the UI stack.
// TODO: work out a better way of doing this
let scroll = self.view.subviews[0] as! NSScrollView
let clip = scroll.subviews[0] as! NSClipView
let log = clip.subviews[0] as! LokinetLog
return log
}
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.runner = LokinetRunner(window: self, interface: "Wi-Fi")
}
self.runner?.start()
}
protocol Appendable {
func append(string: String)
}
final class LokinetLog : NSTextView, Appendable {
func append(string: String) {
self.textStorage?.append(NSAttributedString(string: string + "\n"))
self.scrollToEndOfDocument(nil)

@ -6,35 +6,18 @@
//
import Foundation
import Cocoa
class LokinetRunner {
static let PATH_KEY = "lokinetPath"
static let DEFAULT_PATH = URL(fileURLWithPath: "/usr/local/bin/lokinet")
var lokinetPath: URL?
var process = Process()
let dnsManager: DNSManager
weak var window: LokinetLog?
init(window: LokinetLog, interface: String) {
self.dnsManager = DNSManager(interface: interface)
self.window = window
configure()
}
let lokinetPath: URL
var process = Process()
func configure() {
let defaults = UserDefaults.standard;
var logAppender: Appendable? = nil
self.lokinetPath = defaults.url(forKey: LokinetRunner.PATH_KEY) ?? LokinetRunner.DEFAULT_PATH
defaults.set(self.lokinetPath, forKey: LokinetRunner.PATH_KEY)
}
func enableDNS() {
do {
try dnsManager.setNewSettings()
} catch {
self.window?.presentError(error)
}
init(interface: String, path: String) {
self.lokinetPath = URL(fileURLWithPath: path)
self.dnsManager = DNSManager(interface: interface)
}
func start() {
@ -45,32 +28,33 @@ class LokinetRunner {
process.standardError = outputPipe
do {
try self.dnsManager.setNewSettings()
try process.run()
} catch {
self.window?.presentError(error)
NSApp.presentError(error)
}
guard let reader = StreamReader(fh: outputPipe.fileHandleForReading) else {
let err = NSError(domain: "lokinet", code: 0, userInfo: ["msg": "Failed to read from filehandle"])
self.window?.presentError(err)
NSApp.presentError(err)
return
}
DispatchQueue.global(qos: .background).async {
for line in reader {
print(line)
DispatchQueue.main.async {
self.window?.append(string: line)
self.logAppender?.append(string: line)
}
}
}
enableDNS()
}
deinit {
func stop() {
if process.isRunning {
process.terminate()
process.waitUntilExit()
}
dnsManager.restoreOldSettings()
}
}

@ -1,22 +0,0 @@
//
// ViewController.swift
// lokinet
//
// Copyright © 2019 Loki. All rights reserved.
//
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
Loading…
Cancel
Save