Skip to content

Commit

Permalink
MIME type 放到配置文件中
Browse files Browse the repository at this point in the history
  • Loading branch information
Becivells committed Sep 13, 2021
1 parent 61d40a3 commit d6c7f19
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
10 changes: 6 additions & 4 deletions internal/options/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package options

import (
"goblin/internal/plugin/replace"
"goblin/pkg/cache"
"goblin/pkg/ipinfo"
"goblin/pkg/notice"
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions internal/options/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"goblin/internal/plugin"
"goblin/internal/plugin/replace"
"goblin/pkg/cache"
"goblin/pkg/cache/redis"
"goblin/pkg/ipinfo"
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions internal/plugin/replace/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions internal/plugin/replace/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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静态文件
)
Expand Down Expand Up @@ -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"`
}
3 changes: 3 additions & 0 deletions internal/reverse/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"goblin/internal/plugin/replace"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d6c7f19

Please sign in to comment.