Skip to content

Commit

Permalink
Merge pull request #1 from freenet-mobile/node-config
Browse files Browse the repository at this point in the history
Refactor install class into NodeController and Config classes
  • Loading branch information
desyncr authored Apr 30, 2021
2 parents 241956b + f0f2083 commit d6004cd
Show file tree
Hide file tree
Showing 11 changed files with 722 additions and 287 deletions.
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ plugins {
id 'maven-publish'
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}

group = 'org.freenetproject.mobile'
version = '0.6'
version = '1.0'

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
Expand Down Expand Up @@ -58,12 +64,18 @@ dependencies {
implementation 'com.github.Bombe:jFCPlib:v0.1.6'
// End Freenet dependencies

// Random strings for ssl.sslKeyStorePass
implementation 'org.apache.commons:commons-lang3:3.9'

// Android compatible logging
implementation 'co.trikita:log:1.1.5'

// Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'

testImplementation 'org.mockito:mockito-core:2.28.2'
testImplementation 'org.mockito:mockito-junit-jupiter:2.28.2'

// Use JUnit Jupiter Engine for testing.
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
Expand Down
124 changes: 124 additions & 0 deletions src/main/java/org/freenetproject/mobile/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.freenetproject.mobile;

import org.apache.commons.lang3.*;

import java.io.*;
import java.nio.file.*;
import java.util.*;

/**
* Small abstraction over java.util.Properties class.
*/
class Config {
private final String properties = "freenet.ini";
private Properties config = new Properties();

/**
* Set a given value under key.
*
* @param key Key name.
* @param val Value.
*/
public void set(String key, String val) {
config.setProperty(key, val);
}

/**
* Gets the value for configuration key, defaults to ""
* if the configuration key is not present.
*
* @param key Configuration key.
* @return Value.
*/
public String get(String key) {
return config.getProperty(key, "");
}

/**
* Gets the value for configuration key, defaults to defaultValue
* if the configuration key is not present.
*
* @param key Configuration key.
* @return Value.
*/
public String get(String key, String defaultValue) {
return config.getProperty(key, defaultValue);
}

/**
* Loads node configuration or creates one under path with default values.
*
* @param path Path where the node is located.
*/
public void loadOrDefault(Path path) throws IOException {
Path config = Paths.get(path.toString(), properties);

if (Files.exists(config)) {
InputStream is = new FileInputStream(config.toFile());
this.config = new Properties();
this.config.load(is);
return;
}

this.config = getDefaultConfig(path);
persist();
}

/**
* Stores the current configuration to filesystem.
*
* This method saves the configuration options without using the Properties#store method.
* Thus avoiding the escaping of : (used for IPv6 for example) and other especial characters
* that freenet configuration contains.
*/
public void persist() throws IOException {
File dest = Paths.get(this.config.getProperty("node.install.cfgDir"), properties).toFile();
FileWriter writer = new FileWriter(dest);
this.config.forEach((k, v) -> {
try {
writer.write(String.format("%s=%s%n", k, v));
} catch (IOException e) {
e.printStackTrace();
}
});
writer.write(String.format("%s%n", "End"));
writer.close();
}

/**
* Gets the default configuration and update the paths before returning it.
*
* @param path Node path.
* @return Configured node.
*/
private Properties getDefaultConfig(Path path) throws IOException {
InputStream defaultConfig = getClass()
.getClassLoader()
.getResourceAsStream(
Paths.get("defaults", properties).toString()
);

Properties config = new Properties();
config.load(defaultConfig);
String dir = path.toString();
config.setProperty("node.install.cfgDir", dir);
config.setProperty("node.install.userDir", dir);
config.setProperty("node.install.nodeDir", dir);
config.setProperty("node.install.runDir", dir);
config.setProperty("node.install.storeDir", dir + "/pathstore");
config.setProperty("node.install.tempDir", dir + "/temp");
config.setProperty("node.install.pluginDir", dir + "/plugins");
config.setProperty("node.install.pluginStoresDir", dir + "/plugin-path");
config.setProperty("node.install.persistentTempDir", dir + "/persistent-temp");

config.setProperty("node.masterKeyFile", dir + "/master.keys");
config.setProperty("node.downloadsDir", dir + "/downloads");

config.setProperty("ssl.sslKeyStorePass", RandomStringUtils.randomAscii(64));
config.setProperty("ssl.sslKeyPass", RandomStringUtils.randomAscii(64));

config.setProperty("logger.dirname", dir + "/logs");

return config;
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/freenetproject/mobile/Connector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.freenetproject.mobile;

import net.pterodactylus.fcp.*;
import trikita.log.*;

import java.io.*;
import java.net.*;

class Connector {
private static final String DEFAULT_HOST = "127.0.0.1";
private static final int DEFAULT_PORT = 9481;
private final FcpConnection fcpConnection;

public Connector(FcpConnection fcpConnection) {
this.fcpConnection = fcpConnection;
}

public Connector(String host, int port) throws UnknownHostException {
this(new FcpConnection(host, port));
}

public Connector() throws UnknownHostException {
this(DEFAULT_HOST, DEFAULT_PORT);
}

public void connect() throws IOException {
try {
fcpConnection.connect();
fcpConnection.sendMessage(new ClientHello("freenet-mobile"));
} catch (Exception e) {
Log.i("Freenet", "Failed to connect through FCP. Node shutdown or wrong port: " + e.getMessage());
throw e;
}
}

public void modifyConfiguration(String key, String value) throws IOException {
ModifyConfig modifyConfig = new ModifyConfig("identifier");
modifyConfig.setOption(key, value);
fcpConnection.sendMessage(modifyConfig);
}
}
148 changes: 0 additions & 148 deletions src/main/java/org/freenetproject/mobile/Installer.java

This file was deleted.

Loading

0 comments on commit d6004cd

Please sign in to comment.