BuildChain v1.0.0 — on-device Android build toolchain

System-level KernelSU module: aapt2/aidl/zipalign (API 35), BusyBox,
platform-tools, all as static arm64 binaries in the Android framework.

Termux integration for Java/Kotlin/Gradle/Python via pkg.
CLI tools: buildchain, bc-build, bc-sign.
WebUI on :8089 for toolchain management.

Turns any Android phone into a native compilation powerhouse.
This commit is contained in:
sssnake
2026-03-31 07:18:31 -07:00
commit 6a7c85dc26
31 changed files with 1312 additions and 0 deletions

168
scripts/bc-build Executable file
View File

@@ -0,0 +1,168 @@
#!/system/bin/sh
# bc-build — compile Android project from source on-device
# Supports: single Java/Kotlin file, Gradle project, or raw aapt2+javac+d8 pipeline
#
# Usage:
# bc-build <file.java> Compile single Java file to APK
# bc-build <file.kt> Compile single Kotlin file to APK
# bc-build <project_dir> Run gradle assembleDebug
# bc-build --raw <src_dir> <out> Manual aapt2+javac+d8 pipeline
CONFIG_DIR="/data/adb/buildchain"
MODDIR=$(cat "$CONFIG_DIR/moddir" 2>/dev/null)
TOOLS="$MODDIR/tools"
AAPT2="$TOOLS/build-tools/aapt2"
AIDL="$TOOLS/build-tools/aidl"
ZIPALIGN="$TOOLS/build-tools/zipalign"
TBIN="/data/data/com.termux/files/usr/bin"
ANDROID_JAR="/data/data/com.termux/files/home/android-sdk/platforms/android-35/android.jar"
# Fallback android.jar location
[ ! -f "$ANDROID_JAR" ] && ANDROID_JAR="/system/framework/framework.jar"
die() { echo "ERROR: $1"; exit 1; }
build_single_java() {
local src="$1"
local name=$(basename "$src" .java)
local outdir="${2:-./build}"
echo "=== Building $name.java ==="
mkdir -p "$outdir/classes" "$outdir/dex" "$outdir/apk"
echo "[1/5] Compiling Java..."
"$TBIN/javac" -source 11 -target 11 -cp "$ANDROID_JAR" -d "$outdir/classes" "$src" || die "javac failed"
echo "[2/5] Converting to DEX..."
"$TBIN/d8" --min-api 28 --output "$outdir/dex/" "$outdir/classes"/*.class 2>/dev/null \
|| "$TBIN/java" -jar "$TBIN/../share/d8/d8.jar" --min-api 28 --output "$outdir/dex/" "$outdir/classes"/*.class \
|| die "d8/dex conversion failed"
echo "[3/5] Packaging APK..."
# Create minimal AndroidManifest.xml
cat > "$outdir/AndroidManifest.xml" << MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.buildchain.$name">
<application android:label="$name">
<activity android:name=".$name" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
MANIFEST
"$AAPT2" link -o "$outdir/apk/$name.unsigned.apk" \
--manifest "$outdir/AndroidManifest.xml" \
-I "$ANDROID_JAR" 2>/dev/null || die "aapt2 link failed"
# Add DEX to APK
cd "$outdir/dex" && zip -u "../apk/$name.unsigned.apk" classes.dex 2>/dev/null && cd - >/dev/null
echo "[4/5] Aligning..."
"$ZIPALIGN" -f 4 "$outdir/apk/$name.unsigned.apk" "$outdir/apk/$name.aligned.apk" || die "zipalign failed"
echo "[5/5] Signing..."
bc-sign "$outdir/apk/$name.aligned.apk" "$outdir/$name.apk"
echo ""
echo "Output: $outdir/$name.apk"
ls -la "$outdir/$name.apk" 2>/dev/null
}
build_single_kotlin() {
local src="$1"
local name=$(basename "$src" .kt)
local outdir="${2:-./build}"
echo "=== Building $name.kt ==="
mkdir -p "$outdir/classes" "$outdir/dex" "$outdir/apk"
echo "[1/5] Compiling Kotlin..."
"$TBIN/kotlinc" -cp "$ANDROID_JAR" -d "$outdir/classes" "$src" || die "kotlinc failed"
echo "[2/5] Converting to DEX..."
find "$outdir/classes" -name "*.class" | xargs "$TBIN/d8" --min-api 28 --output "$outdir/dex/" 2>/dev/null \
|| die "d8 failed"
echo "[3/5] Packaging..."
cat > "$outdir/AndroidManifest.xml" << MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.buildchain.$name">
<application android:label="$name"/>
</manifest>
MANIFEST
"$AAPT2" link -o "$outdir/apk/$name.unsigned.apk" \
--manifest "$outdir/AndroidManifest.xml" \
-I "$ANDROID_JAR" 2>/dev/null || die "aapt2 link failed"
cd "$outdir/dex" && zip -u "../apk/$name.unsigned.apk" classes.dex 2>/dev/null && cd - >/dev/null
echo "[4/5] Aligning..."
"$ZIPALIGN" -f 4 "$outdir/apk/$name.unsigned.apk" "$outdir/apk/$name.aligned.apk"
echo "[5/5] Signing..."
bc-sign "$outdir/apk/$name.aligned.apk" "$outdir/$name.apk"
echo ""
echo "Output: $outdir/$name.apk"
}
build_gradle_project() {
local projdir="$1"
echo "=== Building Gradle project: $projdir ==="
if [ ! -f "$projdir/build.gradle" ] && [ ! -f "$projdir/build.gradle.kts" ]; then
die "No build.gradle found in $projdir"
fi
cd "$projdir"
# Set ANDROID_HOME for Gradle
export ANDROID_HOME="/data/data/com.termux/files/home/android-sdk"
export ANDROID_SDK_ROOT="$ANDROID_HOME"
if [ -f "./gradlew" ]; then
chmod +x ./gradlew
./gradlew assembleDebug || die "Gradle build failed"
elif command -v gradle >/dev/null 2>&1; then
gradle assembleDebug || die "Gradle build failed"
else
die "No gradlew and gradle not installed. Run: buildchain setup"
fi
echo ""
echo "Output APKs:"
find . -name "*.apk" -path "*/build/outputs/*" 2>/dev/null
}
# Main
case "$1" in
--help|-h|"")
echo "bc-build — compile Android projects on-device"
echo ""
echo "Usage:"
echo " bc-build MyApp.java Compile Java to APK"
echo " bc-build MyApp.kt Compile Kotlin to APK"
echo " bc-build ./my-project/ Run Gradle assembleDebug"
echo " bc-build MyApp.java ./out/ Specify output directory"
;;
*.java)
build_single_java "$1" "$2"
;;
*.kt)
build_single_kotlin "$1" "$2"
;;
*)
if [ -d "$1" ]; then
build_gradle_project "$1"
else
die "Unknown input: $1 (expected .java, .kt, or directory)"
fi
;;
esac

83
scripts/bc-sign Executable file
View File

@@ -0,0 +1,83 @@
#!/system/bin/sh
# bc-sign — sign APKs with debug or release keystore
# Usage: bc-sign <input.apk> [output.apk] [--release keystore.jks]
CONFIG_DIR="/data/adb/buildchain"
TBIN="/data/data/com.termux/files/usr/bin"
DEBUG_KS="$CONFIG_DIR/debug.keystore"
DEBUG_PASS="android"
DEBUG_ALIAS="androiddebugkey"
die() { echo "ERROR: $1"; exit 1; }
# Create debug keystore if it doesn't exist
ensure_debug_keystore() {
if [ ! -f "$DEBUG_KS" ]; then
echo "Creating debug keystore..."
"$TBIN/keytool" -genkey -v \
-keystore "$DEBUG_KS" \
-storepass "$DEBUG_PASS" \
-alias "$DEBUG_ALIAS" \
-keypass "$DEBUG_PASS" \
-keyalg RSA -keysize 2048 -validity 10000 \
-dname "CN=Android Debug,O=Android,C=US" 2>/dev/null || die "keytool failed — is Java installed? Run: buildchain setup"
fi
}
INPUT="$1"
OUTPUT="${2:-$(echo "$INPUT" | sed 's/\.unsigned\.apk$/.apk/' | sed 's/\.aligned\.apk$/.apk/')}"
[ -z "$INPUT" ] && { echo "Usage: bc-sign <input.apk> [output.apk] [--release keystore.jks]"; exit 1; }
[ ! -f "$INPUT" ] && die "File not found: $INPUT"
# Check for release signing
if [ "$3" = "--release" ] && [ -n "$4" ]; then
KEYSTORE="$4"
[ ! -f "$KEYSTORE" ] && die "Keystore not found: $KEYSTORE"
echo "Signing with release key: $KEYSTORE"
read -p "Keystore password: " KS_PASS
read -p "Key alias: " KS_ALIAS
read -p "Key password: " KEY_PASS
"$TBIN/apksigner" sign \
--ks "$KEYSTORE" \
--ks-pass "pass:$KS_PASS" \
--ks-key-alias "$KS_ALIAS" \
--key-pass "pass:$KEY_PASS" \
--v1-signing-enabled true \
--v2-signing-enabled true \
--v3-signing-enabled true \
--out "$OUTPUT" \
"$INPUT" || die "apksigner failed"
else
# Debug sign
ensure_debug_keystore
# Try apksigner first (from SDK), fall back to jarsigner
if command -v apksigner >/dev/null 2>&1 || [ -f "$TBIN/apksigner" ]; then
SIGNER=$(command -v apksigner 2>/dev/null || echo "$TBIN/apksigner")
"$SIGNER" sign \
--ks "$DEBUG_KS" \
--ks-pass "pass:$DEBUG_PASS" \
--ks-key-alias "$DEBUG_ALIAS" \
--key-pass "pass:$DEBUG_PASS" \
--out "$OUTPUT" \
"$INPUT" 2>/dev/null && echo "Signed (apksigner): $OUTPUT" && exit 0
fi
# Fallback to jarsigner
if [ -f "$TBIN/jarsigner" ]; then
cp "$INPUT" "$OUTPUT" 2>/dev/null
"$TBIN/jarsigner" \
-keystore "$DEBUG_KS" \
-storepass "$DEBUG_PASS" \
-keypass "$DEBUG_PASS" \
-signedjar "$OUTPUT" \
"$INPUT" \
"$DEBUG_ALIAS" 2>/dev/null && echo "Signed (jarsigner): $OUTPUT" && exit 0
fi
die "No signing tool found. Install Java: buildchain setup"
fi
echo "Signed: $OUTPUT"

164
scripts/buildchain Executable file
View File

@@ -0,0 +1,164 @@
#!/system/bin/sh
# BuildChain — main CLI tool
# Usage: buildchain <command> [args]
CONFIG_DIR="/data/adb/buildchain"
MODDIR=$(cat "$CONFIG_DIR/moddir" 2>/dev/null)
TOOLS="$MODDIR/tools"
VERSION="1.0.0"
usage() {
echo "BuildChain v$VERSION — On-device Android build toolchain"
echo ""
echo "Usage: buildchain <command>"
echo ""
echo "Commands:"
echo " status Show installed toolchain components"
echo " setup Install Java, Python, Kotlin, Gradle via Termux"
echo " env Print current build environment"
echo " paths Show all tool paths"
echo " test Test all build tools"
echo " version Show version info"
echo ""
echo "Build helpers:"
echo " bc-build Compile an Android project"
echo " bc-sign Sign an APK with debug or release key"
echo ""
echo "WebUI: http://localhost:8089"
}
cmd_status() {
echo "=== BuildChain Status ==="
echo ""
echo "[Build Tools — API 35]"
for tool in aapt aapt2 aidl dexdump zipalign split-select; do
local path="$TOOLS/build-tools/$tool"
if [ -f "$path" ]; then
echo " [OK] $tool"
else
echo " [--] $tool (missing)"
fi
done
echo ""
echo "[Platform Tools]"
for tool in adb fastboot sqlite3 e2fsdroid etc1tool mke2fs make_f2fs sload_f2fs; do
local path="$TOOLS/platform-tools/$tool"
if [ -f "$path" ]; then
echo " [OK] $tool"
else
echo " [--] $tool (missing)"
fi
done
echo ""
echo "[BusyBox]"
if [ -f "$TOOLS/busybox" ]; then
local applets=$("$TOOLS/busybox" --list 2>/dev/null | wc -l)
echo " [OK] busybox ($applets applets)"
else
echo " [--] busybox (missing)"
fi
echo ""
echo "[Termux Packages]"
local tbin="/data/data/com.termux/files/usr/bin"
for pkg in java javac python3 kotlin kotlinc gradle git cmake make gcc g++ clang; do
if [ -f "$tbin/$pkg" ]; then
echo " [OK] $pkg"
else
echo " [--] $pkg (run: buildchain setup)"
fi
done
echo ""
echo "[Android Environment]"
echo " Device: $(getprop ro.product.model)"
echo " Android: $(getprop ro.build.version.release) (API $(getprop ro.build.version.sdk))"
echo " Arch: $(uname -m)"
echo " Kernel: $(uname -r)"
}
cmd_env() {
[ -f "$CONFIG_DIR/env.sh" ] && cat "$CONFIG_DIR/env.sh" || echo "Environment not configured. Run: buildchain setup"
}
cmd_paths() {
echo "=== Tool Paths ==="
echo "Build tools: $TOOLS/build-tools/"
echo "Platform tools: $TOOLS/platform-tools/"
echo "BusyBox: $TOOLS/busybox"
echo "Config: $CONFIG_DIR/"
echo "Module: $MODDIR"
echo ""
echo "=== Resolved Binaries ==="
for tool in aapt2 aidl zipalign java javac python3 kotlinc gradle git make cmake clang busybox; do
local p=$(which "$tool" 2>/dev/null)
[ -n "$p" ] && echo " $tool -> $p" || echo " $tool -> (not found)"
done
}
cmd_test() {
echo "=== Testing Build Tools ==="
echo ""
echo "aapt2:"
"$TOOLS/build-tools/aapt2" version 2>&1 && echo " PASS" || echo " FAIL"
echo ""
echo "aidl:"
"$TOOLS/build-tools/aidl" --version 2>&1 && echo " PASS" || echo " FAIL"
echo ""
echo "zipalign:"
"$TOOLS/build-tools/zipalign" 2>&1 | head -1 && echo " PASS" || echo " FAIL"
echo ""
echo "dexdump:"
"$TOOLS/build-tools/dexdump" 2>&1 | head -1 && echo " PASS" || echo " FAIL"
echo ""
echo "busybox:"
"$TOOLS/busybox" --help 2>&1 | head -1 && echo " PASS" || echo " FAIL"
echo ""
# Test Termux tools if available
local tbin="/data/data/com.termux/files/usr/bin"
if [ -f "$tbin/java" ]; then
echo "java:"
"$tbin/java" -version 2>&1 | head -1
echo ""
fi
if [ -f "$tbin/python3" ]; then
echo "python:"
"$tbin/python3" --version 2>&1
echo ""
fi
if [ -f "$tbin/kotlinc" ]; then
echo "kotlin:"
"$tbin/kotlinc" -version 2>&1 | head -1
echo ""
fi
if [ -f "$tbin/gradle" ]; then
echo "gradle:"
"$tbin/gradle" --version 2>&1 | grep "Gradle " | head -1
echo ""
fi
}
cmd_version() {
echo "BuildChain v$VERSION"
echo "Build tools: API 35 (35.0.2)"
echo "Target: $(getprop ro.product.model) / Android $(getprop ro.build.version.release)"
}
case "$1" in
status) cmd_status ;;
setup) sh "$MODDIR/scripts/buildchain-setup" ;;
env) cmd_env ;;
paths) cmd_paths ;;
test) cmd_test ;;
version) cmd_version ;;
*) usage ;;
esac

91
scripts/buildchain-setup Executable file
View File

@@ -0,0 +1,91 @@
#!/system/bin/sh
# BuildChain Setup — installs Java, Python, Kotlin, Gradle, and dev tools via Termux
# Must be run from within Termux (or any shell with access to pkg/apt)
TERMUX_PREFIX="/data/data/com.termux/files/usr"
TERMUX_BIN="$TERMUX_PREFIX/bin"
echo "=== BuildChain Setup ==="
echo ""
# Check if we're in a Termux-capable environment
if [ ! -f "$TERMUX_BIN/pkg" ] && ! command -v pkg >/dev/null 2>&1; then
echo "ERROR: Termux pkg not found."
echo "This script must be run inside Termux or after Termux is installed."
echo ""
echo "Install Termux from F-Droid or GitHub, open it once, then run this again."
exit 1
fi
PKG="pkg"
command -v pkg >/dev/null 2>&1 || PKG="$TERMUX_BIN/pkg"
echo "[1/7] Updating package database..."
$PKG update -y 2>&1 | tail -3
echo ""
echo "[2/7] Installing Java (OpenJDK 21)..."
$PKG install -y openjdk-21 2>&1 | tail -3
echo " $(java -version 2>&1 | head -1)"
echo ""
echo "[3/7] Installing Python..."
$PKG install -y python 2>&1 | tail -3
echo " $(python3 --version 2>&1)"
echo ""
echo "[4/7] Installing Kotlin..."
$PKG install -y kotlin 2>&1 | tail -3
echo " $(kotlinc -version 2>&1 | head -1)"
echo ""
echo "[5/7] Installing Gradle..."
$PKG install -y gradle 2>&1 | tail -3
echo " $(gradle --version 2>&1 | grep 'Gradle ' | head -1)"
echo ""
echo "[6/7] Installing build essentials (git, cmake, make, clang, ndk tools)..."
$PKG install -y git cmake make clang ndk-sysroot binutils 2>&1 | tail -3
echo ""
echo "[7/7] Installing extra tools (curl, wget, zip, unzip, tar, openssh, jq)..."
$PKG install -y curl wget zip unzip tar openssh jq 2>&1 | tail -3
echo ""
echo "=== Setting up Android SDK structure ==="
ANDROID_HOME="/data/data/com.termux/files/home/android-sdk"
mkdir -p "$ANDROID_HOME/build-tools/35.0.2"
mkdir -p "$ANDROID_HOME/platform-tools"
# Symlink our build tools into the SDK structure so Gradle/AGP finds them
MODDIR=$(cat /data/adb/buildchain/moddir 2>/dev/null)
if [ -n "$MODDIR" ]; then
for tool in aapt aapt2 aidl dexdump zipalign split-select; do
ln -sf "$MODDIR/tools/build-tools/$tool" "$ANDROID_HOME/build-tools/35.0.2/$tool" 2>/dev/null
done
for tool in adb fastboot sqlite3; do
ln -sf "$MODDIR/tools/platform-tools/$tool" "$ANDROID_HOME/platform-tools/$tool" 2>/dev/null
done
echo " SDK structure created at $ANDROID_HOME"
fi
echo ""
echo "=== Setup Complete ==="
echo ""
echo "Installed:"
echo " Java: $(java -version 2>&1 | head -1)"
echo " Python: $(python3 --version 2>&1)"
echo " Kotlin: $(kotlinc -version 2>&1 | head -1)"
echo " Gradle: $(gradle --version 2>&1 | grep 'Gradle ' | head -1)"
echo " Git: $(git --version 2>&1)"
echo " CMake: $(cmake --version 2>&1 | head -1)"
echo " Clang: $(clang --version 2>&1 | head -1)"
echo ""
echo "Build tools: aapt2, aidl, zipalign (API 35)"
echo "BusyBox: $(busybox 2>&1 | head -1)"
echo ""
echo "Run 'buildchain status' to verify everything."
echo "Run 'buildchain test' to test all tools."
echo ""
echo "Source the environment in your shell:"
echo " source /data/adb/buildchain/env.sh"

157
scripts/webui-server Executable file
View File

@@ -0,0 +1,157 @@
#!/system/bin/sh
# BuildChain WebUI — HTTP server on port 8089
MODDIR=$(cat /data/adb/buildchain/moddir 2>/dev/null)
WEBROOT="$MODDIR/webroot"
CONFIG_DIR="/data/adb/buildchain"
TOOLS="$MODDIR/tools"
PORT=8089
TERMUX_BIN="/data/data/com.termux/files/usr/bin"
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] [webui] $1"; }
json_val() { echo "$1" | sed -n "s/.*\"$2\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p"; }
get_mime() {
case "$1" in *.html) echo "text/html";; *.css) echo "text/css";; *.js) echo "text/javascript";; *) echo "application/json";; esac
}
send_response() {
printf "HTTP/1.1 %s\r\nContent-Type: %s\r\nContent-Length: %d\r\nConnection: close\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET,POST,OPTIONS\r\nAccess-Control-Allow-Headers: Content-Type\r\n\r\n%s" \
"$1" "$2" "${#3}" "$3"
}
send_file() {
[ -f "$1" ] && send_response "200 OK" "$(get_mime "$1")" "$(cat "$1")" || send_response "404 Not Found" "text/plain" "Not Found"
}
# API: get full environment status
api_status() {
cat "$CONFIG_DIR/environment.json" 2>/dev/null || echo '{"error":"not detected yet"}'
}
# API: list all tools and their paths/status
api_tools() {
local result="["
local first=1
for tool in aapt aapt2 aidl dexdump zipalign split-select; do
local path="$TOOLS/build-tools/$tool"
local exists="false"; [ -f "$path" ] && exists="true"
local size=$(stat -c%s "$path" 2>/dev/null || echo 0)
[ "$first" = "1" ] && first=0 || result="$result,"
result="$result{\"name\":\"$tool\",\"category\":\"build-tools\",\"path\":\"$path\",\"exists\":$exists,\"size\":$size}"
done
for tool in adb fastboot sqlite3 e2fsdroid etc1tool mke2fs make_f2fs sload_f2fs hprof-conv; do
local path="$TOOLS/platform-tools/$tool"
local exists="false"; [ -f "$path" ] && exists="true"
local size=$(stat -c%s "$path" 2>/dev/null || echo 0)
result="$result,{\"name\":\"$tool\",\"category\":\"platform-tools\",\"path\":\"$path\",\"exists\":$exists,\"size\":$size}"
done
result="$result,{\"name\":\"busybox\",\"category\":\"system\",\"path\":\"$TOOLS/busybox\",\"exists\":$([ -f "$TOOLS/busybox" ] && echo true || echo false),\"size\":$(stat -c%s "$TOOLS/busybox" 2>/dev/null || echo 0)}"
# Termux packages
for tool in java javac python3 kotlin kotlinc gradle git cmake make clang gcc g++ d8 apksigner jarsigner keytool; do
local tpath="$TERMUX_BIN/$tool"
local exists="false"; [ -f "$tpath" ] && exists="true"
result="$result,{\"name\":\"$tool\",\"category\":\"termux\",\"path\":\"$tpath\",\"exists\":$exists,\"size\":0}"
done
echo "${result}]"
}
# API: get current PATH
api_paths() {
local syspath=$(echo "$PATH" | tr ':' '\n')
local result="["
local first=1
for p in $syspath; do
[ "$first" = "1" ] && first=0 || result="$result,"
result="$result\"$p\""
done
echo "${result}]"
}
# API: run buildchain test
api_test() {
local result=""
result=$(sh "$MODDIR/scripts/buildchain" test 2>&1)
result=$(echo "$result" | sed 's/\\/\\\\/g; s/"/\\"/g' | tr '\n' '|' | sed 's/|/\\n/g')
echo "{\"output\":\"$result\"}"
}
# API: run setup (returns instructions since it needs Termux context)
api_setup_status() {
local missing=""
for pkg in java python3 kotlinc gradle git cmake clang; do
[ ! -f "$TERMUX_BIN/$pkg" ] && missing="$missing $pkg"
done
if [ -z "$missing" ]; then
echo '{"complete":true,"missing":[]}'
else
missing=$(echo "$missing" | sed 's/^ //' | sed 's/ /","/g')
echo "{\"complete\":false,\"missing\":[\"$missing\"]}"
fi
}
# API: get buildchain log
api_log() {
local lines="${1:-50}"
local content=$(tail -n "$lines" "$CONFIG_DIR/buildchain.log" 2>/dev/null)
content=$(echo "$content" | sed 's/\\/\\\\/g; s/"/\\"/g' | tr '\n' '|' | sed 's/|/\\n/g')
echo "{\"log\":\"$content\"}"
}
# API: busybox applet list
api_busybox() {
local applets=$("$TOOLS/busybox" --list 2>/dev/null | tr '\n' ',' | sed 's/,$//')
local count=$("$TOOLS/busybox" --list 2>/dev/null | wc -l)
local version=$("$TOOLS/busybox" 2>&1 | head -1 | sed 's/"/\\"/g')
echo "{\"version\":\"$version\",\"count\":$count,\"applets\":\"$applets\"}"
}
handle_request() {
local method path body content_length
read -r request_line
method=$(echo "$request_line" | awk '{print $1}')
path=$(echo "$request_line" | awk '{print $2}' | cut -d'?' -f1)
while read -r header; do
header=$(echo "$header" | tr -d '\r')
[ -z "$header" ] && break
case "$header" in Content-Length:*) content_length=$(echo "$header" | awk '{print $2}') ;; esac
done
body=""
if [ "$method" = "POST" ] && [ -n "$content_length" ] && [ "$content_length" -gt 0 ] 2>/dev/null; then
body=$(dd bs=1 count="$content_length" 2>/dev/null)
fi
[ "$method" = "OPTIONS" ] && { send_response "200 OK" "text/plain" ""; return; }
case "$method $path" in
"GET /") send_file "$WEBROOT/index.html" ;;
"GET /css/"*|"GET /js/"*) send_file "$WEBROOT$path" ;;
"GET /api/status") send_response "200 OK" "application/json" "$(api_status)" ;;
"GET /api/tools") send_response "200 OK" "application/json" "$(api_tools)" ;;
"GET /api/paths") send_response "200 OK" "application/json" "$(api_paths)" ;;
"GET /api/test") send_response "200 OK" "application/json" "$(api_test)" ;;
"GET /api/setup") send_response "200 OK" "application/json" "$(api_setup_status)" ;;
"GET /api/log") send_response "200 OK" "application/json" "$(api_log)" ;;
"GET /api/busybox") send_response "200 OK" "application/json" "$(api_busybox)" ;;
"POST /api/redetect")
sh "$MODDIR/service.sh" &
send_response "200 OK" "application/json" '{"ok":true}'
;;
*) send_response "404 Not Found" "application/json" '{"error":"not found"}' ;;
esac
}
log "Starting on port $PORT"
while true; do
handle_request | busybox nc -ll -p "$PORT" -s 127.0.0.1 2>/dev/null \
|| handle_request | toybox nc -L -p "$PORT" -s 127.0.0.1 2>/dev/null \
|| { log "ERROR: No listener"; exit 1; }
done