-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor install class into NodeController and Config classes #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c318114
Refactor install class into NodeController and Config classes
desyncr 24b80e5
Add defaults bookmarks, seednodes and freenet.ini
desyncr 9c84fa5
Add missing docblocks and upgrade version
desyncr 152bcb2
Java 8 support
desyncr 86ded31
Use custom config writer to avoid escaping special characters
desyncr 5f1d40f
Add reading configuration test
desyncr 799a48f
Add support for setConfig with InputStream
desyncr f0f2083
Randomize default passwords on setup
desyncr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.