Skip to content

Commit fcd0eb1

Browse files
committedMay 30, 2023
临时增加 dump 完整请求包
1 parent f4ec81c commit fcd0eb1

28 files changed

+1032
-9
lines changed
 

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.17
44

55
require (
66
github.com/go-redis/redis/v8 v8.11.3
7+
github.com/google/uuid v1.1.2
78
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
89
github.com/olivere/elastic v6.2.37+incompatible
910
github.com/olivere/elastic/v7 v7.0.29

‎go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
7171
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7272
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
7373
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
74+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
7475
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7576
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
7677
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=

‎internal/plugin/dump/determine.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dump
33
import (
44
"bytes"
55
"goblin/pkg/utils"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"strconv"
99
"strings"
@@ -58,17 +58,38 @@ func (dump *Dump) Determine(maxContentLength int, response *http.Response) (dete
5858
return true, dump.Notice
5959
}
6060

61-
body, err := ioutil.ReadAll(response.Body)
61+
body, err := io.ReadAll(response.Body)
6262
if err != nil {
6363
log.Info("%s", err.Error())
6464
return false, dump.Notice
6565
}
6666
if bytes.Contains(body, []byte(dump.Response.Body)) {
6767
return true, dump.Notice
6868
}
69-
response.Body = ioutil.NopCloser(bytes.NewReader(body))
69+
response.Body = io.NopCloser(bytes.NewReader(body))
7070
}
7171
}
7272

7373
return false, dump.Notice
7474
}
75+
76+
func (dump *Dump) NeedCache(r *http.Request) bool {
77+
start := time.Now()
78+
defer log.Info("[time] url: %s, dump cache hand time: %v", r.RequestURI, time.Since(start))
79+
if dump == nil {
80+
return false
81+
}
82+
83+
// 如何没有任何请求方式支持直接返回
84+
if len(dump.Request.Method) == 0 {
85+
return false
86+
}
87+
// 判断 method 是否符合 dump 规则
88+
if utils.EleInArray(r.Method, dump.Request.Method) {
89+
//为 nil 不匹配 Response
90+
return true
91+
92+
}
93+
94+
return false
95+
}

‎internal/plugin/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type BasePlugin struct {
2626
UseBody bool `yaml:"-"` //响应body解包一次
2727
}
2828

