Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix host ws connection #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ The architecture consists into different layers:

### Steps

1. Create a `local.properties` file into the project's root folder with the path to the android SDK and NDK
1. Link the Rust library project into the rust folder (be sure it compiles when running `cargo build --release`):
2. Create a `local.properties` file into the project's root folder with the path to the android SDK and NDK
additional properties as follows:

```env
Expand Down Expand Up @@ -180,3 +181,12 @@ a compatible keystore and name it `keystore-path.jks`.

**note:** here we have used the same password for the keystore access and the only alias
`signing-key`.


### Websocket connection customization

You can set the ws host and port to connect by using the launch.sh script:

```bash
./scripts/launch.sh -e wsHost 8.8.8.8 -e wsPort 23000
```
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def verifyStateHash = config.getProperty('build.verifyStateHash', 'false')
def writeStateHash = config.getProperty('build.writeStateHash', 'false')
def buildType = gradle.startParameter.taskNames.any { it.contains("Debug") } ? "debug" : "release"

def ksPath = config.getProperty('ks.path')
def ksPath = config.getProperty('ks.path', './')
def ksPassword = config.getProperty('ks.password')
def ksAlias = config.getProperty('ks.alias')

Expand Down Expand Up @@ -130,11 +130,12 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "io.ktor:ktor-client-core:$ktorVersion"
implementation "io.ktor:ktor-client-cio:$ktorVersion"
implementation "io.ktor:ktor-client-logging:$ktorVersion"
implementation "io.ktor:ktor-client-websockets:$ktorVersion"
implementation "io.ktor:ktor-client-okhttp:$ktorVersion"
implementation 'commons-codec:commons-codec:1.17.0'
implementation 'org.web3j:core:4.8.7-android'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
}
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
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>
43 changes: 35 additions & 8 deletions app/src/main/java/proofcastlabs/tee/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import kotlinx.coroutines.runBlocking
import androidx.appcompat.app.AppCompatActivity
import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.plugins.logging.ANDROID
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.client.plugins.websocket.*
import io.ktor.http.*
import io.ktor.websocket.*
Expand All @@ -26,17 +30,27 @@ import java.time.Duration
class MainActivity : AppCompatActivity() {
private external fun callCore(strongbox: Strongbox, db: DatabaseWiring, input: String): String

private val TAG = "[Main]"
private val WS_RETRY_DELAY = 3_000L
private val WS_PING_INTERVAL = 55_000L
private val WS_DEFAULT_PORT = "3000"
private val WS_DEFAULT_HOST = "localhost"
private val WS_DEFAULT_TIMEOUT = 10_000L

private val INTENT_KEY_WS_HOST = "wsHost"
private val INTENT_KEY_WS_PORT = "wsPort"
private val INTENT_KEY_WS_TIMEOUT = "wsTimeout"

private var wsHost = "localhost"
private var wsPort = 3000
private var wsTimeout = WS_DEFAULT_TIMEOUT
private val verifyStateHash = BuildConfig.VERIFY_STATE_HASH.toBoolean()
private val writeStateHash = BuildConfig.WRITE_STATE_HASH.toBoolean()
private val isStrongboxBacked = BuildConfig.STRONGBOX_ENABLED.toBoolean()
private var client: HttpClient? = null
private var strongbox: Strongbox? = null
private var db: DatabaseWiring? = null

val TAG = "[Main]"

init {
System.loadLibrary("sqliteX")
Expand All @@ -47,10 +61,11 @@ class MainActivity : AppCompatActivity() {

suspend fun receiveWebsocketData(context: Context) {
client!!.webSocket(
method = HttpMethod.Get,
path = "/ws",
host = "127.0.0.1",
port = 3000
// method = HttpMethod.Get,
// path = "/ws",
// host = wsHost,
// port = wsPort
"ws://$wsHost:$wsPort/ws"
) {
Log.i(TAG, "Websocket connected")
strongbox = Strongbox(context)
Expand Down Expand Up @@ -104,6 +119,18 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Send intent extra by adding --es <param> to the launch.sh
// script
if (intent.extras != null) {
wsHost = intent.extras!!.getString(INTENT_KEY_WS_HOST, WS_DEFAULT_HOST)
wsPort = intent.extras!!.getString(INTENT_KEY_WS_PORT, WS_DEFAULT_PORT).toInt()
wsTimeout = intent.extras!!.getString(INTENT_KEY_WS_TIMEOUT, WS_DEFAULT_TIMEOUT.toString()).toLong()
}

Log.d(TAG, "Host: $wsHost")
Log.d(TAG, "Port: $wsPort")
Log.d(TAG, "Timeout: $wsTimeout")

val dns = object : Dns {
override fun lookup(hostname: String): List<InetAddress> {
return Dns.SYSTEM.lookup(hostname).filter {
Expand All @@ -113,15 +140,15 @@ class MainActivity : AppCompatActivity() {
}

val context = this
val timeout = Duration.ofSeconds(100L)
val timeout = Duration.ofMillis(wsTimeout)
client = HttpClient(OkHttp) {
engine { config {
dns(dns)
writeTimeout(timeout)
readTimeout(timeout)
connectTimeout(timeout)
} }

install(Logging) { level = LogLevel.ALL; logger = Logger.ANDROID }
install(WebSockets) { pingInterval = WS_PING_INTERVAL }
}

Expand All @@ -134,4 +161,4 @@ class MainActivity : AppCompatActivity() {
client!!.close()
Log.i(TAG, "Websocket connection closed!")
}
}
}
2 changes: 1 addition & 1 deletion scripts/connect-device.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
#
lsusb | grep -i google | awk '{print "/dev/bus/usb/"$2"/"$4}' | tr -d ':' | xargs sudo chown "$USER:plugdev"
adb kill-server
read -p "Please press 'Allow' on the device display and press enter..."
adb devices
adb reverse tcp:3000 tcp:3000
60 changes: 57 additions & 3 deletions scripts/launch.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
#!/bin/bash
device="$1"
if [[ -n "$device" ]]; then device="-s $device"; fi
adb $device shell am start proofcastlabs.tee/.MainActivity
function usage {
local b # bold
local n # normal
b=$(tput bold)
n=$(tput sgr0)

echo "${b}Usage:${n} $0 [DEVICE_ID] [...ADB_ARGS]

Launch the MainActivity of the Event Attestator on the
selected DEVICE. You may optionally decide to extra adb
parameters, these are going to be appended at the end of
the adb command.

${b}Arguments:${n}

DEVICE_ID The device ID show through adb devices command. An empty
value is accepted only when a single device is attached
to the machine.

ADB_ARGS Parameters accepted by the adb activity manager (am)
command (i.e. intent extras like --es <key> <value>)
Check the acceptable intent extra keys into the MainActivity
code.

${b}Available options:${n}

-h, --help Shows this help

${b}Examples:${n}

1. Launch with websocket host and port settings:

$0 --es wsHost localhost --es wsPort 11111

2. Launch on a specific device (8BKX1BBEE)

$0 8BKX1BBEE
"
}

function main {
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
exit 0
fi

if [[ ! "$1" == -* && -n "$1" ]]; then
device="-s $1"
shift 1
fi

# You can add params to the launching command (i.e. intents)
# ...such coolness :D
adb $device shell am start -n proofcastlabs.tee/.MainActivity -a android.intent.action.MAIN $@
}

main "$@"