mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-05 15:26:36 +08:00
[优化] 添加 OpenRestyResolvers 配置支持自定义 DNS 解析器
This commit is contained in:
@@ -213,6 +213,7 @@ func (e *DockerExecutor) runContainer(ctx context.Context) error {
|
|||||||
runArgs := []string{
|
runArgs := []string{
|
||||||
"run", "-d",
|
"run", "-d",
|
||||||
"--name", e.ContainerName,
|
"--name", e.ContainerName,
|
||||||
|
"--restart", "always",
|
||||||
"-p", "80:80",
|
"-p", "80:80",
|
||||||
"-p", "443:443",
|
"-p", "443:443",
|
||||||
"-p", fmt.Sprintf("127.0.0.1:%d:%d", e.OpenrestyObservabilityPort, e.OpenrestyObservabilityPort),
|
"-p", fmt.Sprintf("127.0.0.1:%d:%d", e.OpenrestyObservabilityPort, e.OpenrestyObservabilityPort),
|
||||||
|
|||||||
@@ -399,6 +399,7 @@ func TestDockerExecutorRunContainerMountsManagedFiles(t *testing.T) {
|
|||||||
expectedArgs := []string{
|
expectedArgs := []string{
|
||||||
"run", "-d",
|
"run", "-d",
|
||||||
"--name", "openflare-openresty",
|
"--name", "openflare-openresty",
|
||||||
|
"--restart", "always",
|
||||||
"-p", "80:80",
|
"-p", "80:80",
|
||||||
"-p", "443:443",
|
"-p", "443:443",
|
||||||
"-p", "127.0.0.1:18081:18081",
|
"-p", "127.0.0.1:18081:18081",
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ var OpenRestyClientBodyTimeout = 15
|
|||||||
var OpenRestyClientMaxBodySize = "64m"
|
var OpenRestyClientMaxBodySize = "64m"
|
||||||
var OpenRestyLargeClientHeaderBuffers = "4 16k"
|
var OpenRestyLargeClientHeaderBuffers = "4 16k"
|
||||||
var OpenRestySendTimeout = 30
|
var OpenRestySendTimeout = 30
|
||||||
|
var OpenRestyResolvers = ""
|
||||||
var OpenRestyProxyConnectTimeout = 3
|
var OpenRestyProxyConnectTimeout = 3
|
||||||
var OpenRestyProxySendTimeout = 60
|
var OpenRestyProxySendTimeout = 60
|
||||||
var OpenRestyProxyReadTimeout = 60
|
var OpenRestyProxyReadTimeout = 60
|
||||||
@@ -127,7 +128,7 @@ http {
|
|||||||
gzip {{OpenRestyGzip}};
|
gzip {{OpenRestyGzip}};
|
||||||
gzip_min_length {{OpenRestyGzipMinLength}};
|
gzip_min_length {{OpenRestyGzipMinLength}};
|
||||||
gzip_comp_level {{OpenRestyGzipCompLevel}};
|
gzip_comp_level {{OpenRestyGzipCompLevel}};
|
||||||
{{OpenRestyCacheBlock}} include {{OpenRestyRouteConfigInclude}};
|
{{OpenRestyResolverDirective}}{{OpenRestyCacheBlock}} include {{OpenRestyRouteConfigInclude}};
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|||||||
@@ -133,7 +133,13 @@ func validateOpenRestyOption(key string, value string) error {
|
|||||||
return fmt.Errorf("%s 仅支持 epoll、kqueue、poll、select、rtsig、/dev/poll、eventport 或留空", key)
|
return fmt.Errorf("%s 仅支持 epoll、kqueue、poll、select、rtsig、/dev/poll、eventport 或留空", key)
|
||||||
}
|
}
|
||||||
case "OpenRestyResolvers":
|
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",
|
case "OpenRestyEventsMultiAcceptEnabled",
|
||||||
"OpenRestyWebsocketEnabled",
|
"OpenRestyWebsocketEnabled",
|
||||||
"OpenRestyProxyRequestBufferingEnabled",
|
"OpenRestyProxyRequestBufferingEnabled",
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ func TestValidateOpenRestyOption(t *testing.T) {
|
|||||||
{name: "worker processes invalid", key: "OpenRestyWorkerProcesses", value: "0", wantErr: true},
|
{name: "worker processes invalid", key: "OpenRestyWorkerProcesses", value: "0", wantErr: true},
|
||||||
{name: "events use empty", key: "OpenRestyEventsUse", value: ""},
|
{name: "events use empty", key: "OpenRestyEventsUse", value: ""},
|
||||||
{name: "events use invalid", key: "OpenRestyEventsUse", value: "io_uring", wantErr: true},
|
{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 valid", key: "OpenRestyProxyBuffers", value: "16 16k"},
|
||||||
{name: "proxy buffers invalid", key: "OpenRestyProxyBuffers", value: "16x16k", wantErr: true},
|
{name: "proxy buffers invalid", key: "OpenRestyProxyBuffers", value: "16x16k", wantErr: true},
|
||||||
{name: "cache max size valid", key: "OpenRestyCacheMaxSize", value: "2g"},
|
{name: "cache max size valid", key: "OpenRestyCacheMaxSize", value: "2g"},
|
||||||
|
|||||||
@@ -153,11 +153,6 @@ func updateOptionMap(key string, value string) {
|
|||||||
common.OptionMap = make(map[string]string)
|
common.OptionMap = make(map[string]string)
|
||||||
}
|
}
|
||||||
common.OptionMap[key] = value
|
common.OptionMap[key] = value
|
||||||
if key == "OpenRestyResolvers" {
|
|
||||||
delete(common.OptionMap, key)
|
|
||||||
common.OptionMapRWMutex.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(key, "Permission") {
|
if strings.HasSuffix(key, "Permission") {
|
||||||
intValue, _ := strconv.Atoi(value)
|
intValue, _ := strconv.Atoi(value)
|
||||||
switch key {
|
switch key {
|
||||||
|
|||||||
@@ -133,6 +133,7 @@ type openRestyConfigSnapshot struct {
|
|||||||
GzipEnabled bool `json:"gzip_enabled"`
|
GzipEnabled bool `json:"gzip_enabled"`
|
||||||
GzipMinLength int `json:"gzip_min_length"`
|
GzipMinLength int `json:"gzip_min_length"`
|
||||||
GzipCompLevel int `json:"gzip_comp_level"`
|
GzipCompLevel int `json:"gzip_comp_level"`
|
||||||
|
Resolvers string `json:"resolvers,omitempty"`
|
||||||
CacheEnabled bool `json:"cache_enabled"`
|
CacheEnabled bool `json:"cache_enabled"`
|
||||||
CachePath string `json:"cache_path,omitempty"`
|
CachePath string `json:"cache_path,omitempty"`
|
||||||
CacheLevels string `json:"cache_levels"`
|
CacheLevels string `json:"cache_levels"`
|
||||||
@@ -766,6 +767,7 @@ func buildOpenRestyConfigSnapshot() openRestyConfigSnapshot {
|
|||||||
GzipEnabled: common.OpenRestyGzipEnabled,
|
GzipEnabled: common.OpenRestyGzipEnabled,
|
||||||
GzipMinLength: common.OpenRestyGzipMinLength,
|
GzipMinLength: common.OpenRestyGzipMinLength,
|
||||||
GzipCompLevel: common.OpenRestyGzipCompLevel,
|
GzipCompLevel: common.OpenRestyGzipCompLevel,
|
||||||
|
Resolvers: common.OpenRestyResolvers,
|
||||||
CacheEnabled: common.OpenRestyCacheEnabled,
|
CacheEnabled: common.OpenRestyCacheEnabled,
|
||||||
CachePath: common.OpenRestyCachePath,
|
CachePath: common.OpenRestyCachePath,
|
||||||
CacheLevels: common.OpenRestyCacheLevels,
|
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("OpenRestyGzipEnabled", fmt.Sprintf("%t", left.GzipEnabled), fmt.Sprintf("%t", right.GzipEnabled))
|
||||||
appendIfChanged("OpenRestyGzipMinLength", fmt.Sprintf("%d", left.GzipMinLength), fmt.Sprintf("%d", right.GzipMinLength))
|
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("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("OpenRestyCacheEnabled", fmt.Sprintf("%t", left.CacheEnabled), fmt.Sprintf("%t", right.CacheEnabled))
|
||||||
appendIfChanged("OpenRestyCachePath", left.CachePath, right.CachePath)
|
appendIfChanged("OpenRestyCachePath", left.CachePath, right.CachePath)
|
||||||
appendIfChanged("OpenRestyCacheLevels", left.CacheLevels, right.CacheLevels)
|
appendIfChanged("OpenRestyCacheLevels", left.CacheLevels, right.CacheLevels)
|
||||||
@@ -1063,7 +1066,7 @@ func renderMainConfigTemplate(templateText string, cfg openRestyConfigSnapshot)
|
|||||||
"{{OpenRestyGzip}}", onOff(cfg.GzipEnabled),
|
"{{OpenRestyGzip}}", onOff(cfg.GzipEnabled),
|
||||||
"{{OpenRestyGzipMinLength}}", fmt.Sprintf("%d", cfg.GzipMinLength),
|
"{{OpenRestyGzipMinLength}}", fmt.Sprintf("%d", cfg.GzipMinLength),
|
||||||
"{{OpenRestyGzipCompLevel}}", fmt.Sprintf("%d", cfg.GzipCompLevel),
|
"{{OpenRestyGzipCompLevel}}", fmt.Sprintf("%d", cfg.GzipCompLevel),
|
||||||
"{{OpenRestyResolverDirective}}", "",
|
"{{OpenRestyResolverDirective}}", renderTemplateDirective(cfg.Resolvers != "", fmt.Sprintf("resolver %s;", cfg.Resolvers)),
|
||||||
"{{OpenRestyCacheBlock}}", renderOpenRestyCacheTemplateBlock(cfg),
|
"{{OpenRestyCacheBlock}}", renderOpenRestyCacheTemplateBlock(cfg),
|
||||||
"{{OpenRestyRouteConfigInclude}}", nginxRouteConfigPlaceholder,
|
"{{OpenRestyRouteConfigInclude}}", nginxRouteConfigPlaceholder,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ const defaultPerformanceFields = {
|
|||||||
OpenRestyCacheLockTimeout: '5s',
|
OpenRestyCacheLockTimeout: '5s',
|
||||||
OpenRestyCacheUseStale:
|
OpenRestyCacheUseStale:
|
||||||
'error timeout updating http_500 http_502 http_503 http_504',
|
'error timeout updating http_500 http_502 http_503 http_504',
|
||||||
|
OpenRestyResolvers: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
const performanceFieldTooltips: Record<string, string> = {
|
const performanceFieldTooltips: Record<string, string> = {
|
||||||
@@ -114,6 +115,8 @@ const performanceFieldTooltips: Record<string, string> = {
|
|||||||
proxy_cache_lock_timeout: '等待缓存锁的最长时间,例如 5s。',
|
proxy_cache_lock_timeout: '等待缓存锁的最长时间,例如 5s。',
|
||||||
proxy_cache_use_stale:
|
proxy_cache_use_stale:
|
||||||
'上游异常时允许返回旧缓存的条件列表,例如 error、timeout、http_500。',
|
'上游异常时允许返回旧缓存的条件列表,例如 error、timeout、http_500。',
|
||||||
|
resolvers:
|
||||||
|
'自定义 DNS 解析器,留空表示不配置。若配置,当上游源站需要动态解析且 DNS 服务器失效时,能防止容器异常挂掉。例如:1.1.1.1 8.8.8.8',
|
||||||
};
|
};
|
||||||
|
|
||||||
type PerformanceTab = 'settings' | 'editor';
|
type PerformanceTab = 'settings' | 'editor';
|
||||||
@@ -256,6 +259,7 @@ export function PerformancePage() {
|
|||||||
OpenRestyCacheUseStale:
|
OpenRestyCacheUseStale:
|
||||||
optionMap.OpenRestyCacheUseStale ??
|
optionMap.OpenRestyCacheUseStale ??
|
||||||
'error timeout updating http_500 http_502 http_503 http_504',
|
'error timeout updating http_500 http_502 http_503 http_504',
|
||||||
|
OpenRestyResolvers: optionMap.OpenRestyResolvers ?? '',
|
||||||
});
|
});
|
||||||
setTemplateContent(optionMap.OpenRestyMainConfigTemplate ?? '');
|
setTemplateContent(optionMap.OpenRestyMainConfigTemplate ?? '');
|
||||||
}, [optionsQuery.data]);
|
}, [optionsQuery.data]);
|
||||||
@@ -333,6 +337,10 @@ export function PerformancePage() {
|
|||||||
'OpenRestyWorkerProcesses',
|
'OpenRestyWorkerProcesses',
|
||||||
performanceFields.OpenRestyWorkerProcesses.trim(),
|
performanceFields.OpenRestyWorkerProcesses.trim(),
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'OpenRestyResolvers',
|
||||||
|
performanceFields.OpenRestyResolvers.trim(),
|
||||||
|
],
|
||||||
[
|
[
|
||||||
'OpenRestyWorkerConnections',
|
'OpenRestyWorkerConnections',
|
||||||
performanceFields.OpenRestyWorkerConnections.trim(),
|
performanceFields.OpenRestyWorkerConnections.trim(),
|
||||||
@@ -689,6 +697,22 @@ export function PerformancePage() {
|
|||||||
}
|
}
|
||||||
>
|
>
|
||||||
<div className="grid gap-5 md:grid-cols-2 xl:grid-cols-3">
|
<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
|
<ResourceField
|
||||||
label="worker_processes"
|
label="worker_processes"
|
||||||
tooltip={performanceFieldTooltips.worker_processes}
|
tooltip={performanceFieldTooltips.worker_processes}
|
||||||
|
|||||||
Reference in New Issue
Block a user