29-
//Rule 规则结构体
29+
// Rule 规则结构体
3030
type Rule struct {
3131
URL string
3232
Match string `yaml:"Match"`

‎internal/reverse/response.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ func (reverse *Reverse) ModifyResponse(shost string) func(response *http.Respons
2626
response.Header.Del("X-XSS-Protection")
2727
//https://stackoverflow.com/questions/27358966/how-to-set-x-frame-options-on-iframe
2828
response.Header.Del("X-Frame-Options")
29-
29+
3030
response.Header.Del("Content-Security-Policy-Report-Only")
3131

3232
// 删除缓存策略
3333
response.Header.Del("Expires")
3434
response.Header.Del("Last-Modified")
3535
response.Header.Del("Date")
36-
36+
3737
if response.Header.Get("Access-Control-Allow-Origin") != "" {
3838
//https://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains
3939
if response.Request.Header.Get("Origin") != "" {
@@ -101,7 +101,14 @@ func (reverse *Reverse) ModifyResponse(shost string) func(response *http.Respons
101101
dete, msg := dp.Determine(reverse.MaxContentLength, response)
102102
start := time.Now()
103103
if dete {
104-
dplog := dumpJson(response.Request)
104+
uid := strings.Join(response.Request.Header["X-Request-ID"], "")
105+
dplog, isCache := cache.DumpCache.Get(uid)
106+
cache.DumpCache.Delete(uid)
107+
if !isCache {
108+
log.Warn("[Plugin:%s.%s]not cache : %s\n", rules.Name, rule.URL, dplog)
109+
dplog = dumpJson(response.Request)
110+
}
111+
105112
logging.AccLogger.WithFields(logrus.Fields{
106113
"method": response.Request.Method,
107114
"url": response.Request.URL.RequestURI(),

‎internal/reverse/reverse.go

+11
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,22 @@ func (reverse *Reverse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5252
"type": "clientReq",
5353
}).Warn(reqraw)
5454
}
55+
5556
log.Info("[c->p] host: %s,RemoteAddr: %s,URI: %s", host, GetClientIP(r), r.RequestURI)
5657
//response
5758
//插件系统 rule 处理响应数据
5859
if rules, ok := plugin.Plugins[host]; ok {
60+
//dump
61+
5962
for _, rule := range rules.Rule {
63+
for _, dp := range rule.Dump {
64+
if dp.NeedCache(r) {
65+
uuidstr := utils.GenerateUUID()
66+
r.Header["X-Request-ID"] = []string{uuidstr}
67+
cache.DumpCache.Set(uuidstr, dumpJson(r), 60*time.Second)
68+
}
69+
}
70+
6071
urlmatch := false
6172
// url 匹配规则
6273
switch strings.ToLower(rule.Match) {

‎pkg/cache/cache.go

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cache
33
import (
44
"context"
55
"fmt"
6+
cacheNew "github.com/patrickmn/go-cache"
67
"time"
78

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

1718
var GlobalCache *Cache
19+
var DumpCache *cacheNew.Cache
1820

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

3740
func (cache *Cache) Set(key string, v interface{}) {
@@ -72,3 +75,21 @@ func (cache *Cache) Get(key string) (interface{}, error) {
7275
}
7376
return nil, fmt.Errorf("no cache type")
7477
}
78+
79+
func (cache *Cache) GetOnce(key string) (interface{}, error) {
80+
switch cache.Type {
81+
case "self":
82+
if val, hasKey := cache.Self.Get(key); hasKey {
83+
cache.Self.Delete(key)
84+
return val, nil
85+
}
86+
return nil, fmt.Errorf("no cache")
87+
case "redis":
88+
result, err := cache.Redis.Get(ctx, key).Result()
89+
cache.Redis.Del(ctx, key).Result()
90+
return result, err
91+
case "none":
92+
return nil, fmt.Errorf("no cache")
93+
}
94+
return nil, fmt.Errorf("no cache type")
95+
}

‎pkg/ipinfo/qqwry/download.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Package qqwry
2-
//nolint
2+
// nolint
33
package qqwry
44

55
import (

‎pkg/utils/password.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package utils
22

3-
import "golang.org/x/crypto/bcrypt"
3+
import (
4+
"github.com/google/uuid"
5+
"golang.org/x/crypto/bcrypt"
6+
)
47

58
// HashAndSalt 加密密码
69
func HashAndSalt(pwd string) (string, error) {
@@ -16,3 +19,9 @@ func ValidatePWD(hashedPwd, plainPwd string) bool {
1619
err := bcrypt.CompareHashAndPassword(byteHash, []byte(plainPwd))
1720
return err == nil
1821
}
22+
23+
func GenerateUUID() string {
24+
// 使用标准库中的 uuid 包生成 UUID
25+
id := uuid.New()
26+
return id.String()
27+
}

‎vendor/github.com/google/uuid/.travis.yml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/CONTRIBUTING.md

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/CONTRIBUTORS

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/LICENSE

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/README.md

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/dce.go

+80
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/doc.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/hash.go

+53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/marshal.go

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/node.go

+90
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/node_js.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/node_net.go

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/sql.go

+59
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/time.go

+123
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/util.go

+43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/uuid.go

+245
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/version1.go

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/google/uuid/version4.go

+43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/modules.txt

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ github.com/go-redis/redis/v8/internal/pool
2020
github.com/go-redis/redis/v8/internal/proto
2121
github.com/go-redis/redis/v8/internal/rand
2222
github.com/go-redis/redis/v8/internal/util
23+
# github.com/google/uuid v1.1.2
24+
## explicit
25+
github.com/google/uuid
2326
# github.com/jonboulle/clockwork v0.2.2
2427
## explicit; go 1.13
2528
# github.com/josharian/intern v1.0.0

0 commit comments

Comments
 (0)
Please sign in to comment.