mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-24 22:26:29 +09:00
integration: add Android emulator image with test helper app
Headless KVM emulator (google_apis, API 33) plus a small helper app: a device-owner DPC that sets the Tailscale app's managed configuration, and a probe that resolves and fetches as an ordinary app, through the VPN. Updates tailscale/tailscale#15660
This commit is contained in:
1 parent
a491228b7f
commit
1591cfb0f4
6 files changed
+196
No files matched your search
@@ -0,0 +1,81 @@
|
||||
# Android emulator for integration tests (integration/androidic).
|
||||
# Adapted from tailscale-android docker/Dockerfile.android-integration
|
||||
# (Copyright (c) Tailscale Inc & AUTHORS, BSD-3-Clause).
|
||||
#
|
||||
# Needs a host with /dev/kvm; the container must run with --device /dev/kvm.
|
||||
|
||||
FROM --platform=linux/amd64 eclipse-temurin:21-jdk-noble
|
||||
|
||||
ARG ANDROID_API=33
|
||||
ARG ANDROID_CMDLINE_TOOLS_VERSION=9477386
|
||||
ARG ANDROID_BUILD_TOOLS=34.0.0
|
||||
|
||||
ENV ANDROID_HOME=/opt/android-sdk
|
||||
ENV ANDROID_SDK_ROOT=/opt/android-sdk
|
||||
ENV ANDROID_AVD_HOME=/root/.android/avd
|
||||
ENV PATH=$PATH:/opt/android-sdk/cmdline-tools/latest/bin:/opt/android-sdk/platform-tools:/opt/android-sdk/emulator
|
||||
|
||||
RUN apt-get -o Acquire::Retries=5 update && \
|
||||
apt-get -o Acquire::Retries=5 -y install --no-install-recommends \
|
||||
ca-certificates \
|
||||
curl \
|
||||
libdbus-1-3 \
|
||||
libfontconfig1 \
|
||||
libgl1 \
|
||||
libglib2.0-0 \
|
||||
libnss3 \
|
||||
libpulse0 \
|
||||
libstdc++6 \
|
||||
libvulkan1 \
|
||||
libx11-6 \
|
||||
libxcb-cursor0 \
|
||||
libxcb1 \
|
||||
libxext6 \
|
||||
libxi6 \
|
||||
libxrender1 \
|
||||
libxtst6 \
|
||||
unzip \
|
||||
zip && \
|
||||
apt-get -y clean && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN mkdir -p /opt/android-sdk/cmdline-tools /tmp/android-sdk && \
|
||||
curl -fsSL -o /tmp/android-sdk/commandlinetools.zip \
|
||||
"https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_CMDLINE_TOOLS_VERSION}_latest.zip" && \
|
||||
unzip -q /tmp/android-sdk/commandlinetools.zip -d /tmp/android-sdk && \
|
||||
mv /tmp/android-sdk/cmdline-tools /opt/android-sdk/cmdline-tools/latest && \
|
||||
rm -rf /tmp/android-sdk
|
||||
|
||||
RUN yes | sdkmanager --licenses >/dev/null && \
|
||||
sdkmanager \
|
||||
"build-tools;${ANDROID_BUILD_TOOLS}" \
|
||||
"emulator" \
|
||||
"platform-tools" \
|
||||
"platforms;android-${ANDROID_API}" \
|
||||
"system-images;android-${ANDROID_API};google_apis;x86_64"
|
||||
|
||||
RUN mkdir -p /root/.android && touch /root/.android/repositories.cfg && \
|
||||
echo "no" | avdmanager create avd -n test \
|
||||
-k "system-images;android-${ANDROID_API};google_apis;x86_64" --device pixel
|
||||
|
||||
# Test helper app: MDM device policy controller and in-VPN probe.
|
||||
COPY integration/androidic/helper /tmp/helper
|
||||
RUN cd /tmp/helper && \
|
||||
bt=/opt/android-sdk/build-tools/${ANDROID_BUILD_TOOLS} && \
|
||||
jar=/opt/android-sdk/platforms/android-${ANDROID_API}/android.jar && \
|
||||
$bt/aapt2 compile --dir res -o res.zip && \
|
||||
$bt/aapt2 link -I $jar --manifest AndroidManifest.xml -o unsigned.apk res.zip && \
|
||||
javac --release 11 -classpath $jar -d classes $(find src -name '*.java') && \
|
||||
$bt/d8 --lib $jar --release --output . $(find classes -name '*.class') && \
|
||||
zip -q unsigned.apk classes.dex && \
|
||||
$bt/zipalign -f 4 unsigned.apk aligned.apk && \
|
||||
keytool -genkeypair -keystore k.jks -storepass android -keypass android \
|
||||
-alias k -keyalg RSA -dname CN=helper -validity 10000 && \
|
||||
$bt/apksigner sign --ks k.jks --ks-pass pass:android --out /opt/helper.apk aligned.apk && \
|
||||
rm -rf /tmp/helper
|
||||
|
||||
# Headless boot; slirp NAT lets the guest reach the container's docker
|
||||
# network. -no-snapshot keeps every test on a cold, clean device.
|
||||
CMD ["emulator", "-avd", "test", "-no-window", "-no-audio", "-no-snapshot", \
|
||||
"-no-boot-anim", "-gpu", "swiftshader_indirect", "-memory", "4096", "-cores", "4", \
|
||||
"-netdelay", "none", "-netspeed", "full"]
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Test helper: a minimal device policy controller that sets the Tailscale
|
||||
app's managed configuration (MDM) as an EMM would, and a probe that
|
||||
resolves names and fetches URLs from an ordinary app, through the VPN. -->
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.headscale.helper">
|
||||
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="33" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<application android:label="headscale-helper" android:usesCleartextTraffic="true">
|
||||
<receiver
|
||||
android:name=".Admin"
|
||||
android:exported="true"
|
||||
android:permission="android.permission.BIND_DEVICE_ADMIN">
|
||||
<meta-data
|
||||
android:name="android.app.device_admin"
|
||||
android:resource="@xml/device_admin" />
|
||||
<intent-filter>
|
||||
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver android:name=".SetRestrictions" android:exported="true" />
|
||||
<receiver android:name=".Probe" android:exported="true" />
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<device-admin>
|
||||
<uses-policies />
|
||||
</device-admin>
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.headscale.helper;
|
||||
|
||||
import android.app.admin.DeviceAdminReceiver;
|
||||
|
||||
public class Admin extends DeviceAdminReceiver {}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.headscale.helper;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URL;
|
||||
|
||||
// Resolves a name or fetches a URL as an ordinary app would, so traffic and
|
||||
// DNS go through the VPN. Result data is "ok <addrs|status>" or "error <e>":
|
||||
// am broadcast -n org.headscale.helper/.Probe --es resolve host.example
|
||||
// am broadcast -n org.headscale.helper/.Probe --es url http://100.64.0.1/
|
||||
public class Probe extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
final PendingResult result = goAsync();
|
||||
final String host = intent.getStringExtra("resolve");
|
||||
final String url = intent.getStringExtra("url");
|
||||
|
||||
// Network access is not allowed on the main thread.
|
||||
new Thread(() -> {
|
||||
String out;
|
||||
try {
|
||||
out = "ok " + (host != null ? resolve(host) : fetch(url));
|
||||
} catch (Exception e) {
|
||||
out = "error " + e;
|
||||
}
|
||||
result.setResultData(out);
|
||||
result.finish();
|
||||
}).start();
|
||||
}
|
||||
|
||||
private static String resolve(String host) throws Exception {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (InetAddress a : InetAddress.getAllByName(host)) {
|
||||
sb.append(a.getHostAddress()).append(' ');
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
private static String fetch(String url) throws Exception {
|
||||
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
|
||||
c.setConnectTimeout(5000);
|
||||
c.setReadTimeout(5000);
|
||||
try {
|
||||
return Integer.toString(c.getResponseCode());
|
||||
} finally {
|
||||
c.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.headscale.helper;
|
||||
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
// Replaces the target package's managed configuration with the broadcast's
|
||||
// extras:
|
||||
// am broadcast -n org.headscale.helper/.SetRestrictions \
|
||||
// --es package com.tailscale.ipn --es LoginURL http://... --es AuthKey ...
|
||||
public class SetRestrictions extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Bundle extras = intent.getExtras() == null ? new Bundle() : new Bundle(intent.getExtras());
|
||||
String pkg = extras.getString("package", "com.tailscale.ipn");
|
||||
extras.remove("package");
|
||||
|
||||
DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
|
||||
try {
|
||||
dpm.setApplicationRestrictions(new ComponentName(context, Admin.class), pkg, extras);
|
||||
setResultData("ok " + extras.keySet());
|
||||
} catch (SecurityException e) {
|
||||
// Not (yet) device owner; the caller retries.
|
||||
setResultData("error " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user