Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions Source/AppleFetcher/Implement/NetworkFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,41 @@ public final class NetworkFetcher: Fetcher {
public init() {}

public func fetch() -> Network {
let ip = fetchIP()
let ipList = fetchIPList()
let type = fetchType()

return Network(
ip: ip,
ip: ipList,
isCellular: type == .cellular,
isWiFi: type == .wifi
)
}
}

public struct InterfaceType: Hashable {
let name: String
let family: sa_family_t
}
Comment on lines +32 to +35
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

InterfaceType 속성의 접근 제어자가 누락되었습니다.

public struct이지만 namefamily 속성에 접근 제어자가 없어 기본값인 internal이 적용됩니다. 외부 모듈에서 IP.interfaceType.name이나 IP.interfaceType.family에 접근할 수 없게 됩니다.

🔧 접근 제어자 추가 제안
 public struct InterfaceType: Hashable {
-    let name: String
-    let family: sa_family_t
+    public let name: String
+    public let family: sa_family_t
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public struct InterfaceType: Hashable {
let name: String
let family: sa_family_t
}
public struct InterfaceType: Hashable {
public let name: String
public let family: sa_family_t
}
🤖 Prompt for AI Agents
In `@Source/AppleFetcher/Implement/NetworkFetcher.swift` around lines 32 - 35, The
InterfaceType struct is declared public but its properties name and family are
implicitly internal, preventing external access (e.g., IP.interfaceType.name);
update the property access control by making both properties public so they are
accessible from other modules—modify InterfaceType to declare public let name:
String and public let family: sa_family_t while keeping the struct public.


public struct IP {
public let ip: String
public let interfaceType: InterfaceType
}

public struct Network {
public let ip: String?
public let ip: [IP]
public let isCellular: Bool
public let isWiFi: Bool
}

extension NetworkFetcher {
private func fetchIP() -> String? {
struct InterfaceType: Hashable {
let name: String
let family: sa_family_t
}
private func fetchIPList() -> [IP] {

var ipStorage: [InterfaceType: String] = [:]

var interfacesPointer: UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&interfacesPointer) == 0 else { return nil }
guard let interfaceFirstPointer = interfacesPointer else { return nil }
guard getifaddrs(&interfacesPointer) == 0 else { return [] }
guard let interfaceFirstPointer = interfacesPointer else { return [] }

for interfacePointer in sequence(
first: interfaceFirstPointer,
Expand Down Expand Up @@ -86,13 +92,11 @@ extension NetworkFetcher {
InterfaceType(name: "utun0", family: sa_family_t(AF_INET6))
]

for search in searchs {
if let ip = ipStorage[search] {
return ip
return ipStorage
.filter { searchs.contains($0.key) }
.map { key, value in
IP(ip: value, interfaceType: key)
}
Comment on lines +95 to 99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

반환되는 IP 배열의 순서가 보장되지 않습니다.

Dictionary의 iteration 순서는 Swift에서 보장되지 않습니다. searchs 배열이 우선순위 순서(en0 → pdp_ip0 → utun0)를 정의하고 있지만, 현재 구현은 이 순서를 보존하지 않습니다.

🔧 순서 보장을 위한 수정 제안
-        return ipStorage
-            .filter { searchs.contains($0.key) }
-            .map { key, value in
-                IP(ip: value, interfaceType: key)
-            }
+        return searchs.compactMap { interfaceType in
+            guard let ip = ipStorage[interfaceType] else { return nil }
+            return IP(ip: ip, interfaceType: interfaceType)
+        }
🤖 Prompt for AI Agents
In `@Source/AppleFetcher/Implement/NetworkFetcher.swift` around lines 95 - 99, The
returned IP array uses ipStorage.filter which iterates a Dictionary and thus
loses the priority order defined by searchs; change the logic to iterate searchs
in order and for each key lookup ipStorage[key], building the result with
searchs.map/compactMap so the resulting [IP] preserves the desired priority
(e.g., en0 → pdp_ip0 → utun0); update the code that constructs IP(ip: value,
interfaceType: key) to be produced from the ordered lookup instead of filtering
the dictionary.

}

return nil
}

private func fetchType() -> NetworkType {
Expand Down