From 9b5e4167250ba3cd3c46fb6806b6b11b995a7e0a Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 3 Sep 2023 02:55:54 +0800 Subject: [PATCH] feat: handle gzip/bzip2/xz archive --- go.mod | 4 +- go.sum | 23 +++++++-- next/assets/demo.go | 110 +++++++++++++++++++++++--------------------- 3 files changed, 78 insertions(+), 59 deletions(-) diff --git a/go.mod b/go.mod index cdc74a0..dd03c6e 100644 --- a/go.mod +++ b/go.mod @@ -9,13 +9,13 @@ require ( github.com/klauspost/compress v1.16.7 github.com/robfig/cron v1.2.0 github.com/sirupsen/logrus v1.9.3 + github.com/ulikunitz/xz v0.5.11 go.uber.org/zap v1.25.0 gopkg.in/yaml.v3 v3.0.1 ) require ( - github.com/gookit/color v1.5.4 // indirect - github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect + github.com/stretchr/testify v1.8.4 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/sys v0.10.0 // indirect ) diff --git a/go.sum b/go.sum index 7a8abcf..3268f8a 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,39 @@ +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= +github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a h1:v6zMvHuY9yue4+QkG/HQ/W67wvtQmWJ4SDo9aK/GIno= github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a/go.mod h1:I79BieaU4fxrw4LMXby6q5OS9XnoR9UIKLOzDFjUmuw= -github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= -github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= -github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8= +github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/next/assets/demo.go b/next/assets/demo.go index 09fa00f..a72a416 100644 --- a/next/assets/demo.go +++ b/next/assets/demo.go @@ -1,79 +1,85 @@ package assets import ( + . "XProxy/next/logger" + "bytes" + "compress/bzip2" "compress/gzip" "fmt" + "github.com/ulikunitz/xz" "io" "os" ) -func gzipExtract(reader io.Reader) ([]byte, error) { - gzipReader, err := gzip.NewReader(reader) +const gzSample = "/root/XProxy/LICENSE.gz" +const xzSample = "/root/XProxy/LICENSE.xz" +const bz2Sample = "/root/XProxy/LICENSE.bz2" + +func gzipExtract(content io.Reader) ([]byte, error) { + Logger.Debugf("Start extracting gzip archive") + reader, err := gzip.NewReader(content) if err != nil { - fmt.Println("gzip content error") + Logger.Errorf("Failed to extract gzip archive -> %v", err) + return nil, err } - // - defer gzipReader.Close() + defer reader.Close() - //var buffer bytes.Buffer - //_, err = io.Copy() - var buffer []byte - buffer, err = io.ReadAll(gzipReader) - fmt.Println(len(buffer)) + var buffer bytes.Buffer + size, err := io.Copy(&buffer, reader) + if err != nil { + Logger.Errorf("Failed to handle gzip archive -> %v", err) + return nil, err + } + Logger.Debugf("Successfully extracted gzip archive -> %d bytes", size) + return buffer.Bytes(), nil +} - //fmt.Println(buffer) - //fmt.Println(string(buffer)) +func bzip2Extract(content io.Reader) ([]byte, error) { + Logger.Debugf("Start extracting bzip2 archive") + reader := bzip2.NewReader(content) - //gzipFile, err := os.Open("") - //if err != nil { - // return nil, nil - //} - //defer gzipFile.Close() - //gzipReader, err := gzip.NewReader(gzipFile) - //if err != nil { - // return nil, nil - //} - //defer gzipReader.Close() - //var buf bytes.Buffer - //_, err = io.Copy(&buf, gzipReader) - //if err != nil { - // return nil, err - //} - //return buf.Bytes(), nil + var buffer bytes.Buffer + size, err := io.Copy(&buffer, reader) + if err != nil { + Logger.Errorf("Failed to extract bzip2 archive -> %v", err) + return nil, err + } + Logger.Debugf("Successfully extracted bzip2 archive -> %d bytes", size) + return buffer.Bytes(), nil +} - return nil, nil +func xzExtract(content io.Reader) ([]byte, error) { + Logger.Debugf("Start extracting xz archive") + reader, err := xz.NewReader(content) + if err != nil { + Logger.Errorf("Failed to extract xz archive -> %v", err) + return nil, err + } + + var buffer bytes.Buffer + size, err := io.Copy(&buffer, reader) + if err != nil { + Logger.Errorf("Failed to handle xz archive -> %v", err) + return nil, err + } + Logger.Debugf("Successfully extracted xz archive -> %d bytes", size) + return buffer.Bytes(), nil } func Demo() { - fmt.Println("assets demo") - - path := "/root/XProxy/LICENSE.gz" + Logger.Infof("Assets demo begin") - fp, err := os.Open(path) + //fp, err := os.Open(gzSample) + //fp, err := os.Open(bz2Sample) + fp, err := os.Open(xzSample) if err != nil { fmt.Println("open failed") } defer fp.Close() - //gzipDemo(fp) - - //fmt.Printf("name -> %s\n", fp.Name()) - - //var buffer []byte - //n, err := fp.Read(buffer) - //fmt.Println(n) - //fmt.Println(err) - //buffer, err := io.ReadAll(fp) - - //buffer, err := os.ReadFile(path) - //if err != nil { - // fmt.Printf("error -> %s\n", err) - //} - //fmt.Printf("buffer size -> %d\n", len(buffer)) - //fmt.Printf("buffer -> %b\n", buffer) - - //gzipExtract(buffer) - gzipExtract(fp) + //gzipExtract(fp) + //bzip2Extract(fp) + xzExtract(fp) //fp.Name()