forked from metabolist/metatext
/
InstanceURLService.swift
156 lines (139 loc) · 9.33 KB
/
InstanceURLService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright © 2020 Metabolist. All rights reserved.
import CodableBloomFilter
import Combine
import Foundation
import HTTP
import Mastodon
import MastodonAPI
public enum InstanceURLError: Error {
case instanceNotSupported
}
public struct InstanceURLService {
private let httpClient: HTTPClient
private var appPreferences: AppPreferences
public init(environment: AppEnvironment) {
httpClient = HTTPClient(session: environment.session, decoder: MastodonDecoder())
appPreferences = AppPreferences(environment: environment)
}
}
public extension InstanceURLService {
func url(text: String) -> Result<URL, Error> {
guard text.count >= Self.shortestPossibleURLLength else {
return .failure(URLError(.badURL))
}
let url: URL
if text.hasPrefix(Self.httpsPrefix), let prefixedURL = URL(string: text) {
url = prefixedURL
} else if let unprefixedURL = URL(string: Self.httpsPrefix.appending(text)) {
url = unprefixedURL
} else {
return .failure(URLError(.badURL))
}
if isFiltered(url: url) {
return .failure(InstanceURLError.instanceNotSupported)
}
return .success(url)
}
func instance(url: URL) -> AnyPublisher<Instance, Error> {
httpClient.request(
MastodonAPITarget(
baseURL: url,
endpoint: InstanceEndpoint.instance,
accessToken: nil))
.eraseToAnyPublisher()
}
func isPublicTimelineAvailable(url: URL) -> AnyPublisher<Bool, Error> {
httpClient.request(
MastodonAPITarget(
baseURL: url,
endpoint: StatusesEndpoint.timelinesPublic(local: true),
accessToken: nil))
.map { _ in true }
.eraseToAnyPublisher()
}
}
private extension InstanceURLService {
static let httpsPrefix = "https://"
static let shortestPossibleURLLength = 4
static let filter = BloomFilter<String>(
hashes: [.djb232, .djb2a32, .sdbm32, .fnv132, .fnv1a32],
data: Data([
0x84, 0x30, 0x85, 0x50, 0x48, 0x48, 0x01, 0x08, 0x0a, 0x91, 0x04, 0x64, 0x00, 0x28, 0xe1, 0x28,
0xc2, 0x44, 0x80, 0x03, 0x00, 0x11, 0x48, 0x8e, 0x40, 0x00, 0x23, 0x8a, 0x02, 0x59, 0x81, 0xd0,
0x8c, 0x20, 0x02, 0x08, 0x01, 0x40, 0x01, 0x00, 0x00, 0x00, 0x88, 0x02, 0x00, 0x87, 0x46, 0x40,
0x04, 0x00, 0x60, 0x50, 0x00, 0x20, 0x82, 0x41, 0x01, 0x08, 0x82, 0x91, 0x10, 0x8a, 0xa0, 0x08,
0xc1, 0x04, 0xcc, 0x02, 0x9c, 0xa0, 0x6a, 0x20, 0x02, 0x04, 0x00, 0x95, 0x21, 0xa2, 0x84, 0x54,
0x12, 0x80, 0x2c, 0x43, 0x00, 0x01, 0x8a, 0x18, 0x88, 0x00, 0x04, 0x40, 0x04, 0x04, 0x06, 0x0a,
0x00, 0x01, 0x08, 0x52, 0x4d, 0x61, 0x09, 0x00, 0x20, 0x10, 0x10, 0x10, 0x04, 0x20, 0x04, 0x1c,
0x2a, 0x00, 0x10, 0x51, 0x02, 0x48, 0x04, 0x12, 0x58, 0x32, 0x8c, 0x40, 0x42, 0x20, 0x18, 0x01,
0x42, 0x01, 0x52, 0xe6, 0x42, 0xb1, 0x09, 0x88, 0x08, 0x30, 0x7c, 0xc4, 0x80, 0xe4, 0x00, 0x00,
0x12, 0x30, 0x00, 0x02, 0x30, 0x21, 0x98, 0x92, 0x04, 0x00, 0x00, 0x0a, 0x00, 0x03, 0x25, 0xb8,
0x20, 0x02, 0x40, 0x47, 0x40, 0x02, 0x06, 0x01, 0x16, 0x00, 0x83, 0x00, 0xf0, 0x04, 0x20, 0x16,
0x00, 0x00, 0x02, 0x90, 0x28, 0xc4, 0x01, 0xd0, 0x02, 0x00, 0x00, 0x48, 0x00, 0x08, 0x14, 0x17,
0x38, 0x20, 0x00, 0x00, 0x54, 0x16, 0x04, 0x16, 0x06, 0x64, 0x04, 0x81, 0x00, 0x88, 0x04, 0x22,
0x80, 0x00, 0x21, 0x00, 0x11, 0x97, 0x20, 0x00, 0xa5, 0x02, 0x09, 0x20, 0x23, 0x00, 0x41, 0x60,
0x05, 0x12, 0x18, 0x22, 0x30, 0x88, 0x40, 0x03, 0x20, 0x21, 0x03, 0x01, 0x44, 0x98, 0x00, 0x00,
0x35, 0x83, 0x91, 0x02, 0x00, 0x00, 0x4b, 0x40, 0x20, 0x14, 0x2e, 0xa1, 0x08, 0x01, 0x00, 0x14,
0x58, 0x00, 0x0b, 0x00, 0x49, 0x80, 0xc4, 0x04, 0x0a, 0x2d, 0x00, 0x41, 0x00, 0x21, 0x03, 0x42,
0x28, 0x22, 0x00, 0x42, 0x08, 0xc1, 0x47, 0x81, 0x20, 0x8a, 0x04, 0x00, 0x14, 0x14, 0x41, 0x00,
0x0c, 0x84, 0x00, 0x50, 0xa5, 0x10, 0x00, 0xc2, 0x14, 0x0f, 0x26, 0xb4, 0x15, 0x00, 0x40, 0x82,
0xd0, 0xa2, 0xd0, 0x12, 0x51, 0x00, 0x23, 0x30, 0x02, 0x07, 0xb2, 0x91, 0x01, 0x00, 0x04, 0x42,
0x4c, 0x51, 0x84, 0x2a, 0x00, 0x21, 0x44, 0xc1, 0x88, 0xe0, 0x80, 0x80, 0x00, 0xc0, 0x10, 0x10,
0x22, 0x08, 0x04, 0x04, 0x40, 0x00, 0x44, 0x11, 0x42, 0x41, 0x84, 0x91, 0x10, 0x18, 0x22, 0x40,
0x01, 0x00, 0x46, 0x82, 0x22, 0x40, 0x40, 0x41, 0x02, 0xa4, 0x24, 0x40, 0x89, 0x21, 0x11, 0x06,
0x00, 0x00, 0x18, 0x06, 0x1c, 0x10, 0x10, 0xe1, 0x70, 0x61, 0x44, 0x20, 0x40, 0x40, 0x02, 0x86,
0x1b, 0x04, 0x01, 0x45, 0x10, 0x14, 0x16, 0x20, 0xf0, 0x08, 0x20, 0x21, 0x00, 0x00, 0x68, 0x98,
0x22, 0x88, 0x00, 0xc2, 0x42, 0x30, 0x00, 0x04, 0x70, 0x03, 0x59, 0x40, 0x59, 0x31, 0x44, 0x14,
0x14, 0x15, 0x02, 0x04, 0x21, 0x01, 0x80, 0x10, 0x21, 0x60, 0x03, 0x10, 0x02, 0x20, 0x00, 0x0c,
0x30, 0x08, 0x80, 0x0a, 0x21, 0x80, 0x19, 0x00, 0x80, 0xc0, 0x40, 0x10, 0x46, 0x44, 0x02, 0x68,
0x03, 0x10, 0x74, 0x80, 0xc1, 0x08, 0x08, 0x00, 0x02, 0x04, 0x01, 0x82, 0x10, 0x62, 0x02, 0xa0,
0x0c, 0x30, 0x10, 0x41, 0x00, 0x01, 0xc6, 0x05, 0xb9, 0x41, 0x83, 0x65, 0xc2, 0x02, 0x44, 0x00,
0x68, 0x0a, 0x89, 0x88, 0x08, 0x40, 0x30, 0x02, 0x54, 0x80, 0x10, 0xc1, 0x40, 0x3a, 0x46, 0x00,
0x9d, 0xc0, 0x82, 0x22, 0x20, 0x26, 0x04, 0x91, 0xa1, 0x81, 0x2c, 0x01, 0x88, 0x08, 0x12, 0x40,
0x50, 0x41, 0x06, 0xd2, 0x95, 0x10, 0x00, 0x05, 0x60, 0x2b, 0x60, 0x00, 0x00, 0x14, 0x0c, 0x8c,
0xa4, 0x28, 0x09, 0x4e, 0x00, 0x20, 0x00, 0x04, 0x22, 0x00, 0x09, 0x06, 0x10, 0x44, 0xa4, 0x00,
0xc4, 0x90, 0x4c, 0x00, 0x05, 0xb8, 0x40, 0x60, 0x48, 0xa4, 0xca, 0xc4, 0xa4, 0x26, 0x24, 0x88,
0x10, 0x40, 0x80, 0x04, 0x10, 0x9d, 0x01, 0x22, 0xc2, 0x45, 0x10, 0xb0, 0x08, 0x00, 0x54, 0x00,
0x4c, 0x1e, 0x10, 0x58, 0x61, 0x11, 0x10, 0xa0, 0x00, 0x00, 0x81, 0x08, 0x10, 0x9a, 0x29, 0x50,
0x81, 0x02, 0x82, 0x16, 0x24, 0x36, 0x01, 0x08, 0xc0, 0x09, 0x40, 0x02, 0x94, 0x58, 0x80, 0x01,
0x98, 0x09, 0x02, 0x02, 0x88, 0x4f, 0x00, 0x02, 0x04, 0x09, 0x42, 0x90, 0x31, 0x40, 0x13, 0x28,
0x19, 0x02, 0x10, 0x25, 0x1a, 0x21, 0x80, 0x2d, 0x6a, 0x0c, 0x00, 0x80, 0xa5, 0x00, 0x66, 0x26,
0x30, 0x00, 0x02, 0x20, 0x93, 0x80, 0x10, 0x82, 0x83, 0xc6, 0x04, 0x58, 0x33, 0x05, 0x81, 0x24,
0x49, 0x80, 0x08, 0x0b, 0x50, 0x45, 0x81, 0x91, 0x04, 0x08, 0x80, 0x28, 0x0c, 0x79, 0x94, 0x9c,
0x10, 0x00, 0xd0, 0x91, 0x00, 0x49, 0xe4, 0xc9, 0x40, 0x44, 0x85, 0xa4, 0x11, 0x00, 0x22, 0xc0,
0x22, 0x03, 0x04, 0x20, 0x42, 0x5f, 0x42, 0x10, 0x00, 0x81, 0x0d, 0x40, 0xb0, 0x01, 0x44, 0x00,
0x0c, 0x20, 0x20, 0x80, 0x11, 0x10, 0x30, 0x70, 0x60, 0x18, 0x08, 0x22, 0xa5, 0x82, 0x2a, 0x10,
0x0d, 0x83, 0x84, 0x84, 0xa0, 0x03, 0x08, 0x02, 0x02, 0x88, 0x00, 0x01, 0x00, 0x00, 0xc2, 0x40,
0x00, 0x88, 0x22, 0x10, 0x0a, 0x00, 0x13, 0x02, 0x00, 0x42, 0x28, 0x40, 0x44, 0x31, 0x00, 0x03,
0x40, 0x44, 0xc9, 0x90, 0x32, 0x03, 0x20, 0x20, 0x28, 0xa0, 0x80, 0x11, 0x08, 0x50, 0x82, 0x02,
0x41, 0x80, 0xc0, 0x00, 0x89, 0x68, 0x48, 0x00, 0x80, 0x02, 0x08, 0x60, 0x84, 0x60, 0x44, 0x08,
0x15, 0x41, 0x20, 0x09, 0x00, 0x00, 0x10, 0x48, 0x00, 0x2a, 0x00, 0x04, 0x52, 0x92, 0x92, 0x00,
0x10, 0x44, 0x10, 0x28, 0x08, 0x39, 0x89, 0xa1, 0x00, 0x00, 0x10, 0xd5, 0x81, 0x41, 0x68, 0x34,
0x09, 0x18, 0x11, 0x00, 0x09, 0x00, 0x41, 0x00, 0x92, 0x19, 0x02, 0x00, 0x90, 0xc2, 0x43, 0x02,
0x09, 0x80, 0x02, 0x64, 0x11, 0x00, 0x00, 0x0c, 0x40, 0x12, 0x10, 0x00, 0x20, 0x29, 0x0d, 0x08,
0x90, 0x08, 0x00, 0x00, 0x5d, 0x08, 0x80, 0x40, 0x20, 0x52, 0x82, 0x20, 0x00, 0x18, 0x18, 0x02,
0x26, 0x0b, 0x2a, 0x24, 0x11, 0x00, 0x48, 0x03, 0x0c, 0xa0, 0x28, 0xd0, 0x82, 0x14, 0x18, 0x40,
0x4a, 0x50, 0x43, 0x02, 0x08, 0x03, 0x4a, 0x78, 0x10, 0x00, 0x00, 0x0b, 0x10, 0x30, 0x08, 0x14,
0x18, 0x00, 0x8b, 0x6b, 0x06, 0x24, 0x00, 0x43, 0x00, 0x03, 0x04, 0x82, 0x00, 0x41, 0x80, 0x2a,
0x21, 0x06, 0x08, 0x20, 0x18, 0x11, 0x43, 0xc0, 0x04, 0x10, 0x00, 0x08, 0x18, 0x80, 0x80, 0x40,
0x2a, 0x06, 0xc2, 0x00, 0x05, 0x0c, 0x80, 0x0d, 0x20, 0x44, 0x10, 0x40, 0x00, 0x34, 0x08, 0x08,
0x21, 0x0c, 0x84, 0x01, 0x01, 0x06, 0x1e, 0x08, 0x48, 0x12, 0x91, 0x30, 0x22, 0xc0, 0x31, 0x0a,
0x10, 0x62, 0x10, 0x11, 0x04, 0x18, 0x0e, 0x60, 0x50, 0x10, 0x10, 0x85, 0x00, 0x08, 0x16, 0x00,
0x15, 0x00, 0xc0, 0x24, 0xe4, 0x01, 0x23, 0x00, 0x08, 0x00, 0x04, 0x81, 0x00, 0x01, 0xa4, 0x1c,
0x19, 0x01, 0x88, 0x41, 0x30, 0x08, 0x04, 0x80, 0x8a, 0x89, 0x80, 0x40, 0x4b, 0x46, 0x02, 0x04,
0x00, 0x21, 0x01, 0x28, 0x96, 0x00, 0x89, 0x3b, 0x02, 0x54, 0x21, 0x2c, 0x5a, 0x82, 0x00, 0x50,
])
)
private func isFiltered(url: URL) -> Bool {
guard let host = url.host else { return true }
let allHostComponents = host.components(separatedBy: ".")
var hostComponents = [String]()
for component in allHostComponents.reversed() {
hostComponents.insert(component, at: 0)
if Self.filter.contains(hostComponents.joined(separator: ".")) {
return true
}
}
return false
}
}