core

package
v0.0.0-...-e477a49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2024 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Open  = 1 // 开启配置
	Close = 0 // 关闭配置

	Direct = "DIRECT" // 直连
	Modify = "MOD"    // 抓包并修改
	Proxy  = "PROXY"  // 抓包代理

	// PacUrl https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file
	PacUrl = "/pac" // pac url

	HeaderProxyBench = "Proxybench"
)
View Source
const (
	PoolSize = 5
	Prefix   = "proxy"
)

Variables

View Source
var (
	Port         int
	ApiPort      int
	ReloadConf   bool
	TrackAllType bool
	Debug        bool
	BuildVersion string
	HttpMode     bool
	JsonPath     string
	PacPath      string
	ID           string
)
View Source
var (
	ProxyConfVar *ProxyConf

	ConfigReload = make(chan bool, 1)

	// DirectContentType https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
	// https://tool.oschina.net/commons/_contenttype.dea
	DirectContentType = []string{"javascript", "css", "image/", "pdf", "msword", "octet-stream", "audio", "video"}
	StaticUrlPattern  = ".*((js)|(css)|(html))$"
)
View Source
var (
	Conn           *redis.Client
	Leveldb        *leveldb.DB
	RequestList    = "" // ZSet redis key (member: 03-16 18:27:45.653 80b85e3c653, score: request nanoTime), leveldb key (80b85e3c653)
	RequestUrlList = "" // Hash: id <-> url

)
View Source
var HelpInfo = ctool.HelpInfo{
	Description:  "Http proxy for reroute and trace",
	BuildVersion: BuildVersion,
	Version:      "1.0.4",
	Flags: []ctool.ParamVO{
		{Short: "-r", BoolVar: &ReloadConf, Comment: "auto reload changed config"},
		{Short: "-d", BoolVar: &Debug, Comment: "debug mode"},
		{Short: "-x", BoolVar: &HttpMode, Comment: "track or modify http, capture https. (default https mode, need install cert)"},
		{Short: "-A", BoolVar: &TrackAllType, Comment: "track all request default track json type request"},
	},
	Options: []ctool.ParamVO{
		{Short: "-p", IntVar: &Port, Int: 1234, Value: "port", Comment: "proxy port"},
		{Short: "-w", IntVar: &ApiPort, Int: 1235, Value: "port", Comment: "web api port"},
		{Short: "-j", StringVar: &JsonPath, String: "", Value: "path", Comment: "json config file abs path"},
		{Short: "-a", StringVar: &PacPath, String: "", Value: "path", Comment: "pac file abs path"},
		{Short: "-i", StringVar: &ID, String: "", Value: "id", Comment: "param id over json id"},
	},
}
View Source
var (
	PacFilePath = "/.dev-proxy/dev-proxy.pac"
)

Functions

func CloseConnection

func CloseConnection()

func ConvertToDbKey

func ConvertToDbKey(key string) string

func CopyFile

func CopyFile(src, dst string) (int64, error)

func CopyResponseHeader

func CopyResponseHeader(w http.ResponseWriter, res *http.Response)

func ExecCommand

func ExecCommand(command string) (string, bool)

func FillReqLogResponse

func FillReqLogResponse(reqLog *ReqLog[Message], res *http.Response)

func FilterFormType

func FilterFormType(s []byte) []byte

request body : start with "------"

func FindReplaceByRegexp

func FindReplaceByRegexp(proxyReq http.Request) (*url.URL, string)

func FmtDuration

func FmtDuration(d time.Duration) string

func Go

func Go(act func())

func HandleCompressed

func HandleCompressed(msg *Message, encoding string)

func HandleRespError

func HandleRespError(w http.ResponseWriter, r *http.Request, err error, reqLog *ReqLog[Message], proxyLog string, waste int64)

func HttpProxy

func HttpProxy()

HttpProxy HTTP代理和修改 HTTPS转发

func InitConfig

func InitConfig()

func InitConnection

func InitConnection()

func InitRedisConn

func InitRedisConn(redisConf *RedisConf)

func IsJsonResponse

func IsJsonResponse(header http.Header) bool

func IsMatch

func IsMatch(pattern, fullUrl string) bool

func IsValidConnection

func IsValidConnection(client *redis.Client) bool

func Json

func Json[T any](serviceFunc func(request *http.Request) T) http.HandlerFunc

func MarkGuiMode

func MarkGuiMode()

func MatchNeedStorage

func MatchNeedStorage(proxyReq http.Request, proxyType string) bool

func ProxyHandler

func ProxyHandler(w http.ResponseWriter, r *http.Request)

