2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// Utils+Network.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/26/22.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2024-09-23 13:02:26 +00:00
|
|
|
#if os(iOS)
|
2022-05-15 20:16:27 +00:00
|
|
|
import NetworkExtension
|
2024-09-23 13:02:26 +00:00
|
|
|
#elseif os(macOS)
|
2022-05-17 09:32:19 +00:00
|
|
|
import CoreWLAN
|
|
|
|
#endif
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
extension Utils {
|
2024-09-23 13:02:26 +00:00
|
|
|
#if targetEnvironment(simulator)
|
2022-04-12 13:09:14 +00:00
|
|
|
public static func hasCellularData() -> Bool {
|
2022-09-04 18:09:31 +00:00
|
|
|
true
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
#else
|
2022-04-12 13:09:14 +00:00
|
|
|
public static func hasCellularData() -> Bool {
|
|
|
|
var addrs: UnsafeMutablePointer<ifaddrs>?
|
|
|
|
guard getifaddrs(&addrs) == 0 else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var isFound = false
|
|
|
|
var cursor = addrs?.pointee
|
|
|
|
while let ifa = cursor {
|
|
|
|
let name = String(cString: ifa.ifa_name)
|
|
|
|
if name == "pdp_ip0" {
|
|
|
|
isFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cursor = ifa.ifa_next?.pointee
|
|
|
|
}
|
|
|
|
freeifaddrs(addrs)
|
|
|
|
return isFound
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
#endif
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-05-15 22:45:26 +00:00
|
|
|
public static func hasEthernet() -> Bool {
|
2024-09-23 13:02:26 +00:00
|
|
|
#if os(macOS)
|
2022-05-15 22:45:26 +00:00
|
|
|
true
|
2024-09-23 13:02:26 +00:00
|
|
|
#else
|
2022-05-15 22:45:26 +00:00
|
|
|
false
|
2024-09-23 13:02:26 +00:00
|
|
|
#endif
|
2022-05-15 22:45:26 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-15 20:16:27 +00:00
|
|
|
public static func currentWifiSSID() async -> String? {
|
2024-09-23 13:02:26 +00:00
|
|
|
#if targetEnvironment(simulator)
|
2022-05-15 20:16:27 +00:00
|
|
|
["My Home Network", "Safe Wi-Fi", "Friend's House"].randomElement()
|
2024-09-23 13:02:26 +00:00
|
|
|
#elseif os(iOS)
|
2022-05-15 20:16:27 +00:00
|
|
|
await withCheckedContinuation { continuation in
|
|
|
|
NEHotspotNetwork.fetchCurrent {
|
|
|
|
guard let network = $0 else {
|
|
|
|
continuation.resume(with: .success(nil))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continuation.resume(with: .success(network.ssid))
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
#elseif os(macOS)
|
2022-05-17 09:32:19 +00:00
|
|
|
CWWiFiClient.shared().interface()?.ssid()
|
2024-09-23 13:02:26 +00:00
|
|
|
#else
|
2023-12-16 19:58:54 +00:00
|
|
|
nil
|
2024-09-23 13:02:26 +00:00
|
|
|
#endif
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Utils {
|
|
|
|
public static func string(fromIPv4 ipv4: UInt32) -> String {
|
|
|
|
var remainder = ipv4
|
|
|
|
var groups: [UInt32] = []
|
|
|
|
var base: UInt32 = 1 << 24
|
|
|
|
while base > 0 {
|
|
|
|
groups.append(remainder / base)
|
|
|
|
remainder %= base
|
|
|
|
base >>= 8
|
|
|
|
}
|
2023-05-28 09:57:35 +00:00
|
|
|
return groups
|
|
|
|
.map { $0.description }
|
|
|
|
.joined(separator: ".")
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static func ipv4(fromString string: String) -> UInt32? {
|
|
|
|
var addr = in_addr()
|
|
|
|
let result = string.withCString {
|
|
|
|
inet_pton(AF_INET, $0, &addr)
|
|
|
|
}
|
|
|
|
guard result > 0 else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return CFSwapInt32BigToHost(addr.s_addr)
|
|
|
|
}
|
|
|
|
}
|