Skip to content

Commit

Permalink
临时增加 dump 完整请求包
Browse files Browse the repository at this point in the history
  • Loading branch information
Becivells committed May 30, 2023
1 parent f4ec81c commit fcd0eb1
Show file tree
Hide file tree
Showing 28 changed files with 1,032 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/go-redis/redis/v8 v8.11.3
github.com/google/uuid v1.1.2
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/olivere/elastic v6.2.37+incompatible
github.com/olivere/elastic/v7 v7.0.29
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand Down
27 changes: 24 additions & 3 deletions internal/plugin/dump/determine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dump
import (
"bytes"
"goblin/pkg/utils"
"io/ioutil"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -58,17 +58,38 @@ func (dump *Dump) Determine(maxContentLength int, response *http.Response) (dete
return true, dump.Notice
}

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
log.Info("%s", err.Error())
return false, dump.Notice
}
if bytes.Contains(body, []byte(dump.Response.Body)) {
return true, dump.Notice
}
response.Body = ioutil.NopCloser(bytes.NewReader(body))
response.Body = io.NopCloser(bytes.NewReader(body))
}
}

return false, dump.Notice
}

func (dump *Dump) NeedCache(r *http.Request) bool {
start := time.Now()
defer log.Info("[time] url: %s, dump cache hand time: %v", r.RequestURI, time.Since(start))
if dump == nil {
return false
}

// 如何没有任何请求方式支持直接返回
if len(dump.Request.Method) == 0 {
return false
}
// 判断 method 是否符合 dump 规则
if utils.EleInArray(r.Method, dump.Request.Method) {
//为 nil 不匹配 Response
return true

}

return false
}
2 changes: 1 addition & 1 deletion internal/plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type BasePlugin struct {
UseBody bool `yaml:"-"` //响应body解包一次
}

//Rule 规则结构体
// Rule 规则结构体
type Rule struct {
URL string
Match string `yaml:"Match"`
Expand Down
13 changes: 10 additions & 3 deletions internal/reverse/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func (reverse *Reverse) ModifyResponse(shost string) func(response *http.Respons
response.Header.Del("X-XSS-Protection")
//https://stackoverflow.com/questions/27358966/how-to-set-x-frame-options-on-iframe
response.Header.Del("X-Frame-Options")

response.Header.Del("Content-Security-Policy-Report-Only")

// 删除缓存策略
response.Header.Del("Expires")
response.Header.Del("Last-Modified")
response.Header.Del("Date")

if response.Header.Get("Access-Control-Allow-Origin") != "" {
//https://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains
if response.Request.Header.Get("Origin") != "" {
Expand Down Expand Up @@ -101,7 +101,14 @@ func (reverse *Reverse) ModifyResponse(shost string) func(response *http.Respons
dete, msg := dp.Determine(reverse.MaxContentLength, response)
start := time.Now()
if dete {
dplog := dumpJson(response.Request)
uid := strings.Join(response.Request.Header["X-Request-ID"], "")
dplog, isCache := cache.DumpCache.Get(uid)
cache.DumpCache.Delete(uid)
if !isCache {
log.Warn("[Plugin:%s.%s]not cache : %s\n", rules.Name, rule.URL, dplog)
dplog = dumpJson(response.Request)
}

logging.AccLogger.WithFields(logrus.Fields{
"method": response.Request.Method,
"url": response.Request.URL.RequestURI(),
Expand Down
11 changes: 11 additions & 0 deletions internal/reverse/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,22 @@ func (reverse *Reverse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"type": "clientReq",
}).Warn(reqraw)
}

log.Info("[c->p] host: %s,RemoteAddr: %s,URI: %s", host, GetClientIP(r), r.RequestURI)
//response
//插件系统 rule 处理响应数据
if rules, ok := plugin.Plugins[host]; ok {
//dump

for _, rule := range rules.Rule {
for _, dp := range rule.Dump {
if dp.NeedCache(r) {
uuidstr := utils.GenerateUUID()
r.Header["X-Request-ID"] = []string{uuidstr}
cache.DumpCache.Set(uuidstr, dumpJson(r), 60*time.Second)
}
}

urlmatch := false
// url 匹配规则
switch strings.ToLower(rule.Match) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"fmt"
cacheNew "github.com/patrickmn/go-cache"
"time"

log "unknwon.dev/clog/v2"
Expand All @@ -15,6 +16,7 @@ import (
var ctx = context.Background()

var GlobalCache *Cache
var DumpCache *cacheNew.Cache

func (db *Config) Init() {
c := &Cache{
Expand All @@ -32,6 +34,7 @@ func (db *Config) Init() {
log.Fatal("unsupported database type: %s", db.Type)
}
GlobalCache = c
DumpCache = cacheNew.New(15*time.Second, 60*time.Second)
}

func (cache *Cache) Set(key string, v interface{}) {
Expand Down Expand Up @@ -72,3 +75,21 @@ func (cache *Cache) Get(key string) (interface{}, error) {
}
return nil, fmt.Errorf("no cache type")
}

func (cache *Cache) GetOnce(key string) (interface{}, error) {
switch cache.Type {
case "self":
if val, hasKey := cache.Self.Get(key); hasKey {
cache.Self.Delete(key)
return val, nil
}
return nil, fmt.Errorf("no cache")
case "redis":
result, err := cache.Redis.Get(ctx, key).Result()
cache.Redis.Del(ctx, key).Result()
return result, err
case "none":
return nil, fmt.Errorf("no cache")
}
return nil, fmt.Errorf("no cache type")
}
2 changes: 1 addition & 1 deletion pkg/ipinfo/qqwry/download.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package qqwry
//nolint
// nolint
package qqwry

import (
Expand Down
11 changes: 10 additions & 1 deletion pkg/utils/password.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import "golang.org/x/crypto/bcrypt"
import (
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)

// HashAndSalt 加密密码
func HashAndSalt(pwd string) (string, error) {
Expand All @@ -16,3 +19,9 @@ func ValidatePWD(hashedPwd, plainPwd string) bool {
err := bcrypt.CompareHashAndPassword(byteHash, []byte(plainPwd))
return err == nil
}

func GenerateUUID() string {
// 使用标准库中的 uuid 包生成 UUID
id := uuid.New()
return id.String()
}
9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fcd0eb1

Please sign in to comment.