惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
The Cloudflare Blog
量子位
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
D
DataBreaches.Net
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
U
Unit 42
博客园 - 聂微东
有赞技术团队
有赞技术团队
A
About on SuperTechFans

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(android): remediate app CodeQL alerts · openclaw/open...
vincentkoc · 2026-04-28 · via Recent Commits to openclaw:main

@@ -1,11 +1,15 @@

11

package ai.openclaw.app.gateway

223+

import android.annotation.TargetApi

34

import android.content.Context

45

import android.net.ConnectivityManager

56

import android.net.DnsResolver

7+

import android.net.Network

68

import android.net.NetworkCapabilities

9+

import android.net.NetworkRequest

710

import android.net.nsd.NsdManager

811

import android.net.nsd.NsdServiceInfo

12+

import android.os.Build

913

import android.os.CancellationSignal

1014

import android.util.Log

1115

import kotlinx.coroutines.CoroutineScope

@@ -34,6 +38,7 @@ import org.xbill.DNS.TXTRecord

3438

import org.xbill.DNS.TextParseException

3539

import org.xbill.DNS.Type

3640

import java.io.IOException

41+

import java.net.InetAddress

3742

import java.net.InetSocketAddress

3843

import java.nio.ByteBuffer

3944

import java.nio.charset.CodingErrorAction

@@ -44,7 +49,6 @@ import java.util.concurrent.Executors

4449

import kotlin.coroutines.resume

4550

import kotlin.coroutines.resumeWithException

465147-

@Suppress("DEPRECATION")

4852

