Skip to content

Commit

Permalink
Merge pull request #4 from mehdiimrz/master
Browse files Browse the repository at this point in the history
Add http and grpc traffic monitor
  • Loading branch information
mehdiimrz authored Jul 12, 2021
2 parents 734263b + ace8b15 commit 294491c
Show file tree
Hide file tree
Showing 49 changed files with 2,471 additions and 22 deletions.
74 changes: 74 additions & 0 deletions Sources/NetShears/Colors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Colors.swift
// NetShears
//
// Created by Mehdi Mirzaie on 7/3/21.
//
import UIKit

struct Colors {

struct UI{
static public let wordsInEvidence = UIColor(hexString: "#dadfe1")
static public let wordFocus = UIColor(hexString: "#f7ca18")
}

struct Gray{
static public let darkestGray = UIColor(hexString: "#666666")
static public let darkerGray = UIColor(hexString: "#888888")
static public let darkGray = UIColor(hexString: "#999999")
static public let midGray = UIColor(hexString: "#BBBBBB")
static public let lightGray = UIColor(hexString: "#CCCCCC")
static public let lighestGray = UIColor(hexString: "#E7E7E7")
}

struct HTTPCode{
static public let Success = UIColor(hexString: "#297E4C") //2xx
static public let Redirect = UIColor(hexString: "#3D4140") //3xx
static public let ClientError = UIColor(hexString: "#D97853") //4xx
static public let ServerError = UIColor(hexString: "#D32C58") //5xx
static public let Generic = UIColor(hexString: "#999999") //Others
}
}

extension UIColor{

convenience init(hexString:String) {
let hexString:String = hexString.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) as String
let scanner = Scanner(string: hexString)
if (hexString.hasPrefix("#")) {
scanner.scanLocation = 1
}

var color:UInt32 = 0
scanner.scanHexInt32(&color)

let mask = 0x000000FF
let r = Int(color >> 16) & mask
let g = Int(color >> 8) & mask
let b = Int(color) & mask

let red = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
self.init(red:red, green:green, blue:blue, alpha:1)
}

func toHexString() -> String {
var r:CGFloat = 0
var g:CGFloat = 0
var b:CGFloat = 0
var a:CGFloat = 0
getRed(&r, green: &g, blue: &b, alpha: &a)
let rgb:Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0
return NSString(format:"#%06x", rgb) as String
}

func randomGreyColor() -> String{
let value = arc4random_uniform(255)
let grayscale = (value << 16) | (value << 8) | value;
let color = "#" + String(grayscale, radix: 16);

return color
}
}
12 changes: 12 additions & 0 deletions Sources/NetShears/Config.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Config.swift
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//

import Foundation

//Notifications
let fireWormholy = NSNotification.Name(rawValue: "wormholy_fire")
let newRequestNotification = NSNotification.Name(rawValue: "wormholy_new_request")
33 changes: 33 additions & 0 deletions Sources/NetShears/Extension/Date+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Date+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 7/4/21.
//

import Foundation

extension Date{
func stringWithFormat(dateFormat: String, timezone: TimeZone? = nil) -> String?{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
if timezone != nil{
dateFormatter.timeZone = timezone!
}
return dateFormatter.string(from: self)
}
}


extension Bundle {
static var NetShearsBundle: Bundle {
let podBundle = Bundle(for: NetShears.classForCoder())
if let bundleURL = podBundle.url(forResource: "NetShears", withExtension: "bundle"){
if let bundle = Bundle(url: bundleURL) {
return bundle
}
}

return Bundle(for: NetShears.classForCoder())
}
}
39 changes: 39 additions & 0 deletions Sources/NetShears/Extension/Double+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Double+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//

import Foundation

