Skip to content

Commit a24890c

Browse files
committed
feat: support probe paramName optional
1 parent e5aa232 commit a24890c

File tree

8 files changed

+239
-372
lines changed

8 files changed

+239
-372
lines changed

generator/src/main/java/com/reajason/javaweb/probe/config/ResponseBodyConfig.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.reajason.javaweb.probe.config;
22

3+
import com.reajason.javaweb.utils.CommonUtil;
4+
import lombok.Builder;
35
import lombok.Getter;
46
import lombok.ToString;
57
import lombok.experimental.SuperBuilder;
8+
import org.apache.commons.lang3.StringUtils;
69

710
/**
811
* @author ReaJason
@@ -17,7 +20,8 @@ public class ResponseBodyConfig extends ProbeContentConfig {
1720
/**
1821
* 获取参数的请求头或请求参数名称
1922
*/
20-
private String reqParamName;
23+
@Builder.Default
24+
private String reqParamName = CommonUtil.getRandomString(8);
2125

2226
/**
2327
* 内置执行类加载的字节码
@@ -28,4 +32,15 @@ public class ResponseBodyConfig extends ProbeContentConfig {
2832
* 命令执行模板,例如 sh -c "{command}" 2>&1,使用 {command} 作为占位符
2933
*/
3034
private String commandTemplate;
35+
36+
public static abstract class ResponseBodyConfigBuilder<C extends ResponseBodyConfig, B extends ResponseBodyConfig.ResponseBodyConfigBuilder<C, B>>
37+
extends ProbeContentConfig.ProbeContentConfigBuilder<C, B> {
38+
public B reqParamName(String reqParamName) {
39+
if (StringUtils.isNotBlank(reqParamName)) {
40+
reqParamName$value = reqParamName;
41+
reqParamName$set = true;
42+
}
43+
return self();
44+
}
45+
}
3146
}

web/app/components/probeshell/basic-info.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ import { useTranslation } from "react-i18next";
33
import { CopyableField } from "@/components/copyable-field";
44
import { FeedbackAlert } from "@/components/memshell/results/feedback-alert";
55
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
6-
import type { ProbeShellResult } from "@/types/probeshell";
6+
import type { ProbeShellResult, ResponseBodyConfig } from "@/types/probeshell";
77

