-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetTool.swift
122 lines (112 loc) · 4.62 KB
/
NetTool.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// NetTool.swift
// iOSWork
//
// Created by Stan Hu on 2024/1/4.
//
import UIKit
import PhoneNetSDK
class NetTool {
fileprivate var traceroute: PNUdpTraceroute?
func getLocalDNSAddress()->[String]{
return BaseBridge().getDnsAddress()
}
func getLocalIpAddress(url:String)->String?{
guard let u = URL(string: url) else { return nil }
if u.host == nil{
return nil
}
let host = CFHostCreateWithName(nil, u.host! as CFString).takeRetainedValue()
CFHostStartInfoResolution(host, .addresses, nil)
var success :DarwinBoolean = false
if let addressArray = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray?,let address = addressArray.firstObject as? NSData{
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
if getnameinfo(address.bytes.assumingMemoryBound(to: sockaddr.self), socklen_t(address.length),
&hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
let numAddress = String(cString: hostname)
return numAddress
}
}
return nil
}
func getDomainIpAddress(url: String) async -> Result<[DomainInfo],Error>{
await withCheckedContinuation { con in
guard let u = URL(string: url) else {
return con.resume(returning: .failure(NSError(domain: "url不合法", code: -1)))
}
guard let host = u.host else {
return con.resume(returning: .failure(NSError(domain: "url没有host", code: -1)))
}
PhoneNetManager.shareInstance().netLookupDomain(host) { arr, err in
if let err = err{
con.resume(returning: .failure(err.error))
} else if let arr = arr,arr.count > 0,let domains = arr as? [DomainLookUpRes] {
var ips = [DomainInfo]()
for item in domains{
let d = DomainInfo(name: item.name,ip: item.ip)
ips.append(d)
}
con.resume(returning: .success(ips))
} else {
con.resume(returning: .failure(NSError(domain: "PhoneNetManager没返回正确的数据", code: -1)))
}
}
}
}
func checkPing(url:String,callback: @escaping NetPingResultHandler, lossCallback: @escaping (_ count: Int,_ error:Error?) -> Void) {
guard let u = URL(string: url) else {
lossCallback(0,NSError(domain: "url不合法", code: -1))
return
}
guard let host = u.host else {
lossCallback(0,NSError(domain: "url没有host", code: -1))
return
}
if PhoneNetManager.shareInstance().isDoingPing() {
PhoneNetManager.shareInstance().netStopPing()
}
if self.traceroute?.isDoingUdpTraceroute() ?? false {
self.traceroute?.stop()
}
let pingRegular = "[\\s\\S]*?packets transmitted , loss:(\\d*?) , delay:[\\s\\S]*?"
PhoneNetManager.shareInstance().netStartPing(host, packetCount: 10) { (res) in
DispatchQueue.main.async {
if let r = res,let result = RegexTool.init(pingRegular).fetch(input: r),result.count > 0{
let count = result.compactMap { s in
return Int(s)
}.first ?? 0
lossCallback(count / 10, nil)
print(result)
}
callback(res)
}
}
}
func traceRoute(url:String,callback: @escaping PNUdpTracerouteHandler, completeCallback: @escaping (_ msg: String?,_ err:Error?) -> Void){
guard let u = URL(string: url) else {
completeCallback(nil,NSError(domain: "url不合法", code: -1))
return
}
guard let host = u.host else {
completeCallback(nil,NSError(domain: "url没有host", code: -1))
return
}
if self.traceroute?.isDoingUdpTraceroute() ?? false {
self.traceroute?.stop()
}
let traceRegular = "udp traceroute complete"
self.traceroute = PNUdpTraceroute.start(host) { (res) in
DispatchQueue.main.async {
callback(res)
if let r = res,let result = RegexTool.init(traceRegular).matchResult(input: r as String),result.count > 0{
completeCallback(r as String,nil)
}
}
}
}
}
struct DomainInfo{
var name = ""
var ip = ""
var url = ""
}