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..731c18a 100644 --- a/src/me/zombie_striker/sr/Main.java +++ b/src/me/zombie_striker/sr/Main.java @@ -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"); @@ -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; @@ -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); @@ -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(); @@ -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();