This commit is contained in:
mohammed jasem alaajel 2022-01-31 03:34:33 +04:00
parent d4b36d574f
commit 914c01f532
5 changed files with 183 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
# intellji idea
/.idea
*.iml
# java stuff

58
pom.xml
View File

@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.glomc.networking</groupId>
<artifactId>SpigotNebulaVpn</artifactId>
<groupId>net.limework.networking</groupId>
<artifactId>AppleTunneler</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
@ -13,4 +13,58 @@
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.1-SNAPSHOT</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.mwiede</groupId>
<artifactId>jsch</artifactId>
<version>0.1.72</version>
</dependency>
<!--
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
!-->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,109 @@
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;
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();
}
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
sshSession = jsch.getSession(getUsername(), getHost(), getPort());
sshSession.setConfig(config);
sshSession.connect(2000);
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();
if (files == null) {
return;
}
if (files.length == 0) {
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());
}
}
private void loadRemoteToLocalPortForwarding() throws JSchException {
for (String line : getConfig().getStringList("ports")) {
String[] array = line.split(":");
sshSession.setPortForwardingL(Integer.parseInt(array[0]), array[1], Integer.parseInt(array[2]));
}
}
@Override
public void onEnable() {
this.saveDefaultConfig();
try {
loadIds();
} catch (JSchException e) {
throw new RuntimeException(e);
}
try {
createNewConnection();
} catch (JSchException e) {
connectionFailedLog(e);
}
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);
}
private int getPort() {
return this.getConfig().getInt("port");
}
private String getUsername() {
return this.getConfig().getString("username");
}
private String getHost() {
return this.getConfig().getString("host");
}
}

View File

@ -0,0 +1,13 @@
# welcome to the config of ${project.artifactId}
# username for ssh
username: apple
# host eg: domains can be used.
host: 192.168.0.100
# port
port: 22
# Syntax is "local-port:remote-host:remote-port"
ports:
- "25565:127.0.0.1:25565"

View File

@ -0,0 +1,4 @@
main: net.limework.networking.AppleTunneler
version: ${project.version}
name: ${project.artifactId}
api-version: 1.13