initialize node, change the sockv4 and multicast sock and token

This commit is contained in:
asxalex 2025-07-29 11:19:21 +08:00
parent 87bee05140
commit f86069d165
3 changed files with 33 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package com.jihe.punchnet.sdlan.network package com.jihe.punchnet.sdlan.network
// import sdlanproto.Message.* // import sdlanproto.Message.*
import android.util.Log
import com.google.protobuf.kotlin.toByteString import com.google.protobuf.kotlin.toByteString
import com.jihe.punchnet.protobuf.PunchProto import com.jihe.punchnet.protobuf.PunchProto
import com.jihe.punchnet.protobuf.PunchProto.SDLStunProbe import com.jihe.punchnet.protobuf.PunchProto.SDLStunProbe
@ -127,24 +128,24 @@ class Node private constructor (
val aes: AES, val aes: AES,
// var encryptKey: ByteArray, // var encryptKey: ByteArray,
val config: NodeConfig, var config: NodeConfig,
val pendingPeers: PeerMap, val pendingPeers: PeerMap,
val knownPeers: PeerMap, val knownPeers: PeerMap,
// 自身的公网ip // 自身的公网ip
val outerIPV4: AtomicInteger, val outerIPV4: AtomicInteger,
val udpSockV4: SDLanSocket, var udpSockV4: SDLanSocket,
val udpSockMulticast: SDLanSocket?, var udpSockMulticast: SDLanSocket?,
val multicastSock: SDLanSock, var multicastSock: SDLanSock,
val stats: NodeStats, val stats: NodeStats,
// send message to socket, if connected, and authorized // send message to socket, if connected, and authorized
var toSocket: SendChannel<ByteArray>, var toSocket: SendChannel<ByteArray>,
val startStopChannel: SendChannel<StartStopChanInfo>, var startStopChannel: SendChannel<StartStopChanInfo>,
val mac: Mac = generateRandomMAC(), val mac: Mac = generateRandomMAC(),
var nat_type: NatType = NatType.Invalid, var nat_type: NatType = NatType.Invalid,
@ -175,6 +176,11 @@ class Node private constructor (
toSocket, toSocket,
startStopChannel, startStopChannel,
) )
} else {
instance?.config = config
instance?.udpSockV4 = v4Sock
instance?.token = token
Log.e("Initialize", "not initialize instance")
} }
return instance!! return instance!!
} }

View File

@ -73,14 +73,19 @@ suspend fun onMessage(scope: CoroutineScope, data: SDLanTCP) {
println("reloading config: node.iface = ${node.iface}") println("reloading config: node.iface = ${node.iface}")
node.iface?.reload_config(node.deviceConfig) node.iface?.reload_config(node.deviceConfig)
println("reloading config ok: node.iface = ${node.iface}") println("reloading config ok: node.iface = ${node.iface}")
println("reloading config ok: node.udp = ${node.udpSockV4}")
node.networkID.set(ack.devAddr.networkId) node.networkID.set(ack.devAddr.networkId)
// println("got aes key: ${aeskey.toList()}, length is ${aeskey.size}") // println("got aes key: ${aeskey.toList()}, length is ${aeskey.size}")
node.sendStunRequest() node.sendStunRequest()
scope.async { scope.launch {
node.probeNatType() try {
TerminalLogger.debugf { "nat type is ${node.nat_type}"} node.probeNatType()
TerminalLogger.debugf { "nat type is ${node.nat_type}"}
} catch (e: Exception) {
Log.e("NAT", "probe nat exited: $e")
}
} }
/* /*
CoroutineScope(Dispatchers.Default).async { CoroutineScope(Dispatchers.Default).async {
@ -306,6 +311,7 @@ suspend fun loopSocketV4(scope: CoroutineScope, node: Node, sock: SDLanSocket, c
node.sendStunRequest() node.sendStunRequest()
} }
} catch (e: Exception) { } 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_stun_request.cancelAndJoin()
job_handle_packet.cancelAndJoin() job_handle_packet.cancelAndJoin()
Log.e("Socket", "loop socket v4 stops")
} }
suspend fun readAndParsePacket(scope: CoroutineScope, node: Node, sock: SDLanSocket) { suspend fun readAndParsePacket(scope: CoroutineScope, node: Node, sock: SDLanSocket) {
@ -431,6 +438,12 @@ fun initEdge(scope: CoroutineScope, iface: Iface, token: String, config: NodeCon
startStopChannel, startStopChannel,
) )
val instance = Node.getInstance() val instance = Node.getInstance()
instance.token = token
instance.udpSockV4 = sockV4
instance.udpSockMulticast = sockMulticast
instance.config = config
instance.toSocket = toSocket
instance.startStopChannel = startStopChannel
println(instance) println(instance)
} }

View File

@ -1,5 +1,6 @@
package com.jihe.punchnet.sdlan.network package com.jihe.punchnet.sdlan.network
import android.util.Log
import com.jihe.punchnet.sdlan.logs.TerminalLogger import com.jihe.punchnet.sdlan.logs.TerminalLogger
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -14,12 +15,14 @@ import java.net.DatagramSocket
import java.net.Inet4Address import java.net.Inet4Address
import java.net.SocketAddress 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 { private val connection: DatagramSocket = run {
val sock = DatagramSocket(port, Inet4Address.getByName(addr)) val sock = DatagramSocket(port, Inet4Address.getByName(addr))
if (reuseAddress) { if (reuseAddress) {
sock.reuseAddress = true sock.reuseAddress = true
} }
port = sock.localPort
Log.d("SDLanSocket", "local port = $port")
sock sock
} }
@ -42,7 +45,7 @@ class SDLanSocket(val scope: CoroutineScope, val addr: String, val port: Int, va
connection.send(packet) connection.send(packet)
} }
} catch(e: Exception) { } 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 return packet
} }
suspend fun close() { fun close() {
connection.close() connection.close()
} }
} }