mirror of https://github.com/dnomd343/XProxy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.8 KiB
107 lines
2.8 KiB
1 year ago
|
package assets
|
||
|
|
||
|
import (
|
||
9 months ago
|
"XProxy/logger"
|
||
1 year ago
|
"bytes"
|
||
|
"compress/bzip2"
|
||
|
"github.com/gabriel-vasile/mimetype"
|
||
|
"github.com/klauspost/compress/gzip"
|
||
|
"github.com/ulikunitz/xz"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
notArchive = iota
|
||
|
gzipArchive
|
||
|
bzip2Archive
|
||
|
xzArchive
|
||
|
)
|
||
|
|
||
|
// gzipExtract use to extract independent gzip archive data.
|
||
|
func gzipExtract(data []byte) ([]byte, error) {
|
||
1 year ago
|
logger.Debugf("Start extracting gzip archive -> %d bytes", len(data))
|
||
1 year ago
|
reader, err := gzip.NewReader(bytes.NewReader(data))
|
||
|
if err != nil {
|
||
1 year ago
|
logger.Errorf("Failed to extract gzip archive -> %v", err)
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
|
defer reader.Close()
|
||
|
|
||
|
var buffer bytes.Buffer
|
||
1 year ago
|
size, err := reader.WriteTo(&buffer)
|
||
1 year ago
|
if err != nil {
|
||
1 year ago
|
logger.Errorf("Failed to handle gzip archive -> %v", err)
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
1 year ago
|
logger.Debugf("Extracted gzip archive successfully -> %d bytes", size)
|
||
1 year ago
|
return buffer.Bytes(), nil
|
||
|
}
|
||
|
|
||
|
// bzip2Extract use to extract independent bzip2 archive data.
|
||
|
func bzip2Extract(data []byte) ([]byte, error) {
|
||
1 year ago
|
logger.Debugf("Start extracting bzip2 archive -> %d bytes", len(data))
|
||
1 year ago
|
reader := bzip2.NewReader(bytes.NewReader(data))
|
||
|
|
||
|
var buffer bytes.Buffer
|
||
|
size, err := io.Copy(&buffer, reader)
|
||
|
if err != nil {
|
||
1 year ago
|
logger.Errorf("Failed to extract bzip2 archive -> %v", err)
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
1 year ago
|
logger.Debugf("Extracted bzip2 archive successfully -> %d bytes", size)
|
||
1 year ago
|
return buffer.Bytes(), nil
|
||
|
}
|
||
|
|
||
|
// xzExtract use to extract independent xz archive data.
|
||
|
func xzExtract(data []byte) ([]byte, error) {
|
||
1 year ago
|
logger.Debugf("Start extracting xz archive -> %d bytes", len(data))
|
||
1 year ago
|
reader, err := xz.NewReader(bytes.NewReader(data))
|
||
|
if err != nil {
|
||
1 year ago
|
logger.Errorf("Failed to extract xz archive -> %v", err)
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var buffer bytes.Buffer
|
||
|
size, err := io.Copy(&buffer, reader)
|
||
|
if err != nil {
|
||
1 year ago
|
logger.Errorf("Failed to handle xz archive -> %v", err)
|
||
1 year ago
|
return nil, err
|
||
|
}
|
||
1 year ago
|
logger.Debugf("Extracted xz archive successfully -> %d bytes", size)
|
||
1 year ago
|
return buffer.Bytes(), nil
|
||
|
}
|
||
|
|
||
|
// archiveType use to determine the type of archive file.
|
||
|
func archiveType(data []byte) uint {
|
||
|
mime := mimetype.Detect(data)
|
||
|
switch mime.String() {
|
||
|
case "application/gzip":
|
||
1 year ago
|
logger.Debugf("Data detected as gzip format")
|
||
1 year ago
|
return gzipArchive
|
||
|
case "application/x-bzip2":
|
||
1 year ago
|
logger.Debugf("Data detected as bzip2 format")
|
||
1 year ago
|
return bzip2Archive
|
||
|
case "application/x-xz":
|
||
1 year ago
|
logger.Debugf("Data detected as xz format")
|
||
1 year ago
|
return xzArchive
|
||
|
default:
|
||
1 year ago
|
logger.Debugf("Data detected as non-archive format -> `%s`", mime)
|
||
1 year ago
|
return notArchive
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
// tryExtract will try to extract the data as a compressed format, and will
|
||
1 year ago
|
// return the original data if it cannot be determined.
|
||
1 year ago
|
func tryExtract(data []byte) ([]byte, error) {
|
||
1 year ago
|
switch archiveType(data) {
|
||
|
case gzipArchive:
|
||
|
return gzipExtract(data)
|
||
|
case bzip2Archive:
|
||
|
return bzip2Extract(data)
|
||
|
case xzArchive:
|
||
|
return xzExtract(data)
|
||
|
default:
|
||
|
return data, nil
|
||
|
}
|
||
|
}
|