Export Root Certificate P12 (#185)

This commit is contained in:
wanghongenpin
2024-07-25 00:29:55 +08:00
parent 134d9efa89
commit b1a17eb349
14 changed files with 275 additions and 45 deletions

View File

@@ -5,7 +5,6 @@ import android.net.ConnectivityManager
import android.os.Build
import android.os.Process
import android.system.OsConstants
import com.google.common.cache.CacheBuilder
import com.network.proxy.plugin.ProcessInfo
import com.network.proxy.vpn.Connection
import java.net.InetSocketAddress
@@ -24,13 +23,12 @@ class ProcessInfoManager private constructor() {
class NetworkInfo(val uid: Int, val remoteHost: String, val remotePort: Int)
private val localPortMap =
CacheBuilder.newBuilder().maximumSize(10_000).expireAfterAccess(60, TimeUnit.SECONDS)
.build<Int, NetworkInfo>()
private val localPortCache =
SimpleCache<Int, NetworkInfo>(10_000, 60, TimeUnit.SECONDS)
private val appInfoCache = SimpleCache<Int, ProcessInfo>(10_000, 300, TimeUnit.SECONDS)
private val appInfoCache =
CacheBuilder.newBuilder().maximumSize(10_000).expireAfterAccess(300, TimeUnit.SECONDS)
.build<Int, ProcessInfo>()
var activity: Context? = null
@@ -51,7 +49,7 @@ class ProcessInfoManager private constructor() {
val localAddress = channel.localAddress as InetSocketAddress
val networkInfo =
NetworkInfo(uid, destinationAddress.hostString, destinationAddress.port)
localPortMap.put(localAddress.port, networkInfo)
localPortCache.put(localAddress.port, networkInfo)
}
}
@@ -63,7 +61,7 @@ class ProcessInfoManager private constructor() {
val channel = connection.channel
if (channel is SocketChannel) {
val localAddress = channel.localAddress as InetSocketAddress
localPortMap.invalidate(localAddress.port)
localPortCache.remove(localAddress.port)
}
}
@@ -102,7 +100,7 @@ class ProcessInfoManager private constructor() {
}
fun getProcessInfoByPort(localPort: Int): ProcessInfo? {
val networkInfo = localPortMap.getIfPresent(localPort)
val networkInfo = localPortCache.get(localPort)
if (networkInfo != null) {
val processInfo = getProcessInfo(networkInfo.uid)
@@ -115,7 +113,7 @@ class ProcessInfoManager private constructor() {
}
private fun getProcessInfo(uid: Int): ProcessInfo? {
var appInfo = appInfoCache.getIfPresent(uid)
var appInfo = appInfoCache.get(uid)
if (appInfo != null) return appInfo
val packageManager = activity?.packageManager

View File

@@ -0,0 +1,68 @@
package com.network.proxy.vpn.util
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
class SimpleCache<K, V>(
private val maxSize: Int,
private val expireAfterAccess: Long,
private val timeUnit: TimeUnit
) {
private val cache = ConcurrentHashMap<K, CacheEntry<V>>()
companion object {
private val EXECUTOR = Executors.newSingleThreadScheduledExecutor()
}
init {
EXECUTOR.scheduleWithFixedDelay(
{ cleanUp() },
expireAfterAccess,
expireAfterAccess,
timeUnit
)
}
fun put(key: K, value: V) {
if (cache.size >= maxSize) {
cache.keys.iterator().next()?.let { cache.remove(it) }
}
cache[key] = CacheEntry(value, System.nanoTime())
}
fun get(key: K): V? {
val entry = cache[key] ?: return null
if (System.nanoTime() - entry.lastAccessTime > timeUnit.toNanos(expireAfterAccess)) {
cache.remove(key)
return null
}
entry.lastAccessTime = System.nanoTime()
return entry.value
}
fun remove(key: K) {
cache.remove(key)
}
fun clear() {
cache.clear()
}
private fun cleanUp() {
val now = System.nanoTime()
val expirationTime = timeUnit.toNanos(expireAfterAccess)
val iterator = cache.entries.iterator()
while (iterator.hasNext()) {
val entry = iterator.next()
if (now - entry.value.lastAccessTime > expirationTime) {
iterator.remove()
}
}
}
private data class CacheEntry<V>(val value: V, var lastAccessTime: Long)
}