-
Notifications
You must be signed in to change notification settings - Fork 267
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
Load active peers from previous session saved into file. #2230
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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
71 changes: 71 additions & 0 deletions
71
rskj-core/src/main/java/co/rsk/net/discovery/KnownPeersHandler.java
This file contains 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,71 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package co.rsk.net.discovery; | ||
|
||
import co.rsk.util.SimpleFileWriter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.stream.Collectors; | ||
|
||
public class KnownPeersHandler { | ||
private static final Logger logger = LoggerFactory.getLogger(KnownPeersHandler.class); | ||
private final Path peerListFileDir; | ||
private final SimpleFileWriter fileDataSaver; | ||
|
||
public KnownPeersHandler(Path peerListFileDir) { | ||
this(peerListFileDir, SimpleFileWriter.getInstance()); | ||
} | ||
|
||
public KnownPeersHandler(Path peerListFileDir, SimpleFileWriter fileDataSaver) { | ||
this.peerListFileDir = peerListFileDir; | ||
this.fileDataSaver = fileDataSaver; | ||
} | ||
public void savePeers(Map<String,String> knownPeers) { | ||
logger.debug("Saving peers {} to file in {}", knownPeers, peerListFileDir); | ||
Properties props = new Properties(); | ||
props.putAll(knownPeers); | ||
try { | ||
fileDataSaver.savePropertiesIntoFile(props, peerListFileDir); | ||
} catch (IOException e) { | ||
logger.error("Error saving active peers to file: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
public List<String> readPeers(){ | ||
File file = peerListFileDir.toFile(); | ||
Properties props = new Properties(); | ||
if (file.canRead()) { | ||
try (FileReader reader = new FileReader(file)) { | ||
props.load(reader); | ||
} catch (IOException e) { | ||
logger.error("Error reading active peers from file: {}", e.getMessage()); | ||
return Collections.emptyList(); | ||
} | ||
} | ||
return props.values().stream().map(Object::toString).collect(Collectors.toList()); | ||
} | ||
} |
This file contains 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 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,62 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2023 RSK Labs Ltd. | ||
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package co.rsk.util; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Properties; | ||
|
||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | ||
|
||
public class SimpleFileWriter { | ||
private static final String TMP = ".tmp"; | ||
private static SimpleFileWriter instance; | ||
|
||
private SimpleFileWriter() { | ||
} | ||
|
||
public static SimpleFileWriter getInstance() { | ||
if (instance == null) { | ||
instance = new SimpleFileWriter(); | ||
} | ||
return instance; | ||
} | ||
|
||
public void savePropertiesIntoFile(Properties properties, Path filePath) throws IOException { | ||
File tempFile = File.createTempFile(filePath.toString(), TMP); | ||
try (FileWriter writer = new FileWriter(tempFile, false)) { | ||
properties.store(writer, null); | ||
} | ||
filePath.toFile().getParentFile().mkdirs(); | ||
Files.move(tempFile.toPath(), filePath, REPLACE_EXISTING); | ||
} | ||
public void saveDataIntoFile(String data, Path filePath) throws IOException { | ||
|
||
File tempFile = File.createTempFile(filePath.toString(), TMP); | ||
Check warning Code scanning / CodeQL Local information disclosure in a temporary directory Medium
Local information disclosure vulnerability due to use of file readable by other local users.
|
||
try (FileWriter writer = new FileWriter(tempFile, false)) { | ||
writer.write(data); | ||
} | ||
filePath.toFile().getParentFile().mkdirs(); | ||
Files.move(tempFile.toPath(), filePath, REPLACE_EXISTING); | ||
} | ||
} | ||
|
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -153,4 +153,6 @@ void nodeIsAlreadyStopped_WhenStopNode_ThenShouldNotThrowError() throws Exceptio | |
fail(); | ||
} | ||
} | ||
|
||
|
||
} |
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.
Check warning
Code scanning / CodeQL
Local information disclosure in a temporary directory Medium