diff --git a/httpurlconnectionclient/.gitignore b/httpurlconnectionclient/.gitignore new file mode 100644 index 0000000..656b534 --- /dev/null +++ b/httpurlconnectionclient/.gitignore @@ -0,0 +1,16 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties +zerotier.properties diff --git a/httpurlconnectionclient/.idea/.gitignore b/httpurlconnectionclient/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/httpurlconnectionclient/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/httpurlconnectionclient/.idea/.name b/httpurlconnectionclient/.idea/.name new file mode 100644 index 0000000..a29660e --- /dev/null +++ b/httpurlconnectionclient/.idea/.name @@ -0,0 +1 @@ +ZeroTier Sockets Android HttpURLConnection client \ No newline at end of file diff --git a/httpurlconnectionclient/.idea/compiler.xml b/httpurlconnectionclient/.idea/compiler.xml new file mode 100644 index 0000000..b589d56 --- /dev/null +++ b/httpurlconnectionclient/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/.idea/gradle.xml b/httpurlconnectionclient/.idea/gradle.xml new file mode 100644 index 0000000..a2d7c21 --- /dev/null +++ b/httpurlconnectionclient/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/.idea/kotlinc.xml b/httpurlconnectionclient/.idea/kotlinc.xml new file mode 100644 index 0000000..6645193 --- /dev/null +++ b/httpurlconnectionclient/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/.idea/misc.xml b/httpurlconnectionclient/.idea/misc.xml new file mode 100644 index 0000000..773fe0f --- /dev/null +++ b/httpurlconnectionclient/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/app/.gitignore b/httpurlconnectionclient/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/httpurlconnectionclient/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/httpurlconnectionclient/app/build.gradle b/httpurlconnectionclient/app/build.gradle new file mode 100644 index 0000000..017073a --- /dev/null +++ b/httpurlconnectionclient/app/build.gradle @@ -0,0 +1,50 @@ +plugins { + id 'com.android.application' + id 'org.jetbrains.kotlin.android' +} + +// Creates a variable called zerotierPropertiesFile, and initializes it to the +// zerotier.properties file. +def zerotierPropertiesFile = rootProject.file("zerotier.properties") + +// Initializes a new Properties() object called zerotierProperties. +def zerotierProperties = new Properties() + +// Loads the zerotier.properties file into the zerotierProperties object. +zerotierProperties.load(new FileInputStream(zerotierPropertiesFile)) + + +android { + namespace 'com.zerotier.sockets.httpurlconnectionclient' + compileSdk 33 + + defaultConfig { + applicationId "com.zerotier.sockets.httpurlconnectionclient" + minSdk 24 + targetSdk 33 + versionCode 1 + versionName "1.0" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'com.google.android.material:material:1.9.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.core:core-ktx:1.10.1' + implementation files(zerotierProperties['libzt.aar']) +} \ No newline at end of file diff --git a/httpurlconnectionclient/app/proguard-rules.pro b/httpurlconnectionclient/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/httpurlconnectionclient/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/AndroidManifest.xml b/httpurlconnectionclient/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..988d846 --- /dev/null +++ b/httpurlconnectionclient/app/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/java/com/zerotier/sockets/httpurlconnectionclient/MainActivity.kt b/httpurlconnectionclient/app/src/main/java/com/zerotier/sockets/httpurlconnectionclient/MainActivity.kt new file mode 100644 index 0000000..effc6be --- /dev/null +++ b/httpurlconnectionclient/app/src/main/java/com/zerotier/sockets/httpurlconnectionclient/MainActivity.kt @@ -0,0 +1,84 @@ +package com.zerotier.sockets.httpurlconnectionclient + +import android.os.Bundle +import android.os.StrictMode +import android.os.StrictMode.ThreadPolicy +import android.util.Log +import androidx.appcompat.app.AppCompatActivity +import com.zerotier.sockets.ZeroTierNative +import com.zerotier.sockets.ZeroTierNode +import java.io.BufferedReader +import java.io.InputStreamReader +import java.net.HttpURLConnection +import java.net.URL + + +class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + val ctxt = this.applicationContext + val storagePath = ctxt.filesDir.absolutePath + val nwid = "0123456789abcdef".toLong(16) + + val remoteUrl = "http://10.147.19.69:8080/" + + + // ZeroTier setup + val node = ZeroTierNode() + node.initFromStorage(storagePath) + node.start() + // + // NOTE: This is demo code and waiting on main thread is not recommended + // + Log.d(TAG, "Waiting for node to come online...") + while (!node.isOnline) { + ZeroTierNative.zts_util_delay(50) + } + Log.d(TAG, "Node ID: " + String.format("%010x", node.id)) + Log.d(TAG, "Joining network...") + node.join(nwid) + Log.d(TAG, "Waiting for network...") + while (!node.isNetworkTransportReady(nwid)) { + ZeroTierNative.zts_util_delay(50) + } + Log.d(TAG, "Joined") + + + // Socket logic + + val url = URL(remoteUrl) + val urlConnection: HttpURLConnection = url.openConnection() as HttpURLConnection + + + // prevent NetworkOnMainThreadException in demo code + StrictMode.setThreadPolicy(ThreadPolicy.LAX) + + + try{ + urlConnection.requestMethod = "GET" + val responseCode: Int = urlConnection.responseCode + + Log.d(TAG, "response: $responseCode") + if (responseCode == HttpURLConnection.HTTP_OK) { + val inn = BufferedReader(InputStreamReader(urlConnection.inputStream)) + var inputLine: String? + val response = StringBuffer() + while (inn.readLine().also { inputLine = it } != null) { + response.append(inputLine) + } + inn.close() + + // print result + println(response.toString()) + } + } + finally { + urlConnection.disconnect() + } + } + + companion object { + private const val TAG = "MainActivity" + } +} \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/httpurlconnectionclient/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/res/drawable/ic_launcher_background.xml b/httpurlconnectionclient/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/httpurlconnectionclient/app/src/main/res/layout/activity_main.xml b/httpurlconnectionclient/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..17eab17 --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/httpurlconnectionclient/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/httpurlconnectionclient/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/httpurlconnectionclient/app/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/httpurlconnectionclient/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/httpurlconnectionclient/app/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/httpurlconnectionclient/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/httpurlconnectionclient/app/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/httpurlconnectionclient/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/httpurlconnectionclient/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/httpurlconnectionclient/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/httpurlconnectionclient/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/httpurlconnectionclient/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/httpurlconnectionclient/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/httpurlconnectionclient/app/src/main/res/values-night/themes.xml b/httpurlconnectionclient/app/src/main/res/values-night/themes.xml new file mode 100644 index 0000000..2e7b55c --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/values-night/themes.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/res/values/colors.xml b/httpurlconnectionclient/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..c8524cd --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/values/colors.xml @@ -0,0 +1,5 @@ + + + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/res/values/strings.xml b/httpurlconnectionclient/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..d40f82c --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + ZeroTier Sockets Android HttpURLConnection client + \ No newline at end of file diff --git a/httpurlconnectionclient/app/src/main/res/values/themes.xml b/httpurlconnectionclient/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..0cca844 --- /dev/null +++ b/httpurlconnectionclient/app/src/main/res/values/themes.xml @@ -0,0 +1,9 @@ + + + + +