Skip to content

Commit

Permalink
- Added port scanning feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Ville Välimaa committed Mar 6, 2017
1 parent 5ee94e6 commit 40d1100
Show file tree
Hide file tree
Showing 30 changed files with 2,731 additions and 1,497 deletions.
4 changes: 4 additions & 0 deletions LANScan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
129CDFFF43C0A916BF010F46 /* Pods_LANScanTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E5DF4CDEFB2558015119594 /* Pods_LANScanTests.framework */; };
32056FFE1E6ADA730046ADDD /* PortScanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32056FFD1E6ADA730046ADDD /* PortScanner.swift */; };
322C6F271E3A2A1C00F08F0D /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 324F2BB81E36983800CAC273 /* Constants.swift */; };
322C6F291E3A2A2600F08F0D /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 324F2BBA1E36983800CAC273 /* Utils.swift */; };
322C6F2B1E3A783300F08F0D /* LANScanUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 322C6F2A1E3A783300F08F0D /* LANScanUITests.swift */; };
Expand Down Expand Up @@ -54,6 +55,7 @@
/* Begin PBXFileReference section */
1C144A5528EDCB8CF69FE560 /* Pods_LANScan.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LANScan.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2F395F1AF363BFA58C05E4DA /* Pods-LANScanTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LANScanTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-LANScanTests/Pods-LANScanTests.debug.xcconfig"; sourceTree = "<group>"; };
32056FFD1E6ADA730046ADDD /* PortScanner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PortScanner.swift; sourceTree = "<group>"; };
322C6F2A1E3A783300F08F0D /* LANScanUITests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LANScanUITests.swift; sourceTree = "<group>"; };
32350FBE1E36936200AEB1C1 /* LANScan.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LANScan.app; sourceTree = BUILT_PRODUCTS_DIR; };
32350FC11E36936200AEB1C1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -273,6 +275,7 @@
isa = PBXGroup;
children = (
324F2BDA1E36993400CAC273 /* LANScanner.swift */,
32056FFD1E6ADA730046ADDD /* PortScanner.swift */,
324F2BDB1E36993400CAC273 /* APIManager.swift */,
324F2BD91E36991A00CAC273 /* HostnameResolver */,
324F2BD51E3698F600CAC273 /* MacFinder */,
Expand Down Expand Up @@ -510,6 +513,7 @@
buildActionMask = 2147483647;
files = (
324F2BCA1E3698B000CAC273 /* Reachability.m in Sources */,
32056FFE1E6ADA730046ADDD /* PortScanner.swift in Sources */,
324F2BDD1E36993400CAC273 /* APIManager.swift in Sources */,
32350FE31E3694A200AEB1C1 /* MainViewController.swift in Sources */,
324F2BDC1E36993400CAC273 /* LANScanner.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions LANScan/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
struct Constants {
static let PING_HOST_NOT_FOUND = "PING_HOST_NOT_FOUND"
static let NO_WIFI_ADDRESS = "NO_WIFI_ADDRESS"
static let NO_SERVICE = "NO_SERVICE"

// UI Text
static let NOTIFICATION_NO_WIFI_TITLE = "No WiFi"
Expand Down
20 changes: 8 additions & 12 deletions LANScan/DeviceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ class DeviceViewController: UIViewController {
self.view.addSubview(self.portScanTerminal!)

self.portScanTerminal?.insertText("\n")
self.portScanTerminal?.insertText("Starting Nmap 7.40 ( https://nmap.org ) at 2017-02-26 11:27 EET\n")
self.portScanTerminal?.insertText("Nmap scan report for 192.168.1.1\n")
self.portScanTerminal?.insertText("Host is up (0.0093s latency).\n")
self.portScanTerminal?.insertText("Not shown: 995 closed ports\n")
self.portScanTerminal?.insertText("PORT STATE SERVICE\n")
self.portScanTerminal?.insertText("53/tcp open domain\n")
self.portScanTerminal?.insertText("80/tcp open http\n")
self.portScanTerminal?.insertText("515/tcp open printer\n")
self.portScanTerminal?.insertText("9100/tcp open jetdirect\n")
self.portScanTerminal?.insertText("9998/tcp open distinct32\n")
self.portScanTerminal?.insertText("\n")
self.portScanTerminal?.insertText("Nmap done: 1 IP address (1 host up) scanned in 0.20 seconds\n")
self.portScanTerminal?.insertText("Scanning for services in \(host?.ipAddress ?? "UNKNOWN")\n")

if let uHost = self.host {
PortScanner.scanPortsOn(host: uHost).subscribeNext { (services:Any?) in
let printable = services as? [String] ?? []
printable.forEach { self.portScanTerminal?.insertText("SERVICE FOUND ON PORT: \($0)\n") }
}
}

self.makeConstraints()
}
Expand Down
50 changes: 50 additions & 0 deletions LANScan/PortScanner.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// PortScanner.swift
// LANScan
//
// Created by Ville Välimaa on 04/03/2017.
// Copyright © 2017 Ville Välimaa. All rights reserved.
//

import Foundation
import SwiftSocket
import ReactiveCocoa

class PortScanner {

static func scanPortsOn(host:Host) -> RACSignal {
guard let ip = host.ipAddress else { return RACSignal() }
return RACSignal.createSignal({ (subscriber: RACSubscriber?) -> RACDisposable? in
DispatchQueue.global(qos: .background).async {
(Array(1...100)
.map { signalForPort(port: $0, ipAddress: ip) } as NSArray
|> RACSignal.combineLatest)
.deliverOnMainThread()
.subscribeNext({ (services:Any?) in
let finalServices = ((services as! RACTuple).allObjects() as! Array<String>)
.filter { $0 != Constants.NO_SERVICE }
subscriber!.sendNext(finalServices)
subscriber?.sendCompleted()
})
}
return RACDisposable(block: {
})
})
}

private static func signalForPort(port:Int32, ipAddress:String) -> RACSignal {
return RACSignal.createSignal({ (subscriber: RACSubscriber?) -> RACDisposable? in
let client = TCPClient(address: ipAddress, port: port)
switch client.connect(timeout: 1) {
case .success:
subscriber?.sendNext(String(port))
subscriber?.sendCompleted()
case .failure:
subscriber?.sendNext(Constants.NO_SERVICE)
subscriber?.sendCompleted()
}
return RACDisposable(block: {
})
})
}
}
2 changes: 1 addition & 1 deletion LANScan/ScanViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ScanViewController: UIViewController, ScanViewDelegate, TopViewDelegate {
}

func didSelectTargetDevice(index: Int) {
let deviceVC:DeviceViewController = DeviceViewController()
let deviceVC:DeviceViewController = DeviceViewController(host: hosts[index])
deviceVC.modalTransitionStyle = .flipHorizontal
self.present(deviceVC, animated: true, completion: nil)
}
Expand Down
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target 'LANScan' do
pod 'Alamofire', '4.3.0'
pod 'SnapKit', '3.1.2'
pod 'SpinKit', '~> 1.2'
pod 'SwiftSocket', '2.0.1'

target 'LANScanTests' do
inherit! :search_paths
Expand Down
5 changes: 4 additions & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ PODS:
- ReactiveCocoa/Core
- SnapKit (3.1.2)
- SpinKit (1.2.0)
- SwiftSocket (2.0.1)

DEPENDENCIES:
- Alamofire (= 4.3.0)
- ReactiveCocoa (= 2.5)
- SnapKit (= 3.1.2)
- SpinKit (~> 1.2)
- SwiftSocket (= 2.0.1)

SPEC CHECKSUMS:
Alamofire: 856a113053a7bc9cbe5d6367a555d773fc5cfef7
ReactiveCocoa: e2db045570aa97c695e7aa97c2bcab222ae51f4a
SnapKit: 12b24f569cb7c143acc9c22b9d91b23e7b1c84a2
SpinKit: 65906a8b534a4e14cbfd3f8b0b6c329131faef4a
SwiftSocket: 2d814afdc97c89217982f86fd036c1874134a63f

PODFILE CHECKSUM: c2713fcc7f920dbbb8e5bffb09465bc1f06531e5
PODFILE CHECKSUM: 6e3d4c6fbf3570284ee27917699c2b8f3b0a238b

COCOAPODS: 1.1.1
5 changes: 4 additions & 1 deletion Pods/Manifest.lock

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

Loading

0 comments on commit 40d1100

Please sign in to comment.