mirror of
https://github.com/yggdrasil-network/crispa-android.git
synced 2024-11-09 12:01:01 +00:00
feat: Implement basic Yggdrasil service
This commit is contained in:
parent
0779cfaaad
commit
42fa14d545
@ -0,0 +1,3 @@
|
||||
package io.github.chronosx88.yggdrasil
|
||||
|
||||
const val YGGDRASIL_VERSION = "0.3.7"
|
11
app/src/main/java/io/github/chronosx88/yggdrasil/Utils.kt
Normal file
11
app/src/main/java/io/github/chronosx88/yggdrasil/Utils.kt
Normal file
@ -0,0 +1,11 @@
|
||||
package io.github.chronosx88.yggdrasil
|
||||
|
||||
import android.content.Context
|
||||
import java.io.File
|
||||
import java.lang.Runtime.getRuntime
|
||||
|
||||
val Context.yggBin get() = File(filesDir, "yggdrasil-0.3.7")
|
||||
|
||||
fun Context.execYgg(cmd: String) = getRuntime().exec(
|
||||
"${yggBin.absolutePath} $cmd"
|
||||
)
|
@ -0,0 +1,69 @@
|
||||
package io.github.chronosx88.yggdrasil
|
||||
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import android.os.Build.CPU_ABI
|
||||
import android.util.Log
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
class YggdrasilService : Service() {
|
||||
private val LOG_TAG: String = YggdrasilService::class.java.simpleName
|
||||
|
||||
override fun onBind(intent: Intent) = null
|
||||
|
||||
companion object {
|
||||
var daemon: Process? = null
|
||||
}
|
||||
|
||||
private fun installBinary() {
|
||||
val type = CPU_ABI.let {
|
||||
when{
|
||||
it.contains("v8") -> "arm64"
|
||||
it.contains("v7") -> "armhf"
|
||||
else -> throw Exception("Unsupported ABI")
|
||||
}
|
||||
}
|
||||
|
||||
yggBin.apply {
|
||||
delete()
|
||||
createNewFile()
|
||||
}
|
||||
|
||||
val input = assets.open(type)
|
||||
val output = yggBin.outputStream()
|
||||
|
||||
try {
|
||||
input.copyTo(output)
|
||||
} finally {
|
||||
input.close(); output.close()
|
||||
}
|
||||
|
||||
yggBin.setExecutable(true)
|
||||
execYgg("-genconf > yggdrasil.conf").waitFor() // Generate config
|
||||
Log.i(LOG_TAG, "# Binary installed successfully")
|
||||
}
|
||||
|
||||
private fun start() {
|
||||
execYgg("-useconffile yggdrasil.conf").apply {
|
||||
daemon = this
|
||||
}
|
||||
}
|
||||
|
||||
private fun stop() {
|
||||
daemon?.destroy()
|
||||
daemon = null
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
when (intent?.action) {
|
||||
"start" -> start()
|
||||
"stop" -> stop()
|
||||
"restart" -> {
|
||||
stop(); start()
|
||||
}
|
||||
"exit" -> exitProcess(0)
|
||||
}
|
||||
return START_STICKY
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user