diff --git a/internal/options/options.go b/internal/options/options.go index 3910702..4e391fc 100644 --- a/internal/options/options.go +++ b/internal/options/options.go @@ -1,6 +1,7 @@ package options import ( + "goblin/internal/plugin/replace" "goblin/pkg/cache" "goblin/pkg/ipinfo" "goblin/pkg/notice" @@ -29,10 +30,11 @@ type Options struct { LogFile string `yaml:"log_file"` Cache *cache.Config `yaml:"cache"` // PrintConfig print config file - PrintConfig bool `yaml:"-"` - CacheType []string `yaml:"CacheType"` - CacheSize int64 `yaml:"CacheSize"` - Plugin []*plugin.Plugin `yaml:"-"` + PrintConfig bool `yaml:"-"` + CacheType []string `yaml:"CacheType"` + CacheSize int64 `yaml:"CacheSize"` + Plugin []*plugin.Plugin `yaml:"-"` + SupportMIME *replace.SupportMIME `yaml:"SupportMIME"` } type noticeConfig struct { diff --git a/internal/options/parse.go b/internal/options/parse.go index c1685fb..06ade84 100644 --- a/internal/options/parse.go +++ b/internal/options/parse.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "goblin/internal/plugin" + "goblin/internal/plugin/replace" "goblin/pkg/cache" "goblin/pkg/cache/redis" "goblin/pkg/ipinfo" @@ -81,6 +82,13 @@ func ParseOptions() *Options { }, }, }, + SupportMIME: &replace.SupportMIME{ + Enable: false, + List: []string{"text", "application/json", "application/javascript", + "application/x-javascript", "message", "application/hta", + "application/rtf", "application/ecmascript", "image/svg+xml", + "application/xhtml", "application/xml"}, + }, } // 显示banner showBanner() diff --git a/internal/plugin/replace/response.go b/internal/plugin/replace/response.go index fc08590..2aec060 100644 --- a/internal/plugin/replace/response.go +++ b/internal/plugin/replace/response.go @@ -13,8 +13,6 @@ import ( ) // https://annevankesteren.nl/2005/02/javascript-mime-type -// todo 移动至配置文件 -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"} func (rpRule *Response) Response(maxContentLength int, response *http.Response) error { if rpRule == nil { @@ -97,10 +95,12 @@ func (rpRule *Response) Response(maxContentLength int, response *http.Response) log.Trace("%s,Content-Type is empty", response.Request.URL) return nil } - //只允许文本类的替换 - if !utils.StrPrefixOrinList(conType, allowType) { - log.Trace("%s,Content-Type is not plan: %s will ignore", response.Request.URL, conType) - return nil + if AllowMIMEType.Enable { + //只允许文本类的替换 + if !utils.StrPrefixOrinList(conType, AllowMIMEType.List) { + log.Trace("%s,Content-Type is not plan: %s will ignore", response.Request.URL, conType) + return nil + } } // append diff --git a/internal/plugin/replace/types.go b/internal/plugin/replace/types.go index aa545c2..165a21d 100644 --- a/internal/plugin/replace/types.go +++ b/internal/plugin/replace/types.go @@ -6,6 +6,7 @@ import ( var Method = []string{"GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE"} +var AllowMIMEType = &SupportMIME{} var ( BodyFiles = make(map[string][]byte) //插件系统注入的js静态文件 ) @@ -50,3 +51,8 @@ type ReplaceStr struct { New string `yaml:"New"` Count int `yaml:"Count"` } + +type SupportMIME struct { + Enable bool `yaml:"Enable"` + List []string `yaml:"List"` +} diff --git a/internal/reverse/server.go b/internal/reverse/server.go index 8af8468..f803b7a 100644 --- a/internal/reverse/server.go +++ b/internal/reverse/server.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "fmt" + "goblin/internal/plugin/replace" "net/http" "net/http/httputil" "net/url" @@ -107,6 +108,8 @@ func initReverse(options *options.Options) (revMap map[string]struct { // 初始化版本 Version = options.VersionInfo plugin.Version = options.VersionInfo + // 初始化 supportMIME + replace.AllowMIMEType = options.SupportMIME revMap = make(map[string]struct { SSL bool Reverse *Reverse