From f1a2ce1395c095b76bb33cc3d63e15f8254fb2e8 Mon Sep 17 00:00:00 2001 From: "novice.li" Date: Sat, 25 Nov 2023 15:31:09 +0800 Subject: [PATCH] init --- .gitignore | 3 + jetbra-agent/pom.xml | 1 + .../main/java/win/novice/li/AgentMain.java | 2 +- ....java => PKIXBuilderParametersAdvice.java} | 3 +- script/install-all-users.vbs | 73 +++++++++++++++ script/install-current-user.vbs | 54 +++++++++++ script/install.sh | 91 +++++++++++++++++++ script/uninstall-all-users.vbs | 28 ++++++ script/uninstall-current-user.vbs | 17 ++++ script/uninstall.sh | 50 ++++++++++ 10 files changed, 319 insertions(+), 3 deletions(-) create mode 100644 .gitignore rename jetbra-agent/src/main/java/win/novice/li/{PKIXBuilderParameters.java => PKIXBuilderParametersAdvice.java} (92%) create mode 100644 script/install-all-users.vbs create mode 100644 script/install-current-user.vbs create mode 100755 script/install.sh create mode 100644 script/uninstall-all-users.vbs create mode 100644 script/uninstall-current-user.vbs create mode 100755 script/uninstall.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..395dde9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.iml +.idea/ +target/ \ No newline at end of file diff --git a/jetbra-agent/pom.xml b/jetbra-agent/pom.xml index 4b09e1c..1e35d70 100644 --- a/jetbra-agent/pom.xml +++ b/jetbra-agent/pom.xml @@ -47,6 +47,7 @@ true + jetbra-agent diff --git a/jetbra-agent/src/main/java/win/novice/li/AgentMain.java b/jetbra-agent/src/main/java/win/novice/li/AgentMain.java index c55076b..ce29191 100644 --- a/jetbra-agent/src/main/java/win/novice/li/AgentMain.java +++ b/jetbra-agent/src/main/java/win/novice/li/AgentMain.java @@ -13,7 +13,7 @@ public class AgentMain { AgentBuilder agentBuilder = newAgentBuilder(); agentBuilder.type(ElementMatchers.named("java.security.cert.PKIXBuilderParameters")) .transform((builder, typeDescription, classLoader, module, protectionDomain) -> builder - .visit(Advice.to(PKIXBuilderParameters.class) + .visit(Advice.to(PKIXBuilderParametersAdvice.class) .on(ElementMatchers.isConstructor().and(ElementMatchers.takesArgument(0, Set.class))))) .asTerminalTransformation() .type(ElementMatchers.named("sun.net.www.http.HttpClient")) diff --git a/jetbra-agent/src/main/java/win/novice/li/PKIXBuilderParameters.java b/jetbra-agent/src/main/java/win/novice/li/PKIXBuilderParametersAdvice.java similarity index 92% rename from jetbra-agent/src/main/java/win/novice/li/PKIXBuilderParameters.java rename to jetbra-agent/src/main/java/win/novice/li/PKIXBuilderParametersAdvice.java index 8848feb..9123702 100644 --- a/jetbra-agent/src/main/java/win/novice/li/PKIXBuilderParameters.java +++ b/jetbra-agent/src/main/java/win/novice/li/PKIXBuilderParametersAdvice.java @@ -2,13 +2,12 @@ package win.novice.li; import net.bytebuddy.asm.Advice; -import java.lang.reflect.Field; import java.lang.reflect.Method; import java.security.cert.TrustAnchor; import java.util.HashSet; import java.util.Set; -public class PKIXBuilderParameters { +public class PKIXBuilderParametersAdvice { @Advice.OnMethodEnter diff --git a/script/install-all-users.vbs b/script/install-all-users.vbs new file mode 100644 index 0000000..cc5512d --- /dev/null +++ b/script/install-all-users.vbs @@ -0,0 +1,73 @@ +If Not WScript.Arguments.Named.Exists("elevate") Then + CreateObject("Shell.Application").ShellExecute WScript.FullName, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 10 + WScript.Quit +End If + +Set oShell = CreateObject("WScript.Shell") +Set oEnvSystem = oShell.Environment("SYSTEM") +Set oFS = CreateObject("Scripting.FileSystemObject") + +Dim sBasePath, sJarFile +sBasePath = oFS.GetParentFolderName(oFS.GetParentFolderName(WScript.ScriptFullName)) +sJarFile = sBasePath & "\jetbra-agent.jar" + +If Not oFS.FileExists(sJarFile) Then + MsgBox "jetbra-agent.jar not found", vbOKOnly Or vbCritical + WScript.Quit -1 +End If + +MsgBox "It may take a few seconds to execute this script." & vbCrLf & vbCrLf & "Click 'OK' button and wait for the prompt of 'Done.' to pop up!" + +Dim sEnvKey, sEnvVal, aJBProducts +aJBProducts = Array("idea", "clion", "phpstorm", "goland", "pycharm", "webstorm", "webide", "rider", "datagrip", "rubymine", "appcode", "dataspell", "gateway", "jetbrains_client", "jetbrainsclient", "studio", "devecostudio") + +Set re = New RegExp +re.Global = True +re.IgnoreCase = True +re.Pattern = "^\-javaagent:.*[\/\\]jetbra\-agent\.jar.*" + +Sub RemoveEnv(env) + On Error Resume Next + + For Each sPrd in aJBProducts + sEnvKey = UCase(sPrd) & "_VM_OPTIONS" + sEnvVal = oShell.ExpandEnvironmentStrings("%" & sEnvKey & "%") + If sEnvVal <> ("%" & sEnvKey & "%") Then + env.Remove(sEnvKey) + End If + Next +End Sub + +RemoveEnv oShell.Environment("USER") + +Dim sVmOptionsFile +For Each sPrd in aJBProducts + sEnvKey = UCase(sPrd) & "_VM_OPTIONS" + sVmOptionsFile = sBasePath & "\vmoptions\" & sPrd & ".vmoptions" + If oFS.FileExists(sVmOptionsFile) Then + ProcessVmOptions sVmOptionsFile + oEnvSystem(sEnvKey) = sVmOptionsFile + End If +Next + +Sub ProcessVmOptions(ByVal file) + Dim sLine, sNewContent, bMatch + Set oFile = oFS.OpenTextFile(file, 1, 0) + + sNewContent = "" + Do Until oFile.AtEndOfStream + sLine = oFile.ReadLine + bMatch = re.Test(sLine) + If Not bMatch Then + sNewContent = sNewContent & sLine & vbLf + End If + Loop + oFile.Close + + sNewContent = sNewContent & "-javaagent:" & sJarFile & "=jetbrains" + Set oFile = oFS.OpenTextFile(file, 2, 0) + oFile.Write sNewContent + oFile.Close +End Sub + +MsgBox "Done." diff --git a/script/install-current-user.vbs b/script/install-current-user.vbs new file mode 100644 index 0000000..c991525 --- /dev/null +++ b/script/install-current-user.vbs @@ -0,0 +1,54 @@ +Set oShell = CreateObject("WScript.Shell") +Set oEnv = oShell.Environment("USER") +Set oFS = CreateObject("Scripting.FileSystemObject") + +Dim sEnvKey, sEnvVal, aJBProducts +aJBProducts = Array("idea", "clion", "phpstorm", "goland", "pycharm", "webstorm", "webide", "rider", "datagrip", "rubymine", "appcode", "dataspell", "gateway", "jetbrains_client", "jetbrainsclient", "studio", "devecostudio") + +Set re = New RegExp +re.Global = True +re.IgnoreCase = True +re.Pattern = "^\-javaagent:.*[\/\\]jetbra\-agent\.jar.*" + +Dim sBasePath, sJarFile +sBasePath = oFS.GetParentFolderName(oShell.CurrentDirectory) +sJarFile = sBasePath & "\jetbra-agent.jar" + +If Not oFS.FileExists(sJarFile) Then + MsgBox "jetbra-agent.jar not found", vbOKOnly Or vbCritical + WScript.Quit -1 +End If + +MsgBox "It may take a few seconds to execute this script." & vbCrLf & vbCrLf & "Click 'OK' button and wait for the prompt of 'Done.' to pop up!" + +Dim sVmOptionsFile +For Each sPrd in aJBProducts + sEnvKey = UCase(sPrd) & "_VM_OPTIONS" + sVmOptionsFile = sBasePath & "\vmoptions\" & sPrd & ".vmoptions" + If oFS.FileExists(sVmOptionsFile) Then + ProcessVmOptions sVmOptionsFile + oEnv(sEnvKey) = sVmOptionsFile + End If +Next + +Sub ProcessVmOptions(ByVal file) + Dim sLine, sNewContent, bMatch + Set oFile = oFS.OpenTextFile(file, 1, 0) + + sNewContent = "" + Do Until oFile.AtEndOfStream + sLine = oFile.ReadLine + bMatch = re.Test(sLine) + If Not bMatch Then + sNewContent = sNewContent & sLine & vbLf + End If + Loop + oFile.Close + + sNewContent = sNewContent & "-javaagent:" & sJarFile & "=jetbrains" + Set oFile = oFS.OpenTextFile(file, 2, 0) + oFile.Write sNewContent + oFile.Close +End Sub + +MsgBox "Done." diff --git a/script/install.sh b/script/install.sh new file mode 100755 index 0000000..b158c60 --- /dev/null +++ b/script/install.sh @@ -0,0 +1,91 @@ +#!/bin/sh + +set -e + +OS_NAME=$(uname -s) +JB_PRODUCTS="idea clion phpstorm goland pycharm webstorm webide rider datagrip rubymine appcode dataspell gateway jetbrains_client jetbrainsclient studio devecostudio" + +BASE_PATH=$(dirname $( + cd $(dirname "$0") + pwd +)) + +JAR_FILE_PATH="${BASE_PATH}/jetbra-agent.jar" + +if [ ! -f "${JAR_FILE_PATH}" ]; then + echo 'jetbra-agent.jar not found' + exit -1 +fi + +KDE_ENV_DIR="${HOME}/.config/plasma-workspace/env" +LAUNCH_AGENTS_DIR="${HOME}/Library/LaunchAgents" + +PROFILE_PATH="${HOME}/.profile" +ZSH_PROFILE_PATH="${HOME}/.zshrc" +PLIST_PATH="${LAUNCH_AGENTS_DIR}/jetbrains.vmoptions.plist" + +if [ $OS_NAME = "Darwin" ]; then + BASH_PROFILE_PATH="${HOME}/.bash_profile" + + mkdir -p "${LAUNCH_AGENTS_DIR}" + echo 'Labeljetbrains.vmoptionsProgramArgumentssh-c' >"${PLIST_PATH}" +else + BASH_PROFILE_PATH="${HOME}/.bashrc" + mkdir -p "${KDE_ENV_DIR}" +fi + +touch "${PROFILE_PATH}" +touch "${BASH_PROFILE_PATH}" +touch "${ZSH_PROFILE_PATH}" + +MY_VMOPTIONS_SHELL_NAME="jetbrains.vmoptions.sh" +MY_VMOPTIONS_SHELL_FILE="${HOME}/.${MY_VMOPTIONS_SHELL_NAME}" +echo '#!/bin/sh' >"${MY_VMOPTIONS_SHELL_FILE}" + +EXEC_LINE='___MY_VMOPTIONS_SHELL_FILE="${HOME}/.jetbrains.vmoptions.sh"; if [ -f "${___MY_VMOPTIONS_SHELL_FILE}" ]; then . "${___MY_VMOPTIONS_SHELL_FILE}"; fi' + +for PRD in $JB_PRODUCTS; do + VM_FILE_PATH="${BASE_PATH}/vmoptions/${PRD}.vmoptions" + if [ ! -f "${VM_FILE_PATH}" ]; then + continue + fi + + if [ $OS_NAME = "Darwin" ]; then + sed -i '' '/^\-javaagent:.*[\/\\]jetbra\-agent\.jar.*/d' "${VM_FILE_PATH}" + else + sed -i '/^\-javaagent:.*[\/\\]jetbra\-agent\.jar.*/d' "${VM_FILE_PATH}" + fi + + echo "-javaagent:${JAR_FILE_PATH}=jetbrains" >>"${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}" + + if [ $OS_NAME = "Darwin" ]; then + launchctl setenv "${ENV_NAME}" "${VM_FILE_PATH}" + echo "launchctl setenv \"${ENV_NAME}\" \"${VM_FILE_PATH}\"" >>"${PLIST_PATH}" + fi +done + +if [ $OS_NAME = "Darwin" ]; then + sed -i '' '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${PROFILE_PATH}" >/dev/null 2>&1 + sed -i '' '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${BASH_PROFILE_PATH}" >/dev/null 2>&1 + sed -i '' '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${ZSH_PROFILE_PATH}" >/dev/null 2>&1 + + echo 'RunAtLoad' >>"${PLIST_PATH}" +else + sed -i '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${PROFILE_PATH}" >/dev/null 2>&1 + sed -i '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${BASH_PROFILE_PATH}" >/dev/null 2>&1 + sed -i '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${ZSH_PROFILE_PATH}" >/dev/null 2>&1 +fi + +echo "${EXEC_LINE}" >>"${PROFILE_PATH}" +echo "${EXEC_LINE}" >>"${BASH_PROFILE_PATH}" +echo "${EXEC_LINE}" >>"${ZSH_PROFILE_PATH}" + +if [ $OS_NAME = "Darwin" ]; then + echo 'done. the "kill Dock" command can fix the crash issue.' +else + ln -sf "${MY_VMOPTIONS_SHELL_FILE}" "${KDE_ENV_DIR}/${MY_VMOPTIONS_SHELL_NAME}" + echo "done. you'd better log off first!" +fi diff --git a/script/uninstall-all-users.vbs b/script/uninstall-all-users.vbs new file mode 100644 index 0000000..e1be5e0 --- /dev/null +++ b/script/uninstall-all-users.vbs @@ -0,0 +1,28 @@ +If Not WScript.Arguments.Named.Exists("elevate") Then + CreateObject("Shell.Application").ShellExecute WScript.FullName, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 10 + WScript.Quit +End If + +MsgBox "It may take a few seconds to execute this script." & vbCrLf & vbCrLf & "Click 'OK' button and wait for the prompt of 'Done.' to pop up!" + +Sub RemoveEnv(env) + On Error Resume Next + + Dim sEnvKey, sEnvVal, aJBProducts + aJBProducts = Array("idea", "clion", "phpstorm", "goland", "pycharm", "webstorm", "webide", "rider", "datagrip", "rubymine", "appcode", "dataspell", "gateway", "jetbrains_client", "jetbrainsclient", "studio", "devecostudio") + + For Each sPrd in aJBProducts + sEnvKey = UCase(sPrd) & "_VM_OPTIONS" + sEnvVal = oShell.ExpandEnvironmentStrings("%" & sEnvKey & "%") + If sEnvVal <> ("%" & sEnvKey & "%") Then + env.Remove(sEnvKey) + End If + Next +End Sub + +Set oShell = CreateObject("WScript.Shell") + +RemoveEnv oShell.Environment("USER") +RemoveEnv oShell.Environment("SYSTEM") + +MsgBox "Done." diff --git a/script/uninstall-current-user.vbs b/script/uninstall-current-user.vbs new file mode 100644 index 0000000..34be3f1 --- /dev/null +++ b/script/uninstall-current-user.vbs @@ -0,0 +1,17 @@ +Set oShell = CreateObject("WScript.Shell") +Set oEnv = oShell.Environment("USER") + +Dim sEnvKey, sEnvVal, aJBProducts +aJBProducts = Array("idea", "clion", "phpstorm", "goland", "pycharm", "webstorm", "webide", "rider", "datagrip", "rubymine", "appcode", "dataspell", "gateway", "jetbrains_client", "jetbrainsclient", "studio", "devecostudio") + +MsgBox "It may take a few seconds to execute this script." & vbCrLf & vbCrLf & "Click 'OK' button and wait for the prompt of 'Done.' to pop up!" + +For Each sPrd in aJBProducts + sEnvKey = UCase(sPrd) & "_VM_OPTIONS" + sEnvVal = oShell.ExpandEnvironmentStrings("%" & sEnvKey & "%") + If sEnvVal <> ("%" & sEnvKey & "%") Then + oEnv.Remove(sEnvKey) + End If +Next + +MsgBox "Done." diff --git a/script/uninstall.sh b/script/uninstall.sh new file mode 100755 index 0000000..4de2fce --- /dev/null +++ b/script/uninstall.sh @@ -0,0 +1,50 @@ +#!/bin/sh + +set -e + +OS_NAME=$(uname -s) +JB_PRODUCTS="idea clion phpstorm goland pycharm webstorm webide rider datagrip rubymine appcode dataspell gateway jetbrains_client jetbrainsclient studio devecostudio" + +KDE_ENV_DIR="${HOME}/.config/plasma-workspace/env" + +PROFILE_PATH="${HOME}/.profile" +ZSH_PROFILE_PATH="${HOME}/.zshrc" +PLIST_PATH="${HOME}/Library/LaunchAgents/jetbrains.vmoptions.plist" + +if [ $OS_NAME = "Darwin" ]; then + BASH_PROFILE_PATH="${HOME}/.bash_profile" +else + BASH_PROFILE_PATH="${HOME}/.bashrc" +fi + +touch "${PROFILE_PATH}" +touch "${BASH_PROFILE_PATH}" +touch "${ZSH_PROFILE_PATH}" + +MY_VMOPTIONS_SHELL_NAME="jetbrains.vmoptions.sh" +MY_VMOPTIONS_SHELL_FILE="${HOME}/.${MY_VMOPTIONS_SHELL_NAME}" + +rm -rf "${MY_VMOPTIONS_SHELL_FILE}" + +if [ $OS_NAME = "Darwin" ]; then + for PRD in $JB_PRODUCTS; do + ENV_NAME=$(echo $PRD | tr '[a-z]' '[A-Z]')"_VM_OPTIONS" + + launchctl unsetenv "${ENV_NAME}" + done + + rm -rf "${PLIST_PATH}" + + sed -i '' '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${PROFILE_PATH}" >/dev/null 2>&1 + sed -i '' '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${BASH_PROFILE_PATH}" >/dev/null 2>&1 + sed -i '' '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${ZSH_PROFILE_PATH}" >/dev/null 2>&1 + + echo 'done.' +else + sed -i '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${PROFILE_PATH}" >/dev/null 2>&1 + sed -i '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${BASH_PROFILE_PATH}" >/dev/null 2>&1 + sed -i '/___MY_VMOPTIONS_SHELL_FILE="${HOME}\/\.jetbrains\.vmoptions\.sh"; if /d' "${ZSH_PROFILE_PATH}" >/dev/null 2>&1 + + rm -rf "${KDE_ENV_DIR}/${MY_VMOPTIONS_SHELL_NAME}" + echo "done. you'd better log off first!" +fi