Skip to content

Commit d6c7f19

Browse files
committed
MIME type 放到配置文件中
1 parent 61d40a3 commit d6c7f19

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

internal/options/options.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package options
22

33
import (
4+
"goblin/internal/plugin/replace"
45
"goblin/pkg/cache"
56
"goblin/pkg/ipinfo"
67
"goblin/pkg/notice"
@@ -29,10 +30,11 @@ type Options struct {
2930
LogFile string `yaml:"log_file"`
3031
Cache *cache.Config `yaml:"cache"`
3132
// PrintConfig print config file
32-
PrintConfig bool `yaml:"-"`
33-
CacheType []string `yaml:"CacheType"`
34-
CacheSize int64 `yaml:"CacheSize"`
35-
Plugin []*plugin.Plugin `yaml:"-"`
33+
PrintConfig bool `yaml:"-"`
34+
CacheType []string `yaml:"CacheType"`
35+
CacheSize int64 `yaml:"CacheSize"`
36+
Plugin []*plugin.Plugin `yaml:"-"`
37+
SupportMIME *replace.SupportMIME `yaml:"SupportMIME"`
3638
}
3739

3840
type noticeConfig struct {

internal/options/parse.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"fmt"
66
"goblin/internal/plugin"
7+
"goblin/internal/plugin/replace"
78
"goblin/pkg/cache"
89
"goblin/pkg/cache/redis"
910
"goblin/pkg/ipinfo"
@@ -81,6 +82,13 @@ func ParseOptions() *Options {
8182
},
8283
},
8384
},
85+
SupportMIME: &replace.SupportMIME{
86+
Enable: false,
87+
List: []string{"text", "application/json", "application/javascript",
88+
"application/x-javascript", "message", "application/hta",
89+
"application/rtf", "application/ecmascript", "image/svg+xml",
90+
"application/xhtml", "application/xml"},
91+
},
8492
}
8593
// 显示banner
8694
showBanner()

internal/plugin/replace/response.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
)
1414

1515
// https://annevankesteren.nl/2005/02/javascript-mime-type
16-
// todo 移动至配置文件
17-
var allowType = []string{"text", "application/json", "application/javascript", "application/x-javascript", "message", "application/hta", "application/rtf", "application/ecmascript", "image/svg+xml", "application/xhtml", "application/xml"}
1816

1917
func (rpRule *Response) Response(maxContentLength int, response *http.Response) error {
2018
if rpRule == nil {
@@ -97,10 +95,12 @@ func (rpRule *Response) Response(maxContentLength int, response *http.Response)
9795
log.Trace("%s,Content-Type is empty", response.Request.URL)
9896
return nil
9997
}
100-
//只允许文本类的替换
101-
if !utils.StrPrefixOrinList(conType, allowType) {
102-
log.Trace("%s,Content-Type is not plan: %s will ignore", response.Request.URL, conType)
103-
return nil
98+
if AllowMIMEType.Enable {
99+
//只允许文本类的替换
100+
if !utils.StrPrefixOrinList(conType, AllowMIMEType.List) {
101+
log.Trace("%s,Content-Type is not plan: %s will ignore", response.Request.URL, conType)
102+
return nil
103+
}
104104
}
105105

106106
// append

internal/plugin/replace/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
var Method = []string{"GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE"}
88

9+
var AllowMIMEType = &SupportMIME{}
910
var (
1011
BodyFiles = make(map[string][]byte) //插件系统注入的js静态文件
1112
)
@@ -50,3 +51,8 @@ type ReplaceStr struct {
5051
New string `yaml:"New"`
5152
Count int `yaml:"Count"`
5253
}
54+
55+
type SupportMIME struct {
56+
Enable bool `yaml:"Enable"`
57+
List []string `yaml:"List"`
58+
}

internal/reverse/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"fmt"
7+
"goblin/internal/plugin/replace"
78
"net/http"
89
"net/http/httputil"
910
"net/url"
@@ -107,6 +108,8 @@ func initReverse(options *options.Options) (revMap map[string]struct {
107108
// 初始化版本
108109
Version = options.VersionInfo
109110
plugin.Version = options.VersionInfo
111+
// 初始化 supportMIME
112+
replace.AllowMIMEType = options.SupportMIME
110113
revMap = make(map[string]struct {
111114
SSL bool
112115
Reverse *Reverse

0 commit comments

Comments
 (0)