install bug and new log api
This commit is contained in:
@@ -4,7 +4,6 @@ import android.app.*
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import java.io.IOException
|
||||
import android.net.VpnService
|
||||
import android.os.Binder
|
||||
import android.os.Build
|
||||
@@ -22,6 +21,13 @@ import java.io.FileOutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.coroutines.*
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.net.InetAddress
|
||||
import java.net.NetworkInterface
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import java.nio.channels.FileChannel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class Node(val name: String, val ip: String, val resource: String? = null)
|
||||
|
||||
@@ -32,9 +38,10 @@ data class Network(
|
||||
val gateway: String,
|
||||
val Nodes: List<Node>
|
||||
)
|
||||
|
||||
class OpenP2PService : VpnService() {
|
||||
companion object {
|
||||
private val LOG_TAG = OpenP2PService::class.simpleName
|
||||
private val LOG_TAG = "OpenP2PService"
|
||||
}
|
||||
|
||||
inner class LocalBinder : Binder() {
|
||||
@@ -44,12 +51,17 @@ class OpenP2PService : VpnService() {
|
||||
private val binder = LocalBinder()
|
||||
private lateinit var network: openp2p.P2PNetwork
|
||||
private lateinit var mToken: String
|
||||
private var running:Boolean =true
|
||||
private var sdwanRunning:Boolean =false
|
||||
private var running: Boolean = true
|
||||
private var sdwanRunning: Boolean = false
|
||||
private var vpnInterface: ParcelFileDescriptor? = null
|
||||
private var sdwanJob: Job? = null
|
||||
private var sdwanJob: Job? = null
|
||||
private val packetQueue = Channel<ByteBuffer>(capacity = 1024)
|
||||
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
override fun onCreate() {
|
||||
Log.i(LOG_TAG, "onCreate - Thread ID = " + Thread.currentThread().id)
|
||||
val logDir = File(getExternalFilesDir(null), "log")
|
||||
Logger.init(logDir)
|
||||
Logger.i(LOG_TAG, "onCreate - Thread ID = " + Thread.currentThread().id)
|
||||
var channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
createNotificationChannel("kim.hsl", "ForegroundService")
|
||||
} else {
|
||||
@@ -64,7 +76,7 @@ class OpenP2PService : VpnService() {
|
||||
|
||||
val notification = channelId?.let {
|
||||
NotificationCompat.Builder(this, it)
|
||||
// .setSmallIcon(R.mipmap.app_icon)
|
||||
// .setSmallIcon(R.mipmap.app_icon)
|
||||
.setContentTitle("My Awesome App")
|
||||
.setContentText("Doing some work...")
|
||||
.setContentIntent(pendingIntent).build()
|
||||
@@ -76,7 +88,7 @@ class OpenP2PService : VpnService() {
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
Log.i(
|
||||
Logger.i(
|
||||
LOG_TAG,
|
||||
"onStartCommand - startId = " + startId + ", Thread ID = " + Thread.currentThread().id
|
||||
)
|
||||
@@ -86,20 +98,20 @@ class OpenP2PService : VpnService() {
|
||||
|
||||
override fun onBind(p0: Intent?): IBinder? {
|
||||
val token = p0?.getStringExtra("token")
|
||||
Log.i(LOG_TAG, "onBind token=$token")
|
||||
Logger.i(LOG_TAG, "onBind token=$token")
|
||||
startOpenP2P(token)
|
||||
return binder
|
||||
}
|
||||
|
||||
private fun startOpenP2P(token : String?): Boolean {
|
||||
private fun startOpenP2P(token: String?): Boolean {
|
||||
if (sdwanRunning) {
|
||||
return true
|
||||
}
|
||||
|
||||
Log.i(LOG_TAG, "startOpenP2P - Thread ID = " + Thread.currentThread().id + token)
|
||||
Logger.i(LOG_TAG, "startOpenP2P - Thread ID = " + Thread.currentThread().id + token)
|
||||
val oldToken = Openp2p.getToken(getExternalFilesDir(null).toString())
|
||||
Log.i(LOG_TAG, "startOpenP2P oldtoken=$oldToken newtoken=$token")
|
||||
if (oldToken=="0" && token==null){
|
||||
Logger.i(LOG_TAG, "startOpenP2P oldtoken=$oldToken newtoken=$token")
|
||||
if (oldToken == "0" && token == null) {
|
||||
return false
|
||||
}
|
||||
sdwanRunning = true
|
||||
@@ -112,7 +124,7 @@ class OpenP2PService : VpnService() {
|
||||
1
|
||||
) // /storage/emulated/0/Android/data/cn.openp2p/files/
|
||||
val isConnect = network.connect(30000) // ms
|
||||
Log.i(LOG_TAG, "login result: " + isConnect.toString());
|
||||
Logger.i(LOG_TAG, "login result: " + isConnect.toString());
|
||||
do {
|
||||
Thread.sleep(1000)
|
||||
} while (network.connect(30000) && running)
|
||||
@@ -123,152 +135,257 @@ class OpenP2PService : VpnService() {
|
||||
|
||||
private fun refreshSDWAN() {
|
||||
GlobalScope.launch {
|
||||
Log.i(OpenP2PService.LOG_TAG, "refreshSDWAN start");
|
||||
Logger.i(OpenP2PService.LOG_TAG, "refreshSDWAN start");
|
||||
while (true) {
|
||||
Log.i(OpenP2PService.LOG_TAG, "waiting new sdwan config");
|
||||
val buf = ByteArray(4096)
|
||||
Logger.i(OpenP2PService.LOG_TAG, "waiting new sdwan config");
|
||||
val buf = ByteArray(32 * 1024)
|
||||
val buffLen = Openp2p.getAndroidSDWANConfig(buf)
|
||||
Log.i(OpenP2PService.LOG_TAG, "closing running sdwan instance");
|
||||
Logger.i(OpenP2PService.LOG_TAG, "closing running sdwan instance");
|
||||
sdwanRunning = false
|
||||
vpnInterface?.close()
|
||||
vpnInterface = null
|
||||
Thread.sleep(10000)
|
||||
runSDWAN(buf.copyOfRange(0,buffLen.toInt() ))
|
||||
sdwanJob?.join()
|
||||
sdwanJob = serviceScope.launch(context = Dispatchers.IO) {
|
||||
runSDWAN(buf.copyOfRange(0, buffLen.toInt()))
|
||||
}
|
||||
}
|
||||
Log.i(OpenP2PService.LOG_TAG, "refreshSDWAN end");
|
||||
Logger.i(OpenP2PService.LOG_TAG, "refreshSDWAN end");
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun readTunLoop() {
|
||||
val inputStream = FileInputStream(vpnInterface?.fileDescriptor).channel
|
||||
if (inputStream==null){
|
||||
Log.i(OpenP2PService.LOG_TAG, "open FileInputStream error: ");
|
||||
if (inputStream == null) {
|
||||
Logger.i(OpenP2PService.LOG_TAG, "open FileInputStream error: ");
|
||||
return
|
||||
}
|
||||
Log.d(LOG_TAG, "read tun loop start")
|
||||
Logger.i(LOG_TAG, "read tun loop start")
|
||||
val buffer = ByteBuffer.allocate(4096)
|
||||
val byteArrayRead = ByteArray(4096)
|
||||
while (sdwanRunning) {
|
||||
buffer.clear()
|
||||
val readBytes = inputStream.read(buffer)
|
||||
if (readBytes <= 0) {
|
||||
// Log.i(OpenP2PService.LOG_TAG, "inputStream.read error: ")
|
||||
delay(1)
|
||||
continue
|
||||
withContext(Dispatchers.IO) {
|
||||
val readBytes = inputStream.read(buffer)
|
||||
if (readBytes > 0) {
|
||||
buffer.flip()
|
||||
buffer.get(byteArrayRead, 0, readBytes)
|
||||
// Logger.i(OpenP2PService.LOG_TAG, String.format("Openp2p.androidRead: %d", readBytes))
|
||||
Openp2p.androidRead(byteArrayRead, readBytes.toLong())
|
||||
// Logger.i(OpenP2PService.LOG_TAG, "inputStream.read error: ")
|
||||
} else {
|
||||
delay(50)
|
||||
}
|
||||
}
|
||||
buffer.flip()
|
||||
buffer.get(byteArrayRead,0,readBytes)
|
||||
Log.i(OpenP2PService.LOG_TAG, String.format("Openp2p.androidRead: %d", readBytes))
|
||||
Openp2p.androidRead(byteArrayRead, readBytes.toLong())
|
||||
|
||||
}
|
||||
Log.d(LOG_TAG, "read tun loop end")
|
||||
Logger.i(LOG_TAG, "read tun loop end")
|
||||
}
|
||||
|
||||
private fun runSDWAN(buf:ByteArray) {
|
||||
sdwanRunning=true
|
||||
sdwanJob=GlobalScope.launch(context = Dispatchers.IO) {
|
||||
Log.i(OpenP2PService.LOG_TAG, "runSDWAN start:${buf.decodeToString()}");
|
||||
try{
|
||||
var builder = Builder()
|
||||
val jsonObject = JSONObject(buf.decodeToString())
|
||||
val id = jsonObject.getLong("id")
|
||||
val name = jsonObject.getString("name")
|
||||
val gateway = jsonObject.getString("gateway")
|
||||
val nodesArray = jsonObject.getJSONArray("Nodes")
|
||||
|
||||
val nodesList = mutableListOf<JSONObject>()
|
||||
for (i in 0 until nodesArray.length()) {
|
||||
nodesList.add(nodesArray.getJSONObject(i))
|
||||
}
|
||||
private suspend fun runSDWAN(buf: ByteArray) {
|
||||
// val localIps = listOf(
|
||||
// "fe80::14b6:a0ff:fe3e:64de" to 64,
|
||||
// "192.168.100.184" to 24,
|
||||
// "10.93.158.91" to 32,
|
||||
// "192.168.3.66" to 24
|
||||
// )
|
||||
//
|
||||
// // 测试用例
|
||||
// val testCases = listOf(
|
||||
// "192.168.3.11" to true,
|
||||
// "192.168.100.1" to true,
|
||||
// "192.168.101.1" to false,
|
||||
// "10.93.158.91" to true,
|
||||
// "10.93.158.90" to false,
|
||||
// "fe80::14b6:a0ff:fe3e:64de" to true,
|
||||
// "fe80::14b6:a0ff:fe3e:64dd" to true // 在同一子网
|
||||
// )
|
||||
//
|
||||
// for ((ip, expected) in testCases) {
|
||||
// val result = isSameSubnet(ip, localIps)
|
||||
// println("Testing IP: $ip, Expected: $expected, Result: $result")
|
||||
// }
|
||||
sdwanRunning = true
|
||||
|
||||
val myNodeName = Openp2p.getAndroidNodeName()
|
||||
Log.i(OpenP2PService.LOG_TAG, "getAndroidNodeName:${myNodeName}");
|
||||
val nodeList = nodesList.map {
|
||||
val nodeName = it.getString("name")
|
||||
val nodeIp = it.getString("ip")
|
||||
if (nodeName==myNodeName){
|
||||
Logger.i(OpenP2PService.LOG_TAG, "runSDWAN start:${buf.decodeToString()}");
|
||||
try {
|
||||
var builder = Builder()
|
||||
val jsonObject = JSONObject(buf.decodeToString())
|
||||
// debug sdwan info
|
||||
// val jsonObject = JSONObject("""{"id":2817104318517097000,"name":"network1","gateway":"10.2.3.254/24","mode":"central","centralNode":"nanjin-192-168-0-82","enable":1,"tunnelNum":3,"mtu":1420,"Nodes":[{"name":"192-168-24-15","ip":"10.2.3.5"},{"name":"Alpine Linux-172.16","ip":"10.2.3.14","resource":"172.16.0.0/24"},{"name":"ctdeMacBook-Pro.local","ip":"10.2.3.22"},{"name":"dengjiandeMBP.sh.chaitin.net","ip":"10.2.3.32"},{"name":"DESKTOP-WIN11-ARM-self","ip":"10.2.3.19"},{"name":"eastdeMBP.sh.chaitin.net","ip":"10.2.3.3"},{"name":"FN-NAS-HP","ip":"10.2.3.1","resource":"192.168.100.0/24"},{"name":"huangruideMBP.sh.chaitin.net","ip":"10.2.3.30"},{"name":"iStoreOS-virtual-machine","ip":"10.2.3.12"},{"name":"k30s-redmi-10.2.33","ip":"10.2.3.27"},{"name":"lincheng-MacBook-Pro-3.sh.chaitin.net","ip":"10.2.3.15"},{"name":"localhost-mi-13","ip":"10.2.3.8"},{"name":"localhost-华为matepad11","ip":"10.2.3.13"},{"name":"luzhanwendeMacBook-Pro.local","ip":"10.2.3.17"},{"name":"Mi-pad2-local","ip":"10.2.3.9"},{"name":"nanjin-192-168-0-82","ip":"10.2.3.34"},{"name":"R7000P-2021","ip":"10.2.3.7"},{"name":"tanxiaolongsMBP.sh.chaitin.net","ip":"10.2.3.20"},{"name":"TUF-AX3000_V2-3804","ip":"10.2.3.25"},{"name":"WIN-CYZ-10.2.3.16","ip":"10.2.3.16"},{"name":"WODOUYAO","ip":"10.2.3.4"},{"name":"Zstrack01","ip":"10.2.3.51","resource":"192.168.24.0/22,192.168.20.0/24"},{"name":"小米14-localhost","ip":"10.2.3.23"}]}""")
|
||||
val id = jsonObject.getLong("id")
|
||||
val mtu = jsonObject.getInt("mtu")
|
||||
val name = jsonObject.getString("name")
|
||||
val gateway = jsonObject.getString("gateway")
|
||||
val nodesArray = jsonObject.getJSONArray("Nodes")
|
||||
|
||||
val nodesList = mutableListOf<JSONObject>()
|
||||
for (i in 0 until nodesArray.length()) {
|
||||
nodesList.add(nodesArray.getJSONObject(i))
|
||||
}
|
||||
|
||||
val myNodeName = Openp2p.getAndroidNodeName()
|
||||
// 使用本地 IP 和子网判断是否需要添加路由
|
||||
val localIps = getLocalIpAndSubnet()
|
||||
Logger.i(OpenP2PService.LOG_TAG, "getAndroidNodeName:${myNodeName}");
|
||||
val nodeList = nodesList.map {
|
||||
val nodeName = it.getString("name")
|
||||
val nodeIp = it.getString("ip")
|
||||
if (nodeName == myNodeName) {
|
||||
try {
|
||||
Logger.i(LOG_TAG, "Attempting to add address: $nodeIp/24")
|
||||
builder.addAddress(nodeIp, 24)
|
||||
Logger.i(LOG_TAG, "Successfully added address")
|
||||
} catch (e: Exception) {
|
||||
Logger.e(LOG_TAG, "Failed to add address $nodeIp: ${e.message}")
|
||||
throw e // or handle gracefully
|
||||
}
|
||||
val nodeResource = if (it.has("resource")) it.getString("resource") else null
|
||||
val parts = nodeResource?.split("/")
|
||||
if (parts?.size == 2) {
|
||||
val ipAddress = parts[0]
|
||||
val subnetMask = parts[1]
|
||||
builder.addRoute(ipAddress, subnetMask.toInt())
|
||||
Log.i(OpenP2PService.LOG_TAG, "sdwan addRoute:${ipAddress},${subnetMask.toInt()}");
|
||||
}
|
||||
val nodeResource = it.optString("resource", null)
|
||||
if (!nodeResource.isNullOrEmpty()) {
|
||||
// 可能是多个网段,用逗号分隔
|
||||
val resourceList = nodeResource.split(",")
|
||||
for (resource in resourceList) {
|
||||
val parts = resource.split("/")
|
||||
if (parts.size == 2) {
|
||||
val ipAddress = parts[0].trim()
|
||||
val subnetMask = parts[1].trim()
|
||||
// 判断是否属于本机网段
|
||||
if (!isSameSubnet(ipAddress, localIps)) {
|
||||
builder.addRoute(ipAddress, subnetMask.toInt())
|
||||
Logger.i(
|
||||
OpenP2PService.LOG_TAG,
|
||||
"sdwan addRoute:${ipAddress},${subnetMask}"
|
||||
)
|
||||
} else {
|
||||
Logger.i(
|
||||
OpenP2PService.LOG_TAG,
|
||||
"Skipped adding route for ${ipAddress}, already in local subnet"
|
||||
)
|
||||
}
|
||||
} else {
|
||||
Logger.w(OpenP2PService.LOG_TAG, "Invalid resource format: $resource")
|
||||
}
|
||||
}
|
||||
Node(nodeName, nodeIp, nodeResource)
|
||||
}
|
||||
|
||||
val network = Network(id, name, gateway, nodeList)
|
||||
println(network)
|
||||
Log.i(OpenP2PService.LOG_TAG, "onBind");
|
||||
builder.addDnsServer("223.5.5.5")
|
||||
builder.addDnsServer("2400:3200::1") // alicloud dns v6 & v4
|
||||
builder.addRoute("10.2.3.0", 24)
|
||||
// builder.addRoute("0.0.0.0", 0);
|
||||
builder.setSession(LOG_TAG!!)
|
||||
builder.setMtu(1420)
|
||||
vpnInterface = builder.establish()
|
||||
if (vpnInterface==null){
|
||||
Log.e(OpenP2PService.LOG_TAG, "start vpnservice error: ");
|
||||
}
|
||||
val outputStream = FileOutputStream(vpnInterface?.fileDescriptor).channel
|
||||
if (outputStream==null){
|
||||
Log.e(OpenP2PService.LOG_TAG, "open FileOutputStream error: ");
|
||||
return@launch
|
||||
}
|
||||
|
||||
val byteArrayWrite = ByteArray(4096)
|
||||
launch {
|
||||
readTunLoop()
|
||||
}
|
||||
|
||||
Log.d(LOG_TAG, "write tun loop start")
|
||||
while (sdwanRunning) {
|
||||
val len = Openp2p.androidWrite(byteArrayWrite)
|
||||
Log.i(OpenP2PService.LOG_TAG, String.format("Openp2p.androidWrite: %d",len));
|
||||
val writeBytes = outputStream?.write(ByteBuffer.wrap(byteArrayWrite))
|
||||
if (writeBytes != null && writeBytes <= 0) {
|
||||
Log.i(OpenP2PService.LOG_TAG, "outputStream?.write error: ");
|
||||
continue
|
||||
}
|
||||
}
|
||||
outputStream.close()
|
||||
// 关闭 VPN 接口
|
||||
vpnInterface?.close()
|
||||
// 置空变量以释放资源
|
||||
vpnInterface = null
|
||||
Log.d(LOG_TAG, "write tun loop end")
|
||||
}catch (e: Exception) {
|
||||
// 捕获异常并记录
|
||||
Log.e("VPN Connection", "发生异常: ${e.message}")
|
||||
Node(nodeName, nodeIp, nodeResource)
|
||||
}
|
||||
Log.i(OpenP2PService.LOG_TAG, "runSDWAN end");
|
||||
|
||||
val network = Network(id, name, gateway, nodeList)
|
||||
println(network)
|
||||
Logger.i(OpenP2PService.LOG_TAG, "onBind");
|
||||
builder.addDnsServer("119.29.29.29")
|
||||
builder.addDnsServer("2400:3200::1") // alicloud dns v6 & v4
|
||||
// builder.addRoute("10.2.3.0", 24)
|
||||
// builder.addRoute("0.0.0.0", 0);
|
||||
val gatewayStr = jsonObject.optString("gateway", "")
|
||||
val subNet = getNetworkAddress(gatewayStr)
|
||||
if (subNet != null) {
|
||||
val (netIp, prefix) = subNet
|
||||
builder.addRoute(netIp, prefix)
|
||||
Logger.i(OpenP2PService.LOG_TAG, "Added route from gateway: $netIp/$prefix")
|
||||
} else {
|
||||
Logger.w(OpenP2PService.LOG_TAG, "Invalid gateway format: $gatewayStr")
|
||||
}
|
||||
|
||||
builder.setSession(LOG_TAG!!)
|
||||
builder.setMtu(mtu)
|
||||
vpnInterface = builder.establish()
|
||||
if (vpnInterface == null) {
|
||||
Log.e(OpenP2PService.LOG_TAG, "start vpnservice error: ");
|
||||
}
|
||||
|
||||
val byteArrayWrite = ByteArray(4096)
|
||||
serviceScope.launch(Dispatchers.IO) {
|
||||
readTunLoop() // 文件读操作,适合 Dispatchers.IO
|
||||
}
|
||||
|
||||
val outputStream = FileOutputStream(vpnInterface?.fileDescriptor).channel
|
||||
if (outputStream == null) {
|
||||
Log.e(OpenP2PService.LOG_TAG, "open FileOutputStream error: ");
|
||||
return
|
||||
}
|
||||
Logger.i(LOG_TAG, "write tun loop start")
|
||||
while (sdwanRunning) {
|
||||
val len = Openp2p.androidWrite(byteArrayWrite, 3000)
|
||||
if (len > mtu || len.toInt() == 0) {
|
||||
continue
|
||||
}
|
||||
try {
|
||||
val writeBytes =
|
||||
outputStream?.write(ByteBuffer.wrap(byteArrayWrite, 0, len.toInt()))
|
||||
if (writeBytes != null && writeBytes <= 0) {
|
||||
Logger.e(LOG_TAG, "outputStream.write failed: $writeBytes")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Logger.e(LOG_TAG, "outputStream.write exception: ${e.message}")
|
||||
e.printStackTrace()
|
||||
continue
|
||||
}
|
||||
}
|
||||
outputStream.close()
|
||||
vpnInterface?.close()
|
||||
vpnInterface = null
|
||||
Logger.i(LOG_TAG, "write tun loop end")
|
||||
} catch (e: Exception) {
|
||||
Logger.i("VPN Connection", "发生异常: ${e.message}")
|
||||
}
|
||||
|
||||
Logger.i(OpenP2PService.LOG_TAG, "runSDWAN end");
|
||||
}
|
||||
/**
|
||||
* 将 "10.2.3.254/16" 这样的 CIDR 转成正确对齐的网络地址,如 "10.2.0.0/16"
|
||||
*/
|
||||
fun getNetworkAddress(cidr: String): Pair<String, Int>? {
|
||||
val parts = cidr.trim().split("/")
|
||||
if (parts.size != 2) return null
|
||||
|
||||
val ip = parts[0]
|
||||
val prefix = parts[1].toIntOrNull() ?: return null
|
||||
if (prefix !in 0..32) return null
|
||||
|
||||
val octets = ip.split(".").map { it.toInt() }
|
||||
if (octets.size != 4) return null
|
||||
|
||||
// 转成整数
|
||||
val ipInt = (octets[0] shl 24) or (octets[1] shl 16) or (octets[2] shl 8) or octets[3]
|
||||
|
||||
// 生成掩码并计算网络地址
|
||||
val mask = if (prefix == 0) 0 else (-1 shl (32 - prefix))
|
||||
val networkInt = ipInt and mask
|
||||
|
||||
// 转回点分十进制
|
||||
val networkIp = listOf(
|
||||
(networkInt shr 24) and 0xFF,
|
||||
(networkInt shr 16) and 0xFF,
|
||||
(networkInt shr 8) and 0xFF,
|
||||
networkInt and 0xFF
|
||||
).joinToString(".")
|
||||
|
||||
return networkIp to prefix
|
||||
}
|
||||
override fun onDestroy() {
|
||||
Log.i(LOG_TAG, "onDestroy - Thread ID = " + Thread.currentThread().id)
|
||||
super.onDestroy()
|
||||
Logger.i(LOG_TAG, "onDestroy - Canceling service scope")
|
||||
serviceScope.cancel() // 取消所有与服务相关的协程
|
||||
}
|
||||
|
||||
override fun onUnbind(intent: Intent?): Boolean {
|
||||
Log.i(LOG_TAG, "onUnbind - Thread ID = " + Thread.currentThread().id)
|
||||
Logger.i(LOG_TAG, "onUnbind - Thread ID = " + Thread.currentThread().id)
|
||||
stopSelf()
|
||||
return super.onUnbind(intent)
|
||||
}
|
||||
|
||||
fun isConnected(): Boolean {
|
||||
if (!::network.isInitialized) return false
|
||||
return network.connect(1000)
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
running=false
|
||||
running = false
|
||||
stopSelf()
|
||||
Openp2p.stop()
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
private fun createNotificationChannel(channelId: String, channelName: String): String? {
|
||||
val chan = NotificationChannel(
|
||||
@@ -281,4 +398,55 @@ class OpenP2PService : VpnService() {
|
||||
service.createNotificationChannel(chan)
|
||||
return channelId
|
||||
}
|
||||
}
|
||||
|
||||
// 获取本机所有IP地址和对应的子网信息
|
||||
fun getLocalIpAndSubnet(): List<Pair<String, Int>> {
|
||||
val localIps = mutableListOf<Pair<String, Int>>()
|
||||
val networkInterfaces = NetworkInterface.getNetworkInterfaces()
|
||||
// 手动添加测试数据
|
||||
//localIps.add(Pair("192.168.3.33", 24))
|
||||
while (networkInterfaces.hasMoreElements()) {
|
||||
val networkInterface = networkInterfaces.nextElement()
|
||||
if (networkInterface.isUp && !networkInterface.isLoopback) {
|
||||
val interfaceAddresses = networkInterface.interfaceAddresses
|
||||
for (interfaceAddress in interfaceAddresses) {
|
||||
val address = interfaceAddress.address
|
||||
val prefixLength = interfaceAddress.networkPrefixLength
|
||||
if (address is InetAddress) {
|
||||
localIps.add(Pair(address.hostAddress, prefixLength.toInt()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return localIps
|
||||
}
|
||||
|
||||
// 判断某个IP是否与本机某网段匹配
|
||||
fun isSameSubnet(ipAddress: String, localIps: List<Pair<String, Int>>): Boolean {
|
||||
val targetIp = InetAddress.getByName(ipAddress).address
|
||||
for ((localIp, prefixLength) in localIps) {
|
||||
val localIpBytes = InetAddress.getByName(localIp).address
|
||||
val mask = createSubnetMask(prefixLength, localIpBytes.size) // 动态生成掩码
|
||||
|
||||
// 比较目标 IP 和本地 IP 的网络部分
|
||||
if (targetIp.indices.all { i ->
|
||||
(targetIp[i].toInt() and mask[i].toInt()) == (localIpBytes[i].toInt() and mask[i].toInt())
|
||||
}) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 根据前缀长度动态生成子网掩码
|
||||
fun createSubnetMask(prefixLength: Int, addressLength: Int): ByteArray {
|
||||
val mask = ByteArray(addressLength)
|
||||
for (i in 0 until prefixLength / 8) {
|
||||
mask[i] = 0xFF.toByte()
|
||||
}
|
||||
if (prefixLength % 8 != 0) {
|
||||
mask[prefixLength / 8] = (0xFF shl (8 - (prefixLength % 8))).toByte()
|
||||
}
|
||||
return mask
|
||||
}
|
||||
@@ -1,45 +1,91 @@
|
||||
package cn.openp2p
|
||||
import android.content.*
|
||||
import android.util.Log
|
||||
import java.text.SimpleDateFormat
|
||||
import java.io.BufferedWriter
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
import android.app.*
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import java.io.IOException
|
||||
import android.net.VpnService
|
||||
import android.os.Binder
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.app.NotificationCompat
|
||||
import cn.openp2p.ui.login.LoginActivity
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import openp2p.Openp2p
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.coroutines.*
|
||||
import org.json.JSONObject
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
object Logger {
|
||||
private val logFile: File = File("app.log")
|
||||
private const val LOG_TAG = "OpenP2PLogger"
|
||||
private var logFile: File? = null
|
||||
private var bufferedWriter: BufferedWriter? = null
|
||||
|
||||
fun log(message: String) {
|
||||
val timestamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())
|
||||
val logMessage = "$timestamp: $message\n"
|
||||
// 初始化日志文件
|
||||
fun init(logDir: File, logFileName: String = "app.log") {
|
||||
if (!logDir.exists()) logDir.mkdirs()
|
||||
logFile = File(logDir, logFileName)
|
||||
|
||||
try {
|
||||
val fileWriter = FileWriter(logFile, true)
|
||||
fileWriter.append(logMessage)
|
||||
fileWriter.close()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
bufferedWriter = BufferedWriter(FileWriter(logFile, true))
|
||||
} catch (e: IOException) {
|
||||
Log.e(LOG_TAG, "Failed to initialize BufferedWriter: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
// 写日志(线程安全)
|
||||
@Synchronized
|
||||
fun log(level: String, tag: String, message: String, throwable: Throwable? = null) {
|
||||
val timestamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())
|
||||
val logMessage = "$timestamp $level $tag: $message"
|
||||
|
||||
// 打印到 console
|
||||
when (level) {
|
||||
"ERROR" -> Log.e(tag, message, throwable)
|
||||
"WARN" -> Log.w(tag, message, throwable)
|
||||
"INFO" -> Log.i(tag, message)
|
||||
"DEBUG" -> Log.d(tag, message)
|
||||
"VERBOSE" -> Log.v(tag, message)
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
try {
|
||||
bufferedWriter?.apply {
|
||||
write(logMessage)
|
||||
newLine()
|
||||
flush()
|
||||
}
|
||||
throwable?.let {
|
||||
bufferedWriter?.apply {
|
||||
write(Log.getStackTraceString(it))
|
||||
newLine()
|
||||
flush()
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e(LOG_TAG, "Failed to write log to file: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
// 清理资源
|
||||
fun close() {
|
||||
try {
|
||||
bufferedWriter?.close()
|
||||
} catch (e: IOException) {
|
||||
Log.e(LOG_TAG, "Failed to close BufferedWriter: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
// 简化方法
|
||||
fun e(tag: String, message: String, throwable: Throwable? = null) {
|
||||
log("ERROR", tag, message, throwable)
|
||||
}
|
||||
|
||||
fun w(tag: String, message: String, throwable: Throwable? = null) {
|
||||
log("WARN", tag, message, throwable)
|
||||
}
|
||||
|
||||
fun i(tag: String, message: String) {
|
||||
log("INFO", tag, message)
|
||||
}
|
||||
|
||||
fun d(tag: String, message: String) {
|
||||
log("DEBUG", tag, message)
|
||||
}
|
||||
|
||||
fun v(tag: String, message: String) {
|
||||
log("VERBOSE", tag, message)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user