add queue support

master
兔子 5 years ago
parent ff45eecd69
commit d9e5206ba9

@ -0,0 +1,56 @@
package starainrt
import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
)
func Unzip(src, dst string, shell func(string)) error {
if !IsFile(src) {
return errors.New(src + " Not Exists")
}
if Exists(dst) && !IsFolder(dst) {
return errors.New(dst + " Exists And Not A Folder")
}
if !Exists(dst) {
err := os.MkdirAll(dst, 0644)
if err != nil {
return nil
}
}
zipreader, err := zip.OpenReader(src)
if err != nil {
return err
}
defer zipreader.Close()
for _, v := range zipreader.File {
if v.FileInfo().IsDir() {
err := os.MkdirAll(dst+string(os.PathSeparator)+v.Name, 0644)
if err != nil {
fmt.Println(err)
}
continue
}
fp, err := v.Open()
if err != nil {
fmt.Println(err)
continue
}
go shell(v.Name)
fpdst, err := os.Create(dst + string(os.PathSeparator) + v.Name)
if err != nil {
fmt.Println(err)
continue
}
_, err = io.Copy(fpdst, fp)
if err != nil {
fmt.Println(err)
}
fp.Close()
fpdst.Close()
}
return nil
}

@ -0,0 +1,10 @@
package starainrt
import (
"fmt"
"testing"
)
func Test_Unzip(t *testing.T) {
Unzip(`C:\Users\Starainrt\Desktop\o\BK.ZIP`, `C:\Users\Starainrt\Desktop\o\lalala`, func(i string) { fmt.Println(i) })
}

