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

fixed SFTP-Upload, added needed config entries, updated pom.xml #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.14.2-R0.1-SNAPSHOT</version>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
Expand Down
52 changes: 44 additions & 8 deletions src/me/zombie_striker/sr/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ public class Main extends JavaPlugin {
private boolean useFTP = false;
private boolean useFTPS = false;
private boolean useSFTP = false;
private boolean useKeyAuthSFTP = false;
private boolean useKeyWithPassphrase = false;
private String serverFTP = "www.example.com";
private String userFTP = "User";
private String passwordFTP = "password";
private String remoteFilePathSFTP = "";
private String privateKeyPathSFTP = "";
private String privateKeyPassphrase = "";
private int portFTP = 80;
private String naming_format = "Backup-%date%";
private SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Expand All @@ -57,6 +62,7 @@ public class Main extends JavaPlugin {
private int maxSaveFiles = 1000;
private boolean deleteZipOnFail = false;
private boolean deleteZipOnFTP = false;
private boolean allowUnknownHost = false;

private int hourToSaveAt = -1;

Expand Down Expand Up @@ -176,12 +182,16 @@ public void onEnable() {
useFTP = (boolean) a("EnableFTP", false);
useFTPS = (boolean) a("EnableFTPS", false);
useSFTP = (boolean) a("EnableSFTP", false);
useKeyAuthSFTP = (boolean) a("UseKeyAuthSFTP",false);
useKeyWithPassphrase = (boolean) a("UseKeyWithPassphrase", false);
serverFTP = (String) a("FTPAdress", serverFTP);
portFTP = (int) a("FTPPort", portFTP);
userFTP = (String) a("FTPUsername", userFTP);
passwordFTP = (String) a("FTPPassword", passwordFTP);


remoteFilePathSFTP = (String) a("RemoteFilepathSFTP", remoteFilePathSFTP);
privateKeyPathSFTP = (String) a("PrivateKeyPathSFTP",privateKeyPathSFTP);
privateKeyPassphrase = (String) a("PrivateKeyPassphrase",privateKeyPassphrase);

compression = (int) a("CompressionLevel_Max_9", compression);

removeFilePath = (String) a("FTP_Directory", removeFilePath);
Expand All @@ -203,6 +213,7 @@ public void onEnable() {

deleteZipOnFTP = (boolean) a("DeleteZipOnFTPTransfer", false);
deleteZipOnFail = (boolean) a("DeleteZipIfFailed", false);
allowUnknownHost = (boolean) a ("AllowUnknownHost",false);
separator = (String) a("FolderSeparator", separator);
if (saveTheConfig)
saveConfig();
Expand Down Expand Up @@ -461,24 +472,49 @@ public void run() {
for (World world : autosave)
world.setAutoSave(true);
if (useSFTP) {
JSch jsch = new JSch();
Session session = null;
try {
sender.sendMessage(prefix + " Starting SFTP Transfer");
JSch jsch = new JSch();
Session session = jsch.getSession(userFTP, serverFTP, portFTP);
session.setConfig("PreferredAuthentications", "password");
session.setPassword(passwordFTP);
session = jsch.getSession(userFTP, serverFTP, portFTP);
if(useKeyAuthSFTP) {
if(useKeyWithPassphrase) {
jsch.addIdentity(privateKeyPathSFTP,privateKeyPassphrase);
} else {
jsch.addIdentity(privateKeyPathSFTP);
}
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
} else {
session.setConfig("PreferredAuthentications", "password");
session.setPassword(passwordFTP);
}
//allows the server to connect to unknown hosts
if(allowUnknownHost) {
session.setConfig( "StrictHostKeyChecking", "no" );
}
session.connect(1000 * 20);
Channel channel = session.openChannel("sftp");
ChannelSftp sftp = (ChannelSftp) channel;
sftp.connect(1000 * 20);
FileInputStream zipFileStream = new FileInputStream(zipFile);
sftp.put(zipFileStream,remoteFilePathSFTP+ zipFile.getName());
sender.sendMessage(prefix+ " Transfer successful");
zipFileStream.close();
sftp.exit();
if (deleteZipOnFTP)
zipFile.delete();
} catch (Exception | Error e) {
sender.sendMessage(
prefix + " FAILED TO SFTP TRANSFER FILE: " + zipFile.getName() + ". ERROR IN CONSOLE.");
if (deleteZipOnFail)
zipFile.delete();
e.printStackTrace();
}
} else if (useFTPS) {
}finally {
if (session.isConnected()) {
sender.sendMessage(prefix + " Disconnecting");
session.disconnect();
}
}} else if (useFTPS) {
sender.sendMessage(prefix + " Starting FTPS Transfer");
FileInputStream zipFileStream = new FileInputStream(zipFile);
FTPSClient ftpClient = new FTPSClient();
Expand Down