From eb46e40b2160dcf3ef1db114294c06f79860b866 Mon Sep 17 00:00:00 2001 From: a-umar Date: Tue, 16 Feb 2021 00:19:34 +0100 Subject: [PATCH 1/2] fixed SFTP-Upload, added needed config entries, updated pom.xml --- pom.xml | 2 +- src/me/zombie_striker/sr/Main.java | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index b431288..0148129 100644 --- a/pom.xml +++ b/pom.xml @@ -50,7 +50,7 @@ org.spigotmc spigot - 1.14.2-R0.1-SNAPSHOT + 1.16.5-R0.1-SNAPSHOT provided diff --git a/src/me/zombie_striker/sr/Main.java b/src/me/zombie_striker/sr/Main.java index c6d8d06..e6d6ab3 100644 --- a/src/me/zombie_striker/sr/Main.java +++ b/src/me/zombie_striker/sr/Main.java @@ -49,6 +49,7 @@ public class Main extends JavaPlugin { private String serverFTP = "www.example.com"; private String userFTP = "User"; private String passwordFTP = "password"; + private String remoteFilePathSFTP =""; private int portFTP = 80; private String naming_format = "Backup-%date%"; private SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); @@ -57,6 +58,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; @@ -180,7 +182,7 @@ public void onEnable() { portFTP = (int) a("FTPPort", portFTP); userFTP = (String) a("FTPUsername", userFTP); passwordFTP = (String) a("FTPPassword", passwordFTP); - + remoteFilePathSFTP = (String) a("RemoteFilepathSFTP", remoteFilePathSFTP); compression = (int) a("CompressionLevel_Max_9", compression); @@ -203,6 +205,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(); @@ -461,24 +464,40 @@ 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 = jsch.getSession(userFTP, serverFTP, portFTP); + //allows the server to connect to unknown hosts + if(allowUnknownHost) { + session.setConfig( "StrictHostKeyChecking", "no" ); + } session.setConfig("PreferredAuthentications", "password"); session.setPassword(passwordFTP); 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(); From e4bc3c5a4d0238dfb4058ec2978ffbf7c002f614 Mon Sep 17 00:00:00 2001 From: a-umar Date: Tue, 16 Feb 2021 18:53:23 +0100 Subject: [PATCH 2/2] Added support for public key authentication for SFTP/SSH --- src/me/zombie_striker/sr/Main.java | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/me/zombie_striker/sr/Main.java b/src/me/zombie_striker/sr/Main.java index e6d6ab3..731c18a 100644 --- a/src/me/zombie_striker/sr/Main.java +++ b/src/me/zombie_striker/sr/Main.java @@ -46,10 +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 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"); @@ -178,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); @@ -469,19 +477,28 @@ public void run() { try { sender.sendMessage(prefix + " Starting SFTP Transfer"); 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.setConfig("PreferredAuthentications", "password"); - session.setPassword(passwordFTP); 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"); + sender.sendMessage(prefix+ " Transfer successful"); zipFileStream.close(); sftp.exit(); if (deleteZipOnFTP) @@ -494,7 +511,7 @@ public void run() { e.printStackTrace(); }finally { if (session.isConnected()) { - sender.sendMessage(prefix + "Disconnecting"); + sender.sendMessage(prefix + " Disconnecting"); session.disconnect(); } }} else if (useFTPS) {