@ -30,6 +30,13 @@ import (
) )
type StarCrypto struct { type StarCrypto struct {
Sha1 []byte
Sha224 []byte
Sha256 []byte
Sha386 []byte
Sha512 []byte
Crc32 []byte
Md5 []byte
} }
/* /*
@ -49,41 +56,51 @@ func (this StarCrypto) Base64Decode(str string) ([]byte, error) {
/* /*
MD5 MD5
*/ */
func (this StarCrypto) MD5(bstr []byte) string { func (this *StarCrypto) MD5(bstr []byte) string {
md5sum := md5.New() md5sum := md5.New()
return hex.EncodeToString(md5sum.Sum(bstr)) md5sum.Write(bstr)
this.Md5 = md5sum.Sum(nil)
return hex.EncodeToString(this.Md5)
} }
/* /*
CRC32 CRC32
*/ */
func (this StarCrypto) CRC32(bstr []byte) string { func (this *StarCrypto) CRC32(bstr []byte) string {
crcsum := crc32.NewIEEE() crcsum := crc32.NewIEEE()
return hex.EncodeToString(crcsum.Sum(bstr)) crcsum.Write(bstr)
this.Crc32 = crcsum.Sum(nil)
return hex.EncodeToString(this.Crc32)
} }
/* /*
SHA512 SHA512
*/ */
func (this StarCrypto) Sha512(bstr []byte) string { func (this *StarCrypto) SHA512(bstr []byte) string {
shasum := sha512.New() shasum := sha512.New()
return hex.EncodeToString(shasum.Sum(bstr)) shasum.Write(bstr)
this.Sha512 = shasum.Sum(nil)
return hex.EncodeToString(this.Sha512)
} }
/* /*
SHA256 SHA256
*/ */
func (this StarCrypto) SHA256(bstr []byte) string { func (this *StarCrypto) SHA256(bstr []byte) string {
shasum := sha256.New() shasum := sha256.New()
return hex.EncodeToString(shasum.Sum(bstr)) shasum.Write(bstr)
this.Sha256 = shasum.Sum(nil)
return hex.EncodeToString(this.Sha256)
} }
/* /*
SHA1 SHA1
*/ */
func (this StarCrypto) SHA1(bstr []byte) string { func (this *StarCrypto) SHA1(bstr []byte) string {
shasum := sha1.New() shasum := sha1.New()
return hex.EncodeToString(shasum.Sum(bstr)) shasum.Write(bstr)
this.Sha1 = shasum.Sum(nil)
return hex.EncodeToString(this.Sha1)
} }
/* /*
@ -570,6 +587,7 @@ func (this StarCrypto) RSAEncrypt(data, public []byte) ([]byte, error) {
func (this StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte, error) { func (this StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte, error) {
var prikey *rsa.PrivateKey var prikey *rsa.PrivateKey
var err error var err error
var bytes []byte
blk, _ := pem.Decode(private) blk, _ := pem.Decode(private)
if blk == nil { if blk == nil {
return []byte{}, errors.New("private key error") return []byte{}, errors.New("private key error")
@ -579,22 +597,25 @@ func (this StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
} }
prikey, err = x509.ParsePKCS1PrivateKey(tmp) bytes = tmp
if err != nil {
return []byte{}, err
}
} else { } else {
prikey, err = x509.ParsePKCS1PrivateKey(blk.Bytes) bytes = blk.Bytes
}
prikey, err = x509.ParsePKCS1PrivateKey(bytes)
if err != nil {
tmp, err := x509.ParsePKCS8PrivateKey(bytes)
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
} }
prikey = tmp.(*rsa.PrivateKey)
} }
return rsa.DecryptPKCS1v15(rand.Reader, prikey, data) return rsa.DecryptPKCS1v15(rand.Reader, prikey, data)
} }
func (this StarCrypto) RSASign(hash256, private []byte, password string) ([]byte, error) { func (this StarCrypto) RSASign(hashdata, private []byte, password string, hashtype crypto.Hash) ([]byte, error) {
var prikey *rsa.PrivateKey var prikey *rsa.PrivateKey
var err error var err error
var bytes []byte
blk, _ := pem.Decode(private) blk, _ := pem.Decode(private)
if blk == nil { if blk == nil {
return []byte{}, errors.New("private key error") return []byte{}, errors.New("private key error")
@ -604,20 +625,22 @@ func (this StarCrypto) RSASign(hash256, private []byte, password string) ([]byte
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
} }
prikey, err = x509.ParsePKCS1PrivateKey(tmp) bytes = tmp
if err != nil {
return []byte{}, err
}
} else { } else {
prikey, err = x509.ParsePKCS1PrivateKey(blk.Bytes) bytes = blk.Bytes
}
prikey, err = x509.ParsePKCS1PrivateKey(bytes)
if err != nil {
tmp, err := x509.ParsePKCS8PrivateKey(bytes)
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
} }
prikey = tmp.(*rsa.PrivateKey)
} }
return rsa.SignPKCS1v15(rand.Reader, prikey, crypto.SHA256, hash256) return rsa.SignPKCS1v15(rand.Reader, prikey, hashtype, hashdata)
} }
func (this StarCrypto) RSAVerify(data, hash256, public []byte) error { func (this StarCrypto) RSAVerify(data, hashdata, public []byte, hashtype crypto.Hash) error {
blk, _ := pem.Decode(public) blk, _ := pem.Decode(public)
if blk == nil { if blk == nil {
return errors.New("public key error") return errors.New("public key error")
@ -626,7 +649,7 @@ func (this StarCrypto) RSAVerify(data, hash256, public []byte) error {
if err != nil { if err != nil {
return err return err
} }
return rsa.VerifyPKCS1v15(pubkey.(*rsa.PublicKey), crypto.SHA256, hash256, data) return rsa.VerifyPKCS1v15(pubkey.(*rsa.PublicKey), hashtype, hashdata, data)
} }
func (this StarCrypto) VicqueEncodeV1(srcdata []byte, key string) []byte { func (this StarCrypto) VicqueEncodeV1(srcdata []byte, key string) []byte {

@ -0,0 +1,395 @@
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, Path: "/"})
}
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, 393213)
n, err = fpsrc.Read(bufs)
if err != nil {
if err == io.EOF {
if n != 0 {
fpdst.Write(bufs[0:n])
go shell(float64(sum+n) / filebig * 100)
}
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, 393213)
n, err = fpsrc.Read(bufs)
if err != nil {
if err == io.EOF {
if n != 0 {
fpdst.Write(bufs[0:n])
go shell(float64(sum+n) / filebig * 100)
}
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, 393213)
n, err = resp.Body.Read(buf)
if err != nil {
if err == io.EOF {
err = nil
if n != 0 {
fpsrc.Write(buf[0:n])
}
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
}

@ -17,7 +17,7 @@ func Test_CfgParse(t *testing.T) {
sakurs = ssss #[ossk]` sakurs = ssss #[ossk]`
//data, _ := ioutil.ReadFile(`c:\Users\Starainrt\Desktop\postgresql.conf`) //data, _ := ioutil.ReadFile(`c:\Users\Starainrt\Desktop\postgresql.conf`)
ini := new(StarCfg) ini := new(StarCfg)
ini.ParseINI([]byte(data)) ini.Parse([]byte(data))
ini.Seg("happy").SetInt64("sakura", 986787, "") ini.Seg("happy").SetInt64("sakura", 986787, "")
fmt.Println(string(ini.Build())) fmt.Println(string(ini.Build()))
} }

470
net.go

@ -2,394 +2,138 @@ package starainrt
import ( import (
"bytes" "bytes"
"crypto/rand" "errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
urls "net/url"
"os"
"path/filepath"
"time"
) )
type StarCurl struct { const SecretKey string = "1996victorique1127B612BTXL"
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() { var header []byte = []byte{11, 27, 19, 96}
this.ReqCookies = []*http.Cookie{} var Crypto *StarCrypto
}
func (this *StarCurl) AddSimpleCookie(key, value string) { func init() {
this.ReqCookies = append(this.ReqCookies, &http.Cookie{Name: key, Value: value}) Crypto = new(StarCrypto)
} }
func randomBoundary() string { type StarQueue struct {
var buf [30]byte Encode bool
_, err := io.ReadFull(rand.Reader, buf[:]) Msgid uint16
if err != nil { MsgPool []MsgUsed
panic(err) UnFinMsg []byte
LastID int //= -1
} }
return fmt.Sprintf("%x", buf[:])
func NewQueue() *StarQueue {
que := new(StarQueue)
que.LastID = -1
que.Encode = true
return que
} }
func (this *StarCurl) CurlWithFile(url string, postdata map[string]string, formname, fpath, savepath string, tofile bool, shell func(float64)) (result []byte, err error) { func (this *StarQueue) BuildMessage(str string) []byte {
fpsrc, err := os.Open(fpath) var msg []byte
if err != nil { var buffer bytes.Buffer
return if this.Encode {
} msg = Crypto.VicqueEncodeV1([]byte(str), SecretKey)
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, 393213)
n, err = fpsrc.Read(bufs)
if err != nil {
if err == io.EOF {
if n != 0 {
fpdst.Write(bufs[0:n])
go shell(float64(sum+n) / filebig * 100)
}
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 { } else {
result, err = this.Curl(url, []byte{}, "POST") msg = []byte(str)
} }
this.ResetReqHeader() buffer.Write([]byte(Crypto.CRC32(msg)))
return buffer.Write([]byte{byte(this.Msgid >> 8), byte(this.Msgid)})
} buffer.Write(msg)
lens := len(buffer.Bytes())
func (this *StarCurl) CurlWithFileByMemory(url string, postdata map[string]string, formname, fpath, savepath string, tofile bool, shell func(float64)) (result []byte, err error) { if lens > 65535 {
buf := &bytes.Buffer{} return nil
bufwriter := multipart.NewWriter(buf) }
if postdata != nil { msg = make([]byte, lens)
for k, v := range postdata { copy(msg, buffer.Bytes())
bufwriter.WriteField(k, v) buffer.Reset()
} ulens := uint16(lens)
} buffer.Write(header)
fpdst, err := bufwriter.CreateFormFile(formname, filepath.Base(fpath)) buffer.Write([]byte{byte(ulens >> 8), byte(ulens)})
if err != nil { buffer.Write(msg)
return this.Msgid++
} return buffer.Bytes()
fpsrc, err := os.Open(fpath) }
if err != nil {
return type MsgUsed struct {
} ID uint16
defer fpsrc.Close() Msg string
fpstat, _ := os.Stat(fpath) Crc32 string
filebig := float64(fpstat.Size()) Ip string
sum, n := 0, 0 }
for {
bufs := make([]byte, 393213) func (this *StarQueue) ParseMessage(msg []byte, ip string) int {
n, err = fpsrc.Read(bufs) var buffer bytes.Buffer
if err != nil { buffer.Write(this.UnFinMsg)
if err == io.EOF { buffer.Write(msg)
if n != 0 { msg = buffer.Bytes()
fpdst.Write(bufs[0:n]) if len(msg) <= 6 {
go shell(float64(sum+n) / filebig * 100) this.UnFinMsg = msg
} return -2
break }
} if msg[0] != byte(11) {
return this.UnFinMsg = []byte{}
} //resend last
sum += n return this.LastID + 1
go shell(float64(sum) / filebig * 100) }
fpdst.Write(bufs[0:n]) if msg[1] != byte(27) || msg[2] != byte(19) || msg[3] != byte(96) {
} //resend last
this.UnFinMsg = []byte{}
this.PostBuffer = buf return this.LastID + 1
this.ReqHeader.Set("Content-Type", "multipart/form-data;boundary="+bufwriter.Boundary()) }
bufwriter.Close() length := uint16(uint(msg[4])<<uint(8) + uint(msg[5]))
if tofile { if 6+length > uint16(len(msg)) {
err = this.CurlDataToFile(url, []byte{}, "POST", savepath, shell) this.UnFinMsg = msg
this.ResetReqHeader() return -2
} 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 { } else {
method = "GET" this.UnFinMsg = []byte{}
} strmsg := msg[6 : length+6]
} crc := strmsg[0:8]
if len(postdata) == 0 && this.PostBuffer == nil && this.CircleBuffer == nil { id := strmsg[8:10]
req, err = http.NewRequest(method, url, nil) strmsg = strmsg[10:]
} else if len(postdata) != 0 { if Crypto.CRC32([]byte(strmsg)) != string(crc) {
req, err = http.NewRequest(method, url, bytes.NewBuffer(postdata)) //resend last
} else if this.PostBuffer != nil { this.UnFinMsg = []byte{}
req, err = http.NewRequest(method, url, this.PostBuffer) return this.LastID + 1
} else { } else {
req, err = http.NewRequest(method, url, this.CircleBuffer) if this.Encode {
} strmsg = Crypto.VicqueDecodeV1(strmsg, SecretKey)
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 msgs := MsgUsed{uint16(uint(id[0])<<8 + uint(id[1])), string(strmsg), string(crc), ip}
}, this.LastID = int(msgs.ID)
this.MsgPool = append(this.MsgPool, msgs)
} }
if this.Proxy != "" { if 6+length == uint16(len(msg)) {
purl, _ := urls.Parse(this.Proxy) return -2
transport.Proxy = http.ProxyURL(purl)
} }
client := &http.Client{ msg = msg[length+6:]
Transport: transport, return this.ParseMessage(msg, ip)
} }
resp, err := client.Do(req) return -2
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, 393213)
n, err = resp.Body.Read(buf)
if err != nil {
if err == io.EOF {
err = nil
if n != 0 {
fpsrc.Write(buf[0:n])
}
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) { func (this *StarQueue) Restore(n int) ([]MsgUsed, error) {
var req *http.Request if n > len(this.MsgPool) {
if method == "" { return nil, errors.New("N is Too Large")
if len(postdata) != 0 {
method = "POST"
} else {
method = "GET"
} }
} tmp := this.MsgPool[0:n]
if len(postdata) == 0 && this.PostBuffer == nil && this.CircleBuffer == nil { if n != len(this.MsgPool) {
req, err = http.NewRequest(method, url, nil) this.MsgPool = this.MsgPool[n:]
} 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 { } else {
req, err = http.NewRequest(method, url, this.CircleBuffer) this.MsgPool = []MsgUsed{}
}
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() return tmp, nil
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 (this *StarQueue) RestoreOne() (MsgUsed, error) {
func HttpNulReset() { if len(this.MsgPool) == 0 {
var tmp map[string]string return MsgUsed{}, errors.New("N is Too Large")
HttpNul, HttpNul2 = tmp, tmp
} }
tmp := this.MsgPool[0]
func Curl(url string, postdata string, header map[string]string, cookie map[string]string, method string) (error, int, []byte, http.Header, []*http.Cookie) { if 1 != len(this.MsgPool) {
var req *http.Request this.MsgPool = this.MsgPool[1:]
if method == "" {
if len(postdata) != 0 {
method = "POST"
} else { } else {
method = "GET" this.MsgPool = []MsgUsed{}
}
}
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() return tmp, nil
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…
Cancel
Save