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

Implemented configurable interface bindings #231

Open
wants to merge 11 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
18 changes: 17 additions & 1 deletion app/src/main/cpp/droidvnc-ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ JNIEXPORT jboolean JNICALL Java_net_christianbeier_droidvnc_1ng_MainService_vncS
}


JNIEXPORT jboolean JNICALL Java_net_christianbeier_droidvnc_1ng_MainService_vncStartServer(JNIEnv *env, jobject thiz, jint width, jint height, jint port, jstring desktopname, jstring password, jstring httpRootDir) {
JNIEXPORT jboolean JNICALL Java_net_christianbeier_droidvnc_1ng_MainService_vncStartServer(JNIEnv *env, jobject thiz, jint width, jint height, jstring listenIf, jint port, jstring desktopname, jstring password, jstring httpRootDir) {

int argc = 0;

Expand All @@ -315,6 +315,22 @@ JNIEXPORT jboolean JNICALL Java_net_christianbeier_droidvnc_1ng_MainService_vncS
theScreen->setXCutTextUTF8 = onCutText;
theScreen->newClientHook = onClientConnected;

in_addr_t address = 0; // Default is 0.0.0.0
if (listenIf != NULL) {
const char *cListenInterface = (*env)->GetStringUTFChars(env, listenIf, NULL);
int addrConvSucceded = rfbStringToAddr((char*)cListenInterface, &address);
(*env)->ReleaseStringUTFChars(env, listenIf, cListenInterface);

if (!addrConvSucceded) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "vncStartServer: invalid listen address");
Java_net_christianbeier_droidvnc_1ng_MainService_vncStopServer(env, thiz);
return JNI_FALSE;
}
}


