Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions internal/biz/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,33 @@ func (b *batch) Start(ctx context.Context, fn saveFile) (res []Cse, err error) {
return
}

// Cse combines a string result and an error.
// Cse combines a string result, file size, and an error.
type Cse struct {
S string
E error
S string
Size int64
E error
}

// FileResult holds path and size for a successfully fetched file.
type FileResult struct {
Path string `json:"path"`
Size int64 `json:"size"`
}

func (b *batch) run(ctx context.Context, task urlTask, ch chan Cse, wg *sync.WaitGroup, fn saveFile) {
defer wg.Done()
data, contentType, err := curlGet(ctx, task.url, task.timeout)
if err != nil {
ch <- Cse{"", err}
ch <- Cse{"", 0, err}
return
}
size := int64(len(data))
relativePath, err := fn(task.url, contentType, data)
if err != nil {
ch <- Cse{"", err}
ch <- Cse{"", 0, err}
return
}
ch <- Cse{relativePath, nil}
ch <- Cse{relativePath, size, nil}
}

func (uc *Usecase) DealRun(ctx context.Context, p BatchParams) (res []Cse, err error) {
Expand Down
10 changes: 6 additions & 4 deletions internal/biz/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (

const curlTimeout = 300 // 5 minutes

func (uc *Usecase) DealRun1(ctx context.Context, uri string) (relativePath string, err error) {
func (uc *Usecase) DealRun1(ctx context.Context, uri string) (relativePath string, size int64, err error) {
uri, err = url.QueryUnescape(uri)
if err != nil {
return
}
relativePath, err = uc.curlOne(ctx, uri)
relativePath, size, err = uc.curlOne(ctx, uri)
return
}
func validateURL(uri string, denyPrivateIP bool) error {
Expand All @@ -38,15 +38,17 @@ func validateURL(uri string, denyPrivateIP bool) error {
return nil
}

func (uc *Usecase) curlOne(ctx context.Context, uri string) (relativePath string, err error) {
func (uc *Usecase) curlOne(ctx context.Context, uri string) (relativePath string, size int64, err error) {
if err = validateURL(uri, uc.denyPrivateIP); err != nil {
return
}
data, contentType, err := curlGet(ctx, uri, curlTimeout)
if err != nil {
return
}
return uc.repo.CreateFile(uri, contentType, data)
size = int64(len(data))
relativePath, err = uc.repo.CreateFile(uri, contentType, data)
return
}

func curlGet(ctx context.Context, uri string, timeout int) (data []byte, contentType string, err error) {
Expand Down
48 changes: 48 additions & 0 deletions internal/biz/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package biz

import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
Expand All @@ -27,6 +28,7 @@ type replaceBody func(body string, currPath string) string
type Uri struct {
Path string
Dir string
Base string
Query string
Route string
ProxyBasePath string
Expand Down Expand Up @@ -71,3 +73,49 @@ func (uc *Usecase) Proxy(u Uri, rw http.ResponseWriter, req *http.Request) (err
proxy.ServeHTTP(rw, req)
return nil
}

func (uc *Usecase) ProxyDiff(u Uri, rw http.ResponseWriter, req *http.Request) (err error) {
baseBytes, err := base64.StdEncoding.DecodeString(u.Base)
if err != nil {
return
}
compareBytes, err := base64.StdEncoding.DecodeString(u.Dir)
if err != nil {
return
}
usePort, err := uc.repo.GetPortByDiff(string(baseBytes), string(compareBytes))
if err != nil {
return
}
currPath := fmt.Sprintf(u.Route, u.Base, u.Dir)
path := strings.Replace(u.Path, currPath, "", 1)
urlX := fmt.Sprintf("http://127.0.0.1:%d%s%s?%s", usePort, u.ProxyBasePath, path, u.Query)
uc.log.Debugf("call:%s", urlX)
urlT, err := url.Parse(urlX)
if err != nil {
return
}
director := func(req *http.Request) {
req.URL = urlT
req.Host = urlT.Host
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ModifyResponse = func(res *http.Response) error {
if res.StatusCode == 200 {
oldPayload, err := io.ReadAll(res.Body)
if err != nil {
return err
}
newPayLoadStr := string(oldPayload)
newPayLoadStr = u.ReBody(newPayLoadStr, currPath)
newPayLoad := []byte(newPayLoadStr)
res.Body = io.NopCloser(bytes.NewBuffer(newPayLoad))
res.ContentLength = int64(len(newPayLoad))
res.Header.Set("Content-Length", fmt.Sprint(len(newPayLoad)))
return nil
}
return nil
}
proxy.ServeHTTP(rw, req)
return nil
}
17 changes: 16 additions & 1 deletion internal/biz/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ package biz

import "github.com/go-kratos/kratos/v2/log"

// FileInfo holds name and size for a file in the storage directory.
type FileInfo struct {
Name string
Size int64
}

// BatchGroup holds files from one collection batch under a date directory.
type BatchGroup struct {
Label string // e.g. "23_39_42"
Dir string // relative path, e.g. "/20260415/23_39_42"
Files []FileInfo
}

type Repo interface {
GetPortByDir(relPath string) (port int, err error)
GetPortByDiff(baseFile, compareFile string) (port int, err error)
GetAbsDir(relPath string) string
GetFileList(date string) (list, files []string, err error)
GetFileList(date string) (list []string, files []FileInfo, err error)
GetBatchGroups(date string) ([]BatchGroup, error)
GetFileType(dir string) string
CreateFile(url string, contentType string, data []byte) (relativePath string, err error)
CreateFileByUpload(fileName string, data []byte) (relativePath string, err error)
Expand Down
5 changes: 4 additions & 1 deletion internal/biz/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ func (uc *Usecase) GetAbsDir(s string) string {
return uc.repo.GetAbsDir(s)
}

func (uc *Usecase) GetFileList(date string) (list, files []string, err error) {
func (uc *Usecase) GetFileList(date string) (list []string, files []FileInfo, err error) {
return uc.repo.GetFileList(date)
}
func (uc *Usecase) GetBatchGroups(date string) ([]BatchGroup, error) {
return uc.repo.GetBatchGroups(date)
}
func (uc *Usecase) GetFileType(dir string) string {
return uc.repo.GetFileType(dir)
}
14 changes: 11 additions & 3 deletions internal/conf/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ type App struct {
DeltaSeconds int `yaml:"DeltaSeconds"`
TraceSeconds int `yaml:"TraceSeconds"`
}
type PluginsConfig struct {
I18nZh bool `yaml:"i18n_zh"`
SourceFold bool `yaml:"source_fold"`
PeekFold bool `yaml:"peek_fold"`
GraphExplain bool `yaml:"graph_explain"`
}

type Bs struct {
Server *Server `yaml:"Server"`
Data *Data `yaml:"Data"`
App *App `yaml:"App"`
Server *Server `yaml:"Server"`
Data *Data `yaml:"Data"`
App *App `yaml:"App"`
Plugins *PluginsConfig `yaml:"Plugins"`
}
3 changes: 2 additions & 1 deletion internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ func newTask(logger log.Logger) *task {
go t.Timer()
return t
}
func newProxy(dir string, l *log.Helper) *Proxy {
func newProxy(dir string, baseFile string, l *log.Helper) *Proxy {
return &Proxy{
dir: dir,
baseFile: baseFile,
fileType: getFileType(dir),
lifeTime: proxyLifetime,
log: l,
Expand Down
20 changes: 12 additions & 8 deletions internal/data/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package data

import (
"fmt"
"github.com/go-kratos/kratos/v2/log"
"github.com/matteo-gz/prof/pkg/filex"
"github.com/matteo-gz/prof/pkg/pproftype"
"os"
"path"
"sort"
"strings"

"github.com/go-kratos/kratos/v2/log"
"github.com/matteo-gz/prof/internal/biz"
"github.com/matteo-gz/prof/pkg/filex"
"github.com/matteo-gz/prof/pkg/pproftype"
)

type file struct {
Expand Down Expand Up @@ -49,7 +52,7 @@ func getFileType(dir string) (fileType string) {
return pproftype.ExtUnknown
}
}
func (f *file) getFileList(date string) (list, files []string, err error) {
func (f *file) getFileList(date string) (list []string, files []biz.FileInfo, err error) {
final := f.getAbsDir(date)
f1, err := os.OpenFile(final, os.O_RDONLY, os.ModeDir)
if err != nil {
Expand All @@ -60,12 +63,13 @@ func (f *file) getFileList(date string) (list, files []string, err error) {
if err != nil {
return
}
for _, fileInfo := range info {
if fileInfo.IsDir() {
list = append(list, fileInfo.Name())
for _, fi := range info {
if fi.IsDir() {
list = append(list, fi.Name())
} else {
files = append(files, fileInfo.Name())
files = append(files, biz.FileInfo{Name: fi.Name(), Size: fi.Size()})
}
}
sort.Sort(sort.Reverse(sort.StringSlice(list)))
return list, files, nil
}
2 changes: 2 additions & 0 deletions internal/data/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Proxy struct {
useTime int64
Cmd *exec.Cmd
dir string
baseFile string
lifeTime int64
fileType string
log *log.Helper
Expand Down Expand Up @@ -120,6 +121,7 @@ func (p *Proxy) start(port int) (err error) {
tools := tool.NewTool(p.fileType)
tools.Arg().SetPort(port)
tools.Arg().SetFile(p.dir)
tools.Arg().SetBaseFile(p.baseFile)
cmdStr := tools.Command()
p.log.Debug(cmdStr)
p.Cmd = process.Shell(cmdStr)
Expand Down
34 changes: 33 additions & 1 deletion internal/data/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,43 @@ func (rp *repo) GetPortByDir(relPath string) (port int, err error) {
}
return rp.data.task.getPortByDir(dir)
}

func (rp *repo) GetBatchGroups(date string) (groups []biz.BatchGroup, err error) {
dirs, _, err := rp.GetFileList(date)
if err != nil {
return
}
for _, d := range dirs {
subDir := date + "/" + d
_, subFiles, err2 := rp.GetFileList(subDir)
if err2 != nil || len(subFiles) == 0 {
continue
}
groups = append(groups, biz.BatchGroup{
Label: d,
Dir: subDir,
Files: subFiles,
})
}
return
}

func (rp *repo) GetPortByDiff(baseFile, compareFile string) (port int, err error) {
base := rp.data.file.getAbsDir(baseFile)
compare := rp.data.file.getAbsDir(compareFile)
if !filex.IsFileExist(base) {
return 0, errors.New("base file not exist")
}
if !filex.IsFileExist(compare) {
return 0, errors.New("compare file not exist")
}
return rp.data.task.getPortByDiff(base, compare)
}
func (rp *repo) GetAbsDir(relPath string) string {
return rp.data.file.getAbsDir(relPath)
}

func (rp *repo) GetFileList(date string) (list, files []string, err error) {
func (rp *repo) GetFileList(date string) (list []string, files []biz.FileInfo, err error) {
return rp.data.file.getFileList(date)
}
func (rp *repo) GetFileType(dir string) string {
Expand Down
24 changes: 23 additions & 1 deletion internal/data/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (t *task) GetProxy(dir string) (p *Proxy) {
t.listLock.Lock()
p, ok := t.list[index]
if !ok {
p = newProxy(filepath.Join(filepath.Split(dir)), t.log)
p = newProxy(filepath.Join(filepath.Split(dir)), "", t.log)
t.list[index] = p
}
t.listLock.Unlock()
Expand All @@ -88,3 +88,25 @@ func (t *task) getPortByDir(dir string) (port int, err error) {
port, err = p.GetCanUsePort()
return
}

func (t *task) getDiffIndex(base, compare string) string {
return base + "::" + compare
}

func (t *task) GetDiffProxy(base, compare string) *Proxy {
index := t.getDiffIndex(base, compare)
t.listLock.Lock()
p, ok := t.list[index]
if !ok {
p = newProxy(compare, base, t.log)
t.list[index] = p
}
t.listLock.Unlock()
p.UpdateUseTime()
return p
}

func (t *task) getPortByDiff(base, compare string) (port int, err error) {
p := t.GetDiffProxy(base, compare)
return p.GetCanUsePort()
}
6 changes: 6 additions & 0 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ func (h *HTTPServerX) router() (r *gin.Engine, err error) {
r.GET("/file", h.srv.File)
r.GET(service.RoutePprofPre+"/:dir/*any", h.srv.PprofProxy)
r.GET(service.RouteTracePre+"/:dir/*any", h.srv.TraceProxy)
r.GET(service.RoutePprofDiffPre+"/:base/:dir/*any", h.srv.PprofDiffProxy)
r.GET("/person/curl", h.srv.PersonCurl)
r.GET("/bootstrap.min.css", h.srv.Css)
r.GET("/static/*filepath", h.srv.StaticFile)
r.POST("/opt/upload", h.srv.Upload)
r.POST("/opt/run", h.srv.Run)
r.POST("/opt/run1", h.srv.Run1)
api := r.Group("/api")
{
api.GET("/plugins", h.srv.APIPlugins)
}
return
}

Expand Down
Loading
Loading