Swiftpack.co -  divar-ir/NetShears as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
divar-ir/NetShears
iOS Network monitor/interceptor framework written in Swift
.package(url: "https://github.com/divar-ir/NetShears.git", from: "v2.0.2")

Logo

NetShears

NetShears is a Network interceptor framework written in Swift.

NetShears adds a Request interceptor mechanisms to be able to modify the HTTP/HTTPS Request before being sent . This mechanism can be used to implement authentication policies, add headers to a request , add log trace or even redirect requests.

Features

  • ☑ Intercept HTTP/HTTPS request header
  • ☑ Intercept HTTP/HTTPS request endpoint
  • ☑ View traffic logs
  • ☐ Intercept HTTP/HTTPS response body
  • ☐ Block HTTP requets

How it works

NetShears working by swizzling the URLProtocol.

How to use

Start NetShears by calling startRecording() in didFinishLaunchingWithOptions

import NetShears

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {

	NetShears.startRecording()

}

Interceptor

Header Modification:

let header = HeaderModifyModel(key: "API-Version", value: "123")
let headerModifier = RequestEvaluatorModifierHeader(header: header)
NetShears.shared.modify(modifier: headerModifier)

Endpoint Modification:

let endpoint = RedirectedRequestModel(originalUrl: "/register", redirectUrl: "/login")
let endpointModifier = RequestEvaluatorModifierEndpoint(redirectedRequest: endpoint)
NetShears.shared.modify(modifier: endpointModifier)

Traffic Monitoring

In order to show network traffics in your app simply call presentNetworkMonitor method and then a view will present containing traffic logs.

NetShears.shared.presentNetworkMonitor()

Icon

gRPC

You can view gRPC calls by constructing the Request and Response from GRPC models:

public func addGRPC(url: String,
                        host: String,
                        requestObject: Data?,
                        responseObject: Data?,
                        success: Bool,
                        statusCode: Int,
                        statusMessage: 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

Create a Package.swift file.

// swift-tools-version:5.0

import PackageDescription

let 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

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
    pod 'NetShears'
end

Replace YOUR_TARGET_NAME and then, in the Podfile directory, type:

$ pod install

Contributing

Please see our Contributing Guide.

Inspiration

License

MIT

GitHub

link
Stars: 11
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

v2.0.2
2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API