-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Contributing to NetShears # | ||
|
||
- If you **found a bug**, open an issue. | ||
- If you **have a feature request**, open an issue. | ||
- If you **want to contribute**, submit a pull request. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Mehdi Mirzaie | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// swift-tools-version:5.3 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "NetShears", | ||
products: [ | ||
// Products define the executables and libraries a package produces, and make them visible to other packages. | ||
.library( | ||
name: "NetShears", | ||
targets: ["NetShears"]), | ||
], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "NetShears", | ||
dependencies: []), | ||
.testTarget( | ||
name: "NetShearsTests", | ||
dependencies: ["NetShears"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,71 @@ | ||
![Logo](./logo.png) | ||
|
||
# NetShears | ||
|
||
NetShears is a Network interceptor framework written in Swift. | ||
|
||
NetShears adds a Request interceptor mechanisms to be able to modify the HTTPRequest 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 | ||
|
||
- [x] Intercept HTTP/HTTPS request header | ||
- [x] Intercept HTTP/HTTPS request endpoint | ||
- [ ] Intercept HTTP/HTTPS response body | ||
- [ ] View traffic logs | ||
- [ ] Block HTTP requets | ||
|
||
|
||
## How to use | ||
Start NetShears by calling ```startRecording()``` in didFinishLaunchingWithOptions | ||
|
||
```swift | ||
import NetShears | ||
|
||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { | ||
|
||
NetShears.startRecording() | ||
|
||
} | ||
``` | ||
Header Modification: | ||
|
||
```swift | ||
let header = HeaderModifyModel(key: "API-Version", value: "123") | ||
let headerModifier = RequestEvaluatorModifierHeader(header: header) | ||
NetShears.shared.modify(modifier: headerModifier) | ||
``` | ||
|
||
Endpoint Modification: | ||
|
||
```swift | ||
let endpoint = RedirectedRequestModel(originalUrl: "/register", redirectUrl: "/login") | ||
let endpointModifier = RequestEvaluatorModifierEndpoint(redirectedRequest: endpoint) | ||
NetShears.shared.modify(modifier: endpointModifier) | ||
``` | ||
|
||
## Installation | ||
NetShears can be used via the [Swift Package Manager](https://swift.org/package-manager/). | ||
Just add it to the dependencies in your Package.swift file: | ||
|
||
```Swift | ||
let package = Package( | ||
name: "MyPackage", | ||
dependencies: [ | ||
... | ||
.package(url: "https://github.com/divar-ir/NetShears.git", from: "1.0.0"), | ||
], | ||
... | ||
) | ||
``` | ||
|
||
|
||
## Contributing | ||
Please see our [Contributing Guide](./CONTRIBUTING.md). | ||
|
||
## Inspiration | ||
|
||
* [depoon/NetworkInterceptor](https://github.com/depoon/NetworkInterceptor) | ||
|
||
## License | ||
[MIT](https://choosealicense.com/licenses/mit/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// URLRequest+Extension.swift | ||
// | ||
// | ||
// Created by Mehdi Mirzaie on 6/9/21. | ||
// | ||
|
||
import Foundation | ||
|
||
extension URLRequest { | ||
public func getHttpBodyStreamData() -> Data? { | ||
guard let httpBodyStream = self.httpBodyStream else { | ||
return nil | ||
} | ||
let data = NSMutableData() | ||
var buffer = [UInt8](repeating: 0, count: 4096) | ||
|
||
httpBodyStream.open() | ||
while httpBodyStream.hasBytesAvailable { | ||
let length = httpBodyStream.read(&buffer, maxLength: 4096) | ||
if length == 0 { | ||
break | ||
} else { | ||
data.append(&buffer, length: length) | ||
} | ||
} | ||
return data as Data | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Sources/NetShears/Extension/URLSessionConfiguration+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// URLSessionConfiguration+Extension.swift | ||
// | ||
// | ||
// Created by Mehdi Mirzaie on 6/9/21. | ||
// | ||
|
||
import Foundation | ||
|
||
extension URLSessionConfiguration { | ||
|
||
@objc func fakeProcotolClasses() -> [AnyClass]? { | ||
guard let fakeProcotolClasses = self.fakeProcotolClasses() else { | ||
return [] | ||
} | ||
var originalProtocolClasses = fakeProcotolClasses.filter { | ||
return $0 != NetworkRequestSniffableUrlProtocol.self | ||
} | ||
originalProtocolClasses.insert(NetworkRequestSniffableUrlProtocol.self, at: 0) | ||
return originalProtocolClasses | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// URLRequestFactory.swift | ||
// | ||
// | ||
// Created by Mehdi Mirzaie on 6/4/21. | ||
// | ||
|
||
import Foundation | ||
|
||
extension URLRequest { | ||
mutating func modifyURLRequestEndpoint(redirectUrl: RedirectedRequestModel) { | ||
var urlString = "\(url!.absoluteString)" | ||
urlString = urlString.replacingOccurrences(of: redirectUrl.originalUrl, with: redirectUrl.redirectUrl) | ||
url = URL(string: urlString)! | ||
} | ||
|
||
mutating func modifyURLRequestHeader(header: HeaderModifyModel) { | ||
setValue(header.value, forHTTPHeaderField: header.key) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// NetShears.swift | ||
// | ||
// | ||
// Created by Mehdi Mirzaie on 6/4/21. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
public final class NetShears: NSObject { | ||
|
||
public static let shared = NetShears() | ||
let networkRequestInterceptor = NetworkRequestInterceptor() | ||
var config: NetworkInterceptorConfig = NetworkInterceptorConfig(modifiers: []) | ||
|
||
|
||
public func startRecording(){ | ||
self.networkRequestInterceptor.startRecording() | ||
} | ||
|
||
public func stopRecording(){ | ||
self.networkRequestInterceptor.stopRecording() | ||
} | ||
|
||
public func modify(modifier: RequestEvaluatorModifier) { | ||
config.addModifier(modifier: modifier) | ||
} | ||
|
||
public func modifiedList() -> [RequestEvaluatorModifier] { | ||
return config.modifiers | ||
} | ||
|
||
public func removeModifier(at index: Int){ | ||
return config.removeModifier(at: index) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// NetworkInterceptor.swift | ||
// | ||
// | ||
// Created by Mehdi Mirzaie on 6/4/21. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
@objc public class NetworkInterceptor: NSObject { | ||
|
||
@objc public static let shared = NetworkInterceptor() | ||
let networkRequestInterceptor = NetworkRequestInterceptor() | ||
|
||
public func startRecording(){ | ||
self.networkRequestInterceptor.startRecording() | ||
} | ||
|
||
public func stopRecording(){ | ||
self.networkRequestInterceptor.stopRecording() | ||
} | ||
|
||
public func shouldRequestModify(urlRequest: URLRequest) -> Bool { | ||
for modifer in NetShears.shared.config.modifiers { | ||
if modifer.isActionAllowed(urlRequest: urlRequest) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// NetworkInterceptorConfig.swift | ||
// | ||
// | ||
// Created by Mehdi Mirzaie on 6/4/21. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct RedirectedRequestModel: Equatable { | ||
public let originalUrl: String | ||
public let redirectUrl: String | ||
|
||
public init (originalUrl: String, redirectUrl: String) { | ||
self.originalUrl = originalUrl | ||
self.redirectUrl = redirectUrl | ||
} | ||
} | ||
|
||
public struct HeaderModifyModel: Equatable { | ||
public let key: String | ||
public let value: String | ||
|
||
public init (key: String, value: String) { | ||
self.key = key | ||
self.value = value | ||
} | ||
} | ||
|
||
public final class NetworkInterceptorConfig { | ||
var modifiers: [RequestEvaluatorModifier] = [] | ||
|
||
init(modifiers: [RequestEvaluatorModifier] = []) { | ||
self.modifiers = modifiers | ||
} | ||
|
||
func addModifier(modifier: RequestEvaluatorModifier) { | ||
self.modifiers.append(modifier) | ||
} | ||
|
||
func getModifiers() -> [RequestEvaluatorModifier] { | ||
return self.modifiers | ||
} | ||
|
||
func removeModifier(at index: Int) { | ||
guard index <= modifiers.count - 1 else { return } | ||
modifiers.remove(at: index) | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// NetworkRequestInterceptor.swift | ||
// | ||
// | ||
// Created by Mehdi Mirzaie on 6/4/21. | ||
// | ||
import Foundation | ||
|
||
|
||
@objc public class NetworkRequestInterceptor: NSObject{ | ||
|
||
func swizzleProtocolClasses(){ | ||
let instance = URLSessionConfiguration.default | ||
let uRLSessionConfigurationClass: AnyClass = object_getClass(instance)! | ||
|
||
let method1: Method = class_getInstanceMethod(uRLSessionConfigurationClass, #selector(getter: uRLSessionConfigurationClass.protocolClasses))! | ||
let method2: Method = class_getInstanceMethod(URLSessionConfiguration.self, #selector(URLSessionConfiguration.fakeProcotolClasses))! | ||
|
||
method_exchangeImplementations(method1, method2) | ||
} | ||
|
||
public func startRecording() { | ||
URLProtocol.registerClass(NetworkRequestSniffableUrlProtocol.self) | ||
swizzleProtocolClasses() | ||
} | ||
|
||
public func stopRecording() { | ||
URLProtocol.unregisterClass(NetworkRequestSniffableUrlProtocol.self) | ||
swizzleProtocolClasses() | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.