diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/Constants.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/Constants.kt new file mode 100644 index 0000000..360aeca --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/Constants.kt @@ -0,0 +1,3 @@ +package io.github.chronosx88.yggdrasil + +const val YGGDRASIL_VERSION = "0.3.7" \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/Utils.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/Utils.kt new file mode 100644 index 0000000..7dcc947 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/Utils.kt @@ -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" +) \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilService.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilService.kt new file mode 100644 index 0000000..8ebd445 --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilService.kt @@ -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 + } +}