mirror of
https://github.com/passepartoutvpn/passepartout-apple.git
synced 2025-02-02 22:12:23 +00:00
1942b82ebb
Simplify development and maintenance immensely by making this a monorepository: - Convert PassepartoutKit and VPN bindings to local packages - OpenVPN/OpenSSL - WireGuard/Go - Make PassepartoutKit available via - Source submodule for production (private) - [Binary XCFramework for development](https://github.com/passepartoutvpn/passepartoutkit) - Add PassepartoutKit Demo in root - Deploy package later
101 lines
2.7 KiB
Swift
101 lines
2.7 KiB
Swift
//
|
|
// Utils+Network.swift
|
|
// Passepartout
|
|
//
|
|
// Created by Davide De Rosa on 2/26/22.
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
//
|
|
// 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
|
|
#if os(iOS)
|
|
import NetworkExtension
|
|
#elseif os(macOS)
|
|
import CoreWLAN
|
|
#endif
|
|
|
|
extension Utils {
|
|
#if targetEnvironment(simulator)
|
|
public static func hasCellularData() -> Bool {
|
|
true
|
|
}
|
|
#else
|
|
// TODO: ###, move this check to kit, and try something similar for Ethernet
|
|
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
|
|
}
|
|
#endif
|
|
|
|
public static func hasEthernet() -> Bool {
|
|
true
|
|
}
|
|
|
|
public static func currentWifiSSID() async -> String? {
|
|
#if targetEnvironment(simulator)
|
|
["My Home Network", "Safe Wi-Fi", "Friend's House"].randomElement()
|
|
#elseif os(iOS)
|
|
await NEHotspotNetwork.fetchCurrent()?.ssid
|
|
#else
|
|
nil
|
|
#endif
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
return groups
|
|
.map { $0.description }
|
|
.joined(separator: ".")
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|