Removed submodules, and doing stuff.
This commit is contained in:
parent
f90617555a
commit
3173807987
|
@ -1,3 +0,0 @@
|
||||||
[submodule "Sources/libmaxminddb"]
|
|
||||||
path = Sources/libmaxminddb
|
|
||||||
url = https://github.com/maxmind/libmaxminddb
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
//
|
||||||
|
// Created by Rocska Ádám on 2020. 04. 28..
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
protocol City {
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
//
|
||||||
|
// Created by Rocska Ádám on 2020. 04. 28..
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct Metadata {
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
//
|
||||||
|
// Created by Rocska Ádám on 2020. 04. 28..
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct MetadataStruct {
|
||||||
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct Continent: ContinentProtocol {
|
|
||||||
let code: String?
|
|
||||||
let names: [String: String]?
|
|
||||||
}
|
|
||||||
|
|
||||||
public protocol ContinentProtocol {
|
|
||||||
var code: String? { get }
|
|
||||||
var names: [String: String]? { get }
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct Country: CountryProtocol {
|
|
||||||
let continent: ContinentProtocol?
|
|
||||||
let isoCode: String?
|
|
||||||
let names: [String: String]?
|
|
||||||
|
|
||||||
init(dictionary: NSDictionary) {
|
|
||||||
if let dict = dictionary["continent"] as? NSDictionary,
|
|
||||||
let code = dict["code"] as? String,
|
|
||||||
let continentNames = dict["names"] as? [String: String] {
|
|
||||||
continent = Continent(code: code, names: continentNames)
|
|
||||||
} else {
|
|
||||||
continent = nil
|
|
||||||
}
|
|
||||||
if let dict = dictionary["country"] as? NSDictionary,
|
|
||||||
let iso = dict["iso_code"] as? String,
|
|
||||||
let countryNames = dict["names"] as? [String: String] {
|
|
||||||
isoCode = iso
|
|
||||||
names = countryNames
|
|
||||||
} else {
|
|
||||||
isoCode = nil
|
|
||||||
names = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public protocol CountryProtocol {
|
|
||||||
var continent: ContinentProtocol? { get }
|
|
||||||
var isoCode: String? { get }
|
|
||||||
var names: [String: String]? { get }
|
|
||||||
}
|
|
|
@ -1,150 +0,0 @@
|
||||||
import Foundation
|
|
||||||
import libmaxminddb_helper
|
|
||||||
import libmaxminddb
|
|
||||||
|
|
||||||
public class GeoIP2 {
|
|
||||||
|
|
||||||
fileprivate var db = MMDB_s()
|
|
||||||
|
|
||||||
fileprivate typealias ListPtr = UnsafeMutablePointer<MMDB_entry_data_list_s>
|
|
||||||
fileprivate typealias StringPtr = UnsafeMutablePointer<String>
|
|
||||||
|
|
||||||
public init?(_ filename: String) {
|
|
||||||
if openDB(atPath: filename) { return }
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
private func openDB(atPath: String) -> Bool {
|
|
||||||
let cfilename = (atPath as NSString).utf8String
|
|
||||||
let cfilenamePtr = UnsafePointer<Int8>(cfilename)
|
|
||||||
let status = MMDB_open(cfilenamePtr, UInt32(MMDB_MODE_MASK), &db)
|
|
||||||
if status != MMDB_SUCCESS {
|
|
||||||
print(String(cString: MMDB_strerror(errno)))
|
|
||||||
return false
|
|
||||||
} else {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fileprivate func lookupString(_ s: String) -> MMDB_lookup_result_s? {
|
|
||||||
let string = (s as NSString).utf8String
|
|
||||||
let stringPtr = UnsafePointer<Int8>(string)
|
|
||||||
|
|
||||||
var gaiError: Int32 = 0
|
|
||||||
var error: Int32 = 0
|
|
||||||
|
|
||||||
let result = MMDB_lookup_string(&db, stringPtr, &gaiError, &error)
|
|
||||||
if gaiError == noErr && error == noErr {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
fileprivate func getString(_ list: ListPtr) -> String {
|
|
||||||
var data = list.pointee.entry_data
|
|
||||||
let type = (Int32)(data.type)
|
|
||||||
|
|
||||||
// Ignore other useless keys
|
|
||||||
guard data.has_data && type == MMDB_DATA_TYPE_UTF8_STRING else {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
let str = MMDB_get_entry_data_char(&data)
|
|
||||||
let size = size_t(data.data_size)
|
|
||||||
let cKey = mmdb_strndup(str, size)
|
|
||||||
let key = String(cString: cKey!)
|
|
||||||
free(cKey)
|
|
||||||
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
|
|
||||||
fileprivate func getType(_ list: ListPtr) -> Int32 {
|
|
||||||
let data = list.pointee.entry_data
|
|
||||||
return (Int32)(data.type)
|
|
||||||
}
|
|
||||||
|
|
||||||
fileprivate func getSize(_ list: ListPtr) -> UInt32 {
|
|
||||||
return list.pointee.entry_data.data_size
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public func lookup(_ IPString: String) -> CountryProtocol? {
|
|
||||||
guard let dict = lookup(ip: IPString) else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
let country = Country(dictionary: dict)
|
|
||||||
|
|
||||||
return country
|
|
||||||
}
|
|
||||||
|
|
||||||
private func dump(list: ListPtr?) -> (ptr: ListPtr?, out: Any?) {
|
|
||||||
var list = list
|
|
||||||
switch getType(list!) {
|
|
||||||
|
|
||||||
case MMDB_DATA_TYPE_MAP:
|
|
||||||
let dict = NSMutableDictionary()
|
|
||||||
var size = getSize(list!)
|
|
||||||
|
|
||||||
list = list?.pointee.next
|
|
||||||
while size > 0 && list != nil {
|
|
||||||
let key = getString(list!)
|
|
||||||
list = list?.pointee.next
|
|
||||||
let sub = dump(list: list)
|
|
||||||
list = sub.ptr
|
|
||||||
if let out = sub.out, key.count > 0 {
|
|
||||||
dict[key] = out
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
size -= 1
|
|
||||||
}
|
|
||||||
return (ptr: list, out: dict)
|
|
||||||
|
|
||||||
case MMDB_DATA_TYPE_UTF8_STRING:
|
|
||||||
let str = getString(list!)
|
|
||||||
list = list?.pointee.next
|
|
||||||
return (ptr: list, out: str)
|
|
||||||
|
|
||||||
case MMDB_DATA_TYPE_UINT32:
|
|
||||||
var res: NSNumber = 0
|
|
||||||
if let entryData = list?.pointee.entry_data {
|
|
||||||
var mutableEntryData = entryData
|
|
||||||
if let uint = MMDB_get_entry_data_uint32(&mutableEntryData) {
|
|
||||||
let v: UInt32 = uint.pointee
|
|
||||||
res = NSNumber(value: v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
list = list?.pointee.next
|
|
||||||
return (ptr: list, out: res)
|
|
||||||
|
|
||||||
default: ()
|
|
||||||
|
|
||||||
}
|
|
||||||
return (ptr: list, out: nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
public func lookup(ip: String) -> NSDictionary? {
|
|
||||||
guard let result = lookupString(ip) else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var entry = result.entry
|
|
||||||
var list: ListPtr?
|
|
||||||
let status = MMDB_get_entry_data_list(&entry, &list)
|
|
||||||
if status != MMDB_SUCCESS {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
let res = self.dump(list: list)
|
|
||||||
if let dict = res.out, let d = dict as? NSDictionary {
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
deinit {
|
|
||||||
MMDB_close(&db)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit cc7b880263813fbb09b3e62b8b7f9e6e4444eb28
|
|
|
@ -1,24 +0,0 @@
|
||||||
# libmaxminddb_config
|
|
||||||
|
|
||||||
## Why this module exists?
|
|
||||||
|
|
||||||
[libmaxminddb](https://github.com/maxmind/libmaxminddb) is build using
|
|
||||||
[automake](https://www.gnu.org/software/automake/).
|
|
||||||
The tool
|
|
||||||
[autoreconf](https://www.gnu.org/software/autoconf/manual/autoconf-2.68/html_node/autoreconf-Invocation.html)
|
|
||||||
ran as `autoreconf -fiv` will generate the
|
|
||||||
[configure script](https://developer.gnome.org/anjuta-build-tutorial/stable/create-autotools.html.en)
|
|
||||||
necessary for building libmaxminddb, which will also produce a configuration
|
|
||||||
header file called `maxminddb_config.h`.
|
|
||||||
|
|
||||||
In order for us to keep the library decoupled, as a git submodule, we need a way
|
|
||||||
to either build the project via Swift Package Manager, either inject a
|
|
||||||
configuration header file. At the time of writing **Swift Package Manager** is
|
|
||||||
not able to solve this problem, so we need to opt in to some automated injection.
|
|
||||||
|
|
||||||
This is where this clumsy module comes into play.
|
|
||||||
|
|
||||||
## Redundant definitions in header & c file
|
|
||||||
|
|
||||||
I know.
|
|
||||||
Sadly there was no way around via Swift Package Manager at the time of writing.
|
|
|
@ -1,15 +0,0 @@
|
||||||
/* include/maxminddb_config.h. Generated from maxminddb_config.h.in by configure. */
|
|
||||||
#ifndef MAXMINDDB_CONFIG_H
|
|
||||||
#define MAXMINDDB_CONFIG_H
|
|
||||||
|
|
||||||
#ifndef MMDB_UINT128_USING_MODE
|
|
||||||
/* Define as 1 if we we use unsigned int __atribute__ ((__mode__(TI))) for uint128 values */
|
|
||||||
#define MMDB_UINT128_USING_MODE 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef MMDB_UINT128_IS_BYTE_ARRAY
|
|
||||||
/* Define as 1 if we don't have an unsigned __int128 type */
|
|
||||||
#define MMDB_UINT128_IS_BYTE_ARRAY 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* MAXMINDDB_CONFIG_H */
|
|
|
@ -1,15 +0,0 @@
|
||||||
/* include/maxminddb_config.h. Generated from maxminddb_config.h.in by configure. */
|
|
||||||
#ifndef MAXMINDDB_CONFIG_H
|
|
||||||
#define MAXMINDDB_CONFIG_H
|
|
||||||
|
|
||||||
#ifndef MMDB_UINT128_USING_MODE
|
|
||||||
/* Define as 1 if we we use unsigned int __atribute__ ((__mode__(TI))) for uint128 values */
|
|
||||||
#define MMDB_UINT128_USING_MODE 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef MMDB_UINT128_IS_BYTE_ARRAY
|
|
||||||
/* Define as 1 if we don't have an unsigned __int128 type */
|
|
||||||
#define MMDB_UINT128_IS_BYTE_ARRAY 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* MAXMINDDB_CONFIG_H */
|
|
|
@ -1,45 +0,0 @@
|
||||||
# libmaxminddb_helper
|
|
||||||
|
|
||||||
## Why this module exists?
|
|
||||||
|
|
||||||
**There are 2 reasons :**
|
|
||||||
* maxminddb_typecast.c & h
|
|
||||||
* maxminddb-compat-util.h
|
|
||||||
|
|
||||||
## maxminddb_typecast.c
|
|
||||||
|
|
||||||
Since [libmaxminddb](https://github.com/maxmind/libmaxminddb) operates with all
|
|
||||||
sorts of pointer tricks, it's not too comfortable to acquire results from the C
|
|
||||||
API in Swift. So we need a simple solution to represent these values in a more
|
|
||||||
digestible way.
|
|
||||||
|
|
||||||
Kudos once again go to [lexrus](https://github.com/lexrus)'s
|
|
||||||
[MMDB-Swift](https://github.com/lexrus/MMDB-Swift) where this whole repository
|
|
||||||
started from. Original sources :
|
|
||||||
* [maxminddb_unions.c](https://github.com/lexrus/MMDB-Swift/blob/639b0d811694a27eab6cc6834a968888f666972d/Sources/libmaxminddb/maxminddb_unions.c)
|
|
||||||
* [maxminddb_unions.h](https://github.com/lexrus/MMDB-Swift/blob/639b0d811694a27eab6cc6834a968888f666972d/Sources/libmaxminddb/maxminddb_unions.h)
|
|
||||||
|
|
||||||
## maxminddb-compat-util.h
|
|
||||||
|
|
||||||
Again, another case of "I have no idea what I'm doing 🙂". Sorry.
|
|
||||||
So, [libmaxminddb](https://github.com/maxmind/libmaxminddb) depends on a few
|
|
||||||
functions which are included in its source, however Swift Package Manager for
|
|
||||||
whatever reason couldn't resolve it. I assume it's because the header file is in
|
|
||||||
the `src` directory, and so far I had the impression, that Swift Package Manager
|
|
||||||
(quite rightfully) prefers to keep things separated.
|
|
||||||
|
|
||||||
Anyho', it's here, and it compiles. There's another catch though. Why do I have
|
|
||||||
only a header file and in the include dir. Another case of
|
|
||||||
**I have no idea what I'm doing**. When I separated the definitions from the
|
|
||||||
source in seperate C and header files, compilation failed with only one function
|
|
||||||
definition being undefined (which isn't undefined imho...)
|
|
||||||
|
|
||||||
```
|
|
||||||
Undefined symbols for architecture x86_64:
|
|
||||||
"_mmdb_strndup", referenced from:
|
|
||||||
_$s6GeoIP2AAC9getString024_01864207E763388F378F6E4G7DD973A2LLySSSpySo22MMDB_entry_data_list_sVGF in GeoIP2.swift.o
|
|
||||||
ld: symbol(s) not found for architecture x86_64
|
|
||||||
```
|
|
||||||
|
|
||||||
In its current state it helps [libmaxminddb](https://github.com/maxmind/libmaxminddb)
|
|
||||||
compile.
|
|
|
@ -1,167 +0,0 @@
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
/* *INDENT-OFF* */
|
|
||||||
|
|
||||||
/* The memmem, strdup, and strndup functions were all copied from the
|
|
||||||
* FreeBSD source, along with the relevant copyright notice.
|
|
||||||
*
|
|
||||||
* It'd be nicer to simply use the functions available on the system if they
|
|
||||||
* exist, but there doesn't seem to be a good way to detect them without also
|
|
||||||
* defining things like _GNU_SOURCE, which we want to avoid, because then we
|
|
||||||
* end up _accidentally_ using GNU features without noticing, which then
|
|
||||||
* breaks on systems like OSX.
|
|
||||||
*
|
|
||||||
* C is fun! */
|
|
||||||
|
|
||||||
/* Applies to memmem implementation */
|
|
||||||
/*-
|
|
||||||
* Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. The name of the author may not be used to endorse or promote
|
|
||||||
* products derived from this software without specific prior written
|
|
||||||
* permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
static void *
|
|
||||||
mmdb_memmem(const void *l, size_t l_len, const void *s, size_t s_len)
|
|
||||||
{
|
|
||||||
register char *cur, *last;
|
|
||||||
const char *cl = (const char *)l;
|
|
||||||
const char *cs = (const char *)s;
|
|
||||||
|
|
||||||
/* we need something to compare */
|
|
||||||
if (l_len == 0 || s_len == 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
/* "s" must be smaller or equal to "l" */
|
|
||||||
if (l_len < s_len)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
/* special case where s_len == 1 */
|
|
||||||
if (s_len == 1)
|
|
||||||
return memchr(l, (int)*cs, l_len);
|
|
||||||
|
|
||||||
/* the last position where its possible to find "s" in "l" */
|
|
||||||
last = (char *)cl + l_len - s_len;
|
|
||||||
|
|
||||||
for (cur = (char *)cl; cur <= last; cur++)
|
|
||||||
if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
|
|
||||||
return cur;
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Applies to strnlen implementation */
|
|
||||||
/*-
|
|
||||||
* Copyright (c) 2009 David Schultz <das@FreeBSD.org>
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
static size_t
|
|
||||||
mmdb_strnlen(const char *s, size_t maxlen)
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
for (len = 0; len < maxlen; len++, s++) {
|
|
||||||
if (!*s)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return (len);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Applies to strdup and strndup implementation */
|
|
||||||
/*
|
|
||||||
* Copyright (c) 1988, 1993
|
|
||||||
* The Regents of the University of California. All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. Neither the name of the University nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
static char *
|
|
||||||
mmdb_strdup(const char *str)
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
char *copy;
|
|
||||||
|
|
||||||
len = strlen(str) + 1;
|
|
||||||
if ((copy = malloc(len)) == NULL)
|
|
||||||
return (NULL);
|
|
||||||
memcpy(copy, str, len);
|
|
||||||
return (copy);
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
mmdb_strndup(const char *str, size_t n)
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
char *copy;
|
|
||||||
|
|
||||||
len = mmdb_strnlen(str, n);
|
|
||||||
if ((copy = malloc(len + 1)) == NULL)
|
|
||||||
return (NULL);
|
|
||||||
memcpy(copy, str, len);
|
|
||||||
copy[len] = '\0';
|
|
||||||
return (copy);
|
|
||||||
}
|
|
||||||
/* *INDENT-ON* */
|
|
|
@ -1,14 +0,0 @@
|
||||||
#ifndef GEOIP2_SWIFT_MAXMINDDB_TYPECAST_H
|
|
||||||
#define GEOIP2_SWIFT_MAXMINDDB_TYPECAST_H
|
|
||||||
|
|
||||||
#include <maxminddb.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
const char *MMDB_get_entry_data_char(MMDB_entry_data_s *ptr);
|
|
||||||
|
|
||||||
uint32_t *MMDB_get_entry_data_uint32(MMDB_entry_data_s *ptr);
|
|
||||||
|
|
||||||
bool MMDB_get_entry_data_bool(MMDB_entry_data_s *ptr);
|
|
||||||
|
|
||||||
#endif //GEOIP2_SWIFT_MAXMINDDB_TYPECAST_H
|
|
|
@ -1,13 +0,0 @@
|
||||||
#include <maxminddb_typecast.h>
|
|
||||||
|
|
||||||
const char *MMDB_get_entry_data_char(MMDB_entry_data_s *ptr) {
|
|
||||||
return ptr->utf8_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t *MMDB_get_entry_data_uint32(MMDB_entry_data_s *ptr) {
|
|
||||||
return &ptr->uint32;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MMDB_get_entry_data_bool(MMDB_entry_data_s *ptr) {
|
|
||||||
return ptr->boolean;
|
|
||||||
}
|
|
|
@ -1,71 +0,0 @@
|
||||||
import Foundation
|
|
||||||
import XCTest
|
|
||||||
import GeoIP2
|
|
||||||
|
|
||||||
class GeoIP2Tests: XCTestCase {
|
|
||||||
|
|
||||||
lazy var database: GeoIP2 = {
|
|
||||||
guard let countryFilePath = bundle.path(
|
|
||||||
forResource: "GeoLite2-Country_20200421/GeoLite2-Country",
|
|
||||||
ofType: "mmdb"
|
|
||||||
) else {
|
|
||||||
fatalError("GeoLite2 Country DB file was not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let geoIp2 = GeoIP2(countryFilePath) else {
|
|
||||||
fatalError("GeoIP2 database couldn't initialize.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return geoIp2
|
|
||||||
}()
|
|
||||||
|
|
||||||
func testExample() {
|
|
||||||
XCTAssertEqual(database.lookup("202.108.22.220")?.isoCode, "CN")
|
|
||||||
XCTAssertEqual(database.lookup("8.8.8.8")?.isoCode, "US")
|
|
||||||
XCTAssertEqual(database.lookup("8.8.4.4")?.isoCode, "US")
|
|
||||||
|
|
||||||
XCTAssertNotNil(database.lookup("80.99.18.166"))
|
|
||||||
XCTAssertNotNil(database.lookup("172.217.18.78"))
|
|
||||||
XCTAssertNotNil(database.lookup("31.13.84.36"))
|
|
||||||
XCTAssertNotNil(database.lookup("104.244.42.129"))
|
|
||||||
XCTAssertNotNil(database.lookup("52.86.229.116"))
|
|
||||||
XCTAssertNotNil(database.lookup("172.217.19.110"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func testCloudFlare() {
|
|
||||||
let lookup = database.lookup("1.1.1.1")
|
|
||||||
XCTAssertNotNil(lookup)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// See http://stackoverflow.com/questions/25890533/how-can-i-get-a-real-ip-address-from-dns-query-in-swift
|
|
||||||
func IPOfHost(_ host: String) -> String? {
|
|
||||||
let host = CFHostCreateWithName(nil, host as CFString).takeRetainedValue()
|
|
||||||
CFHostStartInfoResolution(host, .addresses, nil)
|
|
||||||
var success = DarwinBoolean(false)
|
|
||||||
guard let addressing = CFHostGetAddressing(host, &success) else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
let addresses = addressing.takeUnretainedValue() as NSArray
|
|
||||||
if addresses.count > 0 {
|
|
||||||
let theAddress = addresses[0] as! Data
|
|
||||||
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
|
|
||||||
let infoResult = getnameinfo(
|
|
||||||
(theAddress as NSData).bytes.bindMemory(to: sockaddr.self, capacity: theAddress.count),
|
|
||||||
socklen_t(theAddress.count),
|
|
||||||
&hostname,
|
|
||||||
socklen_t(hostname.count),
|
|
||||||
nil,
|
|
||||||
0,
|
|
||||||
NI_NUMERICHOST
|
|
||||||
)
|
|
||||||
if infoResult == 0 {
|
|
||||||
if let numAddress = String(validatingUTF8: hostname) {
|
|
||||||
return numAddress
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
Database and Contents Copyright (c) 2020 MaxMind, Inc.
|
|
Binary file not shown.
|
@ -1,3 +0,0 @@
|
||||||
Use of this MaxMind product is governed by MaxMind's GeoLite2 End User License Agreement, which can be viewed at https://www.maxmind.com/en/geolite2/eula.
|
|
||||||
|
|
||||||
This database incorporates GeoNames [https://www.geonames.org] geographical data, which is made available under the Creative Commons Attribution 4.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/.
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
//
|
||||||
|
// Created by Rocska Ádám on 2020. 04. 28..
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class MetadataTest {
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
import Foundation
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
extension XCTestCase {
|
||||||
|
|
||||||
|
var bundle: Bundle {
|
||||||
|
get {
|
||||||
|
guard let currentFileUrl = URL(string: #file) else {
|
||||||
|
return Bundle.main
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let testBundle = Bundle(path: currentFileUrl.deletingLastPathComponent().path) else {
|
||||||
|
return Bundle.main
|
||||||
|
}
|
||||||
|
|
||||||
|
return testBundle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
#if !canImport(ObjectiveC)
|
||||||
|
public func allTests() -> [XCTestCaseEntry] {
|
||||||
|
return [
|
||||||
|
testCase(GeoIP2_swiftTests.allTests),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
#endif
|
Loading…
Reference in New Issue