Skip to content

Commit bd20c59

Browse files
authored
feature: implement dynamic parameter resolution and add utility functions for random data generation (#9)
# Description - implement dynamic parameter resolution and add utility functions for random data generation Fixes #6 ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] This change requires a documentation update ## Checklist - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation
2 parents c10b045 + 0101eea commit bd20c59

15 files changed

Lines changed: 2134 additions & 114 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ go.work.sum
3333
.vscode/
3434

3535
# Generated files
36-
data/
36+
/data/

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PROJECT_NAME=ponghub
33
BINARY=bin/$(PROJECT_NAME)
44
SRC=cmd/$(PROJECT_NAME)/*.go
55

6-
.PHONY: all build run clean
6+
.PHONY: all build run test clean
77

88
all: build
99

@@ -13,5 +13,8 @@ build:
1313
run: build
1414
$(BINARY)
1515

16+
test:
17+
go test ./...
18+
1619
clean:
1720
del $(BINARY)

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,81 @@ Ensure that the environment variable is set in your GitHub repository settings u
163163
- `{{hash_short}}` - Short hash value (6-digit hexadecimal)
164164
- `{{hash_md5_like}}` - MD5-style long hash value (32-digit hexadecimal)
165165

166+
#### 🌐 Network and System Information Parameters
167+
168+
- `{{local_ip}}` - Gets the local IP address of the system
169+
- `{{hostname}}` - Gets the hostname of the system
170+
- `{{user_agent}}` - Generates a random User-Agent string for HTTP requests
171+
- `{{http_method}}` - Generates a random HTTP method (GET, POST, PUT, DELETE, etc.)
172+
173+
#### 🔐 Encoding and Decoding Parameters
174+
175+
- `{{base64(content)}}` - Base64 encodes the provided content
176+
- Example: `{{base64(hello world)}}` - Encodes "hello world" to Base64
177+
- `{{url_encode(content)}}` - URL encodes the provided content
178+
- Example: `{{url_encode(hello world)}}` - URL encodes "hello world"
179+
- `{{json_escape(content)}}` - JSON escapes the provided content
180+
- Example: `{{json_escape("test")}}` - Escapes quotes and special characters for JSON
181+
182+
#### 🔢 Mathematical Operation Parameters
183+
184+
- `{{add(a,b)}}` - Adds two numbers
185+
- Example: `{{add(10,5)}}` - Returns 15
186+
- `{{sub(a,b)}}` - Subtracts two numbers
187+
- Example: `{{sub(10,5)}}` - Returns 5
188+
- `{{mul(a,b)}}` - Multiplies two numbers
189+
- Example: `{{mul(10,5)}}` - Returns 50
190+
- `{{div(a,b)}}` - Divides two numbers
191+
- Example: `{{div(10,5)}}` - Returns 2
192+
193+
#### 📝 Text Processing Parameters
194+
195+
- `{{upper(text)}}` - Converts text to uppercase
196+
- Example: `{{upper(hello)}}` - Returns "HELLO"
197+
- `{{lower(text)}}` - Converts text to lowercase
198+
- Example: `{{lower(HELLO)}}` - Returns "hello"
199+
- `{{reverse(text)}}` - Reverses the text
200+
- Example: `{{reverse(hello)}}` - Returns "olleh"
201+
- `{{substr(text,start,length)}}` - Extracts substring from text
202+
- Example: `{{substr(hello world,0,5)}}` - Returns "hello"
203+
204+
#### 🎨 Color Generation Parameters
205+
206+
- `{{color_hex}}` - Generates a random hexadecimal color code
207+
- Example: `#FF5733`
208+
- `{{color_rgb}}` - Generates a random RGB color value
209+
- Example: `rgb(255, 87, 51)`
210+
- `{{color_hsl}}` - Generates a random HSL color value
211+
- Example: `hsl(120, 50%, 75%)`
212+
213+
#### 📁 File and MIME Type Parameters
214+
215+
- `{{mime_type}}` - Generates a random MIME type
216+
- Example: `application/json`, `image/png`, `text/html`
217+
- `{{file_ext}}` - Generates a random file extension
218+
- Example: `.jpg`, `.pdf`, `.txt`
219+
220+
#### 👤 Fake Data Generation Parameters
221+
222+
- `{{fake_email}}` - Generates a realistic fake email address
223+
- Example: `john.smith@example.com`
224+
- `{{fake_phone}}` - Generates a fake phone number
225+
- Example: `+1-555-0123`
226+
- `{{fake_name}}` - Generates a fake person name
227+
- Example: `John Smith`
228+
- `{{fake_domain}}` - Generates a fake domain name
229+
- Example: `example-site.com`
230+
231+
#### ⏰ Time Calculation Parameters
232+
233+
- `{{time_add(duration)}}` - Adds duration to current time
234+
- Example: `{{time_add(1h)}}` - Adds 1 hour to current time
235+
- Example: `{{time_add(30m)}}` - Adds 30 minutes to current time
236+
- Supported units: s (seconds), m (minutes), h (hours), d (days)
237+
- `{{time_sub(duration)}}` - Subtracts duration from current time
238+
- Example: `{{time_sub(1d)}}` - Subtracts 1 day from current time
239+
- Example: `{{time_sub(2h30m)}}` - Subtracts 2 hours and 30 minutes
240+
166241
</div>
167242
</details>
168243

README_CN.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,81 @@ ponghub 现已支持强大的参数化配置功能,允许在配置文件中使
162162
- `{{hash_short}}` - 短哈希值(6位十六进制)
163163
- `{{hash_md5_like}}` - MD5风格的长哈希值(32位十六进制)
164164

165+
#### 🌐 网络和系统信息参数
166+
167+
- `{{local_ip}}` - 获取系统本地IP地址
168+
- `{{hostname}}` - 获取系统主机名
169+
- `{{user_agent}}` - 生成随机的User-Agent字符串
170+
- `{{http_method}}` - 生成随机的HTTP方法(GET、POST、PUT、DELETE等)
171+
172+
#### 🔐 编码和解码参数
173+
174+
- `{{base64(内容)}}` - 对提供的内容进行Base64编码
175+
- 示例:`{{base64(hello world)}}` - 将"hello world"编码为Base64
176+
- `{{url_encode(内容)}}` - 对提供的内容进行URL编码
177+
- 示例:`{{url_encode(hello world)}}` - 对"hello world"进行URL编码
178+
- `{{json_escape(内容)}}` - 对提供的内容进行JSON转义
179+
- 示例:`{{json_escape("test")}}` - 转义引号和特殊字符以用于JSON
180+
181+
#### 🔢 数学运算参数
182+
183+
- `{{add(a,b)}}` - 两数相加
184+
- 示例:`{{add(10,5)}}` - 返回15
185+
- `{{sub(a,b)}}` - 两数相减
186+
- 示例:`{{sub(10,5)}}` - 返回5
187+
- `{{mul(a,b)}}` - 两数相乘
188+
- 示例:`{{mul(10,5)}}` - 返回50
189+
- `{{div(a,b)}}` - 两数相除
190+
- 示例:`{{div(10,5)}}` - 返回2
191+
192+
#### 📝 文本处理参数
193+
194+
- `{{upper(文本)}}` - 将文本转换为大写
195+
- 示例:`{{upper(hello)}}` - 返回"HELLO"
196+
- `{{lower(文本)}}` - 将文本转换为小写
197+
- 示例:`{{lower(HELLO)}}` - 返回"hello"
198+
- `{{reverse(文本)}}` - 反转文本
199+
- 示例:`{{reverse(hello)}}` - 返回"olleh"
200+
- `{{substr(文本,起始位置,长度)}}` - 从文本中提取子字符串
201+
- 示例:`{{substr(hello world,0,5)}}` - 返回"hello"
202+
203+
#### 🎨 颜色生成参数
204+
205+
- `{{color_hex}}` - 生成随机的十六进制颜色代码
206+
- 示例:`#FF5733`
207+
- `{{color_rgb}}` - 生成随机的RGB颜色值
208+
- 示例:`rgb(255, 87, 51)`
209+
- `{{color_hsl}}` - 生成随机的HSL颜色值
210+
- 示例:`hsl(120, 50%, 75%)`
211+
212+
#### 📁 文件和MIME类型参数
213+
214+
- `{{mime_type}}` - 生成随机的MIME类型
215+
- 示例:`application/json`、`image/png`、`text/html`
216+
- `{{file_ext}}` - 生成随机的文件扩展名
217+
- 示例:`.jpg`、`.pdf`、`.txt`
218+
219+
#### 👤 虚拟数据生成参数
220+
221+
- `{{fake_email}}` - 生成逼真的虚拟邮箱地址
222+
- 示例:`john.smith@example.com`
223+
- `{{fake_phone}}` - 生成虚拟电话号码
224+
- 示例:`+1-555-0123`
225+
- `{{fake_name}}` - 生成虚拟人名
226+
- 示例:`张三`
227+
- `{{fake_domain}}` - 生成虚拟域名
228+
- 示例:`example-site.com`
229+
230+
#### ⏰ 时间计算参数
231+
232+
- `{{time_add(时长)}}` - 在当前时间基础上增加指定时长
233+
- 示例:`{{time_add(1h)}}` - 在当前时间上增加1小时
234+
- 示例:`{{time_add(30m)}}` - 在当前时间上增加30分钟
235+
- 支持的单位:s(秒)、m(分钟)、h(小时)、d(天)
236+
- `{{time_sub(时长)}}` - 在当前时间基础上减去指定时长
237+
- 示例:`{{time_sub(1d)}}` - 在当前时间上减去1天
238+
- 示例:`{{time_sub(2h30m)}}` - 在当前时间上减去2小时30分钟
239+
165240
</div>
166241
</details>
167242

internal/checker/endpoints.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/wcy-dt/ponghub/internal/common"
13+
"github.com/wcy-dt/ponghub/internal/common/params"
1414
"github.com/wcy-dt/ponghub/internal/types/structures/checker"
1515
"github.com/wcy-dt/ponghub/internal/types/structures/configure"
1616
)
@@ -33,7 +33,7 @@ func checkEndpoint(cfg *configure.Endpoint, timeout int, maxRetryTimes int, serv
3333
isCertExpired := false
3434

3535
// Generate display URL for smart showing of template vs resolved URL
36-
resolver := common.NewParameterResolver()
36+
resolver := params.NewParameterResolver()
3737
displayURL, highlightSegments := resolver.HighlightChanges(cfg.OriginalURL)
3838
originalURL := cfg.OriginalURL
3939
if originalURL == "" {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package params
2+
3+
// Character sets for random string generation
4+
const (
5+
DefaultCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
6+
HexCharset = "0123456789abcdef"
7+
)
8+
9+
// TimeFormatReplacements Time format pattern replacements for strftime-like patterns to Go time format
10+
var TimeFormatReplacements = map[string]string{
11+
"%Y": "2006", // 4-digit year
12+
"%y": "06", // 2-digit year
13+
"%m": "01", // month (01-12)
14+
"%d": "02", // day (01-31)
15+
"%H": "15", // hour (00-23)
16+
"%M": "04", // minute (00-59)
17+
"%S": "05", // second (00-59)
18+
"%B": "January", // full month name
19+
"%b": "Jan", // abbreviated month name
20+
"%A": "Monday", // full weekday name
21+
"%a": "Mon", // abbreviated weekday name
22+
"%j": "002", // day of year (001-366)
23+
"%U": "", // week of year (placeholder)
24+
"%W": "", // week of year (placeholder)
25+
"%w": "", // weekday (placeholder)
26+
"%Z": "MST", // timezone name
27+
"%z": "-0700", // timezone offset
28+
"%s": "", // Unix timestamp (placeholder)
29+
}
30+
31+
// HTTPMethods HTTP methods for random HTTP method generation
32+
var HTTPMethods = []string{
33+
"GET",
34+
"POST",
35+
"PUT",
36+
"DELETE",
37+
"PATCH",
38+
"HEAD",
39+
"OPTIONS",
40+
}
41+
42+
// SensitivePatterns Sensitive environment variable patterns for masking
43+
var SensitivePatterns = []string{
44+
"key",
45+
"secret",
46+
"token",
47+
"password",
48+
"pass",
49+
"pwd",
50+
"auth",
51+
"credential",
52+
"private",
53+
"api_key",
54+
"access",
55+
"jwt",
56+
"bearer",
57+
"signature",
58+
"hash",
59+
"salt",
60+
}
61+
62+
// UserAgents User agent strings for random user agent generation
63+
var UserAgents = LoadUserAgents()
64+
65+
// MimeTypes MIME types for random MIME type generation
66+
var MimeTypes = []string{
67+
"image/jpeg",
68+
"image/png",
69+
"image/gif",
70+
"image/webp",
71+
"video/mp4",
72+
"video/x-msvideo",
73+
"video/x-flv",
74+
"audio/mpeg",
75+
"audio/ogg",
76+
"application/pdf",
77+
"application/zip",
78+
"application/x-rar-compressed",
79+
"text/html",
80+
"text/css",
81+
"text/javascript",
82+
"application/json",
83+
"application/xml",
84+
}
85+
86+
// FileExtensions File extensions for random file extension generation
87+
var FileExtensions = []string{
88+
".jpg",
89+
".jpeg",
90+
".png",
91+
".gif",
92+
".webp",
93+
".mp4",
94+
".avi",
95+
".flv",
96+
".mp3",
97+
".ogg",
98+
".pdf",
99+
".zip",
100+
".rar",
101+
".html",
102+
".css",
103+
".js",
104+
".json",
105+
".xml",
106+
}
107+
108+
// FirstNames First names for fake name generation
109+
var FirstNames = LoadFirstNames()
110+
111+
// LastNames Last names for fake name generation
112+
var LastNames = LoadLastNames()
113+
114+
// FakeDomains Domain names for fake domain generation
115+
var FakeDomains = LoadFakeDomains()

0 commit comments

Comments
 (0)