// With the listenInterface property one can define where the server will be available
theScreen->listenInterface = address;
elluisian marked this conversation as resolved.
Show resolved Hide resolved
theScreen->port = port;
theScreen->ipv6port = port;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Constants {
/*
user settings
*/
public static final String PREFS_KEY_SETTINGS_LISTEN_INTERFACE = "settings_listen_interface";
public static final String PREFS_KEY_SETTINGS_PORT = "settings_port";
public static final String PREFS_KEY_SETTINGS_PASSWORD = "settings_password" ;
public static final String PREFS_KEY_SETTINGS_START_ON_BOOT = "settings_start_on_boot" ;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class Defaults {
private const val PREFS_KEY_DEFAULTS_ACCESS_KEY = "defaults_access_key"
}

@EncodeDefault
var listenInterface = "loopback"
private set

@EncodeDefault
var port = 5900
private set
Expand Down
43 changes: 43 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/INetIfData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* DroidVNC-NG INetIfData, interface defining what is needed to properly gather all the data for a network interface.
*
* Author: elluisian <[email protected]>
*
* Copyright (C) 2024 Christian Beier ([email protected]>).
*
* You can redistribute and/or modify this program under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
*/
package net.christianbeier.droidvnc_ng;


import android.net.Network;
import java.net.NetworkInterface;


public interface INetIfData {
public static final String ANY_OPTION_ID = "any";
public static final String ANY_OPTION_ID_ADDR = "0.0.0.0";
public static final String LOOPBACK_OPTION_ID = "loopback";
public static final String LOOPBACK_OPTION_ID_ADDR_1 = "localhost";
public static final String LOOPBACK_OPTION_ID_ADDR_2 = "127.0.0.1";


public String getName();
public String getOptionId();
public boolean isLoopback();
public boolean isAny();

public NetworkInterface getNetworkInterface();
public Network getNetwork();
}
144 changes: 144 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/IfCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* DroidVNC-NG IfCollector is a collector of interfaces, basically, with these, one can check the available NetIfData instances.
*
* Author: elluisian <[email protected]>
*
* Copyright (C) 2024 Christian Beier ([email protected]>).
*
* You can redistribute and/or modify this program under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
*/
package net.christianbeier.droidvnc_ng;

import android.net.LinkProperties;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.net.NetworkCapabilities;

import android.content.Context;
import android.util.Log;


import java.net.SocketException;
import java.net.NetworkInterface;
import java.util.List;
import java.util.ArrayList;

public class IfCollector {
private static IfCollector COLL;

private NetIfData any;
private NetIfData loopback;
private List<NetIfData> nics;
private int nicsSize;


public static synchronized IfCollector getInstance() {
if (IfCollector.COLL == null) {
IfCollector.COLL = new IfCollector();
}

return IfCollector.COLL;
}


private IfCollector() {
this.any = NetIfData.getAny();

List<NetworkInterface> ifs = Utils.getAvailableNICs();
this.nics = new ArrayList<>();

for (NetworkInterface inf : ifs) {
NetIfData nid = NetIfData.getNetIf(inf);
if (!nid.isLoopback()) {
this.nics.add(nid);
} else {
this.loopback = nid;
}
}

this.nicsSize = this.nics.size();
}




public int searchForNetIfByOptionId(String optionId) {
int i = 0;
boolean found = false;
for (i = 0; !found && i < this.nicsSize; i++) {
if (optionId.equals(this.nics.get(i).getOptionId())) {
i--;
found = true;
}
}

return found ? i : -1;
}


public int searchForNetIfByNetwork(Network network) {
int i = 0;
boolean found = false;
for (i = 0; !found && i < this.nicsSize; i++) {
if (network.equals(this.nics.get(i).getNetwork())) {
i--;
found = true;
}
}

return found ? i : -1;
}



public void addNetIf(NetworkInterface iface, Network network) {
this.nics.add(NetIfData.getNetIf(iface, network));
this.nicsSize++;
}


public List<NetIfData> getEnabledNetIfs() {
List<NetIfData> ls = new ArrayList<>();

for (int i = 0; i < this.nicsSize; i++) {
NetIfData nid = this.nics.get(i);
if (nid.isEnabled()) {
ls.add(nid);
}
}

return ls;
}

public NetIfData getAny() {
return this.any;
}

public NetIfData getLoopback() {
return this.loopback;
}

public NetIfData getNetIf(int i) {
if (0 <= i && i < this.nicsSize) {
return this.nics.get(i);
}

return null;
}

public int getSize() {
return this.nicsSize;
}
}
153 changes: 153 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/ListenIfAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* DroidVNC-NG ListenIfAdapter, used to let the user choose what network interface to listen on.
*
* Author: elluisian <[email protected]>
*
* Copyright (C) 2024 Christian Beier.
*
* You can redistribute and/or modify this program under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
*/
package net.christianbeier.droidvnc_ng;


import java.util.List;
import java.util.ArrayList;

import android.content.res.Resources;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Spinner;
import android.view.LayoutInflater;



public class ListenIfAdapter extends ArrayAdapter<NetIfData> implements NetworkInterfaceTester.OnNetworkStateChangedListener {
// This adapter uses the ViewHolder pattern
private static class ViewHolder {
public TextView txtLabel;
}

// Data to be shown with the adapter
private List<NetIfDataDecorator> data;
private int dataSize;


// Some context data for "easy retrieval"
private Context mContext;
private LayoutInflater mInflater;


// UI related
private Handler handler;



public ListenIfAdapter(NetworkInterfaceTester nit, Context context) {
super(context, R.layout.spinner_row, R.id.spinner_text);

this.mContext = context;
this.mInflater = LayoutInflater.from(this.mContext);
this.handler = new Handler(Looper.getMainLooper());

nit.addOnNetworkStateChangedListener(this);
this.onNetworkStateChanged(nit);
}



public int getItemPositionByOptionId(String optionId) {
int i = 0;
for (NetIfDataDecorator nid : this.data) {
if (nid.getOptionId().equals(optionId)) {
return i;
}
i++;
}

return 1; // return loopback position as default
}



@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return this.handleViewRecreation(position, convertView, parent);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
return this.handleViewRecreation(position, convertView, parent);
}


private View handleViewRecreation(int position, View convertView, ViewGroup parent) {
if (convertView == null) { // Check if view must be recreated using the famous ViewHolder pattern
convertView = this.mInflater.inflate(R.layout.spinner_row, parent, false);

ViewHolder vh = new ViewHolder();
vh.txtLabel = convertView.findViewById(R.id.spinner_text);
convertView.setTag(vh);
}

ViewHolder vh = (ViewHolder)convertView.getTag();
vh.txtLabel.setText(this.data.get(position).getName());

return convertView;
}



@Override
public NetIfData getItem(int position) {
if (0 <= position && position < this.getCount()) {
return this.data.get(position).getWrapped();
}
return null;
}



@Override
public int getCount() {
return this.dataSize;
}



public void onNetworkStateChanged(NetworkInterfaceTester nit) {
List<NetIfData> avNetIfs = nit.getAvailableNetIfs();

this.data = new ArrayList<>();
for (NetIfData nid : avNetIfs) {
this.data.add(new NetIfDataDefaultNameDecorator(nid, this.mContext));
}
this.dataSize = this.data.size();


// update Spinner
this.handler.post(new Runnable() {
public void run() {
ListenIfAdapter.this.notifyDataSetChanged();
}
});
}
}
Loading