Skip to content

Instantly share code, notes, and snippets.

@superblaubeere27
Created December 28, 2019 14:20
Show Gist options
  • Save superblaubeere27/97cba0f22d06232cb98033d28c21d37b to your computer and use it in GitHub Desktop.
Save superblaubeere27/97cba0f22d06232cb98033d28c21d37b to your computer and use it in GitHub Desktop.
AgentAttacher
package net.superblaubeere27.agentattacher;
import com.sun.tools.attach.*;
import joptsimple.*;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
public class Main {
public static void main(String[] args) throws IOException, AttachNotSupportedException {
OptionParser optionParser = new OptionParser();
ArgumentAcceptingOptionSpec<File> agentPath = optionParser.accepts("agentPath").withRequiredArg().ofType(File.class).required();
ArgumentAcceptingOptionSpec<String> username = optionParser.accepts("username", "The username of the minecraft session").withOptionalArg();
AbstractOptionSpec<Void> help = optionParser.accepts("help").forHelp();
OptionSet optionSet;
try {
optionSet = optionParser.parse(args);
} catch (OptionException e) {
System.err.println("Failed to parse options: " + e.getMessage() + " (try --help)");
return;
}
if (optionSet.has(help)) {
optionParser.printHelpOn(System.err);
return;
}
File agent = optionSet.valueOf(agentPath);
if (!agent.exists()) {
System.err.println("Agent doesn't exist");
return;
}
OptionSet finalOptionSet = optionSet;
Optional<VirtualMachineDescriptor> first = VirtualMachine.list()
.stream()
.filter(vm_desc -> vm_desc.displayName().startsWith("net.minecraft.client.main.Main") && (!finalOptionSet.has(username) || vm_desc.displayName().contains(finalOptionSet.valueOf(username))))
.findFirst();
if (!first.isPresent()) {
System.err.println("Minecraft session wasn't found");
return;
}
VirtualMachine attach = VirtualMachine.attach(first.get());
try {
attach.loadAgent(agent.getAbsolutePath());
System.out.println("Agent was loaded.");
} catch (AgentLoadException e) {
System.err.println("Agent failed to load: ");
e.printStackTrace();
} catch (AgentInitializationException e) {
System.err.println("Agent loaded, but failed to initialize: ");
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.superblaubeere27</groupId>
<artifactId>AgentAttacher</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>net.superblaubeere27.agentattacher.Main</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.sf.jopt-simple</groupId>
<artifactId>jopt-simple</artifactId>
<version>5.0.4</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment