Skip to content

Commit

Permalink
Add ports listening on GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Jun 25, 2020
1 parent b717011 commit 332b733
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
@@ -1,6 +1,7 @@
package de.tudarmstadt.informatik.hostage.ui.adapter;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
Expand All @@ -14,6 +15,8 @@
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import de.tudarmstadt.informatik.hostage.Listener;
import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
import de.tudarmstadt.informatik.hostage.model.Profile;
Expand All @@ -30,7 +33,6 @@
public class ServicesListAdapter extends ArrayAdapter<ServicesListItem> {
private final Context context;
private final List<ServicesListItem> values;
int sdk = Build.VERSION.SDK_INT;
private Context mActivity;
private Switch mServicesSwitch;
private CompoundButton.OnCheckedChangeListener mListener;
Expand Down Expand Up @@ -89,6 +91,7 @@ public View getView(final int position, View convertView, ViewGroup parent) {
assert rowView != null;
holder.protocolName = rowView.findViewById(R.id.services_item_name);
holder.recordedAttacks = rowView.findViewById(R.id.services_item_rec_attacks);
holder.port = rowView.findViewById(R.id.services_item_port);
holder.activated = rowView.findViewById(R.id.services_item_switch);
holder.circle = rowView.findViewById(R.id.services_circle);
rowView.setTag(holder);
Expand All @@ -97,6 +100,7 @@ public View getView(final int position, View convertView, ViewGroup parent) {
}

holder.protocolName.setText(item.protocol);
setRealPortListening(holder,item);
holder.activated.setTag(item);

this.updateStatus(item, holder);
Expand Down Expand Up @@ -267,6 +271,24 @@ private void setBackground(ViewHolder holder, int drawable) {

}

/**
* Adds the real port number in every item of the list
*/
private void setRealPortListening(ViewHolder holder,ServicesListItem item){
Map<String,Integer> ports = Listener.getRealPorts();
String protocol = item.protocol;
if(ports.containsKey(protocol)){
int realPort = ports.entrySet().stream()
.filter(e -> e.getKey().equals(protocol))
.map(Map.Entry::getValue)
.findFirst().get();

holder.port.setText(String.format(MainActivity.getContext().getResources().getString(R.string.open_ports) + " %d", realPort));
}else {
holder.port.setText(String.format(MainActivity.getContext().getResources().getString(R.string.open_ports) + " %d", item.port));
}
}

/**
* ViewHolder stands for a row in the view
*/
Expand All @@ -276,6 +298,8 @@ private class ViewHolder {

public TextView recordedAttacks;

public TextView port;

public Switch activated;

public View circle;
Expand Down
Expand Up @@ -60,6 +60,8 @@ public class ServicesFragment extends TrackerFragment {

private String[] protocols;

private int[] originalPorts;

private SharedPreferences mConnectionInfo;

private boolean mReceiverRegistered = false;
Expand Down Expand Up @@ -173,6 +175,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
assignViews();

protocols = getResources().getStringArray(R.array.protocols);
originalPorts = getResources().getIntArray(R.array.ports);
mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);

updateUI();
Expand All @@ -182,7 +185,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
protocolList = new ArrayList<ServicesListItem>();
int i = 0;
for (String protocol : protocols) {
protocolList.add(new ServicesListItem(protocol));
protocolList.add(new ServicesListItem(protocol,originalPorts[i]));
protocolList.get(i).attacks = daoHelper.getAttackRecordDAO().getNumAttacksSeenByBSSID(protocolList.get(i).protocol,
mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
i++;
Expand Down Expand Up @@ -261,6 +264,7 @@ public void onClick(DialogInterface dialog, int which) {

}


/**
* called on start of this fragment.
* registers broadcast receiver and binds change listener to main switch
Expand Down
Expand Up @@ -9,6 +9,7 @@
public class ServicesListItem {
public String protocol;
public int attacks;
public int port;

public boolean activated;

Expand All @@ -17,8 +18,9 @@ public class ServicesListItem {
*
* @param protocol protocol of this item, e.g. ftp
*/
public ServicesListItem(String protocol){
public ServicesListItem(String protocol,int port){
this.protocol = protocol;
this.port = port;
this.activated = false;
}
}
24 changes: 17 additions & 7 deletions src/main/res/layout/services_list_item.xml
Expand Up @@ -39,22 +39,32 @@
android:textStyle="bold"
android:textSize="12dp"/>

<TextView
android:id="@+id/services_item_port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="178dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="@string/open_ports"
android:textSize="12sp"
android:textStyle="bold" />

<Switch
android:id="@+id/services_item_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="false"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"
android:layout_gravity="right"
android:layout_marginLeft="0dp"
android:layout_gravity="end"
android:layout_marginStart="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="0dp"
android:layout_weight="1"
android:checked="false"
android:textSize="10dp" />
android:textSize="10sp" />
</RelativeLayout>

</LinearLayout>
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Expand Up @@ -114,6 +114,7 @@
<string name="UUID">9fc4f490-659e-11e3-949a-0800200c9a66</string>

<string name="recorded_attacks">recorded attacks: </string>
<string name="open_ports" translatable="false">port:</string>
<string name="current_connection">Current connection</string>
<string name="monitor_services">Monitor services</string>

Expand Down

0 comments on commit 332b733

Please sign in to comment.