Skip to content
Merged
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions Plugins/IPOverview/Sources/IPOverviewService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ struct IPOverviewService: IPOverviewProviding {
if let info = IPOverviewParser.geoInfoFromIPAPI(data, ip: ip) {
return info
}
case .internetdata:
if let info = IPOverviewParser.geoInfoFromInternetData(data, ip: ip) {
return info
}
}
} catch {
continue
Expand Down Expand Up @@ -574,13 +578,16 @@ struct IPOverviewPublicIPSource: Sendable {
enum IPOverviewGeoSource: CaseIterable {
case ipwhois
case ipapi
case internetdata

func url(ip: String) -> URL {
switch self {
case .ipwhois:
return url(ip: ip, languageCode: IPOverviewLocale.geoLanguageCode())
case .ipapi:
return URL(string: "https://ipapi.co/\(ip)/json/")!
case .internetdata:
return URL(string: "https://api.internetdata.io/\(ip)")!
}
}

Expand All @@ -594,6 +601,8 @@ enum IPOverviewGeoSource: CaseIterable {
return components.url!
case .ipapi:
return URL(string: "https://ipapi.co/\(ip)/json/")!
case .internetdata:
return URL(string: "https://api.internetdata.io/\(ip)")!
}
}
}
Expand Down Expand Up @@ -767,4 +776,36 @@ enum IPOverviewParser {
source: "ipapi.co"
)
}

static func geoInfoFromInternetData(_ data: Data, ip: String) -> IPOverviewGeoInfo? {
guard let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let location = object["location"] as? [String: Any] else {
return nil
}

let asn = object["asn"] as? [String: Any]
let company = object["company"] as? [String: Any]
let organization = asn?["name"] as? String
let isp = company?["name"] as? String ?? organization
let isHosting = (asn?["type"] as? String).map { $0 == "hosting" }
return IPOverviewGeoInfo(
ip: ip,
country: location["country_name"] as? String,
countryCode: location["country"] as? String,
region: location["region"] as? String,
city: location["city"] as? String,
isp: isp,
organization: organization,
asn: (asn?["asn"] as? Int).map { "AS\($0)" },
timezone: location["timezone"] as? String,
networkType: IPOverviewNetworkType.infer(
organization: organization,
isp: isp,
isHosting: isHosting
),
isProxy: nil,
isHosting: isHosting,
source: "internetdata.io"
)
}
}
31 changes: 31 additions & 0 deletions Plugins/IPOverview/Tests/IPOverviewParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,37 @@ final class IPOverviewParserTests: XCTestCase {
XCTAssertEqual(info?.timezone, "America/Los_Angeles")
}

func testParsesInternetDataGeoPayload() {
let data = """
{
"ip": "8.8.8.8",
"location": {
"country": "US",
"country_name": "United States",
"timezone": "America/Los_Angeles"
},
"asn": {
"asn": 15169,
"name": "Google LLC",
"type": "hosting"
},
"company": {
"name": "Google LLC"
}
}
""".data(using: .utf8)!

let info = IPOverviewParser.geoInfoFromInternetData(data, ip: "8.8.8.8")

XCTAssertEqual(info?.locationText, "United States")
XCTAssertEqual(info?.countryCode, "US")
XCTAssertEqual(info?.asn, "AS15169")
XCTAssertEqual(info?.organization, "Google LLC")
XCTAssertEqual(info?.timezone, "America/Los_Angeles")
XCTAssertEqual(info?.isHosting, true)
XCTAssertEqual(info?.networkType, .datacenter)
}

func testParsesIPAPIGeoPayload() {
let data = """
{
Expand Down
1 change: 1 addition & 0 deletions Plugins/IPOverview/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
"api6.ipify.org",
"ipapi.co",
"ipwho.is",
"api.internetdata.io",
"res.wx.qq.com",
"www.google.com",
"www.cloudflare.com",
Expand Down
6 changes: 6 additions & 0 deletions changes/unreleased/ip-overview-internetdata-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
release: plugin
type: changed
---

IP Overview falls back to InternetData for location and network details when ipwho.is and ipapi.co are unavailable.
1 change: 1 addition & 0 deletions site/src/generated/plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -14091,6 +14091,7 @@
"api6.ipify.org",
"ipapi.co",
"ipwho.is",
"api.internetdata.io",
"res.wx.qq.com",
"www.google.com",
"www.cloudflare.com",
Expand Down
Loading