Building a network monitoring app isn’t a new idea. In fact, there are a lot of network monitoring/intercepting tools available, from the open source libpcap, to commercial solutions from firms like NetStumbler or CoreStumbler. All of these tools have a significant drawback: they rely on a command line interface to get the job done. The problem with a CLI is that it is very hard to maintain, and you have to install it on every computer that you need to monitor network traffic.
There are many things that make iOS great for productive development. From the Outline tab, to Pinned Tabs, to Split View and more. However, one of the most useful and most overlooked features of iOS is the networking functionality and APIs.
The only thing I know about this project is that it is a tool to intercept and monitor packet data on a network. I don’t know a lot about the tool itself, it’s purpose or even if it already exists. Anyways, this is a hypothetical project that I’m thinking of creating as a developer. I’m not really sure where I would want to use it. I guess it could be a tool to monitor Wifi traffic, maybe as a debugging tool for an app.
NetShears
NetShears is a Swift-based network interceptor framework.
NetShears has a Request interceptor method for modifying HTTP/HTTPS requests before they are delivered. This method may be used to add headers to a request, add a log trail, or even redirect requests.
Features
- [x] Intercept request headers for HTTP/HTTPS
- [x] Intercept endpoints for HTTP/HTTPS requests
- [x] Examine the traffic records
- [ ] Intercept the content of an HTTP/HTTPS response
- [ ] Disallow HTTP requests
How does it work?
The URLProtocol is swizzled by NetShears.
How to Make Use of
In did, use startRecording() to start NetShears. FinishLaunchingWithOptions
import NetShears func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { NetShears.startRecording() }
Interceptor
Changes to the header:
if header = HeaderModifyModel; if header = HeaderModifyModel; if header (key: “API-Version”, value: “123”) requestEvaluatorModifierHeader(header: header) NetShears.shared.modify let headerModifier = RequestEvaluatorModifierHeader(header: header) (headerModifier is a modifier)
Modification of the endpoint:
if endpoint = RedirectedRequestModel(originalUrl: “/register”, redirectUrl: “/login”), then NetShears.shared.modify endpointModifier = RequestEvaluatorModifierEndpoint(redirectedRequest: endpoint) (endpointModifier) (modifier) (modifier) (modifier) (modifier) (modifier
To display network traffic in your app, just use the presentNetworkMonitor function, and a view with traffic logs will appear.
NetShears.shared.presentNetworkMonitor()
gRPC
By building the Request and Response using GRPC models, you can see gRPC calls:
addGRPC(url: String, host: String, request: String) is a public function. Object: Is it data?, or is it a response? success: Bool, statusCode: Int, status: Int, status: Int, status: Int, status: Int, status: Int, status: Int, status: In HPACKHeadersRequest: [String: String]?, HPACKHeadersResponse: [String: String]?) Message: String?, duration: Double?, HPACKHeadersRequest: [String: String]?, HPACKHeadersResponse: [String: String]?)
Example
// Your GRPC services that is generated from SwiftGRPC private let client = NoteServiceServiceClient.init(address: “127.0.0.1:12345”, secure: false) func insertNote(note: Note, completion: @escaping(Note?, CallResult?) -> Void) { _ = try? client.insert(note, completion: { (createdNote, result) in NetShears.shared.addGRPC(url: “https://test.com/grpc”, requestObject: try? note.jsonUTF8Data(), responseObject: try? createdNote.jsonUTF8Data(), success: result.success, statusCode: result.statusCode.rawValue, statusMessage: result.statusMessage) }) }
Installation
Swift Package Manager is a program that allows you to manage your
Make a file called Package.swift.
import swift-tools-version:5.0 package = Package( name: “YourProject”, dependencies: [.package(url: “https://github.com/divar-ir/NetShears.git”, from: “1.0.0”), ], targets: [.target(name: “YourProject”, dependencies: [“NetShears”]) ] ) $ swift build
CocoaPods
use frameworks is a podfile! do pod ‘NetShears’ end target ‘YOUR TARGET NAME’
Replace YOUR TARGET NAME with YOUR TARGET NAME, and then type: in the Podfile directory:
$ install pod
GitHub
https://github.com/divar-ir/NetShears
Ever wanted to see what a user’s iPhone or iPad is doing behind the scenes on a network that uses a VPN? Well, it is possible, but not without some effort. For some time now I have had a solution that I have been using to extract the name of the VPN being used and the network interface being used.. Read more about continuously check internet connection swift and let us know what you think.
Related Tags
- willpresentnotification not called in foreground
- nwpathmonitor not working
- nwpathmonitor swift
- swift reachability ios 13
- how to check internet connection in ios programmatically