AppleTunneler/src/main/java/net/limework/networking/AppleTunneler.java

134 lines
4.2 KiB
Java
Raw Normal View History

2022-01-30 23:34:33 +00:00
package net.limework.networking;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
2022-01-30 23:34:33 +00:00
public class AppleTunneler extends JavaPlugin {
private Session sshSession;
private final JSch jsch = new JSch();
private synchronized void createNewConnection() throws JSchException {
if (sshSession != null) {
sshSession.disconnect();
}
sshSession = jsch.getSession(getUsername(), getHost(), getPort());
sshSession.connect(2000);
2022-02-04 11:51:25 +00:00
getLogger().info("Ssh connection started successfully!");
2022-01-30 23:34:33 +00:00
loadRemoteToLocalPortForwarding();
}
private void connectionFailedLog(JSchException exception) {
getLogger().severe("==========================");
getLogger().severe("failed to start a connection");
exception.printStackTrace();
getLogger().severe("==========================");
}
private void loadIds() throws JSchException {
File file = new File(getDataFolder(), "Identities");
if (file.mkdir()) {
getLogger().info("created Identities folder.");
}
File[] files = file.listFiles();
2022-06-17 02:43:26 +00:00
if (files == null || files.length == 0) {
2022-01-30 23:34:33 +00:00
getLogger().warning("no Identities files found, please put them in " + file.getPath());
throw new RuntimeException("no Identities files found");
}
for (File id : files) {
jsch.addIdentity(id.getPath());
}
2022-02-04 11:51:25 +00:00
getLogger().info("Ids were loaded!");
2022-01-30 23:34:33 +00:00
}
private void loadRemoteToLocalPortForwarding() throws JSchException {
for (String line : getConfig().getStringList("ports")) {
2022-02-04 11:51:25 +00:00
sshSession.setPortForwardingL(line);
getLogger().info(line);
2022-01-30 23:34:33 +00:00
}
2022-02-04 11:51:25 +00:00
getLogger().info("ports has been set!");
2022-01-30 23:34:33 +00:00
}
private void loadKnownHostFile() throws FileNotFoundException, JSchException {
File knownHosts = new File(getDataFolder(), ".known_hosts");
2022-06-17 02:46:41 +00:00
// check whatever known hosts file is folder somehow if so delete it.
if (knownHosts.isDirectory()) {
knownHosts.delete();
}
if (!knownHosts.exists()) {
getLogger().severe("FILE at path: " + knownHosts.getAbsolutePath() + " Does not exists");
throw new RuntimeException("Known host file does not exists in plugin folder");
}
this.jsch.setKnownHosts(new FileInputStream(knownHosts));
2022-02-04 11:51:25 +00:00
getLogger().info("host file was loaded!");
}
2022-01-30 23:34:33 +00:00
@Override
public void onLoad() {
2022-01-30 23:34:33 +00:00
this.saveDefaultConfig();
try {
loadIds();
loadKnownHostFile();
} catch (JSchException | FileNotFoundException e) {
2022-01-30 23:34:33 +00:00
throw new RuntimeException(e);
}
try {
createNewConnection();
} catch (JSchException e) {
connectionFailedLog(e);
}
}
@Override
public void onEnable() {
2022-01-30 23:34:33 +00:00
new BukkitRunnable() {
private boolean isConnecting = false;
@Override
public void run() {
if (isConnecting) {
return;
}
if (!sshSession.isConnected()) {
isConnecting = true;
try {
createNewConnection();
} catch (JSchException e) {
connectionFailedLog(e);
}
isConnecting = false;
}
}
}.runTaskTimerAsynchronously(this, 0, 20);
getLogger().info("reconnecting task was registered successfully");
2022-01-30 23:34:33 +00:00
}
/*
Currently there is no onDisable because plugin might disable itself before another
which might causes disconnections and data never saved
*/
2022-01-30 23:34:33 +00:00
private int getPort() {
return this.getConfig().getInt("port");
}
private String getUsername() {
return this.getConfig().getString("username");
}
private String getHost() {
return this.getConfig().getString("host");
}
}