Skip to content

Commit d6004cd

Browse files
authored
Merge pull request #1 from freenet-mobile/node-config
Refactor install class into NodeController and Config classes
2 parents 241956b + f0f2083 commit d6004cd

11 files changed

Lines changed: 722 additions & 287 deletions

File tree

build.gradle

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ plugins {
33
id 'maven-publish'
44
}
55

6+
java {
7+
toolchain {
8+
languageVersion.set(JavaLanguageVersion.of(8))
9+
}
10+
}
11+
612
group = 'org.freenetproject.mobile'
7-
version = '0.6'
13+
version = '1.0'
814

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

67+
// Random strings for ssl.sslKeyStorePass
68+
implementation 'org.apache.commons:commons-lang3:3.9'
69+
6170
// Android compatible logging
6271
implementation 'co.trikita:log:1.1.5'
6372

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

76+
testImplementation 'org.mockito:mockito-core:2.28.2'
77+
testImplementation 'org.mockito:mockito-junit-jupiter:2.28.2'
78+
6779
// Use JUnit Jupiter Engine for testing.
6880
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
6981
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package org.freenetproject.mobile;
2+
3+
import org.apache.commons.lang3.*;
4+
5+
import java.io.*;
6+
import java.nio.file.*;
7+
import java.util.*;
8+
9+
/**
10+
* Small abstraction over java.util.Properties class.
11+
*/
12+
class Config {
13+
private final String properties = "freenet.ini";
14+
private Properties config = new Properties();
15+
16+
/**
17+
* Set a given value under key.
18+
*
19+
* @param key Key name.
20+
* @param val Value.
21+
*/
22+
public void set(String key, String val) {
23+
config.setProperty(key, val);
24+
}
25+
26+
/**
27+
* Gets the value for configuration key, defaults to ""
28+
* if the configuration key is not present.
29+
*
30+
* @param key Configuration key.
31+
* @return Value.
32+
*/
33+
public String get(String key) {
34+
return config.getProperty(key, "");
35+
}
36+
37+
/**
38+
* Gets the value for configuration key, defaults to defaultValue
39+
* if the configuration key is not present.
40+
*
41+
* @param key Configuration key.
42+
* @return Value.
43+
*/
44+
public String get(String key, String defaultValue) {
45+
return config.getProperty(key, defaultValue);
46+
}
47+
48+
/**
49+
* Loads node configuration or creates one under path with default values.
50+
*
51+
* @param path Path where the node is located.
52+
*/
53+
public void loadOrDefault(Path path) throws IOException {
54+
Path config = Paths.get(path.toString(), properties);
55+
56+
if (Files.exists(config)) {
57+
InputStream is = new FileInputStream(config.toFile());
58+
this.config = new Properties();
59+
this.config.load(is);
60+
return;
61+
}
62+
63+
this.config = getDefaultConfig(path);
64+
persist();
65+
}
66+
67+
/**
68+
* Stores the current configuration to filesystem.
69+
*
70+
* This method saves the configuration options without using the Properties#store method.
71+
* Thus avoiding the escaping of : (used for IPv6 for example) and other especial characters
72+
* that freenet configuration contains.
73+
*/
74+
public void persist() throws IOException {
75+
File dest = Paths.get(this.config.getProperty("node.install.cfgDir"), properties).toFile();
76+
FileWriter writer = new FileWriter(dest);
77+
this.config.forEach((k, v) -> {
78+
try {
79+
writer.write(String.format("%s=%s%n", k, v));
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
});
84+
writer.write(String.format("%s%n", "End"));
85+
writer.close();
86+
}
87+
88+
/**
89+
* Gets the default configuration and update the paths before returning it.
90+
*
91+
* @param path Node path.
92+
* @return Configured node.
93+
*/
94+
private Properties getDefaultConfig(Path path) throws IOException {
95+
InputStream defaultConfig = getClass()
96+
.getClassLoader()
97+
.getResourceAsStream(
98+
Paths.get("defaults", properties).toString()
99+
);
100+
101+
Properties config = new Properties();
102+
config.load(defaultConfig);
103+
String dir = path.toString();
104+
config.setProperty("node.install.cfgDir", dir);
105+
config.setProperty("node.install.userDir", dir);
106+
config.setProperty("node.install.nodeDir", dir);
107+
config.setProperty("node.install.runDir", dir);
108+
config.setProperty("node.install.storeDir", dir + "/pathstore");
109+
config.setProperty("node.install.tempDir", dir + "/temp");
110+
config.setProperty("node.install.pluginDir", dir + "/plugins");
111+
config.setProperty("node.install.pluginStoresDir", dir + "/plugin-path");
112+
config.setProperty("node.install.persistentTempDir", dir + "/persistent-temp");
113+
114+
config.setProperty("node.masterKeyFile", dir + "/master.keys");
115+
config.setProperty("node.downloadsDir", dir + "/downloads");
116+
117+
config.setProperty("ssl.sslKeyStorePass", RandomStringUtils.randomAscii(64));
118+
config.setProperty("ssl.sslKeyPass", RandomStringUtils.randomAscii(64));
119+
120+
config.setProperty("logger.dirname", dir + "/logs");
121+
122+
return config;
123+
}
124+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.freenetproject.mobile;
2+
3+
import net.pterodactylus.fcp.*;
4+
import trikita.log.*;
5+
6+
import java.io.*;
7+
import java.net.*;
8+
9+
class Connector {
10+
private static final String DEFAULT_HOST = "127.0.0.1";
11+
private static final int DEFAULT_PORT = 9481;
12+
private final FcpConnection fcpConnection;
13+
14+
public Connector(FcpConnection fcpConnection) {
15+
this.fcpConnection = fcpConnection;
16+
}
17+
18+
public Connector(String host, int port) throws UnknownHostException {
19+
this(new FcpConnection(host, port));
20+
}
21+
22+
public Connector() throws UnknownHostException {
23+
this(DEFAULT_HOST, DEFAULT_PORT);
24+
}
25+
26+
public void connect() throws IOException {
27+
try {
28+
fcpConnection.connect();
29+
fcpConnection.sendMessage(new ClientHello("freenet-mobile"));
30+
} catch (Exception e) {
31+
Log.i("Freenet", "Failed to connect through FCP. Node shutdown or wrong port: " + e.getMessage());
32+
throw e;
33+
}
34+
}
35+
36+
public void modifyConfiguration(String key, String value) throws IOException {
37+
ModifyConfig modifyConfig = new ModifyConfig("identifier");
38+
modifyConfig.setOption(key, value);
39+
fcpConnection.sendMessage(modifyConfig);
40+
}
41+
}

src/main/java/org/freenetproject/mobile/Installer.java

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)