mirror of
https://github.com/noviceli/jetbra
synced 2024-12-25 04:43:46 +03:00
refactor agent
This commit is contained in:
parent
82dfca233c
commit
e0a4daa14e
19 changed files with 101 additions and 65 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
*.iml
|
||||
.idea/
|
||||
target/
|
||||
target/
|
||||
.fastRequest
|
2
block_url_keywords
Normal file
2
block_url_keywords
Normal file
|
@ -0,0 +1,2 @@
|
|||
https://account.jetbrains.com/lservice/rpc/validateKey.action
|
||||
116.62.33.138
|
|
@ -13,8 +13,8 @@
|
|||
<artifactId>jetbra-agent</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
@ -29,6 +29,13 @@
|
|||
<artifactId>byte-buddy-agent</artifactId>
|
||||
<version>1.14.8</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -11,15 +11,18 @@ public class AgentMain {
|
|||
public static void premain(String agentArgs, Instrumentation inst) throws Exception {
|
||||
printLogo();
|
||||
AgentBuilder agentBuilder = newAgentBuilder();
|
||||
agentBuilder.type(ElementMatchers.named("java.security.cert.PKIXBuilderParameters"))
|
||||
agentBuilder
|
||||
.type(ElementMatchers.named("java.security.cert.PKIXBuilderParameters"))
|
||||
.transform((builder, typeDescription, classLoader, module, protectionDomain) -> builder
|
||||
.visit(Advice.to(PKIXBuilderParametersAdvice.class)
|
||||
.on(ElementMatchers.isConstructor().and(ElementMatchers.takesArgument(0, Set.class)))))
|
||||
.asTerminalTransformation()
|
||||
|
||||
|
||||
.type(ElementMatchers.named("sun.net.www.http.HttpClient"))
|
||||
.transform((builder, typeDescription, classLoader, module, protectionDomain) -> builder
|
||||
.visit(Advice.to(HttpClientAdvice.class)
|
||||
.on(ElementMatchers.named("openServer"))))
|
||||
.on(ElementMatchers.named("openServer").and(ElementMatchers.takesArgument(0, String.class)))))
|
||||
.asTerminalTransformation()
|
||||
|
||||
.type(ElementMatchers.named("java.lang.System"))
|
||||
|
@ -28,12 +31,6 @@ public class AgentMain {
|
|||
.on(ElementMatchers.named("getProperty"))))
|
||||
.asTerminalTransformation()
|
||||
|
||||
.type(ElementMatchers.named("java.net.Socket"))
|
||||
.transform((builder, typeDescription, classLoader, module, protectionDomain) -> builder
|
||||
.visit(Advice.to(SocketAdvice.class)
|
||||
.on(ElementMatchers.named("connect"))))
|
||||
.asTerminalTransformation()
|
||||
|
||||
.installOn(inst);
|
||||
|
||||
agentBuilder.installOn(inst);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package win.novice.li;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.TrustAnchor;
|
||||
|
@ -11,9 +11,9 @@ import java.security.cert.X509Certificate;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class TrustAnchorHolder {
|
||||
public class ConfigHelper {
|
||||
public static Set<TrustAnchor> TRUST_ANCHORS;
|
||||
|
||||
public static Set<String> BLOCK_URL_KEYWORDS;
|
||||
|
||||
public static Set<TrustAnchor> loadTrustAnchors() throws Exception {
|
||||
if (TRUST_ANCHORS != null) {
|
||||
|
@ -22,8 +22,8 @@ public class TrustAnchorHolder {
|
|||
TRUST_ANCHORS = new HashSet<>();
|
||||
|
||||
String certDir;
|
||||
if (System.getenv("JB_HOME") != null) {
|
||||
certDir = System.getenv("JB_HOME");
|
||||
if (System.getenv("TRUST_CRT_DIR") != null) {
|
||||
certDir = System.getenv("TRUST_CRT_DIR");
|
||||
} else {
|
||||
URI jarURI = getJarURI();
|
||||
if (jarURI == null) {
|
||||
|
@ -39,23 +39,52 @@ public class TrustAnchorHolder {
|
|||
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
|
||||
for (File item : files) {
|
||||
if (item.getName().endsWith(".crt")) {
|
||||
X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(new FileInputStream(item));
|
||||
X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(Files.newInputStream(item.toPath()));
|
||||
TRUST_ANCHORS.add(new TrustAnchor(cert, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("loaded " + TRUST_ANCHORS.size() + " crts");
|
||||
System.out.println("loaded " + TRUST_ANCHORS.size() + " crts");
|
||||
return TRUST_ANCHORS;
|
||||
}
|
||||
|
||||
public static Set<String> loadBlockUrlKeywords() throws Exception {
|
||||
if (BLOCK_URL_KEYWORDS != null) {
|
||||
return BLOCK_URL_KEYWORDS;
|
||||
}
|
||||
BLOCK_URL_KEYWORDS = new HashSet<>();
|
||||
String blockUrlKeywordFilePath;
|
||||
if (System.getenv("BLOCK_URL_KEYWORD_FILE_PATH") != null) {
|
||||
blockUrlKeywordFilePath = System.getenv("BLOCK_URL_KEYWORD_FILE_PATH");
|
||||
} else {
|
||||
URI jarURI = getJarURI();
|
||||
if (jarURI == null) {
|
||||
return BLOCK_URL_KEYWORDS;
|
||||
}
|
||||
blockUrlKeywordFilePath = Paths.get(jarURI).getParent().resolve("block_url_keywords").toString();
|
||||
}
|
||||
System.out.println("load block url keywords from " + blockUrlKeywordFilePath);
|
||||
File file = new File(blockUrlKeywordFilePath);
|
||||
if (file.exists()) {
|
||||
for (String line : Files.readAllLines(file.toPath())) {
|
||||
if (!line.trim().isEmpty()) {
|
||||
BLOCK_URL_KEYWORDS.add(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("loaded " + BLOCK_URL_KEYWORDS.size() + " keywords");
|
||||
return BLOCK_URL_KEYWORDS;
|
||||
}
|
||||
|
||||
|
||||
public static URI getJarURI() throws Exception {
|
||||
URL url = TrustAnchorHolder.class.getProtectionDomain().getCodeSource().getLocation();
|
||||
URL url = ConfigHelper.class.getProtectionDomain().getCodeSource().getLocation();
|
||||
if (null != url) {
|
||||
return url.toURI();
|
||||
}
|
||||
String resourcePath = "/jarLocation.txt";
|
||||
url = TrustAnchorHolder.class.getResource(resourcePath);
|
||||
url = ConfigHelper.class.getResource(resourcePath);
|
||||
if (null == url) {
|
||||
return null;
|
||||
}
|
|
@ -1,14 +1,26 @@
|
|||
package win.novice.li;
|
||||
|
||||
import net.bytebuddy.asm.Advice;
|
||||
import sun.net.www.http.HttpClient;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class HttpClientAdvice {
|
||||
@Advice.OnMethodExit
|
||||
public static void intercept(@Advice.This Object x) throws Exception {
|
||||
if (x.toString().contains("validateKey.action")){
|
||||
throw new SocketTimeoutException();
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void intercept(@Advice.This Object httpClient) throws Exception {
|
||||
Class<?> clazz = Class.forName("win.novice.li.ConfigHelper", true, ClassLoader.getSystemClassLoader());
|
||||
Method method = clazz.getDeclaredMethod("loadBlockUrlKeywords");
|
||||
Set<String> BLOCK_URL_KEYWORDS = (Set<String>) method.invoke(null);
|
||||
String clientString = httpClient.toString();
|
||||
for (String keyword : BLOCK_URL_KEYWORDS) {
|
||||
if (clientString.contains(keyword)) {
|
||||
throw new SocketTimeoutException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ public class PKIXBuilderParametersAdvice {
|
|||
@Advice.OnMethodEnter
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void intercept(@Advice.Argument(value = 0, readOnly = false) Set<TrustAnchor> trustAnchors) throws Exception {
|
||||
Class<?> clazz = Class.forName("win.novice.li.TrustAnchorHolder", true, ClassLoader.getSystemClassLoader());
|
||||
Class<?> clazz = Class.forName("win.novice.li.ConfigHelper", true, ClassLoader.getSystemClassLoader());
|
||||
Method method = clazz.getDeclaredMethod("loadTrustAnchors");
|
||||
Set<TrustAnchor> loadedTrustAnchors = (Set<TrustAnchor>)method.invoke(null);
|
||||
HashSet<TrustAnchor> newTrustAnchors = new HashSet<>(trustAnchors);
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
package win.novice.li;
|
||||
|
||||
import net.bytebuddy.asm.Advice;
|
||||
|
||||
import java.net.ConnectException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
|
||||
public class SocketAdvice {
|
||||
@Advice.OnMethodExit
|
||||
public static void intercept(@Advice.Argument(value = 0,readOnly = false) SocketAddress socketAddress) throws Exception {
|
||||
if (socketAddress instanceof InetSocketAddress){
|
||||
InetAddress address = ((InetSocketAddress) socketAddress).getAddress();
|
||||
if (address.getHostAddress().equals("116.62.33.138")){
|
||||
throw new ConnectException("拒绝连接");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,13 +2,15 @@ package win.novice.li;
|
|||
|
||||
import net.bytebuddy.asm.Advice;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class SystemAdvice {
|
||||
|
||||
|
||||
// System.getProperty
|
||||
@Advice.OnMethodExit
|
||||
public static void intercept(@Advice.Argument(0) Object x, @Advice.Return(readOnly = false) String r) throws Exception {
|
||||
if (x.toString().equals("jb.vmOptionsFile")) {
|
||||
if (Objects.equals(x, "jb.vmOptionsFile")) {
|
||||
RuntimeException exception = new RuntimeException();
|
||||
int nullCnt = 0;
|
||||
boolean hasReflect = false;
|
||||
|
|
|
@ -41,5 +41,9 @@
|
|||
<source>${project.parent.basedir}/jetbra-agent/target/jetbra-agent.jar</source>
|
||||
<destName>jetbra-agent.jar</destName>
|
||||
</file>
|
||||
<file>
|
||||
<source>${project.parent.basedir}/block_url_keywords</source>
|
||||
<destName>block_url_keywords</destName>
|
||||
</file>
|
||||
</files>
|
||||
</assembly>
|
||||
|
|
|
@ -13,8 +13,8 @@
|
|||
<artifactId>jetbra-dist</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<build>
|
||||
|
|
|
@ -13,8 +13,8 @@
|
|||
<artifactId>jetbra-server</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
@ -40,12 +40,12 @@
|
|||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcpkix-jdk18on</artifactId>
|
||||
<version>1.72</version>
|
||||
<version>1.77</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk18on</artifactId>
|
||||
<version>1.72</version>
|
||||
<version>1.77</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
|
|
@ -19,6 +19,7 @@ import java.security.*;
|
|||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
|
@ -45,8 +46,7 @@ public class LicenseController {
|
|||
String sigResultsBase64 = Base64.getEncoder().encodeToString(signatureBytes);
|
||||
|
||||
String result = licenseId + "-" + licensePartBase64 + "-" + sigResultsBase64 + "-" + Base64.getEncoder().encodeToString(CRT.getEncoded());
|
||||
|
||||
return Map.of("license", result);
|
||||
return Collections.singletonMap("license", result);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package win.novice.li.model;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
|
|
|
@ -2,10 +2,11 @@
|
|||
package win.novice.li.model;
|
||||
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
public class Product {
|
||||
@NotBlank
|
||||
|
|
4
pom.xml
4
pom.xml
|
@ -5,7 +5,7 @@
|
|||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<version>2.7.14</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<groupId>win.novice</groupId>
|
||||
|
@ -20,7 +20,7 @@
|
|||
<module>jetbra-dist</module>
|
||||
</modules>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -64,7 +64,7 @@ Sub ProcessVmOptions(ByVal file)
|
|||
Loop
|
||||
oFile.Close
|
||||
|
||||
sNewContent = sNewContent & "-javaagent:" & sJarFile & "=jetbrains"
|
||||
sNewContent = sNewContent & "-javaagent:" & sJarFile
|
||||
Set oFile = oFS.OpenTextFile(file, 2, 0)
|
||||
oFile.Write sNewContent
|
||||
oFile.Close
|
||||
|
|
|
@ -45,7 +45,7 @@ Sub ProcessVmOptions(ByVal file)
|
|||
Loop
|
||||
oFile.Close
|
||||
|
||||
sNewContent = sNewContent & "-javaagent:" & sJarFile & "=jetbrains"
|
||||
sNewContent = sNewContent & "-javaagent:" & sJarFile
|
||||
Set oFile = oFS.OpenTextFile(file, 2, 0)
|
||||
oFile.Write sNewContent
|
||||
oFile.Close
|
||||
|
|
|
@ -56,7 +56,7 @@ for PRD in $JB_PRODUCTS; do
|
|||
sed -i '/^\-javaagent:.*[\/\\]jetbra\-agent\.jar.*/d' "${VM_FILE_PATH}"
|
||||
fi
|
||||
|
||||
echo "-javaagent:${JAR_FILE_PATH}=jetbrains" >>"${VM_FILE_PATH}"
|
||||
echo "-javaagent:${JAR_FILE_PATH}" >>"${VM_FILE_PATH}"
|
||||
|
||||
ENV_NAME=$(echo $PRD | tr '[a-z]' '[A-Z]')"_VM_OPTIONS"
|
||||
echo "export ${ENV_NAME}=\"${VM_FILE_PATH}\"" >>"${MY_VMOPTIONS_SHELL_FILE}"
|
||||
|
|
Loading…
Add table
Reference in a new issue