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

Randomize ports #95

Draft
wants to merge 1 commit into
base: upgrade-node-wrapper
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions app/src/main/java/org/freenetproject/mobile/proxy/Simple.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,17 @@ public void run() {
}
}

@Override
public void run() {
public void run(String address, int port) {
Log.d("Freenet", "Proxy thread started");
try {
run("127.0.0.1", 8888, defaultLocalPort);
run(address, port, defaultLocalPort);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run() {
run("127.0.0.1", 8888);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import com.jakewharton.processphoenix.ProcessPhoenix;

import org.freenetproject.mobile.Config;
import org.freenetproject.mobile.Connector;
import org.freenetproject.mobile.NodeController;
import org.freenetproject.mobile.NodeControllerImpl;
import org.freenetproject.mobile.R;
Expand All @@ -21,6 +23,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

/**
* Class responsible for exposing data to the UI. It also exposes methods for the UI to interact with,
Expand Down Expand Up @@ -78,46 +81,81 @@ public LiveData<Status> getStatus() {
* @param context Application context.
*/
public void startService(Context context) throws IOException {
SharedPreferences prefs = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE
);

Resources res = context.getResources();

File path = context.getDir("data", Context.MODE_PRIVATE);

status.postValue(Status.STARTING_UP);

nc = new NodeControllerImpl(path.toPath());
final boolean isFirstRun = prefs.getBoolean("first-run", true);

SharedPreferences prefs = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE
);
// on first install
if (isFirstRun) {
initializeConfiguration(path, context);
}

// instantiate connector with given port
Connector connector = new Connector("127.0.0.1", prefs.getInt("fcp.port", 9481));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no randomized IP? 127.x.y.z should be possible.

nc = new NodeControllerImpl(path.toPath(), new Config(), connector);

Resources res = context.getResources();
// Setup first-run configuration
if (prefs.getBoolean("first-run", true)) {
if (isFirstRun) {
nc.setConfig("seednodes.fref", res.openRawResource(R.raw.seednodes));
nc.setConfig("bookmarks.dat", res.openRawResource(R.raw.bookmarks));
}

nc.start();

if (nc.isRunning()) {

if (prefs.getBoolean("first-run", true)) {
// Setup first-run runtime configuration
nc.setConfig("node.l10n", res.getConfiguration().getLocales().get(0).toLanguageTag());
}

SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first-run", false);
editor.apply();

Intent serviceIntent = new Intent(context, Service.class);
context.startForegroundService(serviceIntent);
status.postValue(Status.STARTED);

} else {
status.postValue(Status.ERROR);
}

}

/**
*
* @param path
* @param context
* @throws IOException
*/
private void initializeConfiguration(File path, Context context) throws IOException {
SharedPreferences prefs = context.getSharedPreferences(
context.getPackageName(), Context.MODE_PRIVATE
);

// generate random port
final int TOP_RANGE = 32767, BOTTOM_RANGE = 5001;

// store in shared preferences
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("fproxy.port", new Random().nextInt(TOP_RANGE - BOTTOM_RANGE) + BOTTOM_RANGE);
editor.putInt("fcp.port", new Random().nextInt(TOP_RANGE - BOTTOM_RANGE) + BOTTOM_RANGE);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also change the bindTo IP address?

editor.apply();

// update config with given ports
Config config = new Config();
config.loadOrDefault(path.toPath());
config.set("fproxy.port", String.valueOf(prefs.getInt("fproxy.port", 8888)));
config.set("fcp.port", String.valueOf(prefs.getInt("fcp.port", 9481)));

Resources res = context.getResources();
config.set("node.l10n", res.getConfiguration().getLocales().get(0).toLanguageTag());

config.persist();
}

/**
* Stops the service through the Runner class. Also stops the Services.Node.Service.
*
Expand Down