88
export function BasicInfo({
99
generateResult,
1010
}: Readonly<{ generateResult?: ProbeShellResult }>) {
1111
const { t } = useTranslation();
12+
console.log(generateResult);
13+
const isBodyContent =
14+
generateResult?.probeConfig.probeMethod === "ResponseBody";
15+
const isBodyCommand =
16+
isBodyContent && generateResult?.probeConfig.probeContent === "Command";
1217
return (
1318
<Card>
1419
<CardHeader>
@@ -22,6 +27,34 @@ export function BasicInfo({
2227
</CardHeader>
2328
<CardContent>
2429
<div className="grid grid-cols-1 gap-2">
30+
{isBodyContent && (
31+
<CopyableField
32+
label={t("common:paramName")}
33+
value={
34+
(generateResult?.probeContentConfig as ResponseBodyConfig)
35+
.reqParamName
36+
}
37+
text={
38+
(generateResult?.probeContentConfig as ResponseBodyConfig)
39+
.reqParamName
40+
}
41+
/>
42+
)}
43+
{isBodyCommand &&
44+
(generateResult?.probeContentConfig as ResponseBodyConfig)
45+
.commandTemplate && (
46+
<CopyableField
47+
label={t("common:commandTemplate")}
48+
value={
49+
(generateResult?.probeContentConfig as ResponseBodyConfig)
50+
.commandTemplate
51+
}
52+
text={
53+
(generateResult?.probeContentConfig as ResponseBodyConfig)
54+
.commandTemplate
55+
}
56+
/>
57+
)}
2558
<CopyableField
2659
label={t("probeshell:shellClassName")}
2760
value={generateResult?.shellClassName}

web/app/components/probeshell/main-config-card.tsx

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ const MIDDLEWARE_OPTIONS = [
5151
] as const;
5252

5353
const PROBE_METHOD_OPTIONS = [
54-
{ value: "Sleep", label: "Sleep" },
55-
{ value: "DNSLog", label: "DNSLog" },
5654
{ value: "ResponseBody", label: "ResponseBody" },
55+
{ value: "DNSLog", label: "DNSLog" },
56+
{ value: "Sleep", label: "Sleep" },
5757
] as const;
5858

5959
const DEFAULT_FORM_VALUES = {
60-
reqParamName: "payload",
6160
sleepServer: "Tomcat",
6261
seconds: 5,
6362
} as const;
@@ -143,7 +142,9 @@ export default function MainConfigCard({ form, servers }: MainConfigCardProps) {
143142
name="reqParamName"
144143
render={({ field }) => (
145144
<FormFieldItem>
146-
<FormFieldLabel>{t("common:paramName")}</FormFieldLabel>
145+
<FormFieldLabel>
146+
{t("common:paramName")} {t("common:optional")}
147+
</FormFieldLabel>
147148
<FormControl>
148149
<Input placeholder={t("placeholders.input")} {...field} />
149150
</FormControl>
@@ -158,45 +159,28 @@ export default function MainConfigCard({ form, servers }: MainConfigCardProps) {
158159

159160
const CommandTemplateField = useMemo(
160161
() => (
161-
<>
162-
<div className="space-y-2 pt-4 border-t mt-4">
163-
<FormField
164-
control={form.control}
165-
name="reqParamName"
166-
render={({ field }) => (
167-
<FormFieldItem>
168-
<FormFieldLabel>{t("common:paramName")}</FormFieldLabel>
169-
<FormControl>
170-
<Input placeholder={t("placeholders.input")} {...field} />
171-
</FormControl>
172-
<FormMessage />
173-
</FormFieldItem>
174-
)}
175-
/>
176-
</div>
177-
<div className="space-y-2">
178-
<FormField
179-
control={form.control}
180-
name="commandTemplate"
181-
render={({ field }) => (
182-
<FormFieldItem>
183-
<FormFieldLabel>
184-
{t("common:commandTemplate")} {t("common:optional")}
185-
</FormFieldLabel>
186-
<FormControl>
187-
<Input
188-
{...field}
189-
placeholder={t("common:commandTemplate.placeholder")}
190-
/>
191-
</FormControl>
192-
<p className="text-xs text-muted-foreground mt-1">
193-
{t("common:commandTemplate.description")}
194-
</p>
195-
</FormFieldItem>
196-
)}
197-
/>
198-
</div>
199-
</>
162+
<div className="space-y-2">
163+
<FormField
164+
control={form.control}
165+
name="commandTemplate"
166+
render={({ field }) => (
167+
<FormFieldItem>
168+
<FormFieldLabel>
169+
{t("common:commandTemplate")} {t("common:optional")}
170+
</FormFieldLabel>
171+
<FormControl>
172+
<Input
173+
{...field}
174+
placeholder={t("common:commandTemplate.placeholder")}
175+
/>
176+
</FormControl>
177+
<p className="text-xs text-muted-foreground mt-1">
178+
{t("common:commandTemplate.description")}
179+
</p>
180+
</FormFieldItem>
181+
)}
182+
/>
183+
</div>
200184
),
201185
[form.control, t],
202186
);
@@ -251,35 +235,6 @@ export default function MainConfigCard({ form, servers }: MainConfigCardProps) {
251235
[form.control, t],
252236
);
253237

254-
const renderDynamicFields = useCallback(() => {
255-
const isBodyMethod = watchedProbeMethod === "ResponseBody";
256-
const needParam =
257-
watchedProbeContent === "Command" ||
258-
watchedProbeContent === "Bytecode" ||
259-
watchedProbeContent === "ScriptEngine";
260-
const isSleepMethod = watchedProbeMethod === "Sleep";
261-
const isServerContent = watchedProbeContent === "Server";
262-
263-
if (isBodyMethod && needParam) {
264-
if (watchedProbeContent === "Command") {
265-
return CommandTemplateField;
266-
}
267-
return RequestParamField;
268-
}
269-
270-
if (isSleepMethod && isServerContent) {
271-
return SleepFields;
272-
}
273-
274-
return null;
275-
}, [
276-
watchedProbeMethod,
277-
watchedProbeContent,
278-
RequestParamField,
279-
SleepFields,
280-
CommandTemplateField,
281-
]);
282-
283238
const DNSLogSection = useMemo(
284239
() => (
285240
<FormField
@@ -421,6 +376,15 @@ export default function MainConfigCard({ form, servers }: MainConfigCardProps) {
421376
[form.control, t],
422377
);
423378

379+
const isBodyMethod = watchedProbeMethod === "ResponseBody";
380+
const isCommandBody = watchedProbeContent === "Command";
381+
const needParam =
382+
isCommandBody ||
383+
watchedProbeContent === "Bytecode" ||
384+
watchedProbeContent === "ScriptEngine";
385+
const isSleepMethod = watchedProbeMethod === "Sleep";
386+
const isServerContent = watchedProbeContent === "Server";
387+
424388
return (
425389
<FormProvider {...form}>
426390
<Card>
@@ -463,7 +427,9 @@ export default function MainConfigCard({ form, servers }: MainConfigCardProps) {
463427
{watchedProbeMethod === "DNSLog" && DNSLogSection}
464428
{watchedProbeMethod && ContentOptionsSelect}
465429
{SwitchGroup}
466-
{renderDynamicFields()}
430+
{isBodyMethod && needParam && RequestParamField}
431+
{isBodyMethod && isCommandBody && CommandTemplateField}
432+
{isSleepMethod && isServerContent && SleepFields}
467433
<Separator />
468434
<FormField
469435
control={form.control}

web/app/routes/probeshell.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ export default function ProbeShellGenerator() {
5151
const form = useForm<ProbeShellFormSchema>({
5252
resolver: useYupValidationProbeResolver(probeShellFormSchema, t),
5353
defaultValues: {
54-
probeMethod: "Sleep",
55-
probeContent: "Server",
54+
probeMethod: "ResponseBody",
55+
probeContent: "Command",
5656
host: "",
5757
server: "Tomcat",
58-
reqParamName: "payload",
58+
reqParamName: "",
5959
seconds: 5,
6060
sleepServer: "Tomcat",
6161
shrink: true,

web/app/types/probeshell.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface SleepConfig {
4141
export interface ResponseBodyConfig {
4242
server: string;
4343
reqParamName: string;
44+
commandTemplate: string;
4445
}
4546

4647
export interface PayloadFormValues {

web/app/types/schema.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ export const useYupValidationProbeResolver = (
189189
})) as ProbeShellFormSchema;
190190

191191
const host: keyof ProbeShellFormSchema = "host";
192-
const reqParamName: keyof ProbeShellFormSchema = "reqParamName";
193192
const errors = {} as any;
194193

195194
if (values.probeMethod === "DNSLog" && !values.host) {
@@ -198,13 +197,6 @@ export const useYupValidationProbeResolver = (
198197
message: t("probeshell:tips.dnslog.host.required"),
199198
};
200199
}
201-
202-
if (values.probeMethod === "ResponseBody" && !values.reqParamName) {
203-
errors[reqParamName] = {
204-
type: "custom",
205-
message: t("probeshell:tips.response.reqParamName.required"),
206-
};
207-
}
208200
return {
209201
values,
210202
errors,

0 commit comments

Comments
 (0)