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

Dynamic agent #19

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
improved interface list
  • Loading branch information
mutex-pup committed Sep 7, 2024
commit ce37807790ddd6fac2b49565746ddc2e49f09285
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.WithSecure.dz.activities;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import com.WithSecure.dz.Agent;
import com.WithSecure.dz.R;
import com.WithSecure.dz.models.NetworkInterfaceModel;
import com.WithSecure.dz.models.ServerSettings;
import com.WithSecure.dz.views.CheckListItemView;
import com.WithSecure.dz.views.ConnectorStatusIndicator;
import com.WithSecure.dz.views.NetworkInterfaceListAdapter;
import com.WithSecure.dz.views.logger.LogMessageAdapter;
import com.WithSecure.jsolar.api.connectors.Connector;
import com.WithSecure.jsolar.api.connectors.Endpoint;
@@ -20,7 +24,6 @@
import android.app.ProgressDialog;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ServerActivity extends ConnectorActivity implements Observer, Server.OnDetailedStatusListener {
@@ -31,7 +34,9 @@ public class ServerActivity extends ConnectorActivity implements Observer, Serve
private ListView server_messages = null;
private ConnectorStatusIndicator server_status_indicator = null;

private TextView server_endpoint_text = null;
private ListView server_interface_list = null;
private NetworkInterfaceListAdapter server_interface_adapter = null;
private List<NetworkInterfaceModel> server_interface_data;

private CheckListItemView status_enabled = null;
private CheckListItemView status_listening = null;
@@ -58,8 +63,14 @@ public void onCreate(Bundle savedInstanceState) {

this.server_status_indicator = (ConnectorStatusIndicator)this.findViewById(R.id.server_status_indicator);

this.server_endpoint_text = (TextView)this.findViewById(R.id.server_endpoint);
server_endpoint_text.setText(ServerSettings.interfacesAsString());
this.server_interface_list = (ListView)this.findViewById(R.id.server_endpoint);
this.server_interface_data = new ArrayList<>();
this.server_interface_adapter = new NetworkInterfaceListAdapter(this, server_interface_data);
server_interface_list.setAdapter(server_interface_adapter);

server_interface_adapter.clear();
server_interface_adapter.addAll(ServerSettings.getInterfaces());
server_interface_adapter.notifyDataSetChanged();

this.server_enabled = (CompoundButton)this.findViewById(R.id.server_enabled);
this.server_enabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@@ -106,7 +117,9 @@ public void onDetailedStatus(Bundle status) {
* Refresh the status indicators, to show the current status of the Endpoint.
*/
protected void refreshStatus() {
server_endpoint_text.setText(ServerSettings.interfacesAsString());
server_interface_adapter.clear();
server_interface_adapter.addAll(ServerSettings.getInterfaces());
server_interface_adapter.notifyDataSetChanged();
this.getDetailedServerStatus();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.WithSecure.dz.models;


import java.util.List;

public class NetworkInterfaceModel
{
public NetworkInterfaceModel(String name, List<String> ips)
{
this.name = name;
this.ips = ips;
}

public final String name;
public final List<String> ips;
}
36 changes: 21 additions & 15 deletions agent/src/main/java/com/WithSecure/dz/models/ServerSettings.java
Original file line number Diff line number Diff line change
@@ -62,29 +62,29 @@ public void load(Server server) {

this.getSettings().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(Server.SERVER_PORT))
this.server.setPort(this.getPort());
if(key.equals(Server.SERVER_PASSWORD))
this.server.setPassword(this.getPassword());

if(key.equals(Server.SERVER_SSL)) {
this.server.setSSL(this.isSSL());
server.setKeyStorePath(this.getKeyStorePath());
server.setKeyStorePassword(this.getKeyStorePassword());
server.setKeyPassword(this.getKeyPassword());
}

if(key.equals(Server.SERVER_KEYSTORE_PATH))
server.setKeyStorePath(this.getKeyStorePath());
if(key.equals(Server.SERVER_KEYSTORE_PASSWORD))
server.setKeyStorePassword(this.getKeyStorePassword());
if(key.equals(Server.SERVER_KEY_PASSWORD))
server.setKeyPassword(this.getKeyPassword());
}

public boolean save(Server server) {
SharedPreferences.Editor editor = Agent.getInstance().getSettings().edit();

@@ -94,33 +94,39 @@ public boolean save(Server server) {
return editor.commit();
}

public static String interfacesAsString() {
try {
public static List<NetworkInterfaceModel> getInterfaces() {
List<NetworkInterfaceModel> ret = new ArrayList<>();
try {
List<String> ipAddrs = new ArrayList<>();

Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface i = interfaces.nextElement();
NetworkInterface intr = interfaces.nextElement();

if (!i.isUp()) {
if (!intr.isUp()) {
continue;
}

Enumeration<InetAddress> ips = i.getInetAddresses();
NetworkInterfaceModel inter_data = new NetworkInterfaceModel(intr.getName(), new ArrayList<>());

Enumeration<InetAddress> ips = intr.getInetAddresses();
while (ips.hasMoreElements()) {
InetAddress ip = ips.nextElement();

if (ip.isLinkLocalAddress() || ip.isLoopbackAddress()) {
if (ip.isLinkLocalAddress()) {
continue;
}

ipAddrs.add(ip.getHostAddress());
inter_data.ips.add(ip.getHostAddress());
}
}

return String.join(", ", ipAddrs);
} catch (Exception ignored) { }
return "could not retrieve interfaces";
if (!inter_data.ips.isEmpty())
{
ret.add(inter_data);
}
}
} catch (Exception ignored) { }
return ret;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.WithSecure.dz.views;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.WithSecure.dz.R;
import com.WithSecure.dz.models.NetworkInterfaceModel;

import java.util.List;

public class NetworkInterfaceListAdapter extends ArrayAdapter<NetworkInterfaceModel>
{
private final Activity context;

public NetworkInterfaceListAdapter(@NonNull Activity context, List<NetworkInterfaceModel> interfaces) {
super(context, R.layout.list_view_network_interface, interfaces);

this.context = context;
}

@NonNull
@Override
public View getView(int position, View view, @NonNull ViewGroup parent)
{
LayoutInflater inflater=this.context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.list_view_network_interface, null,true);

TextView name = rowView.findViewById(R.id.interface_name);
TextView ips = rowView.findViewById(R.id.ips);

NetworkInterfaceModel data = this.getItem(position);
assert data != null;

name.setText(data.name);
ips.setText(String.join("\n", data.ips));

return rowView;
}
}
9 changes: 5 additions & 4 deletions agent/src/main/res/layout/activity_server.xml
Original file line number Diff line number Diff line change
@@ -32,16 +32,17 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/label_server_details"
android:text="@string/embedded_server_interfaces"/>
<TextView
<ListView
android:id="@+id/server_endpoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:layout_toRightOf="@+id/server_endpoint_text"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/label_server_details"/>
android:paddingLeft="10dp"
android:layout_below="@+id/server_endpoint_text"/>
<com.WithSecure.dz.views.CheckListItemView
android:id="@+id/server_status_ssl"
android:layout_width="wrap_content"
18 changes: 18 additions & 0 deletions agent/src/main/res/layout/list_view_network_interface.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/interface_name"
android:layout_weight="1"/>

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/ips"
android:layout_weight="2"/>
</LinearLayout>