new starainrt release
parent
81efabb6ca
commit
fe167d7281
@ -0,0 +1,536 @@
|
||||
package starainrt
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/rc4"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type StarCrypto struct {
|
||||
}
|
||||
|
||||
func (this StarCrypto) SmallEncodeFileByBase64(filepath, outputname string, buffer int) bool {
|
||||
if !Exists(filepath) {
|
||||
return false
|
||||
}
|
||||
rempar := regexp.MustCompile(`(.*)\*(.*)`)
|
||||
if !rempar.Match([]byte(outputname)) {
|
||||
return false
|
||||
}
|
||||
rest := rempar.FindSubmatch([]byte(outputname))
|
||||
par1 := string(rest[1])
|
||||
par2 := string(rest[2])
|
||||
bfile, _ := ioutil.ReadFile(filepath)
|
||||
bitlen := len(bfile)
|
||||
i := 1
|
||||
var seed int = buffer
|
||||
if bitlen > seed {
|
||||
for bitlen > seed {
|
||||
coder := base64.StdEncoding.EncodeToString(bfile[seed*(i-1) : seed*i])
|
||||
ioutil.WriteFile(par1+strconv.Itoa(i)+par2, []byte(coder), 0755)
|
||||
i++
|
||||
bitlen -= seed
|
||||
}
|
||||
}
|
||||
coder := base64.StdEncoding.EncodeToString(bfile[seed*(i-1):])
|
||||
ioutil.WriteFile(par1+strconv.Itoa(i)+par2, []byte(coder), 0755)
|
||||
return true
|
||||
}
|
||||
|
||||
func (this StarCrypto) SmallDecodeFileByBase64(inputname, outputname string, filesum int) bool {
|
||||
rempar := regexp.MustCompile(`(.*)\*(.*)`)
|
||||
if !rempar.Match([]byte(inputname)) {
|
||||
return false
|
||||
}
|
||||
rest := rempar.FindSubmatch([]byte(inputname))
|
||||
par1 := string(rest[1])
|
||||
par2 := string(rest[2])
|
||||
for i := 1; i <= filesum; i++ {
|
||||
if !Exists(par1 + strconv.Itoa(i) + par2) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
i := 1
|
||||
big := filesum
|
||||
res := []byte{}
|
||||
for i <= big {
|
||||
bfile, _ := ioutil.ReadFile(par1 + strconv.Itoa(i) + par2)
|
||||
coder, _ := base64.StdEncoding.DecodeString(string(bfile))
|
||||
for _, v := range coder {
|
||||
res = append(res, v)
|
||||
}
|
||||
i++
|
||||
//time.Sleep(time.Microsecond * 560)
|
||||
}
|
||||
ioutil.WriteFile(outputname, res, 0755)
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
需要改写
|
||||
*/
|
||||
func (this StarCrypto) EncodeFileByBase64(filepath, outputname string, buffer int) bool {
|
||||
if !Exists(filepath) {
|
||||
return false
|
||||
}
|
||||
rempar := regexp.MustCompile(`(.*)\*(.*)`)
|
||||
if !rempar.Match([]byte(outputname)) {
|
||||
return false
|
||||
}
|
||||
rest := rempar.FindSubmatch([]byte(outputname))
|
||||
par1 := string(rest[1])
|
||||
par2 := string(rest[2])
|
||||
ret := this.SlitFile(filepath, outputname, 0, int64(buffer), int64(buffer)/10)
|
||||
if !ret {
|
||||
return false
|
||||
}
|
||||
i := 1
|
||||
for {
|
||||
if !Exists(par1 + strconv.Itoa(i) + par2) {
|
||||
break
|
||||
} else {
|
||||
bfile, _ := ioutil.ReadFile(par1 + strconv.Itoa(i) + par2)
|
||||
coder := base64.StdEncoding.EncodeToString(bfile)
|
||||
ioutil.WriteFile(par1+strconv.Itoa(i)+par2, []byte(coder), 0755)
|
||||
|
||||
}
|
||||
i++
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this StarCrypto) DecodeFileByBase64(inputname, outputname string, filesum int) bool {
|
||||
rempar := regexp.MustCompile(`(.*)\*(.*)`)
|
||||
if !rempar.Match([]byte(inputname)) {
|
||||
return false
|
||||
}
|
||||
rest := rempar.FindSubmatch([]byte(inputname))
|
||||
par1 := string(rest[1])
|
||||
par2 := string(rest[2])
|
||||
if filesum != 0 {
|
||||
for i := 1; i <= filesum; i++ {
|
||||
if !Exists(par1 + strconv.Itoa(i) + par2) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var i int
|
||||
for i = 1; ; i++ {
|
||||
if !Exists(par1 + strconv.Itoa(i) + par2) {
|
||||
break
|
||||
}
|
||||
}
|
||||
filesum = i - 1
|
||||
}
|
||||
i := 1
|
||||
big := filesum
|
||||
fpw, _ := os.Create(outputname)
|
||||
defer fpw.Close()
|
||||
for i <= big {
|
||||
bfile, _ := ioutil.ReadFile(par1 + strconv.Itoa(i) + par2)
|
||||
coder, _ := base64.StdEncoding.DecodeString(string(bfile))
|
||||
fpw.Write(coder)
|
||||
i++
|
||||
//time.Sleep(time.Microsecond * 560)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this StarCrypto) SlitFile(filename, outputname string, num int, databig, buffer int64) bool {
|
||||
rempar := regexp.MustCompile(`(.*)\*(.*)`)
|
||||
if !rempar.Match([]byte(outputname)) {
|
||||
return false
|
||||
}
|
||||
rest := rempar.FindSubmatch([]byte(outputname))
|
||||
par1 := string(rest[1])
|
||||
par2 := string(rest[2])
|
||||
if !Exists(filename) {
|
||||
return false
|
||||
}
|
||||
fileinfo, _ := os.Stat(filename)
|
||||
filesize := fileinfo.Size()
|
||||
var spbyte int64
|
||||
if num != 0 {
|
||||
spbyte = filesize / int64(num)
|
||||
}
|
||||
if databig != 0 {
|
||||
spbyte = databig
|
||||
num = int(filesize/spbyte + 1)
|
||||
}
|
||||
if buffer == 0 {
|
||||
buffer = spbyte / 10
|
||||
}
|
||||
if buffer > 100*1024*1024 {
|
||||
buffer = 50 * 1024 * 1024
|
||||
}
|
||||
fp, _ := os.Open(filename)
|
||||
defer fp.Close()
|
||||
data := make([]byte, buffer)
|
||||
var i int64 = 0
|
||||
var k int = 1
|
||||
fpw, _ := os.Create(par1 + strconv.Itoa(k) + par2)
|
||||
for {
|
||||
data = data[:cap(data)]
|
||||
|
||||
// read bytes to slice
|
||||
n, err := fp.Read(data)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
i += int64(n)
|
||||
data = data[:n]
|
||||
fpw.Write(data)
|
||||
if i > spbyte && k < num {
|
||||
fpw.Close()
|
||||
k++
|
||||
i = 1
|
||||
fpw, _ = os.Create(par1 + strconv.Itoa(k) + par2)
|
||||
defer fpw.Close()
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func SpliceFile(inputname, outputname string, num int, buffer int64) bool {
|
||||
rempar := regexp.MustCompile(`(.*)\*(.*)`)
|
||||
if !rempar.Match([]byte(inputname)) {
|
||||
return false
|
||||
}
|
||||
rest := rempar.FindSubmatch([]byte(inputname))
|
||||
par1 := string(rest[1])
|
||||
par2 := string(rest[2])
|
||||
if num != 0 {
|
||||
for i := 1; i <= num; i++ {
|
||||
if !Exists(par1 + strconv.Itoa(i) + par2) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var i int
|
||||
for i = 1; ; i++ {
|
||||
if !Exists(par1 + strconv.Itoa(i) + par2) {
|
||||
break
|
||||
}
|
||||
}
|
||||
num = i - 1
|
||||
}
|
||||
fpw, _ := os.Create(outputname)
|
||||
defer fpw.Close()
|
||||
fileinfo, _ := os.Stat(par1 + "1" + par2)
|
||||
filesize := fileinfo.Size()
|
||||
if buffer == 0 {
|
||||
buffer = filesize / 5
|
||||
}
|
||||
if buffer > 1024*1024*100 {
|
||||
buffer = 1024 * 1024 * 100
|
||||
}
|
||||
data := make([]byte, buffer)
|
||||
for i := 1; i <= num; i++ {
|
||||
fp, _ := os.Open(par1 + strconv.Itoa(i) + par2)
|
||||
for {
|
||||
data = data[:cap(data)]
|
||||
// read bytes to slice
|
||||
n, err := fp.Read(data)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
data = data[:n]
|
||||
fpw.Write(data)
|
||||
}
|
||||
fp.Close()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this StarCrypto) MixFile(filepath, outputname string, buffer int64, remix bool) bool {
|
||||
if !Exists(filepath) || !IsFile(filepath) {
|
||||
return false
|
||||
}
|
||||
fileinfo, _ := os.Stat(filepath)
|
||||
filesize := fileinfo.Size()
|
||||
if buffer == 0 {
|
||||
buffer = filesize / 5
|
||||
}
|
||||
if buffer > 1024*1024*100 {
|
||||
buffer = 1024 * 1024 * 100
|
||||
}
|
||||
var spbyte int64
|
||||
if !remix {
|
||||
spbyte = filesize / 2
|
||||
} else {
|
||||
spbyte = filesize - filesize/2
|
||||
}
|
||||
fpw, _ := os.Create(outputname)
|
||||
defer fpw.Close()
|
||||
fp, _ := os.Open(filepath)
|
||||
defer fp.Close()
|
||||
data := make([]byte, buffer)
|
||||
_, _ = fp.Seek(spbyte, 0)
|
||||
for {
|
||||
data = data[:cap(data)]
|
||||
|
||||
// read bytes to slice
|
||||
n, err := fp.Read(data)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
data = data[:n]
|
||||
fpw.Write(data)
|
||||
}
|
||||
_, _ = fp.Seek(0, 0)
|
||||
i := int64(0)
|
||||
for {
|
||||
data = data[:cap(data)]
|
||||
|
||||
// read bytes to slice
|
||||
n, err := fp.Read(data)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
i += int64(n)
|
||||
if i < spbyte {
|
||||
data = data[:n]
|
||||
fpw.Write(data)
|
||||
} else {
|
||||
data = data[:(int64(n) - i + spbyte)]
|
||||
fpw.Write(data)
|
||||
break
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func Md5File(filepath string) string {
|
||||
f, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
fmt.Println("Open", err)
|
||||
return ""
|
||||
}
|
||||
defer f.Close()
|
||||
md5hash := md5.New()
|
||||
if _, err := io.Copy(md5hash, f); err != nil {
|
||||
fmt.Println("Copy", err)
|
||||
return ""
|
||||
}
|
||||
result := md5hash.Sum(nil)
|
||||
return hex.EncodeToString(result)
|
||||
}
|
||||
|
||||
func Crc32File(filepath string) string {
|
||||
f, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
fmt.Println("Open", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
crchash := crc32.NewIEEE()
|
||||
if _, err := io.Copy(crchash, f); err != nil {
|
||||
fmt.Println("Copy", err)
|
||||
return ""
|
||||
}
|
||||
result := crchash.Sum(nil)
|
||||
return hex.EncodeToString(result)
|
||||
}
|
||||
|
||||
func Sha1File(filepath string) string {
|
||||
f, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
fmt.Println("Open", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
sha1hash := sha1.New()
|
||||
if _, err := io.Copy(sha1hash, f); err != nil {
|
||||
fmt.Println("Copy", err)
|
||||
return ""
|
||||
}
|
||||
result := sha1hash.Sum(nil)
|
||||
return hex.EncodeToString(result)
|
||||
}
|
||||
|
||||
func Sha256File(filepath string) string {
|
||||
f, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
fmt.Println("Open", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
sha256hash := sha256.New()
|
||||
if _, err := io.Copy(sha256hash, f); err != nil {
|
||||
fmt.Println("Copy", err)
|
||||
return ""
|
||||
}
|
||||
result := sha256hash.Sum(nil)
|
||||
return hex.EncodeToString(result)
|
||||
}
|
||||
|
||||
func Sha512File(filepath string) string {
|
||||
f, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
fmt.Println("Open", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
sha512hash := sha512.New()
|
||||
if _, err := io.Copy(sha512hash, f); err != nil {
|
||||
fmt.Println("Copy", err)
|
||||
return ""
|
||||
}
|
||||
result := sha512hash.Sum(nil)
|
||||
return hex.EncodeToString(result)
|
||||
}
|
||||
|
||||
func RC4File(filepath string, key []byte) string {
|
||||
rc4obj, _ := rc4.NewCipher(key)
|
||||
plain, _ := ioutil.ReadFile(filepath)
|
||||
res := make([]byte, len(plain))
|
||||
rc4obj.XORKeyStream(res, plain)
|
||||
return hex.EncodeToString(res)
|
||||
}
|
||||
func DetachFile(source, output1, output2 string, key int64) bool {
|
||||
if !Exists(source) {
|
||||
return false
|
||||
}
|
||||
fin, _ := os.Stat(source)
|
||||
filesize := fin.Size()
|
||||
fpw, _ := os.Create(output1)
|
||||
fp, _ := os.Open(source)
|
||||
defer fp.Close()
|
||||
buffer := filesize / 10
|
||||
if buffer > 100*1024*1024 {
|
||||
buffer = 100 * 1024 * 1024
|
||||
}
|
||||
data := make([]byte, buffer)
|
||||
i := int64(0)
|
||||
for {
|
||||
data = data[:cap(data)]
|
||||
|
||||
// read bytes to slice
|
||||
n, err := fp.Read(data)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
i += int64(n)
|
||||
if i < key {
|
||||
data = data[:n]
|
||||
fpw.Write(data)
|
||||
} else {
|
||||
data = data[:(int64(n) - i + key)]
|
||||
fpw.Write(data)
|
||||
break
|
||||
}
|
||||
}
|
||||
fpw.Close()
|
||||
_, _ = fp.Seek(key+20, 0)
|
||||
fpw, _ = os.Create(output2)
|
||||
for {
|
||||
data = data[:cap(data)]
|
||||
|
||||
// read bytes to slice
|
||||
n, err := fp.Read(data)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
data = data[:n]
|
||||
fpw.Write(data)
|
||||
}
|
||||
return true
|
||||
}
|
||||
func AttachFile(source, target, output string) (bool, string) {
|
||||
if !Exists(source) {
|
||||
return false, ""
|
||||
}
|
||||
if !Exists(target) {
|
||||
return false, ""
|
||||
}
|
||||
fin, _ := os.Stat(source)
|
||||
filesize := fin.Size()
|
||||
_ = ioutil.WriteFile(output+".ini", []byte(strconv.FormatInt(filesize, 10)), 0755)
|
||||
fpw, _ := os.Create(output)
|
||||
defer fpw.Close()
|
||||
fp, _ := os.Open(source)
|
||||
buffer := filesize / 10
|
||||
if buffer > 100*1024*1024 {
|
||||
buffer = 100 * 1024 * 1024
|
||||
}
|
||||
data := make([]byte, buffer)
|
||||
for {
|
||||
data = data[:cap(data)]
|
||||
// read bytes to slice
|
||||
n, err := fp.Read(data)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
data = data[:n]
|
||||
fpw.Write(data)
|
||||
}
|
||||
fp.Close()
|
||||
fpw.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
|
||||
fin, _ = os.Stat(target)
|
||||
filesize2 := fin.Size()
|
||||
fp, _ = os.Open(target)
|
||||
buffer = filesize2 / 10
|
||||
if buffer > 100*1024*1024 {
|
||||
buffer = 100 * 1024 * 1024
|
||||
}
|
||||
data = make([]byte, buffer)
|
||||
for {
|
||||
data = data[:cap(data)]
|
||||
// read bytes to slice
|
||||
n, err := fp.Read(data)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
data = data[:n]
|
||||
fpw.Write(data)
|
||||
}
|
||||
fp.Close()
|
||||
return true, strconv.FormatInt(filesize, 10)
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package starainrt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_FileSumAll(t *testing.T) {
|
||||
cry := new(StarCrypto)
|
||||
fmt.Println(cry.FileSumAll("D:\\Download\\macos.iso", func(pect float64) {
|
||||
fmt.Printf("已处理%f\r", pect)
|
||||
}))
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package starainrt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func TestDataBase(t *testing.T) {
|
||||
mysql := new(StarDB)
|
||||
defer mysql.Close()
|
||||
mysql.Open("sqlite3", "D:\\Ttest.db")
|
||||
mysql.Exec("CREATE TABLE IF NOT EXISTS sakura(id INT PRIMARY KEY,xihuan TEXT)")
|
||||
mysql.Exec("INSERT INTO sakura VALUES(?,?)", 2, "dssfsdfdf")
|
||||
sakura, _ := mysql.Query("SELECT * FROM sakura")
|
||||
defer sakura.Close()
|
||||
fmt.Println(sakura.Row(0).MustString("xihuan"))
|
||||
}
|
@ -0,0 +1,385 @@
|
||||
package starainrt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
urls "net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type StarCurl struct {
|
||||
TimeOut int
|
||||
DialTimeOut int
|
||||
ReqHeader http.Header
|
||||
ReqCookies []*http.Cookie
|
||||
RespHeader http.Header
|
||||
RespCookies []*http.Cookie
|
||||
RespHttpCode int
|
||||
PostBuffer *bytes.Buffer
|
||||
CircleBuffer *CircleByteBuffer
|
||||
Proxy string
|
||||
}
|
||||
|
||||
func NewStarCurl() *StarCurl {
|
||||
star := new(StarCurl)
|
||||
star.ReqHeader = make(http.Header)
|
||||
star.TimeOut = 60
|
||||
star.DialTimeOut = 15
|
||||
star.PostBuffer = nil
|
||||
return star
|
||||
}
|
||||
|
||||
func (this *StarCurl) ResetReqHeader() {
|
||||
this.ReqHeader = make(http.Header)
|
||||
}
|
||||
|
||||
func (this *StarCurl) ResetReqCookies() {
|
||||
this.ReqCookies = []*http.Cookie{}
|
||||
}
|
||||
|
||||
func (this *StarCurl) AddSimpleCookie(key, value string) {
|
||||
this.ReqCookies = append(this.ReqCookies, &http.Cookie{Name: key, Value: value})
|
||||
}
|
||||
|
||||
func randomBoundary() string {
|
||||
var buf [30]byte
|
||||
_, err := io.ReadFull(rand.Reader, buf[:])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return fmt.Sprintf("%x", buf[:])
|
||||
|
||||
}
|
||||
|
||||
func (this *StarCurl) CurlWithFile(url string, postdata map[string]string, formname, fpath, savepath string, tofile bool, shell func(float64)) (result []byte, err error) {
|
||||
fpsrc, err := os.Open(fpath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer fpsrc.Close()
|
||||
boundary := randomBoundary()
|
||||
boundarybytes := []byte("\r\n--" + boundary + "\r\n")
|
||||
endbytes := []byte("\r\n--" + boundary + "--\r\n")
|
||||
fpstat, _ := os.Stat(fpath)
|
||||
filebig := float64(fpstat.Size())
|
||||
sum, n := 0, 0
|
||||
fpdst := NewCircleByteBuffer(1048576)
|
||||
if postdata != nil {
|
||||
for k, v := range postdata {
|
||||
header := fmt.Sprintf("Content-Disposition: form-data; name=\"%s\";\r\nContent-Type: x-www-form-urlencoded \r\n\r\n", k)
|
||||
fpdst.Write(boundarybytes)
|
||||
fpdst.Write([]byte(header))
|
||||
fpdst.Write([]byte(v))
|
||||
}
|
||||
}
|
||||
header := fmt.Sprintf("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n", formname, fpstat.Name())
|
||||
fpdst.Write(boundarybytes)
|
||||
fpdst.Write([]byte(header))
|
||||
go func() {
|
||||
for {
|
||||
bufs := make([]byte, 1048576)
|
||||
n, err = fpsrc.Read(bufs)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
sum += n
|
||||
go shell(float64(sum) / filebig * 100)
|
||||
fpdst.Write(bufs[0:n])
|
||||
}
|
||||
fpdst.Write(endbytes)
|
||||
fpdst.Write(nil)
|
||||
|
||||
}()
|
||||
this.CircleBuffer = fpdst
|
||||
this.ReqHeader.Set("Content-Type", "multipart/form-data;boundary="+boundary)
|
||||
if tofile {
|
||||
err = this.CurlDataToFile(url, []byte{}, "POST", savepath, shell)
|
||||
this.ResetReqHeader()
|
||||
} else {
|
||||
result, err = this.Curl(url, []byte{}, "POST")
|
||||
}
|
||||
this.ResetReqHeader()
|
||||
return
|
||||
}
|
||||
|
||||
func (this *StarCurl) CurlWithFileByMemory(url string, postdata map[string]string, formname, fpath, savepath string, tofile bool, shell func(float64)) (result []byte, err error) {
|
||||
buf := &bytes.Buffer{}
|
||||
bufwriter := multipart.NewWriter(buf)
|
||||
if postdata != nil {
|
||||
for k, v := range postdata {
|
||||
bufwriter.WriteField(k, v)
|
||||
}
|
||||
}
|
||||
fpdst, err := bufwriter.CreateFormFile(formname, filepath.Base(fpath))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fpsrc, err := os.Open(fpath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer fpsrc.Close()
|
||||
fpstat, _ := os.Stat(fpath)
|
||||
filebig := float64(fpstat.Size())
|
||||
sum, n := 0, 0
|
||||
for {
|
||||
bufs := make([]byte, 1048576)
|
||||
n, err = fpsrc.Read(bufs)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
sum += n
|
||||
go shell(float64(sum) / filebig * 100)
|
||||
fpdst.Write(bufs[0:n])
|
||||
}
|
||||
|
||||
this.PostBuffer = buf
|
||||
this.ReqHeader.Set("Content-Type", "multipart/form-data;boundary="+bufwriter.Boundary())
|
||||
bufwriter.Close()
|
||||
if tofile {
|
||||
err = this.CurlDataToFile(url, []byte{}, "POST", savepath, shell)
|
||||
this.ResetReqHeader()
|
||||
} else {
|
||||
result, err = this.Curl(url, []byte{}, "POST")
|
||||
}
|
||||
this.ResetReqHeader()
|
||||
return
|
||||
}
|
||||
|
||||
func (this *StarCurl) CurlDataToFile(url string, postdata []byte, method, fpath string, shell func(float64)) (err error) {
|
||||
var req *http.Request
|
||||
if method == "" {
|
||||
if len(postdata) != 0 {
|
||||
method = "POST"
|
||||
} else {
|
||||
method = "GET"
|
||||
}
|
||||
}
|
||||
if len(postdata) == 0 && this.PostBuffer == nil && this.CircleBuffer == nil {
|
||||
req, err = http.NewRequest(method, url, nil)
|
||||
} else if len(postdata) != 0 {
|
||||
req, err = http.NewRequest(method, url, bytes.NewBuffer(postdata))
|
||||
} else if this.PostBuffer != nil {
|
||||
req, err = http.NewRequest(method, url, this.PostBuffer)
|
||||
} else {
|
||||
req, err = http.NewRequest(method, url, this.CircleBuffer)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if (this.ReqHeader == nil) && method == "POST" {
|
||||
this.ReqHeader.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
req.Header = this.ReqHeader
|
||||
if len(this.ReqCookies) != 0 {
|
||||
for _, v := range this.ReqCookies {
|
||||
req.AddCookie(v)
|
||||
}
|
||||
}
|
||||
transport := &http.Transport{
|
||||
Dial: func(netw, addr string) (net.Conn, error) {
|
||||
deadline := time.Now().Add(time.Duration(this.TimeOut) * time.Second)
|
||||
c, err := net.DialTimeout(netw, addr, time.Second*time.Duration(this.DialTimeOut))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if this.TimeOut != 0 {
|
||||
c.SetDeadline(deadline)
|
||||
}
|
||||
return c, nil
|
||||
},
|
||||
}
|
||||
if this.Proxy != "" {
|
||||
purl, _ := urls.Parse(this.Proxy)
|
||||
transport.Proxy = http.ProxyURL(purl)
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
this.PostBuffer = nil
|
||||
this.CircleBuffer = nil
|
||||
this.RespHttpCode = resp.StatusCode
|
||||
this.RespHeader = resp.Header
|
||||
this.RespCookies = resp.Cookies()
|
||||
fpsrc, err := os.Create(fpath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer fpsrc.Close()
|
||||
filebig := float64(resp.ContentLength)
|
||||
if filebig <= 0 {
|
||||
filebig = 100
|
||||
}
|
||||
var n, sum int = 0, 0
|
||||
for {
|
||||
buf := make([]byte, 1048576)
|
||||
n, err = resp.Body.Read(buf)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
go shell(100.00)
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
sum += n
|
||||
go shell(float64(sum) / filebig * 100.00)
|
||||
fpsrc.Write(buf[0:n])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *StarCurl) Curl(url string, postdata []byte, method string) (body []byte, err error) {
|
||||
var req *http.Request
|
||||
if method == "" {
|
||||
if len(postdata) != 0 {
|
||||
method = "POST"
|
||||
} else {
|
||||
method = "GET"
|
||||
}
|
||||
}
|
||||
if len(postdata) == 0 && this.PostBuffer == nil && this.CircleBuffer == nil {
|
||||
req, err = http.NewRequest(method, url, nil)
|
||||
} else if len(postdata) != 0 {
|
||||
req, err = http.NewRequest(method, url, bytes.NewBuffer(postdata))
|
||||
} else if this.PostBuffer != nil {
|
||||
req, err = http.NewRequest(method, url, this.PostBuffer)
|
||||
} else {
|
||||
req, err = http.NewRequest(method, url, this.CircleBuffer)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if (this.ReqHeader == nil) && method == "POST" {
|
||||
this.ReqHeader.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
req.Header = this.ReqHeader
|
||||
if len(this.ReqCookies) != 0 {
|
||||
for _, v := range this.ReqCookies {
|
||||
req.AddCookie(v)
|
||||
}
|
||||
}
|
||||
transport := &http.Transport{
|
||||
Dial: func(netw, addr string) (net.Conn, error) {
|
||||
deadline := time.Now().Add(time.Duration(this.TimeOut) * time.Second)
|
||||
c, err := net.DialTimeout(netw, addr, time.Second*time.Duration(this.DialTimeOut))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if this.TimeOut != 0 {
|
||||
c.SetDeadline(deadline)
|
||||
}
|
||||
return c, nil
|
||||
},
|
||||
}
|
||||
if this.Proxy != "" {
|
||||
purl, _ := urls.Parse(this.Proxy)
|
||||
transport.Proxy = http.ProxyURL(purl)
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
this.PostBuffer = nil
|
||||
this.CircleBuffer = nil
|
||||
this.RespHttpCode = resp.StatusCode
|
||||
this.RespHeader = resp.Header
|
||||
this.RespCookies = resp.Cookies()
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
return
|
||||
}
|
||||
|
||||
//HttpNulReset将重置Header和Cookie为空
|
||||
func HttpNulReset() {
|
||||
var tmp map[string]string
|
||||
HttpNul, HttpNul2 = tmp, tmp
|
||||
}
|
||||
|
||||
func Curl(url string, postdata string, header map[string]string, cookie map[string]string, method string) (error, int, []byte, http.Header, []*http.Cookie) {
|
||||
var req *http.Request
|
||||
if method == "" {
|
||||
if len(postdata) != 0 {
|
||||
method = "POST"
|
||||
} else {
|
||||
method = "GET"
|
||||
}
|
||||
}
|
||||
BytePostData := []byte(postdata)
|
||||
if postdata == "" || len(postdata) == 0 {
|
||||
req, _ = http.NewRequest(method, url, nil)
|
||||
} else {
|
||||
req, _ = http.NewRequest(method, url, bytes.NewBuffer(BytePostData))
|
||||
}
|
||||
|
||||
if (len(header) == 0 || header == nil) && method == "POST" {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
for k, v := range header {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
if len(cookie) != 0 {
|
||||
for k, v := range cookie {
|
||||
req.AddCookie(&http.Cookie{Name: k, Value: v, HttpOnly: true})
|
||||
}
|
||||
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Dial: func(netw, addr string) (net.Conn, error) {
|
||||
deadline := time.Now().Add(time.Duration(HttpTimeOut) * time.Second)
|
||||
c, err := net.DialTimeout(netw, addr, time.Second*time.Duration(HttpTimeOut))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.SetDeadline(deadline)
|
||||
return c, nil
|
||||
},
|
||||
}}
|
||||
resp, err := client.Do(req)
|
||||
var rte []*http.Cookie
|
||||
if err != nil {
|
||||
return err, 0, []byte(""), req.Header, rte
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
statuscode := resp.StatusCode
|
||||
hea := resp.Header
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
return nil, statuscode, body, hea, resp.Cookies()
|
||||
|
||||
}
|
||||
|
||||
//CurlGet发起一个HTTP GET请求
|
||||
func CurlGet(url string) (error, []byte) {
|
||||
err, _, res, _, _ := Curl(url, "", HttpNul, HttpNul2, "GET")
|
||||
return err, res
|
||||
}
|
||||
|
||||
//CurlPost发起一个基于表单的HTTP Post请求
|
||||
func CurlPost(url, postdata string) (error, []byte) {
|
||||
err, _, res, _, _ := Curl(url, postdata, HttpNul, HttpNul2, "POST")
|
||||
return err, res
|
||||
}
|
Loading…
Reference in New Issue