From 9473d7fd4985df4a0d5b3d3de7a44f7474238722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20=C5=BBygie=C5=82o?= Date: Thu, 16 May 2024 12:03:49 +0200 Subject: [PATCH 1/2] Refine RE to allow host in jdwp agent address --- .../sun/enterprise/admin/launcher/GFLauncher.java | 12 +++++++----- .../admin/launcher/GFLauncherExtractPortTest.java | 9 ++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java b/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java index 39b54f47ed5..02d78fb1c7f 100644 --- a/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java +++ b/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java @@ -37,7 +37,7 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2016-2023] [Payara Foundation and/or its affiliates] +// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates] package com.sun.enterprise.admin.launcher; @@ -382,15 +382,17 @@ static boolean isJdwpOption(String option) { return option.startsWith("-Xrunjdwp:") || option.startsWith("-agentlib:jdwp"); } + private static final String DEBUG_ADDRESS_PORT_GROUP = "port"; + private static final Pattern DEBUG_ADDRESS_PATTERN = Pattern.compile(".*address=(?(?[^:]*):)?(?\\d*).*"); + static int extractDebugPort(String option) { - Pattern portRegex = Pattern.compile(".*address=(?\\d*).*"); - Matcher m = portRegex.matcher(option); + Matcher m = DEBUG_ADDRESS_PATTERN.matcher(option); if (!m.matches()) { return -1; } try { - String addressGroup = m.group("port"); - return Integer.parseInt(addressGroup); + String portGroup = m.group(DEBUG_ADDRESS_PORT_GROUP); + return Integer.parseInt(portGroup); } catch (NumberFormatException nfex) { return -1; } diff --git a/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java b/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java index e1775e1cd09..b7fbe67c052 100644 --- a/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java +++ b/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) 2019-2024 Payara Foundation and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -51,6 +51,13 @@ public void shouldExtractPortNumberFromDebugOption() { assertEquals(9876, port); } + @Test + public void shouldExtractPortNumberFromDebugOptionWithHost() { + int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,address=*:9876,suspend=n"); + + assertEquals(9876, port); + } + @Test public void shouldNotFindPortNumberInOption() { int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n"); From 5a0edd96bda05aded10b457b637585ea0a7d2bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20=C5=BBygie=C5=82o?= Date: Sat, 18 May 2024 11:37:23 +0200 Subject: [PATCH 2/2] Allow IPv6 as host in jdwp agent address --- .../enterprise/admin/launcher/GFLauncher.java | 2 +- .../launcher/GFLauncherExtractPortTest.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java b/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java index 02d78fb1c7f..0e03b82fa34 100644 --- a/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java +++ b/nucleus/admin/launcher/src/main/java/com/sun/enterprise/admin/launcher/GFLauncher.java @@ -383,7 +383,7 @@ static boolean isJdwpOption(String option) { } private static final String DEBUG_ADDRESS_PORT_GROUP = "port"; - private static final Pattern DEBUG_ADDRESS_PATTERN = Pattern.compile(".*address=(?(?[^:]*):)?(?\\d*).*"); + private static final Pattern DEBUG_ADDRESS_PATTERN = Pattern.compile(".*address=(?(?.+):)?(?\\d*).*"); static int extractDebugPort(String option) { Matcher m = DEBUG_ADDRESS_PATTERN.matcher(option); diff --git a/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java b/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java index b7fbe67c052..384d4656a79 100644 --- a/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java +++ b/nucleus/admin/launcher/src/test/java/com/sun/enterprise/admin/launcher/GFLauncherExtractPortTest.java @@ -58,6 +58,27 @@ public void shouldExtractPortNumberFromDebugOptionWithHost() { assertEquals(9876, port); } + @Test + public void shouldExtractPortNumberFromDebugOptionWithHostName() { + int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,address=localhost:9876,suspend=n"); + + assertEquals(9876, port); + } + + @Test + public void shouldExtractPortNumberFromDebugOptionWithIPv4Host() { + int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,address=127.0.0.1:9876,suspend=n"); + + assertEquals(9876, port); + } + + @Test + public void shouldExtractPortNumberFromDebugOptionWithIPv6Host() { + int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,address=[fd27:2024:0518::1]:9876,suspend=n"); + + assertEquals(9876, port); + } + @Test public void shouldNotFindPortNumberInOption() { int port = GFLauncher.extractDebugPort("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n");