func ReloadConfByCacheObj

func ReloadConfByCacheObj()

func RemoveReqMember

func RemoveReqMember(member any)

func RemoveReqUrlKey

func RemoveReqUrlKey(key string)

func RspStr

func RspStr(writer http.ResponseWriter, val string)

func SaveAs

func SaveAs(src, suffix string, newContent []byte) error

func SaveReqLog

func SaveReqLog(log *ReqLog[Message])

func StartAndCloseHook

func StartAndCloseHook(server *http.Server, fns ...func() error)

func StartLog

func StartLog(mode string)

func StoreByMemory

func StoreByMemory(conf *ProxyConf)

func TrySaveLog

func TrySaveLog(reqLog *ReqLog[Message], res *http.Response)

TrySaveLog 尝试保存,忽略静态资源及无类型标记的接口

func Watch

func Watch(fns ...func() error)

Watch 监听进程收到的信号量

func WriteJsonError

func WriteJsonError(writer http.ResponseWriter, code int, msg string)

func WriteJsonParamError

func WriteJsonParamError(writer http.ResponseWriter, msg string)

func WriteJsonRsp

func WriteJsonRsp(writer http.ResponseWriter, val any)

Types

type Message

type Message struct {
	Header http.Header `json:"header"`
	Body   []byte      `json:"body"`
}

Message storage in leveldb

type MessageVO

type MessageVO struct {
	Header  http.Header `json:"header"`
	Body    any         `json:"body"`
	BodyStr *string     `json:"bodyStr,omitempty"`
}

MessageVO use in rest api

type ProxyConf

type ProxyConf struct {
	Id          string        `json:"id"`
	Redis       *RedisConf    `json:"redis"`
	Groups      []*ProxyGroup `json:"groups"`
	ProxySelf   *ProxySelf    `json:"proxy"`  // 抓包
	ProxyDirect *ProxySelf    `json:"direct"` // 不抓包,不存储
}

type ProxyGroup

type ProxyGroup struct {
	Name      string        `json:"name"`
	ProxyType int           `json:"proxy_type"`
	Routers   []ProxyRouter `json:"routers"`
}

func (*ProxyGroup) GetName

func (p *ProxyGroup) GetName() string

func (*ProxyGroup) HasUse

func (g *ProxyGroup) HasUse() bool

func (*ProxyGroup) SwitchUse

func (g *ProxyGroup) SwitchUse()

type ProxyRouter

type ProxyRouter struct {
	Src       string `json:"src"`
	Dst       string `json:"dst"`
	ProxyType int    `json:"proxy_type"`
}

type ProxySelf

type ProxySelf struct {
	Name      string   `json:"name"`
	ProxyType int      `json:"proxy_type"`
	Paths     []string `json:"paths"`
}

func (*ProxySelf) GetName

func (p *ProxySelf) GetName() string

func (*ProxySelf) HasUse

func (g *ProxySelf) HasUse() bool

func (*ProxySelf) SwitchUse

func (g *ProxySelf) SwitchUse()

type ProxySwitch

type ProxySwitch interface {
	HasUse() bool
	SwitchUse()
	GetName() string
}

type RedisConf

type RedisConf struct {
	Addr     string `json:"addr"`
	Password string `json:"password"`
	DB       int    `json:"db"`
	PoolSize int    `json:"pool_size"`
}

type ReqLog

type ReqLog[T any] struct {
	Id          string    `json:"id"`
	CacheId     string    `json:"cacheId"`
	Method      string    `json:"method"`
	Url         string    `json:"url"`
	Status      string    `json:"status"`
	StatusCode  int       `json:"statusCode"`
	ReqTime     time.Time `json:"reqTime"`
	ResTime     time.Time `json:"resTime"`
	ElapsedTime string    `json:"useTime"`
	Request     T         `json:"request"`
	Response    T         `json:"response"`
}

func ConvertLog

func ConvertLog(v *ReqLog[Message]) *ReqLog[MessageVO]

func GetDetailByKey

func GetDetailByKey(key string) *ReqLog[Message]

func MatchDetailByKeyAndKwd

func MatchDetailByKeyAndKwd(key, kwd string) *ReqLog[Message]

MatchDetailByKeyAndKwd key: redis key kwd: 搜索关键字 (url 以及header等所有字符串)

func QueryLogDetail

func QueryLogDetail(keyList []string) []*ReqLog[Message]

func RewriteRequestAndBuildLog

func RewriteRequestAndBuildLog(newUrl *url.URL, proxyReq *http.Request, needStorage bool) (string, *ReqLog[Message])

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL