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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.vauth.foxyvpn.vpn.socks
import com.vauth.foxyvpn.data.AppLogger
import com.vauth.foxyvpn.vpn.RelayDispatchers
import com.vauth.foxyvpn.vpn.upstream.UpstreamSession
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import java.io.IOException
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
import java.net.InetSocketAddress
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
private const val TAG = "Socks5UdpDnsRelay"
internal const val DNS_PORT = 53
internal const val MAX_DNS_MESSAGE_BYTES = 4096
private const val SOCKS5_UDP_HEADER_MIN_BYTES = 10
private const val MAX_INFLIGHT_QUERIES = 32
internal class Socks5UdpDnsRelay(
private val bindAddress: InetAddress,
private val clientAddress: InetAddress,
private val socksProxy: InetSocketAddress?,
private val dohUrl: String? = null,
private val sessionProvider: () -> UpstreamSession?,
private val onUnsupportedFlow: () -> Unit = {},
) {
private val socket = DatagramSocket(0, bindAddress)
private val inflight = Semaphore(MAX_INFLIGHT_QUERIES)
private val queriesOverHttps = AtomicInteger(0)
private val nonDnsDropped = AtomicInteger(0)
private val foreignSourceDropped = AtomicInteger(0)
private val otherDropped = AtomicInteger(0)
private val unsupportedFlowReported = AtomicBoolean(false)
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
AppLogger.d(TAG, "dropped a DNS datagram: ${throwable.message}")
otherDropped.incrementAndGet()
}
private val scope = CoroutineScope(RelayDispatchers.relay + SupervisorJob() + exceptionHandler)
val boundAddress: InetAddress get() = bindAddress
val boundPort: Int get() = socket.localPort
fun start() {
AppLogger.d(TAG, "started UDP DNS relay on $bindAddress:${socket.localPort}; upstream DoH=${dohUrl ?: "<per-resolver>"}")
scope.launch {
val buffer = ByteArray(MAX_DNS_MESSAGE_BYTES + SOCKS5_UDP_HEADER_MIN_BYTES + 256)
while (!socket.isClosed) {
val packet = DatagramPacket(buffer, buffer.size)
try {
socket.receive(packet)
} catch (e: IOException) {
break
}
if (packet.address != clientAddress) {
if (foreignSourceDropped.getAndIncrement() == 0) {
AppLogger.w(
TAG,
"dropping a datagram from ${packet.address}: this association belongs to " +
"$clientAddress, and relaying for anyone else would make this an open resolver",
)
}
continue
}
val datagram = packet.data.copyOfRange(packet.offset, packet.offset + packet.length)
val replyTo = InetSocketAddress(packet.address, packet.port)
if (!inflight.tryAcquire()) {
otherDropped.incrementAndGet()
continue
}
scope.launch {
try {
relay(datagram, replyTo)
} finally {
inflight.release()
}
}
}
}
}
fun stop() {
runCatching { socket.close() }
scope.cancel()
val https = queriesOverHttps.get()
val nonDns = nonDnsDropped.get()
val foreign = foreignSourceDropped.get()
val other = otherDropped.get()
if (https > 0 || nonDns > 0 || foreign > 0 || other > 0) {
AppLogger.d(
TAG,
"UDP association closed: $https DNS queries over HTTPS, $nonDns non-DNS datagrams dropped, " +
"$foreign from unassociated sources, $other other drops",
)
}
}
private suspend fun relay(datagram: ByteArray, replyTo: InetSocketAddress) {
val request = parseRequest(datagram)
if (request == null) {
otherDropped.incrementAndGet()
return
}
if (request.port != DNS_PORT) {
nonDnsDropped.incrementAndGet()
if (unsupportedFlowReported.compareAndSet(false, true)) {
AppLogger.d(
TAG,
"dropping a datagram for port ${request.port}: this tunnel carries TCP only, so general " +
"UDP (QUIC included) cannot be relayed and the sender has to fall back to TCP. " +
"The DNS association stays up.",
)
runCatching { onUnsupportedFlow() }
}
return
}
val query = datagram.copyOfRange(request.payloadOffset, datagram.size)
if (query.isEmpty() || query.size > MAX_DNS_MESSAGE_BYTES) {
otherDropped.incrementAndGet()
return
}
val session = sessionProvider()?.takeIf { it.isConnected }
if (session == null) {
otherDropped.incrementAndGet()
return
}
val answer = resolve(request.host, query)
if (answer == null) {
otherDropped.incrementAndGet()
return
}
val response = datagram.copyOfRange(0, request.payloadOffset) + answer
runCatching { socket.send(DatagramPacket(response, response.size, replyTo)) }
.onFailure { otherDropped.incrementAndGet() }
}
private fun resolve(
resolverHost: String,
query: ByteArray,
): ByteArray? {
if (socksProxy == null) return null
val endpoint = dohUrl ?: DohDnsClient.endpointFor(resolverHost) ?: return null
val answer = DohDnsClient.query(socksProxy, endpoint, query)
if (answer != null) {
queriesOverHttps.incrementAndGet()
return answer
}
return null
}
private class Request(val payloadOffset: Int, val host: String, val port: Int)
private fun parseRequest(datagram: ByteArray): Request? {
if (datagram.size < SOCKS5_UDP_HEADER_MIN_BYTES) return null
if (datagram[0].toInt() != 0 || datagram[1].toInt() != 0) return null
if (datagram[2].toInt() != 0) return null
var offset = 3
val addressType = datagram[offset++].toInt() and 0xFF
val host = when (addressType) {
0x01 -> {
if (datagram.size < offset + 4 + 2) return null
val bytes = datagram.copyOfRange(offset, offset + 4)
offset += 4
bytes.joinToString(".") { (it.toInt() and 0xFF).toString() }
}
0x03 -> {
val length = datagram[offset++].toInt() and 0xFF
if (length == 0) return null
if (datagram.size < offset + length + 2) return null
val name = String(datagram, offset, length, Charsets.US_ASCII)
offset += length
name
}
0x04 -> {
if (datagram.size < offset + 16 + 2) return null
val bytes = datagram.copyOfRange(offset, offset + 16)
offset += 16
InetAddress.getByAddress(bytes).hostAddress ?: return null
}
else -> return null
}
val port = ((datagram[offset].toInt() and 0xFF) shl 8) or (datagram[offset + 1].toInt() and 0xFF)
offset += 2
return Request(offset, host, port)
}
}