refactor: Some refactor and upgrade to yggdrasil 0.3.8

This commit is contained in:
ChronosX88 2019-08-25 14:55:46 +04:00
parent 592fe8138d
commit cfd86afac3
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
8 changed files with 39 additions and 117 deletions

2
.gitignore vendored
View File

@ -13,3 +13,5 @@
.externalNativeBuild
/yggdrasil/yggdrasil.aar
/yggdrasil/yggdrasil-sources.jar
/app/src/main/assets/yggdrasil-0.3.8-linux-arm64
/app/src/main/assets/yggdrasil-0.3.8-linux-armhf

View File

@ -1,3 +1,3 @@
package io.github.chronosx88.yggdrasil
const val YGGDRASIL_VERSION = "0.3.7"
const val YGGDRASIL_VERSION = "0.3.8"

View File

@ -1,7 +1,9 @@
package io.github.chronosx88.yggdrasil
import android.content.Context
import android.os.Build.CPU_ABI
import com.google.gson.Gson
import config.NodeConfig
import io.github.chronosx88.yggdrasil.models.config.Config
import org.hjson.JsonValue
import org.hjson.Stringify
@ -10,26 +12,50 @@ import java.lang.Runtime.getRuntime
val gson = Gson()
val Context.yggBin get() = File(filesDir, "yggdrasil-0.3.7")
fun Context.execYgg(cmd: String) = getRuntime().exec(
"${yggBin.absolutePath} $cmd"
)
val Context.yggBin get() = File(filesDir, "yggdrasil-$YGGDRASIL_VERSION-linux-${CPU_ABI.let {
when {
it.contains("v7") -> "armhf"
it.contains("v8") -> "arm64"
else -> throw Exception("Unsupported ABI")
}
}
}")
@Throws(RuntimeException::class)
fun Context.getYggConfig(): Config {
val configFile = File(filesDir, "yggdrasil.conf")
if(!configFile.exists()) {
throw RuntimeException("Config file don't exist!")
generateYggConfig()
}
val configStr = configFile.readText()
val configHjsonObject = JsonValue.readHjson(configStr)
return gson.fromJson(configHjsonObject.toString(Stringify.PLAIN), Config::class.java)
}
fun Context.generateYggConfig() {
execYgg("-genconf > yggdrasil.conf").waitFor()
}
fun createNativeYggConfig(config: Config): NodeConfig {
val nativeConfig = NodeConfig()
nativeConfig.adminListen = config.adminListen
nativeConfig.encryptionPrivateKey = config.encryptionPrivateKey
nativeConfig.encryptionPublicKey = config.encryptionPublicKey
nativeConfig.ifMTU = config.ifMTU
nativeConfig.ifName = config.ifName
nativeConfig.ifTAPMode = config.ifTAPMode
nativeConfig.nodeInfoPrivacy = config.nodeInfoPrivacy
nativeConfig.signingPrivateKey = config.signingPrivateKey
nativeConfig.signingPublicKey = config.signingPublicKey
return nativeConfig
}
fun Context.saveYggConfig(config: Config) {
val configJson = gson.toJson(config)
val configHjson = JsonValue.readHjson(configJson).toString(Stringify.HJSON)
val configFile = File(filesDir, "yggdrasil.conf")
configFile.writeText(configHjson)
configFile.writeText(configJson)
}

View File

@ -16,13 +16,13 @@ class YggdrasilService : Service() {
}
private fun installBinary() {
val type = CPU_ABI.let {
val type = "yggdrasil-$YGGDRASIL_VERSION-linux-${CPU_ABI.let {
when{
it.contains("v8") -> "arm64"
//it.contains("v7") -> "armhf"
it.contains("v7") -> "armhf"
else -> throw Exception("Unsupported ABI")
}
}
}}"
yggBin.apply {
delete()
@ -39,7 +39,7 @@ class YggdrasilService : Service() {
}
yggBin.setExecutable(true)
execYgg("-genconf > yggdrasil.conf").waitFor() // Generate config
generateYggConfig()
Log.i(LOG_TAG, "# Binary installed successfully")
}

View File

@ -1,45 +0,0 @@
package io.github.chronosx88.yggdrasil.address;
import androidx.annotation.NonNull;
public class NodeID {
public static final short NODEID_LENGTH = 64; // SHA512 size
private byte[] bytes;
public NodeID(String hex) {
bytes = Utils.hexToBytes(hex);
}
public byte[] getBytes() {
return bytes;
}
@NonNull
@Override
public String toString() {
return Utils.bytesToHex(bytes);
}
private static class Utils {
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
private static byte[] hexToBytes(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
}

View File

@ -1,61 +0,0 @@
package io.github.chronosx88.yggdrasil.address;
import java.io.ByteArrayOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class YggAddress {
private static final short IPV6_ADDRESS_LENGTH = 16; // represents an IPv6 address in the yggdrasil address range.
private static final short IPV6_SUBNET_LENGTH = 16; // represents an IPv6 /64 subnet in the yggdrasil subnet range.
private byte[] addressBytes;
private InetAddress address;
public YggAddress(NodeID nodeID) {
addressBytes = new byte[IPV6_ADDRESS_LENGTH];
ByteArrayOutputStream temp = new ByteArrayOutputStream();
boolean done = false;
byte ones = 0;
byte bits = 0;
byte nBits = 0;
for(int idx = 0; idx < 8 * nodeID.getBytes().length; idx++) {
byte bit = (byte) ((nodeID.getBytes()[idx/8] & (0x80 >> (byte)(idx%8))) >> (byte)(7-(idx%8)));
if(!done && bit != 0) {
ones++;
continue;
}
if(!done && bit == 0) {
done = true;
continue;
}
bits = (byte) ((bits << 1) | bit);
nBits++;
if(nBits == 8) {
nBits = 0;
temp.write(bits);
}
}
byte[] prefix = getPrefix();
System.arraycopy(prefix, 0, addressBytes, 0, prefix.length);
addressBytes[prefix.length] = ones;
System.arraycopy(temp.toByteArray(), 0, addressBytes, prefix.length+1, addressBytes.length-(prefix.length+1));
try {
address = InetAddress.getByAddress(addressBytes);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public byte[] getAddressBytes() {
return addressBytes;
}
public InetAddress getInetAddress() {
return address;
}
public static byte[] getPrefix() {
return new byte[]{0x02};
}
}

View File

@ -16,7 +16,7 @@ data class Config (
@SerializedName("linkLocalTCPPort") val linkLocalTCPPort : Int,
@SerializedName("ifName") val ifName : String,
@SerializedName("ifTAPMode") val ifTAPMode : Boolean,
@SerializedName("ifMTU") val ifMTU : Int,
@SerializedName("ifMTU") val ifMTU : Long,
@SerializedName("sessionFirewall") val sessionFirewall : SessionFirewall,
@SerializedName("tunnelRouting") val tunnelRouting : TunnelRouting,
@SerializedName("switchOptions") val switchOptions : SwitchOptions,