From f86069d16523e1439db86603fffadc18de506094 Mon Sep 17 00:00:00 2001 From: asxalex Date: Tue, 29 Jul 2025 11:19:21 +0800 Subject: [PATCH] initialize node, change the sockv4 and multicast sock and token --- .../com/jihe/punchnet/sdlan/network/edge.kt | 16 +++++++++++----- .../com/jihe/punchnet/sdlan/network/libs.kt | 19 ++++++++++++++++--- .../com/jihe/punchnet/sdlan/network/socket.kt | 9 ++++++--- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/jihe/punchnet/sdlan/network/edge.kt b/app/src/main/java/com/jihe/punchnet/sdlan/network/edge.kt index ad3490d..14d7e8a 100644 --- a/app/src/main/java/com/jihe/punchnet/sdlan/network/edge.kt +++ b/app/src/main/java/com/jihe/punchnet/sdlan/network/edge.kt @@ -1,6 +1,7 @@ package com.jihe.punchnet.sdlan.network // import sdlanproto.Message.* +import android.util.Log import com.google.protobuf.kotlin.toByteString import com.jihe.punchnet.protobuf.PunchProto import com.jihe.punchnet.protobuf.PunchProto.SDLStunProbe @@ -127,24 +128,24 @@ class Node private constructor ( val aes: AES, // var encryptKey: ByteArray, - val config: NodeConfig, + var config: NodeConfig, val pendingPeers: PeerMap, val knownPeers: PeerMap, // 自身的公网ip val outerIPV4: AtomicInteger, - val udpSockV4: SDLanSocket, + var udpSockV4: SDLanSocket, - val udpSockMulticast: SDLanSocket?, - val multicastSock: SDLanSock, + var udpSockMulticast: SDLanSocket?, + var multicastSock: SDLanSock, val stats: NodeStats, // send message to socket, if connected, and authorized var toSocket: SendChannel, - val startStopChannel: SendChannel, + var startStopChannel: SendChannel, val mac: Mac = generateRandomMAC(), var nat_type: NatType = NatType.Invalid, @@ -175,6 +176,11 @@ class Node private constructor ( toSocket, startStopChannel, ) + } else { + instance?.config = config + instance?.udpSockV4 = v4Sock + instance?.token = token + Log.e("Initialize", "not initialize instance") } return instance!! } diff --git a/app/src/main/java/com/jihe/punchnet/sdlan/network/libs.kt b/app/src/main/java/com/jihe/punchnet/sdlan/network/libs.kt index 4cb757c..00b3856 100644 --- a/app/src/main/java/com/jihe/punchnet/sdlan/network/libs.kt +++ b/app/src/main/java/com/jihe/punchnet/sdlan/network/libs.kt @@ -73,14 +73,19 @@ suspend fun onMessage(scope: CoroutineScope, data: SDLanTCP) { println("reloading config: node.iface = ${node.iface}") node.iface?.reload_config(node.deviceConfig) println("reloading config ok: node.iface = ${node.iface}") + println("reloading config ok: node.udp = ${node.udpSockV4}") node.networkID.set(ack.devAddr.networkId) // println("got aes key: ${aeskey.toList()}, length is ${aeskey.size}") node.sendStunRequest() - scope.async { - node.probeNatType() - TerminalLogger.debugf { "nat type is ${node.nat_type}"} + scope.launch { + try { + node.probeNatType() + TerminalLogger.debugf { "nat type is ${node.nat_type}"} + } catch (e: Exception) { + Log.e("NAT", "probe nat exited: $e") + } } /* CoroutineScope(Dispatchers.Default).async { @@ -306,6 +311,7 @@ suspend fun loopSocketV4(scope: CoroutineScope, node: Node, sock: SDLanSocket, c node.sendStunRequest() } } catch (e: Exception) { + Log.e("Socket", "job stun request exits: $e") } } @@ -332,6 +338,7 @@ suspend fun loopSocketV4(scope: CoroutineScope, node: Node, sock: SDLanSocket, c } job_stun_request.cancelAndJoin() job_handle_packet.cancelAndJoin() + Log.e("Socket", "loop socket v4 stops") } suspend fun readAndParsePacket(scope: CoroutineScope, node: Node, sock: SDLanSocket) { @@ -431,6 +438,12 @@ fun initEdge(scope: CoroutineScope, iface: Iface, token: String, config: NodeCon startStopChannel, ) val instance = Node.getInstance() + instance.token = token + instance.udpSockV4 = sockV4 + instance.udpSockMulticast = sockMulticast + instance.config = config + instance.toSocket = toSocket + instance.startStopChannel = startStopChannel println(instance) } diff --git a/app/src/main/java/com/jihe/punchnet/sdlan/network/socket.kt b/app/src/main/java/com/jihe/punchnet/sdlan/network/socket.kt index 78a4d1a..878c3ca 100644 --- a/app/src/main/java/com/jihe/punchnet/sdlan/network/socket.kt +++ b/app/src/main/java/com/jihe/punchnet/sdlan/network/socket.kt @@ -1,5 +1,6 @@ package com.jihe.punchnet.sdlan.network +import android.util.Log import com.jihe.punchnet.sdlan.logs.TerminalLogger import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -14,12 +15,14 @@ import java.net.DatagramSocket import java.net.Inet4Address import java.net.SocketAddress -class SDLanSocket(val scope: CoroutineScope, val addr: String, val port: Int, val reuseAddress: Boolean = false) { +class SDLanSocket(val scope: CoroutineScope, val addr: String, var port: Int, val reuseAddress: Boolean = false) { private val connection: DatagramSocket = run { val sock = DatagramSocket(port, Inet4Address.getByName(addr)) if (reuseAddress) { sock.reuseAddress = true } + port = sock.localPort + Log.d("SDLanSocket", "local port = $port") sock } @@ -42,7 +45,7 @@ class SDLanSocket(val scope: CoroutineScope, val addr: String, val port: Int, va connection.send(packet) } } catch(e: Exception) { - TerminalLogger.errorf {"Failed to send to: $e, localport: ${connection.localPort}"} + TerminalLogger.errorf {"Failed to send to: $e, localport: ${port}"} } } @@ -56,7 +59,7 @@ class SDLanSocket(val scope: CoroutineScope, val addr: String, val port: Int, va return packet } - suspend fun close() { + fun close() { connection.close() } }