[优化] 添加 OpenRestyResolvers 配置支持自定义 DNS 解析器

This commit is contained in:
ryan
2026-05-26 09:10:08 +08:00
parent 83a426d3d6
commit bddf641bf1
8 changed files with 41 additions and 9 deletions
@@ -213,6 +213,7 @@ func (e *DockerExecutor) runContainer(ctx context.Context) error {
runArgs := []string{
"run", "-d",
"--name", e.ContainerName,
"--restart", "always",
"-p", "80:80",
"-p", "443:443",
"-p", fmt.Sprintf("127.0.0.1:%d:%d", e.OpenrestyObservabilityPort, e.OpenrestyObservabilityPort),
@@ -399,6 +399,7 @@ func TestDockerExecutorRunContainerMountsManagedFiles(t *testing.T) {
expectedArgs := []string{
"run", "-d",
"--name", "openflare-openresty",
"--restart", "always",
"-p", "80:80",
"-p", "443:443",
"-p", "127.0.0.1:18081:18081",
+2 -1
View File
@@ -71,6 +71,7 @@ var OpenRestyClientBodyTimeout = 15
var OpenRestyClientMaxBodySize = "64m"
var OpenRestyLargeClientHeaderBuffers = "4 16k"
var OpenRestySendTimeout = 30
var OpenRestyResolvers = ""
var OpenRestyProxyConnectTimeout = 3
var OpenRestyProxySendTimeout = 60
var OpenRestyProxyReadTimeout = 60
@@ -127,7 +128,7 @@ http {
gzip {{OpenRestyGzip}};
gzip_min_length {{OpenRestyGzipMinLength}};
gzip_comp_level {{OpenRestyGzipCompLevel}};
{{OpenRestyCacheBlock}} include {{OpenRestyRouteConfigInclude}};
{{OpenRestyResolverDirective}}{{OpenRestyCacheBlock}} include {{OpenRestyRouteConfigInclude}};
}
`
+7 -1
View File
@@ -133,7 +133,13 @@ func validateOpenRestyOption(key string, value string) error {
return fmt.Errorf("%s 仅支持 epoll、kqueue、poll、select、rtsig、/dev/poll、eventport 或留空", key)
}
case "OpenRestyResolvers":
return fmt.Errorf("%s 已废弃,不再支持配置 resolver", key)
if trimmed == "" {
return nil
}
if !regexp.MustCompile(`^[a-zA-Z0-9.:\-\s]+$`).MatchString(trimmed) {
return fmt.Errorf("%s 包含非法字符,请填入有效的 IP 地址或域名,以空格分隔", key)
}
return nil
case "OpenRestyEventsMultiAcceptEnabled",
"OpenRestyWebsocketEnabled",
"OpenRestyProxyRequestBufferingEnabled",
+2 -1
View File
@@ -14,7 +14,8 @@ func TestValidateOpenRestyOption(t *testing.T) {
{name: "worker processes invalid", key: "OpenRestyWorkerProcesses", value: "0", wantErr: true},
{name: "events use empty", key: "OpenRestyEventsUse", value: ""},
{name: "events use invalid", key: "OpenRestyEventsUse", value: "io_uring", wantErr: true},
{name: "resolvers deprecated", key: "OpenRestyResolvers", value: "1.1.1.1", wantErr: true},
{name: "resolvers valid", key: "OpenRestyResolvers", value: "1.1.1.1 8.8.8.8"},
{name: "resolvers invalid", key: "OpenRestyResolvers", value: "1.1.1.1; 8.8.8.8", wantErr: true},
{name: "proxy buffers valid", key: "OpenRestyProxyBuffers", value: "16 16k"},
{name: "proxy buffers invalid", key: "OpenRestyProxyBuffers", value: "16x16k", wantErr: true},
{name: "cache max size valid", key: "OpenRestyCacheMaxSize", value: "2g"},
-5
View File
@@ -153,11 +153,6 @@ func updateOptionMap(key string, value string) {
common.OptionMap = make(map[string]string)
}
common.OptionMap[key] = value
if key == "OpenRestyResolvers" {
delete(common.OptionMap, key)
common.OptionMapRWMutex.Unlock()
return
}
if strings.HasSuffix(key, "Permission") {
intValue, _ := strconv.Atoi(value)
switch key {
+4 -1
View File
@@ -133,6 +133,7 @@ type openRestyConfigSnapshot struct {
GzipEnabled bool `json:"gzip_enabled"`
GzipMinLength int `json:"gzip_min_length"`
GzipCompLevel int `json:"gzip_comp_level"`
Resolvers string `json:"resolvers,omitempty"`
CacheEnabled bool `json:"cache_enabled"`
CachePath string `json:"cache_path,omitempty"`
CacheLevels string `json:"cache_levels"`
@@ -766,6 +767,7 @@ func buildOpenRestyConfigSnapshot() openRestyConfigSnapshot {
GzipEnabled: common.OpenRestyGzipEnabled,
GzipMinLength: common.OpenRestyGzipMinLength,
GzipCompLevel: common.OpenRestyGzipCompLevel,
Resolvers: common.OpenRestyResolvers,
CacheEnabled: common.OpenRestyCacheEnabled,
CachePath: common.OpenRestyCachePath,
CacheLevels: common.OpenRestyCacheLevels,
@@ -827,6 +829,7 @@ func diffOpenRestyOptionDetails(left openRestyConfigSnapshot, right openRestyCon
appendIfChanged("OpenRestyGzipEnabled", fmt.Sprintf("%t", left.GzipEnabled), fmt.Sprintf("%t", right.GzipEnabled))
appendIfChanged("OpenRestyGzipMinLength", fmt.Sprintf("%d", left.GzipMinLength), fmt.Sprintf("%d", right.GzipMinLength))
appendIfChanged("OpenRestyGzipCompLevel", fmt.Sprintf("%d", left.GzipCompLevel), fmt.Sprintf("%d", right.GzipCompLevel))
appendIfChanged("OpenRestyResolvers", left.Resolvers, right.Resolvers)
appendIfChanged("OpenRestyCacheEnabled", fmt.Sprintf("%t", left.CacheEnabled), fmt.Sprintf("%t", right.CacheEnabled))
appendIfChanged("OpenRestyCachePath", left.CachePath, right.CachePath)
appendIfChanged("OpenRestyCacheLevels", left.CacheLevels, right.CacheLevels)
@@ -1063,7 +1066,7 @@ func renderMainConfigTemplate(templateText string, cfg openRestyConfigSnapshot)
"{{OpenRestyGzip}}", onOff(cfg.GzipEnabled),
"{{OpenRestyGzipMinLength}}", fmt.Sprintf("%d", cfg.GzipMinLength),
"{{OpenRestyGzipCompLevel}}", fmt.Sprintf("%d", cfg.GzipCompLevel),
"{{OpenRestyResolverDirective}}", "",
"{{OpenRestyResolverDirective}}", renderTemplateDirective(cfg.Resolvers != "", fmt.Sprintf("resolver %s;", cfg.Resolvers)),
"{{OpenRestyCacheBlock}}", renderOpenRestyCacheTemplateBlock(cfg),
"{{OpenRestyRouteConfigInclude}}", nginxRouteConfigPlaceholder,
)
@@ -65,6 +65,7 @@ const defaultPerformanceFields = {
OpenRestyCacheLockTimeout: '5s',
OpenRestyCacheUseStale:
'error timeout updating http_500 http_502 http_503 http_504',
OpenRestyResolvers: '',
};
const performanceFieldTooltips: Record<string, string> = {
@@ -114,6 +115,8 @@ const performanceFieldTooltips: Record<string, string> = {
proxy_cache_lock_timeout: '等待缓存锁的最长时间,例如 5s。',
proxy_cache_use_stale:
'上游异常时允许返回旧缓存的条件列表,例如 error、timeout、http_500。',
resolvers:
'自定义 DNS 解析器,留空表示不配置。若配置,当上游源站需要动态解析且 DNS 服务器失效时,能防止容器异常挂掉。例如:1.1.1.1 8.8.8.8',
};
type PerformanceTab = 'settings' | 'editor';
@@ -256,6 +259,7 @@ export function PerformancePage() {
OpenRestyCacheUseStale:
optionMap.OpenRestyCacheUseStale ??
'error timeout updating http_500 http_502 http_503 http_504',
OpenRestyResolvers: optionMap.OpenRestyResolvers ?? '',
});
setTemplateContent(optionMap.OpenRestyMainConfigTemplate ?? '');
}, [optionsQuery.data]);
@@ -333,6 +337,10 @@ export function PerformancePage() {
'OpenRestyWorkerProcesses',
performanceFields.OpenRestyWorkerProcesses.trim(),
],
[
'OpenRestyResolvers',
performanceFields.OpenRestyResolvers.trim(),
],
[
'OpenRestyWorkerConnections',
performanceFields.OpenRestyWorkerConnections.trim(),
@@ -689,6 +697,22 @@ export function PerformancePage() {
}
>
<div className="grid gap-5 md:grid-cols-2 xl:grid-cols-3">
<ResourceField
label="自定义 DNS (resolver)"
hint="例如:1.1.1.1 8.8.8.8"
tooltip={performanceFieldTooltips.resolvers}
>
<ResourceInput
value={performanceFields.OpenRestyResolvers}
onChange={(event) =>
setPerformanceFields((previous) => ({
...previous,
OpenRestyResolvers: event.target.value,
}))
}
placeholder="留空表示不配置"
/>
</ResourceField>
<ResourceField
label="worker_processes"
tooltip={performanceFieldTooltips.worker_processes}