extension Double {
func formattedMilliseconds() -> String{

let rounded = self
if rounded < 1000 {
return "\(Int(rounded))ms"
} else if rounded < 1000 * 60 {
let seconds = rounded / 1000
return "\(Int(seconds))s"
} else if rounded < 1000 * 60 * 60 {
let secondsTemp = rounded / 1000
let minutes = secondsTemp / 60
let seconds = (rounded - minutes * 60 * 1000) / 1000
return "\(Int(minutes))m \(Int(seconds))s"
} else if self < 1000 * 60 * 60 * 24 {
let minutesTemp = rounded / 1000 / 60
let hours = minutesTemp / 60
let minutes = (rounded - hours * 60 * 60 * 1000) / 1000 / 60
let seconds = (rounded - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000
return "\(Int(hours))h \(Int(minutes))m \(Int(seconds))s"
} else {
let hoursTemp = rounded / 1000 / 60 / 60
let days = hoursTemp / 24
let hours = (rounded - days * 24 * 60 * 60 * 1000) / 1000 / 60 / 60
let minutes = (rounded - days * 24 * 60 * 60 * 1000 - hours * 60 * 60 * 1000) / 1000 / 60
let seconds = (rounded - days * 24 * 60 * 60 * 1000 - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000
return "\(Int(days))d \(Int(hours))h \(Int(minutes))m \(Int(seconds))s"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// NSMutableAttributedString+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//

import UIKit

extension NSMutableAttributedString {
@discardableResult func bold(_ text: String) -> NSMutableAttributedString {
let attrs: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 15)]
let boldString = NSMutableAttributedString(string:text, attributes: attrs)
append(boldString)
return self
}

@discardableResult func normal(_ text: String) -> NSMutableAttributedString {
let attrs: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 14)]
let normal = NSMutableAttributedString(string:text, attributes: attrs)
append(normal)
return self
}

func changeTextColor(to color: UIColor) -> NSMutableAttributedString {
self.addAttribute(NSAttributedString.Key.foregroundColor, value: color , range: NSRange(location: 0,length: string.count))
return self
}
}
12 changes: 12 additions & 0 deletions Sources/NetShears/Extension/NSNotification+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// NSNotification+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//

import Foundation

extension NSNotification.Name {
static let NewRequestNotification = NSNotification.Name(rawValue: "Name.NetShearsNewRequest")
}
24 changes: 24 additions & 0 deletions Sources/NetShears/Extension/String+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// String+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//

import Foundation

extension String {
//substrings of equal length
func characters(n: Int) -> String{
return String(prefix(n))
}

var prettyPrintedJSON: String? {
guard let stringData = self.data(using: .utf8),
let object = try? JSONSerialization.jsonObject(with: stringData, options: []),
let jsonData = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let formattedJSON = String(data: jsonData, encoding: .utf8) else { return nil }

return formattedJSON.replacingOccurrences(of: "\\/", with: "/")
}
}
26 changes: 26 additions & 0 deletions Sources/NetShears/Extension/UIApplication+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// UIApplication+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 7/1/21.
//

import UIKit

extension UIApplication {
// class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
// if let navigationController = controller as? UINavigationController {
// return topViewController(controller: navigationController.visibleViewController)
// }
// if let tabController = controller as? UITabBarController {
// if let selected = tabController.selectedViewController {
// return topViewController(controller: selected)
// }
// }
// if let presented = controller?.presentedViewController {
// return topViewController(controller: presented)
// }
// return controller
// }

}
14 changes: 14 additions & 0 deletions Sources/NetShears/Extension/UIStoryBoard+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// UIStoryBoard+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//

import UIKit

extension UIStoryboard {
static var NetShearsStoryBoard: UIStoryboard {
return UIStoryboard(name: "Flow", bundle: Bundle.NetShearsBundle)
}
}
23 changes: 23 additions & 0 deletions Sources/NetShears/Extension/UIView+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// UIView+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//

import UIKit

extension UIView {
@discardableResult
func fillInSuperview(top: CGFloat = 0, trailing: CGFloat = 0, bottom: CGFloat = 0, leading: CGFloat = 0) -> Bool {
guard let superview = superview else {
return false
}
translatesAutoresizingMaskIntoConstraints = false
topAnchor.constraint(equalTo: superview.topAnchor, constant: top).isActive = true
bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: -bottom).isActive = true
leftAnchor.constraint(equalTo: superview.leftAnchor, constant: leading).isActive = true
rightAnchor.constraint(equalTo: superview.rightAnchor, constant: -trailing).isActive = true
return true
}
}
35 changes: 35 additions & 0 deletions Sources/NetShears/Extension/UIViewController+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// UIViewController+Extension.swift
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//

import UIKit

extension UIViewController {

static func currentViewController(_ viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
guard let viewController = viewController else { return nil }

if let viewController = viewController as? UINavigationController {
if let viewController = viewController.visibleViewController {
return currentViewController(viewController)
} else {
return currentViewController(viewController.topViewController)
}
} else if let viewController = viewController as? UITabBarController {
if let viewControllers = viewController.viewControllers, viewControllers.count > 5, viewController.selectedIndex >= 4 {
return currentViewController(viewController.moreNavigationController)
} else {
return currentViewController(viewController.selectedViewController)
}
} else if let viewController = viewController.presentedViewController {
return currentViewController(viewController)
} else if viewController.children.count > 0 {
return viewController.children[0]
} else {
return viewController
}
}
}
2 changes: 1 addition & 1 deletion Sources/NetShears/Extension/URLRequest+Extension.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// URLRequest+Extension.swift
//
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// URLSessionConfiguration+Extension.swift
//
// NetShears
//
// Created by Mehdi Mirzaie on 6/9/21.
//
Expand All @@ -14,9 +14,10 @@ extension URLSessionConfiguration {
return []
}
var originalProtocolClasses = fakeProcotolClasses.filter {
return $0 != NetworkRequestSniffableUrlProtocol.self
return $0 != NetworkInterceptorUrlProtocol.self && $0 != NetworkLoggerUrlProtocol.self
}
originalProtocolClasses.insert(NetworkRequestSniffableUrlProtocol.self, at: 0)
originalProtocolClasses.insert(NetworkInterceptorUrlProtocol.self, at: 0)
originalProtocolClasses.insert(NetworkLoggerUrlProtocol.self, at: 0)
return originalProtocolClasses
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/NetShears/Helpers/URLRequestFactory.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// URLRequestFactory.swift
//
// NetShears
//
// Created by Mehdi Mirzaie on 6/4/21.
//
Expand Down
Loading

0 comments on commit 294491c

Please sign in to comment.