Skip to content

Commit

Permalink
插件中 Location 302 跳转移至 goblin 发起请求 #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Becivells committed Sep 11, 2021
1 parent 092c7c8 commit 91204f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
10 changes: 10 additions & 0 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ func (base *BasePlugin) SetInitConfig(PluginVar *TmpVariable) {
if rule.Replace != nil {
for _, rp := range rule.Replace {
if rp.Response != nil {
if rp.Response.Location != "" {
// 替换并且检查替换的变量
tplStr, err := utils.TempStr(rp.Response.Location, PluginVar)
if err != nil {
log.Fatal(err.Error())
}
log.Info("[plugin] Parse:Response.Header.Location: %s ==> %s ", rp.Response.Location, tplStr)
rp.Response.Location = tplStr
}

if rp.Response.Header != nil {
if location, ok := rp.Response.Header["Location"]; ok {
// 替换并且检查替换的变量
Expand Down
9 changes: 5 additions & 4 deletions internal/plugin/replace/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ type Request struct {

// Response 响应头
type Response struct {
Status int `yaml:"Status"`
Header map[string]string `yaml:"Header"`
Cookie *Cookie `yaml:"Cookie"`
Body *Body `yaml:"Body"`
Status int `yaml:"Status"`
Header map[string]string `yaml:"Header"`
Cookie *Cookie `yaml:"Cookie"`
Body *Body `yaml:"Body"`
Location string `yaml:"Location"`
}

type Cookie struct { // 由于有默认值需要一起设置
Expand Down
39 changes: 39 additions & 0 deletions internal/reverse/reverse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package reverse

import (
"goblin/internal/plugin"
"goblin/pkg/utils"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -32,6 +34,43 @@ func (reverse *Reverse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Info("record:\n%s", dumpReq(r))
}
log.Info("[c->p] host: %s,RemoteAddr: %s,URI: %s", host, GetClientIP(r), r.RequestURI)
//response
//插件系统 rule 处理响应数据
if rules, ok := plugin.Plugins[host]; ok {
for _, rule := range rules.Rule {
urlmatch := false
// url 匹配规则
switch strings.ToLower(rule.Match) {
case "word":
urlmatch = r.URL.Path == rule.URL
case "prefix":
urlmatch = strings.HasPrefix(r.URL.Path, rule.URL)
case "suffix":
urlmatch = strings.HasPrefix(r.URL.Path, rule.URL)
}

if urlmatch {
log.Info("[plugin:%s] hit url: %s", rules.Name, r.RequestURI)
// replace
if rule.Replace != nil {
for _, rp := range rule.Replace {
// 判断请求方法是否在里面
if utils.EleInArray(r.Method, rp.Request.Method) {
log.Info("[plugin:%s.Replace.%s] Method match:%s", rules.Name, rule.URL, rp.Request.Method)
//处理响应数据
if rp.Response.Location != "" {
log.Info("[plugin: %s.Location]: %s", rules.Name, rp.Response.Location)
w.Header().Set("Location", rp.Response.Location)
w.WriteHeader(302)
return
}
}
}
}
}
}
}

// 处理缓存
if cache.GlobalCache.Type != "none" {
urlobj, err := decodeCache(r.URL.String())
Expand Down

0 comments on commit 91204f1

Please sign in to comment.