Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSystem.swift
More file actions
Latest commit
285 lines (233 loc) · 10.6 KB
/
Copy pathSystem.swift
File metadata and controls
285 lines (233 loc) · 10.6 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// Created by Jon on 9/9/17.
// Copyright © 2017 Jonathan Cardasis. All rights reserved.
//
// Uses Darwin's `sysctl` (and a few other C libraries) to retrieve system info
// Note: will need a bridging header to `#include <ifaddrs.h>`.
import Foundation
#if os(OSX)
import CoreWLAN
#elseif os(iOS) || os(tvOS)
import SystemConfiguration.CaptiveNetwork
#endif
classSystem{
enumDarwinError:Error{
case unknown
case malformedUTF8
case invalidSize
case posixError(POSIXErrorCode)
}
/// Uses sysctl to return a byte array of data for given keys to retrieve system configurations
fileprivatestaticfunc dataForKeys(_ keys:[Int32])throws->[Int8]{
returntry keys.withUnsafeBufferPointer{ keysPointer throws->[Int8]in
// Interface with sysctl to first retrieve the size of the byte array
varrequiredSize:Int=0
guardDarwin.sysctl(UnsafeMutablePointer<Int32>(mutating: keysPointer.baseAddress),UInt32(keys.count),nil,&requiredSize,nil,0)==0else{
throwPOSIXErrorCode(rawValue: errno).map{DarwinError.posixError($0)}??DarwinError.unknown
}
// Retrieve data now that the appropriate size is known
letdata=[Int8](repeating:0, count: requiredSize)
letresultCode= data.withUnsafeBufferPointer(){ dataPointer ->Int32in
returnDarwin.sysctl(UnsafeMutablePointer<Int32>(mutating: keysPointer.baseAddress),UInt32(keys.count),UnsafeMutableRawPointer(mutating: dataPointer.baseAddress),&requiredSize,nil,0)
}
guard resultCode ==0else{
throwPOSIXErrorCode(rawValue: errno).map{DarwinError.posixError($0)}??DarwinError.unknown
}
return data
}
}
/// Returns a string representation of sysctl for given keys
fileprivatestaticfunc stringForKeys(_ keys:[Int32])throws->String{
letutf8DataString=trydataForKeys(keys).withUnsafeBufferPointer(){ dataPointer ->String?in
dataPointer.baseAddress.flatMap{String(validatingUTF8: $0)}
}
guardlet string = utf8DataString else{
throwDarwinError.malformedUTF8
}
return string
}
/// Convert a string like "machdep.cpu.brand_string" to an array of integer keys reference values
fileprivatestaticfunc keysForName(_ name:String)throws->[Int32]{
varkeysBufferSize=Int(CTL_MAXNAME)
varkeysBuffer=[Int32](repeating:0, count: keysBufferSize)
try keysBuffer.withUnsafeMutableBufferPointer(){(bytePointer:inoutUnsafeMutableBufferPointer<Int32>)throwsin
try name.withCString(){(cString:UnsafePointer<Int8>)throwsin
guardsysctlnametomib(cString, bytePointer.baseAddress,&keysBufferSize)==0else{
throwPOSIXErrorCode(rawValue: errno).map{DarwinError.posixError($0)}??DarwinError.unknown
}
}
}
// truncate range if needed
if keysBuffer.count > keysBufferSize {
keysBuffer.removeSubrange(keysBufferSize..<keysBuffer.count)
}
return keysBuffer
}
/// Will rebind data as the memory type provided. Throws errors if memory type expected is invalid
fileprivatestaticfunc interpretDataAsType<T>(_ data:[Int8], type:T.Type)throws->T{
if data.count != MemoryLayout<T>.size {
throwDarwinError.invalidSize
}
returntry data.withUnsafeBufferPointer(){ bufferPointer throws->Tin
guardlet baseAddress = bufferPointer.baseAddress else{throwDarwinError.unknown }
return baseAddress.withMemoryRebound(to:T.self, capacity:1){ $0.pointee }
}
}
}
/// System + General
extensionSystem{
/// Hostname of the machine
/// Retrived from: (System Preferences -> Sharing -> Computer Name) or (Settings -> General -> About -> Name)
/// ex. "MacBookPro.local" or "Jon-iPhone"
staticvarhostName:String{
returntry!stringForKeys([CTL_KERN, KERN_HOSTNAME])
}
/// OS type
/// ex. "Darwin"
staticvarosType:String{
returntry!System.stringForKeys([CTL_KERN, KERN_OSVERSION])
}
/// Model of the machine
/// ex. "MacBookPro11,2" or "iPhone7,1"
staticvarmodel:String{
// Due to a known inverse key match since beginning of iOS, the model and machine keys are flipped for iOS vs macOS
#if os(iOS) && !arch(x86_64) && !arch(i386)
returntry!stringForKeys([CTL_HW, HW_MACHINE])
#else
returntry!stringForKeys([CTL_HW, HW_MODEL])
#endif
}
}
/// System + CPU
extensionSystem{
classCPU{
/// CPU model info
/// ex. "x86_64" or "N71mAP"
staticvarmodel:String{
// Due to a known inverse key match since beginning of iOS, the model and machine keys are flipped for iOS vs macOS
#if os(iOS) && !arch(x86_64) && !arch(i386)
returntry!System.stringForKeys([CTL_HW, HW_MODEL])
#else
returntry!System.stringForKeys([CTL_HW, HW_MACHINE])
#endif
}
/// Number of available (physical and virtual) cpus for the system
staticvaravailableCPUs:Int32{
letsysData=try!System.dataForKeys([CTL_HW, HW_AVAILCPU])
returntry!System.interpretDataAsType(sysData, type:Int32.self)
}
// MARK: - macOS Specific Properties
/// Brand of the cpu
/// ex. "Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz"
#if os(macOS) // MACHDEP is only defined for mac architecture
staticvarbrand:String{
letkeys=try!System.keysForName("machdep.cpu.brand_string")
returntry!System.stringForKeys(keys)
}
/// Vendor of the cpu
/// ex. "GenuineIntel"
staticvarvendor:String{
letkeys=try!System.keysForName("machdep.cpu.vendor")
returntry!System.stringForKeys(keys)
}
/// Number of physical machine cores
staticvarcores:Int32{
letkeys=try!System.keysForName("machdep.cpu.core_count")
letsysData=try!System.dataForKeys(keys)
returntry!System.interpretDataAsType(sysData, type:Int32.self)
}
#endif
/// Whether the processor is 64 bit
staticvaris64Bit:Bool{
// size of Int guaranteed to be same as platform word size (Swift)
return MemoryLayout<Int>.size == MemoryLayout<Int64>.size
}
}
}
/// System + Network
extensionSystem{
classNetwork{
privatestructBroadcomWifiInterfaces{
staticletstandardWifi="en0" //Standard Wifi Interface
staticlettethering="ap1" //Access point interface used for Wifi tethering
staticlettetheringConnected="bridge100" //Interface for communicating to connected device via tether (Seems like the interface is always bridge100)
staticletawdl="awdl0" //Apple Wireless Direct Link interface - used for AirDrop, GameKit, AirPlay, etc.
}
/// The networks SSID
/// Is nil if no network ssid is found. Such as when wifi is disabled
staticvarnetworkName:String?{
#if os(iOS) || os(tvOS)
guardlet interfaceNames =CNCopySupportedInterfaces()as?[String]else{
returnnil
}
return interfaceNames.flatMap({ name in
guardlet info =CNCopyCurrentNetworkInfo(name asCFString)as?[String:AnyObject]else{
returnnil
}
guardlet ssid =info[kCNNetworkInfoKeySSID asString]as?Stringelse{
returnnil
}
return ssid
}).first
#elseif os(macOS)
returnCWWiFiClient()?.interface(withName:nil)?.ssid()
#endif
}
#if os(iOS)
#endif
/// If the device has Wifi turned on
staticfunc wifiEnabled()->Bool{
ifSystem.activeNetworkInterfaces().filter({ $0 ==BroadcomWifiInterfaces.awdl }).count >1{
//If more than 1 awdl10 interface, then wifi is turned on
returntrue
}
returnfalse
}
/// If the device is connected to a Wifi network
staticfunc wifiConnected()->Bool{
ifSystem.activeNetworkInterfaces().filter({ $0 ==BroadcomWifiInterfaces.standardWifi }).count >1{
//If more than 1 en0 interface then its connected
returntrue
}
returnfalse
}
#if os(iOS)
/// If the device is currently tethering its connection to another device
staticfunc isTethering()->Bool{
ifSystem.activeNetworkInterfaces().filter({ $0.range(of:BroadcomWifiInterfaces.tetheringConnected)!=nil}).count >0{
//If theres a bridge100 interface, then theres at least one connected device
returntrue
}
returnfalse
}
#endif
}
fileprivatestaticfunc activeNetworkInterfaces()->[String]{
varinterfaces=[String]()
varifaddr:UnsafeMutablePointer<ifaddrs>?
guardgetifaddrs(&ifaddr)==0else{return[]} //obtain network interfaces
guardlet firstAddr = ifaddr else{return[]}
forpointerinsequence(first: firstAddr, next:{ $0.pointee.ifa_next }){
letinterface= pointer.pointee asifaddrs
if(Int32(interface.ifa_flags)& IFF_UP)== IFF_UP {
letinterfaceName=String(cString: interface.ifa_name)
interfaces.append(interfaceName)
}
}
freeifaddrs(ifaddr)
return interfaces
}
}
/// System + RAM (macOS only)
extensionSystem{
#if os(macOS)
classRAM{
/// Size (in gb) of system ram available
staticvarramSize:UInt32{
letsysData=try!System.dataForKeys([CTL_HW, HW_MEMSIZE])
letmemInBytes=try!System.interpretDataAsType(sysData, type:UInt64.self)
returnUInt32(memInBytes /1024/1024/1024) // result in GB
}
}
#endif
}