class GatewayDiscovery(

4953

context: Context,

5054

private val scope: CoroutineScope,

@@ -66,11 +70,24 @@ class GatewayDiscovery(

66706771

private var unicastJob: Job? = null

6872

private val dnsExecutor: Executor = Executors.newCachedThreadPool()

73+

private val availableNetworks = ConcurrentHashMap.newKeySet<Network>()

74+

private val serviceInfoCallbacks = ConcurrentHashMap<String, Any>()

69757076

@Volatile private var lastWideAreaRcode: Int? = null

71777278

@Volatile private var lastWideAreaCount: Int = 0

737980+

private val networkCallback =

81+

object : ConnectivityManager.NetworkCallback() {

82+

override fun onAvailable(network: Network) {

83+

availableNetworks.add(network)

84+

}

85+86+

override fun onLost(network: Network) {

87+

availableNetworks.remove(network)

88+

}

89+

}

90+7491

private val discoveryListener =

7592

object : NsdManager.DiscoveryListener {

7693

override fun onStartDiscoveryFailed(

@@ -96,17 +113,29 @@ class GatewayDiscovery(

96113

val serviceName = BonjourEscapes.decode(serviceInfo.serviceName)

97114

val id = stableId(serviceName, "local.")

98115

localById.remove(id)

116+

unregisterServiceInfoCallback(id)

99117

publish()

100118

}

101119

}

102120103121

init {

122+

startNetworkTracking()

104123

startLocalDiscovery()

105124

if (!wideAreaDomain.isNullOrBlank()) {

106125

startUnicastDiscovery(wideAreaDomain)

107126

}

108127

}

109128129+

private fun startNetworkTracking() {

130+

val cm = connectivity ?: return

131+

cm.activeNetwork?.let(availableNetworks::add)

132+

try {

133+

cm.registerNetworkCallback(NetworkRequest.Builder().build(), networkCallback)

134+

} catch (_: Throwable) {

135+

// ignore (best-effort)

136+

}

137+

}

138+110139

private fun startLocalDiscovery() {

111140

try {

112141

nsd.discoverServices(serviceType, NsdManager.PROTOCOL_DNS_SD, discoveryListener)

@@ -138,46 +167,124 @@ class GatewayDiscovery(

138167

}

139168140169

private fun resolve(serviceInfo: NsdServiceInfo) {

141-

nsd.resolveService(

142-

serviceInfo,

170+

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {

171+

resolveWithServiceInfoCallback(serviceInfo)

172+

} else {

173+

resolveLegacy(serviceInfo)

174+

}

175+

}

176+177+

@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)

178+

private fun resolveWithServiceInfoCallback(serviceInfo: NsdServiceInfo) {

179+

val serviceName = BonjourEscapes.decode(serviceInfo.serviceName)

180+

val id = stableId(serviceName, "local.")

181+

if (serviceInfoCallbacks.containsKey(id)) return

182+183+

val callback =

184+

object : NsdManager.ServiceInfoCallback {

185+

override fun onServiceInfoCallbackRegistrationFailed(errorCode: Int) {

186+

serviceInfoCallbacks.remove(id, this)

187+

}

188+189+

override fun onServiceInfoCallbackUnregistered() {

190+

serviceInfoCallbacks.remove(id, this)

191+

}

192+193+

override fun onServiceLost() {

194+

localById.remove(id)

195+

publish()

196+

}

197+198+

override fun onServiceUpdated(serviceInfo: NsdServiceInfo) {

199+

upsertResolvedService(serviceInfo)

200+

}

201+

}

202+203+

serviceInfoCallbacks[id] = callback

204+

try {

205+

nsd.registerServiceInfoCallback(serviceInfo, dnsExecutor, callback)

206+

} catch (_: Throwable) {

207+

serviceInfoCallbacks.remove(id, callback)

208+

}

209+

}

210+211+

private fun unregisterServiceInfoCallback(id: String) {

212+

val callback = serviceInfoCallbacks.remove(id) ?: return

213+

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) return

214+

try {

215+

nsd.unregisterServiceInfoCallback(callback as NsdManager.ServiceInfoCallback)

216+

} catch (_: Throwable) {

217+

// ignore (best-effort)

218+

}

219+

}

220+221+

private fun resolveLegacy(serviceInfo: NsdServiceInfo) {

222+

val listener =

143223

object : NsdManager.ResolveListener {

144224

override fun onResolveFailed(

145225

serviceInfo: NsdServiceInfo,

146226

errorCode: Int,

147227

) {}

148228149229

override fun onServiceResolved(resolved: NsdServiceInfo) {

150-

val host = resolved.host?.hostAddress ?: return

151-

val port = resolved.port

152-

if (port <= 0) return

153-154-

val rawServiceName = resolved.serviceName

155-

val serviceName = BonjourEscapes.decode(rawServiceName)

156-

val displayName = BonjourEscapes.decode(txt(resolved, "displayName") ?: serviceName)

157-

val lanHost = txt(resolved, "lanHost")

158-

val tailnetDns = txt(resolved, "tailnetDns")

159-

val gatewayPort = txtInt(resolved, "gatewayPort")

160-

val canvasPort = txtInt(resolved, "canvasPort")

161-

val tlsEnabled = txtBool(resolved, "gatewayTls")

162-

val tlsFingerprint = txt(resolved, "gatewayTlsSha256")

163-

val id = stableId(serviceName, "local.")

164-

localById[id] =

165-

GatewayEndpoint(

166-

stableId = id,

167-

name = displayName,

168-

host = host,

169-

port = port,

170-

lanHost = lanHost,

171-

tailnetDns = tailnetDns,

172-

gatewayPort = gatewayPort,

173-

canvasPort = canvasPort,

174-

tlsEnabled = tlsEnabled,

175-

tlsFingerprintSha256 = tlsFingerprint,

176-

)

177-

publish()

230+

upsertResolvedService(resolved)

178231

}

179-

},

180-

)

232+

}

233+234+

try {

235+

NsdManager::class.java

236+

.getMethod("resolveService", NsdServiceInfo::class.java, NsdManager.ResolveListener::class.java)

237+

.invoke(nsd, serviceInfo, listener)

238+

} catch (_: Throwable) {

239+

// ignore (best-effort)

240+

}

241+

}

242+243+

private fun upsertResolvedService(resolved: NsdServiceInfo) {

244+

val host = resolvedHostAddress(resolved) ?: return

245+

val port = resolved.port

246+

if (port <= 0) return

247+248+

val rawServiceName = resolved.serviceName

249+

val serviceName = BonjourEscapes.decode(rawServiceName)

250+

val displayName = BonjourEscapes.decode(txt(resolved, "displayName") ?: serviceName)

251+

val lanHost = txt(resolved, "lanHost")

252+

val tailnetDns = txt(resolved, "tailnetDns")

253+

val gatewayPort = txtInt(resolved, "gatewayPort")

254+

val canvasPort = txtInt(resolved, "canvasPort")

255+

val tlsEnabled = txtBool(resolved, "gatewayTls")

256+

val tlsFingerprint = txt(resolved, "gatewayTlsSha256")

257+

val id = stableId(serviceName, "local.")

258+

localById[id] =

259+

GatewayEndpoint(

260+

stableId = id,

261+

name = displayName,

262+

host = host,

263+

port = port,

264+

lanHost = lanHost,

265+

tailnetDns = tailnetDns,

266+

gatewayPort = gatewayPort,

267+

canvasPort = canvasPort,

268+

tlsEnabled = tlsEnabled,

269+

tlsFingerprintSha256 = tlsFingerprint,

270+

)

271+

publish()

272+

}

273+274+

private fun resolvedHostAddress(resolved: NsdServiceInfo): String? {

275+

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {

276+

return resolved.hostAddresses.firstOrNull()?.hostAddress

277+

}

278+

return legacyHostAddress(resolved)

279+

}

280+281+

private fun legacyHostAddress(resolved: NsdServiceInfo): String? {

282+

return try {

283+

val host = NsdServiceInfo::class.java.getMethod("getHost").invoke(resolved) as? InetAddress

284+

host?.hostAddress

285+

} catch (_: Throwable) {

286+

null

287+

}

181288

}

182289183290

private fun publish() {

@@ -422,21 +529,27 @@ class GatewayDiscovery(

422529

val cm = connectivity ?: return null

423530424531

// Prefer VPN (Tailscale) when present; otherwise use the active network.

425-

cm.allNetworks

426-

.firstOrNull { n ->

427-

val caps = cm.getNetworkCapabilities(n) ?: return@firstOrNull false

428-

caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)

429-

}?.let { return it }

532+

trackedNetworks(cm).firstOrNull { n ->

533+

val caps = cm.getNetworkCapabilities(n) ?: return@firstOrNull false

534+

caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)

535+

}?.let { return it }

430536431537

return cm.activeNetwork

432538

}

433539540+

private fun trackedNetworks(cm: ConnectivityManager): List<Network> {

541+

return buildList {

542+

cm.activeNetwork?.let(::add)

543+

addAll(availableNetworks)

544+

}.distinct()

545+

}

546+434547

private fun createDirectResolver(): Resolver? {

435548

val cm = connectivity ?: return null

436549437550

val candidateNetworks =

438551

buildList {

439-

cm.allNetworks

552+

trackedNetworks(cm)

440553

.firstOrNull { n ->

441554

val caps = cm.getNetworkCapabilities(n) ?: return@firstOrNull false

442555

caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)