Skip to content

Commit 4c5a707

Browse files
committed
Merge branch 'refs/heads/develop'
2 parents 963d4bc + a695b29 commit 4c5a707

File tree

6 files changed

+46
-19
lines changed

6 files changed

+46
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# 项目介绍
88

99
**Bifrost**
10-
是基于golang语言开发的项目,它目前还处于测试阶段,用于对Nginx配置文件解析并提供配置文件展示和修改的接口,支持json、字符串格式与golang结构相互转换。该项目持续更新中。最新可用版本为[v1.1.0-alpha.12](https://github.com/ClessLi/bifrost/tree/v1.1.0-alpha.12)
10+
是基于golang语言开发的项目,它目前还处于测试阶段,用于对Nginx配置文件解析并提供配置文件展示和修改的接口,支持json、字符串格式与golang结构相互转换。该项目持续更新中。最新可用版本为[v1.1.0-alpha.13](https://github.com/ClessLi/bifrost/tree/v1.1.0-alpha.13)
1111

1212
# 项目特点
1313

pkg/resolv/V3/nginx/configuration/context/local/basic_context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (b *BasicContext) Insert(ctx context.Context, idx int) context.Context {
6565
// set father for inserted ctx
6666
err := ctx.SetFather(b.self)
6767
if err != nil {
68-
return context.ErrContext(errors.WithCode(code.ErrV3SetFatherContextFailed, err.Error()))
68+
return context.ErrContext(errors.WithCode(code.ErrV3SetFatherContextFailed, "%s", err.Error()))
6969
}
7070

7171
return b.self
@@ -81,7 +81,7 @@ func (b *BasicContext) Remove(idx int) context.Context {
8181
// release father ctx
8282
err := b.Children[idx].SetFather(context.NullContext())
8383
if err != nil {
84-
return context.ErrContext(errors.WithCode(code.ErrV3SetFatherContextFailed, err.Error()))
84+
return context.ErrContext(errors.WithCode(code.ErrV3SetFatherContextFailed, "%s", err.Error()))
8585
}
8686

8787
b.Children = append(b.Children[:idx], b.Children[idx+1:]...)

pkg/resolv/V3/nginx/configuration/context/local/basic_context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ func TestBasicContext_Remove(t *testing.T) {
17941794
args: args{
17951795
idx: hasErrChildCtx.Len() - 1,
17961796
},
1797-
want: context.ErrContext(errors.WithCode(code.ErrV3SetFatherContextFailed, context.NullContext().(*context.ErrorContext).
1797+
want: context.ErrContext(errors.WithCode(code.ErrV3SetFatherContextFailed, "%s", context.NullContext().(*context.ErrorContext).
17981798
AppendError(context.ErrSetFatherToErrorContext).Error().Error())),
17991799
wantRemovedCtx: hasErrChildCtx.Child(hasErrChildCtx.Len() - 1),
18001800
},

pkg/resolv/V3/nginx/configuration/context/local/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ func (e *JSONError) ToError() error {
3434
return errors.New(e.Message)
3535
}
3636

37-
return errors.WithCode(e.Code, e.Message)
37+
return errors.WithCode(e.Code, "%s", e.Message)
3838
}

pkg/resolv/V3/nginx/configuration/context/local/proxy_pass_test.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ func fakeProxyPassTestMainCtx() (MainContext, error) {
125125
Insert(
126126
NewContext(context_type.TypeDirHTTPProxyPass, "http://error.pos.http.proxy.pass2"),
127127
8,
128+
).
129+
Insert(
130+
NewContext(context_type.TypeLocation, "~ /disabled-unknown-upstream").
131+
Insert(
132+
NewContext(context_type.TypeDirHTTPProxyPass, "https://unknown.upstream/localiptest"),
133+
0,
134+
).Disable(),
135+
9,
128136
),
129137
0,
130138
).
@@ -243,6 +251,18 @@ func fakeProxyPassTestMainCtx() (MainContext, error) {
243251
),
244252
7,
245253
).
254+
Insert(
255+
NewContext(context_type.TypeServer, "").
256+
Insert(
257+
NewContext(context_type.TypeComment, "disabled stream server"),
258+
0,
259+
).
260+
Insert(
261+
NewContext(context_type.TypeDirStreamProxyPass, "unknown.domain:9876"),
262+
1,
263+
).Disable(),
264+
8,
265+
).
246266
Insert(
247267
NewContext(context_type.TypeUpstream, "upstream_server").
248268
Insert(
@@ -1137,20 +1157,22 @@ func TestHTTPProxyPass_MarshalJSON(t *testing.T) {
11371157
context.NewKeyWordsByType(context_type.TypeDirHTTPProxyPass),
11381158
).Target().(*HTTPProxyPass),
11391159
},
1140-
want: []byte(`{"enabled":true,"context-type":"dir_http_proxy_pass","value":"https://baidu.com","proxy-pass":{"original-url":"https://baidu.com","protocol":"https","addresses":[{"domain-name":"baidu.com","port":443,"ipv4-list":["10.1.11.111","10.1.12.122"],"resolve-err":null}],"uri":""}}`), //nolint:lll
1160+
want: []byte(`{"enabled":true,"context-type":"dir_http_proxy_pass","value":"https://baidu.com","proxy-pass":{"original-url":"https://baidu.com","protocol":"https","addresses":[{"domain-name":"baidu.com","port":443,"sockets":[{"ipv4":"10.1.11.111","port":443,"tcp-connectivity":0,"udp-connectivity":0},{"ipv4":"10.1.12.122","port":443,"tcp-connectivity":0,"udp-connectivity":0}],"resolve-err":null}],"uri":""}}`), //nolint:lll
11411161
},
11421162
{
11431163
name: "http location proxy pass with some errors",
11441164
fields: fields{
11451165
proxyPass: testMain.MainConfig().ChildrenPosSet().
11461166
QueryOne(
1147-
context.NewKeyWordsByType(context_type.TypeLocation).SetStringMatchingValue("unknown-upstream"),
1167+
context.NewKeyWordsByType(context_type.TypeLocation).
1168+
SetStringMatchingValue("unknown-upstream").
1169+
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc),
11481170
).
11491171
QueryOne(
11501172
context.NewKeyWordsByType(context_type.TypeDirHTTPProxyPass),
11511173
).Target().(*HTTPProxyPass),
11521174
},
1153-
want: []byte("{\"enabled\":true,\"context-type\":\"dir_http_proxy_pass\",\"value\":\"http://unknown.upstream/localiptest\",\"proxy-pass\":{\"original-url\":\"http://unknown.upstream/localiptest\",\"protocol\":\"http\",\"addresses\":[{\"domain-name\":\"unknown.upstream\",\"port\":80,\"ipv4-list\":null,\"resolve-err\":{\"message\":\"Domain name resolution failed\",\"error\":\"the domain name resolution record for `unknown.upstream` does not exist\",\"code\":110020}}],\"uri\":\"/localiptest\"}}"), //nolint:lll
1175+
want: []byte("{\"enabled\":true,\"context-type\":\"dir_http_proxy_pass\",\"value\":\"http://unknown.upstream/localiptest\",\"proxy-pass\":{\"original-url\":\"http://unknown.upstream/localiptest\",\"protocol\":\"http\",\"addresses\":[{\"domain-name\":\"unknown.upstream\",\"port\":80,\"sockets\":[],\"resolve-err\":{\"message\":\"Domain name resolution failed\",\"error\":\"the domain name resolution record for `unknown.upstream` does not exist\",\"code\":110020}}],\"uri\":\"/localiptest\"}}"), //nolint:lll
11541176
},
11551177
{
11561178
name: "http location proxy pass to upstream servers, which have some errors",
@@ -1163,7 +1185,7 @@ func TestHTTPProxyPass_MarshalJSON(t *testing.T) {
11631185
context.NewKeyWordsByType(context_type.TypeDirHTTPProxyPass),
11641186
).Target().(*HTTPProxyPass),
11651187
},
1166-
want: []byte("{\"enabled\":true,\"context-type\":\"dir_http_proxy_pass\",\"value\":\"http://has_unknown_server/proxy/to/upstreamserver\",\"proxy-pass\":{\"original-url\":\"http://has_unknown_server/proxy/to/upstreamserver\",\"protocol\":\"http\",\"addresses\":[{\"domain-name\":\"test.cn\",\"port\":443,\"ipv4-list\":[\"10.1.12.122\",\"10.1.13.133\"],\"resolve-err\":null},{\"domain-name\":\"example.com\",\"port\":8443,\"ipv4-list\":[\"10.1.12.122\",\"10.1.13.133\"],\"resolve-err\":null},{\"domain-name\":\"127.0.0.1\",\"port\":8080,\"ipv4-list\":[\"127.0.0.1\"],\"resolve-err\":null},{\"domain-name\":\"unknown.domain\",\"port\":8080,\"ipv4-list\":null,\"resolve-err\":{\"message\":\"Domain name resolution failed\",\"error\":\"the domain name resolution record for `unknown.domain` does not exist\",\"code\":110020}}],\"uri\":\"/proxy/to/upstreamserver\"}}"), //nolint:lll
1188+
want: []byte("{\"enabled\":true,\"context-type\":\"dir_http_proxy_pass\",\"value\":\"http://has_unknown_server/proxy/to/upstreamserver\",\"proxy-pass\":{\"original-url\":\"http://has_unknown_server/proxy/to/upstreamserver\",\"protocol\":\"http\",\"addresses\":[{\"domain-name\":\"test.cn\",\"port\":443,\"sockets\":[{\"ipv4\":\"10.1.12.122\",\"port\":443,\"tcp-connectivity\":0,\"udp-connectivity\":0},{\"ipv4\":\"10.1.13.133\",\"port\":443,\"tcp-connectivity\":0,\"udp-connectivity\":0}],\"resolve-err\":null},{\"domain-name\":\"example.com\",\"port\":8443,\"sockets\":[{\"ipv4\":\"10.1.12.122\",\"port\":8443,\"tcp-connectivity\":0,\"udp-connectivity\":0},{\"ipv4\":\"10.1.13.133\",\"port\":8443,\"tcp-connectivity\":0,\"udp-connectivity\":0}],\"resolve-err\":null},{\"domain-name\":\"127.0.0.1\",\"port\":8080,\"sockets\":[{\"ipv4\":\"127.0.0.1\",\"port\":8080,\"tcp-connectivity\":0,\"udp-connectivity\":0}],\"resolve-err\":null},{\"domain-name\":\"unknown.domain\",\"port\":8080,\"sockets\":[],\"resolve-err\":{\"message\":\"Domain name resolution failed\",\"error\":\"the domain name resolution record for `unknown.domain` does not exist\",\"code\":110020}}],\"uri\":\"/proxy/to/upstreamserver\"}}"), //nolint:lll
11671189
},
11681190
}
11691191
for _, tt := range tests {
@@ -1212,7 +1234,7 @@ func TestStreamProxyPass_MarshalJSON(t *testing.T) {
12121234
context.NewKeyWordsByType(context_type.TypeDirStreamProxyPass).SetStringMatchingValue("abc.com:22"),
12131235
).Target().(*StreamProxyPass),
12141236
},
1215-
want: []byte(`{"enabled":true,"context-type":"dir_stream_proxy_pass","value":"abc.com:22","ProxyPass":{"original-address":"abc.com:22","addresses":[{"domain-name":"abc.com","port":22,"ipv4-list":["10.2.12.122"],"resolve-err":null}]}}`), //nolint:lll
1237+
want: []byte(`{"enabled":true,"context-type":"dir_stream_proxy_pass","value":"abc.com:22","proxy-pass":{"original-address":"abc.com:22","addresses":[{"domain-name":"abc.com","port":22,"sockets":[{"ipv4":"10.2.12.122","port":22,"tcp-connectivity":0,"udp-connectivity":0}],"resolve-err":null}]}}`), //nolint:lll
12161238
},
12171239
{
12181240
name: "stream proxy pass to upstream servers, which have unknown server",
@@ -1225,7 +1247,7 @@ func TestStreamProxyPass_MarshalJSON(t *testing.T) {
12251247
context.NewKeyWordsByType(context_type.TypeDirStreamProxyPass).SetStringMatchingValue("has_unknown_server"),
12261248
).Target().(*StreamProxyPass),
12271249
},
1228-
want: []byte("{\"enabled\":true,\"context-type\":\"dir_stream_proxy_pass\",\"value\":\"has_unknown_server\",\"ProxyPass\":{\"original-address\":\"has_unknown_server\",\"addresses\":[{\"domain-name\":\"test.cn\",\"port\":22,\"ipv4-list\":[\"10.1.12.122\",\"10.1.13.133\"],\"resolve-err\":null},{\"domain-name\":\"example.com\",\"port\":22,\"ipv4-list\":[\"10.1.12.122\",\"10.1.13.133\"],\"resolve-err\":null},{\"domain-name\":\"127.0.0.1\",\"port\":123,\"ipv4-list\":[\"127.0.0.1\"],\"resolve-err\":null},{\"domain-name\":\"unknown.domain\",\"port\":8080,\"ipv4-list\":null,\"resolve-err\":{\"message\":\"Domain name resolution failed\",\"error\":\"the domain name resolution record for `unknown.domain` does not exist\",\"code\":110020}}]}}"), //nolint:lll
1250+
want: []byte("{\"enabled\":true,\"context-type\":\"dir_stream_proxy_pass\",\"value\":\"has_unknown_server\",\"proxy-pass\":{\"original-address\":\"has_unknown_server\",\"addresses\":[{\"domain-name\":\"test.cn\",\"port\":22,\"sockets\":[{\"ipv4\":\"10.1.12.122\",\"port\":22,\"tcp-connectivity\":0,\"udp-connectivity\":0},{\"ipv4\":\"10.1.13.133\",\"port\":22,\"tcp-connectivity\":0,\"udp-connectivity\":0}],\"resolve-err\":null},{\"domain-name\":\"example.com\",\"port\":22,\"sockets\":[{\"ipv4\":\"10.1.12.122\",\"port\":22,\"tcp-connectivity\":0,\"udp-connectivity\":0},{\"ipv4\":\"10.1.13.133\",\"port\":22,\"tcp-connectivity\":0,\"udp-connectivity\":0}],\"resolve-err\":null},{\"domain-name\":\"127.0.0.1\",\"port\":123,\"sockets\":[{\"ipv4\":\"127.0.0.1\",\"port\":123,\"tcp-connectivity\":0,\"udp-connectivity\":0}],\"resolve-err\":null},{\"domain-name\":\"unknown.domain\",\"port\":8080,\"sockets\":[],\"resolve-err\":{\"message\":\"Domain name resolution failed\",\"error\":\"the domain name resolution record for `unknown.domain` does not exist\",\"code\":110020}}]}}"), //nolint:lll
12291251
},
12301252
{
12311253
name: "stream proxy pass to an address, which with an unknown domain name",
@@ -1235,10 +1257,12 @@ func TestStreamProxyPass_MarshalJSON(t *testing.T) {
12351257
context.NewKeyWordsByType(context_type.TypeStream),
12361258
).
12371259
QueryOne(
1238-
context.NewKeyWordsByType(context_type.TypeDirStreamProxyPass).SetStringMatchingValue("unknown.domain"),
1260+
context.NewKeyWordsByType(context_type.TypeDirStreamProxyPass).
1261+
SetStringMatchingValue("unknown.domain").
1262+
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc),
12391263
).Target().(*StreamProxyPass),
12401264
},
1241-
want: []byte("{\"enabled\":true,\"context-type\":\"dir_stream_proxy_pass\",\"value\":\"unknown.domain:9876\",\"ProxyPass\":{\"original-address\":\"unknown.domain:9876\",\"addresses\":[{\"domain-name\":\"unknown.domain\",\"port\":9876,\"ipv4-list\":null,\"resolve-err\":{\"message\":\"Domain name resolution failed\",\"error\":\"the domain name resolution record for `unknown.domain` does not exist\",\"code\":110020}}]}}"), //nolint:lll
1265+
want: []byte("{\"enabled\":true,\"context-type\":\"dir_stream_proxy_pass\",\"value\":\"unknown.domain:9876\",\"proxy-pass\":{\"original-address\":\"unknown.domain:9876\",\"addresses\":[{\"domain-name\":\"unknown.domain\",\"port\":9876,\"sockets\":[],\"resolve-err\":{\"message\":\"Domain name resolution failed\",\"error\":\"the domain name resolution record for `unknown.domain` does not exist\",\"code\":110020}}]}}"), //nolint:lll
12421266
},
12431267
}
12441268
for _, tt := range tests {
@@ -1639,7 +1663,7 @@ func Test_tmpProxyPass_MarshalJSON(t1 *testing.T) {
16391663
fields: fields{
16401664
proxyPass: streamTmp,
16411665
},
1642-
want: []byte("{\"enabled\":true,\"context-type\":\"dir_stream_proxy_pass\",\"value\":\"example.com\",\"ProxyPass\":{\"original-address\":\"\",\"addresses\":null}}"),
1666+
want: []byte("{\"enabled\":true,\"context-type\":\"dir_stream_proxy_pass\",\"value\":\"example.com\",\"proxy-pass\":{\"original-address\":\"\",\"addresses\":null}}"),
16431667
},
16441668
{
16451669
name: "wrong pos stream proxy pass",

pkg/resolv/V3/nginx/configuration/nginx_config_manager.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,14 @@ func (m *nginxConfigManager) refresh() error {
284284
func (m *nginxConfigManager) reparseProxyInfo() error {
285285
logV1.Debugf("start to reparse proxy info")
286286

287-
return m.configuration.Main().ChildrenPosSet().QueryAll(nginxContext.NewKeyWords(func(targetCtx nginxContext.Context) bool {
288-
return targetCtx.Type() == context_type.TypeDirHTTPProxyPass ||
289-
targetCtx.Type() == context_type.TypeDirStreamProxyPass ||
290-
targetCtx.Type() == context_type.TypeDirUninitializedProxyPass
291-
})).
287+
return m.configuration.Main().ChildrenPosSet().QueryAll(
288+
nginxContext.NewKeyWords(func(targetCtx nginxContext.Context) bool {
289+
return targetCtx.Type() == context_type.TypeDirHTTPProxyPass ||
290+
targetCtx.Type() == context_type.TypeDirStreamProxyPass ||
291+
targetCtx.Type() == context_type.TypeDirUninitializedProxyPass
292+
}).
293+
SetSkipQueryFilter(nginxContext.SkipDisabledCtxFilterFunc), // skip reparsing the disabled `ProxyPass`.
294+
).
292295
Map(func(pos nginxContext.Pos) (nginxContext.Pos, error) {
293296
if err := pos.Target().Error(); err != nil {
294297
return pos, err

0 commit comments

Comments
 (0)