Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7df7c90d14 | |||
| d9e5206ba9 | |||
| ff45eecd69 | |||
| 6339e3cdf2 | |||
| fe167d7281 |
+58
@@ -0,0 +1,58 @@
|
|||||||
|
package starainrt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Unzip 读取位于src的zip文件,并解压到dst文件夹中
|
||||||
|
// shell传入当前解压的文件名称
|
||||||
|
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) })
|
||||||
|
}
|
||||||
+536
@@ -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",[]string{"md5"}, func(pect float64) {
|
||||||
|
fmt.Printf("已处理%f\r", pect)
|
||||||
|
}))
|
||||||
|
}
|
||||||
+557
@@ -3,9 +3,564 @@ package starainrt
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var DBRes *sql.DB
|
||||||
|
var DBRows *sql.Rows
|
||||||
|
|
||||||
|
type StarDB struct {
|
||||||
|
DB *sql.DB
|
||||||
|
Rows *sql.Rows
|
||||||
|
}
|
||||||
|
|
||||||
|
type StarRows struct {
|
||||||
|
Rows *sql.Rows
|
||||||
|
Length int
|
||||||
|
StringResult []map[string]string
|
||||||
|
Columns []string
|
||||||
|
ColumnsType []reflect.Type
|
||||||
|
columnref map[string]int
|
||||||
|
result [][]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type StarResult struct {
|
||||||
|
Result []interface{}
|
||||||
|
Columns []string
|
||||||
|
columnref map[string]int
|
||||||
|
ColumnsType []reflect.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
type StarResultCol struct {
|
||||||
|
Result []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResultCol) MustBytes() [][]byte {
|
||||||
|
var res [][]byte
|
||||||
|
for _, v := range this.Result {
|
||||||
|
res = append(res, v.([]byte))
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResultCol) MustBool() []bool {
|
||||||
|
var res []bool
|
||||||
|
var tmp bool
|
||||||
|
for _, v := range this.Result {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
case nil:
|
||||||
|
tmp = false
|
||||||
|
case bool:
|
||||||
|
tmp = vtype
|
||||||
|
case float64, float32:
|
||||||
|
if vtype.(float64) > 0 {
|
||||||
|
tmp = true
|
||||||
|
} else {
|
||||||
|
tmp = false
|
||||||
|
}
|
||||||
|
case int, int32, int64:
|
||||||
|
if vtype.(int) > 0 {
|
||||||
|
tmp = true
|
||||||
|
} else {
|
||||||
|
tmp = false
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
tmp, _ = strconv.ParseBool(vtype)
|
||||||
|
default:
|
||||||
|
tmp, _ = strconv.ParseBool(string(vtype.([]byte)))
|
||||||
|
}
|
||||||
|
res = append(res, tmp)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func (this *StarResultCol) MustFloat32() []float32 {
|
||||||
|
var res []float32
|
||||||
|
var tmp float32
|
||||||
|
for _, v := range this.Result {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
case nil:
|
||||||
|
tmp = 0
|
||||||
|
case float64, float32:
|
||||||
|
tmp = float32(vtype.(float64))
|
||||||
|
case string:
|
||||||
|
tmps, _ := strconv.ParseFloat(vtype, 32)
|
||||||
|
tmp = float32(tmps)
|
||||||
|
case int, int32, int64:
|
||||||
|
tmp = float32(vtype.(int64))
|
||||||
|
default:
|
||||||
|
tmp = v.(float32)
|
||||||
|
}
|
||||||
|
res = append(res, tmp)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func (this *StarResultCol) MustFloat64() []float64 {
|
||||||
|
var res []float64
|
||||||
|
var tmp float64
|
||||||
|
for _, v := range this.Result {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
case nil:
|
||||||
|
tmp = 0
|
||||||
|
case float64, float32:
|
||||||
|
tmp = vtype.(float64)
|
||||||
|
case string:
|
||||||
|
tmp, _ = strconv.ParseFloat(vtype, 64)
|
||||||
|
case int, int32, int64:
|
||||||
|
tmp = float64(vtype.(int64))
|
||||||
|
default:
|
||||||
|
tmp = v.(float64)
|
||||||
|
}
|
||||||
|
res = append(res, tmp)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResultCol) MustString() []string {
|
||||||
|
var res []string
|
||||||
|
var tmp string
|
||||||
|
for _, v := range this.Result {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
case nil:
|
||||||
|
tmp = ""
|
||||||
|
case string:
|
||||||
|
tmp = vtype
|
||||||
|
case int64:
|
||||||
|
tmp = strconv.FormatInt(vtype, 10)
|
||||||
|
case int32:
|
||||||
|
tmp = strconv.Itoa(int(vtype))
|
||||||
|
case bool:
|
||||||
|
tmp = strconv.FormatBool(vtype)
|
||||||
|
case float64:
|
||||||
|
tmp = strconv.FormatFloat(vtype, 'f', 10, 64)
|
||||||
|
case float32:
|
||||||
|
tmp = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
|
||||||
|
case int:
|
||||||
|
tmp = strconv.Itoa(vtype)
|
||||||
|
default:
|
||||||
|
tmp = string(vtype.([]byte))
|
||||||
|
}
|
||||||
|
res = append(res, tmp)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResultCol) MustInt32() []int32 {
|
||||||
|
var res []int32
|
||||||
|
var tmp int32
|
||||||
|
for _, v := range this.Result {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
case nil:
|
||||||
|
tmp = 0
|
||||||
|
case float64, float32:
|
||||||
|
tmp = int32(vtype.(float64))
|
||||||
|
case string:
|
||||||
|
tmps, _ := strconv.ParseInt(vtype, 10, 32)
|
||||||
|
tmp = int32(tmps)
|
||||||
|
case int, int32, int64:
|
||||||
|
tmp = int32(vtype.(int64))
|
||||||
|
default:
|
||||||
|
tmp = v.(int32)
|
||||||
|
}
|
||||||
|
res = append(res, tmp)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func (this *StarResultCol) MustInt64() []int64 {
|
||||||
|
var res []int64
|
||||||
|
var tmp int64
|
||||||
|
for _, v := range this.Result {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
case nil:
|
||||||
|
tmp = 0
|
||||||
|
case float64, float32:
|
||||||
|
tmp = int64(vtype.(float64))
|
||||||
|
case string:
|
||||||
|
tmp, _ = strconv.ParseInt(vtype, 10, 64)
|
||||||
|
case int, int32, int64:
|
||||||
|
tmp = (vtype.(int64))
|
||||||
|
default:
|
||||||
|
tmp = v.(int64)
|
||||||
|
}
|
||||||
|
res = append(res, tmp)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func (this *StarResultCol) MustInt() []int {
|
||||||
|
var res []int
|
||||||
|
var tmp int
|
||||||
|
for _, v := range this.Result {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
case nil:
|
||||||
|
tmp = 0
|
||||||
|
case float64, float32:
|
||||||
|
tmp = int(vtype.(float64))
|
||||||
|
case string:
|
||||||
|
tmps, _ := strconv.ParseInt(vtype, 10, 64)
|
||||||
|
tmp = int(tmps)
|
||||||
|
case int, int32, int64:
|
||||||
|
tmp = int(vtype.(int64))
|
||||||
|
default:
|
||||||
|
tmp = int(v.(int64))
|
||||||
|
}
|
||||||
|
res = append(res, tmp)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResult) MustInt64(name string) int64 {
|
||||||
|
var res int64
|
||||||
|
num, ok := this.columnref[name]
|
||||||
|
if !ok {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
tmp := this.Result[num]
|
||||||
|
switch vtype := tmp.(type) {
|
||||||
|
case nil:
|
||||||
|
res = 0
|
||||||
|
case float64, float32:
|
||||||
|
res = int64(vtype.(float64))
|
||||||
|
case string:
|
||||||
|
res, _ = strconv.ParseInt(vtype, 10, 64)
|
||||||
|
case int, int32, int64:
|
||||||
|
res = (vtype.(int64))
|
||||||
|
default:
|
||||||
|
res, _ = strconv.ParseInt(string(tmp.([]byte)), 10, 64)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func (this *StarResult) MustInt32(name string) int32 {
|
||||||
|
var res int32
|
||||||
|
num, ok := this.columnref[name]
|
||||||
|
if !ok {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
tmp := this.Result[num]
|
||||||
|
switch vtype := tmp.(type) {
|
||||||
|
case nil:
|
||||||
|
res = 0
|
||||||
|
case float64, float32:
|
||||||
|
res = int32(vtype.(float64))
|
||||||
|
case string:
|
||||||
|
ress, _ := strconv.ParseInt(vtype, 10, 32)
|
||||||
|
res = int32(ress)
|
||||||
|
case int, int32, int64:
|
||||||
|
res = int32(vtype.(int64))
|
||||||
|
default:
|
||||||
|
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 32)
|
||||||
|
res = int32(ress)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func (this *StarResult) MustString(name string) string {
|
||||||
|
var res string
|
||||||
|
num, ok := this.columnref[name]
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
switch vtype := this.Result[num].(type) {
|
||||||
|
case nil:
|
||||||
|
res = ""
|
||||||
|
case string:
|
||||||
|
res = vtype
|
||||||
|
case int64:
|
||||||
|
res = strconv.FormatInt(vtype, 10)
|
||||||
|
case int32:
|
||||||
|
res = strconv.Itoa(int(vtype))
|
||||||
|
case bool:
|
||||||
|
res = strconv.FormatBool(vtype)
|
||||||
|
case float64:
|
||||||
|
res = strconv.FormatFloat(vtype, 'f', 10, 64)
|
||||||
|
case float32:
|
||||||
|
res = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
|
||||||
|
case int:
|
||||||
|
res = strconv.Itoa(vtype)
|
||||||
|
default:
|
||||||
|
res = string(vtype.([]byte))
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResult) MustFloat64(name string) float64 {
|
||||||
|
var res float64
|
||||||
|
num, ok := this.columnref[name]
|
||||||
|
if !ok {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
switch vtype := this.Result[num].(type) {
|
||||||
|
case nil:
|
||||||
|
res = 0
|
||||||
|
case string:
|
||||||
|
res, _ = strconv.ParseFloat(vtype, 64)
|
||||||
|
case float64:
|
||||||
|
res = vtype
|
||||||
|
case int, int64, int32, float32:
|
||||||
|
res = vtype.(float64)
|
||||||
|
default:
|
||||||
|
res, _ = strconv.ParseFloat(string(vtype.([]byte)), 64)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResult) MustFloat32(name string) float32 {
|
||||||
|
var res float32
|
||||||
|
num, ok := this.columnref[name]
|
||||||
|
if !ok {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
switch vtype := this.Result[num].(type) {
|
||||||
|
case nil:
|
||||||
|
res = 0
|
||||||
|
case string:
|
||||||
|
tmp, _ := strconv.ParseFloat(vtype, 32)
|
||||||
|
res = float32(tmp)
|
||||||
|
case float64:
|
||||||
|
res = float32(vtype)
|
||||||
|
case float32:
|
||||||
|
res = vtype
|
||||||
|
case int, int64, int32:
|
||||||
|
res = vtype.(float32)
|
||||||
|
default:
|
||||||
|
tmp, _ := strconv.ParseFloat(string(vtype.([]byte)), 32)
|
||||||
|
res = float32(tmp)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResult) MustInt(name string) int {
|
||||||
|
var res int
|
||||||
|
num, ok := this.columnref[name]
|
||||||
|
if !ok {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
tmp := this.Result[num]
|
||||||
|
switch vtype := tmp.(type) {
|
||||||
|
case nil:
|
||||||
|
res = 0
|
||||||
|
case float64, float32:
|
||||||
|
res = int(vtype.(float64))
|
||||||
|
case string:
|
||||||
|
ress, _ := strconv.ParseInt(vtype, 10, 64)
|
||||||
|
res = int(ress)
|
||||||
|
case int, int32, int64:
|
||||||
|
res = int(vtype.(int64))
|
||||||
|
default:
|
||||||
|
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 64)
|
||||||
|
res = int(ress)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarResult) MustBool(name string) bool {
|
||||||
|
var res bool
|
||||||
|
num, ok := this.columnref[name]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
tmp := this.Result[num]
|
||||||
|
switch vtype := tmp.(type) {
|
||||||
|
case nil:
|
||||||
|
res = false
|
||||||
|
case bool:
|
||||||
|
res = vtype
|
||||||
|
case float64, float32:
|
||||||
|
if vtype.(float64) > 0 {
|
||||||
|
res = true
|
||||||
|
} else {
|
||||||
|
res = false
|
||||||
|
}
|
||||||
|
case int, int32, int64:
|
||||||
|
if vtype.(int) > 0 {
|
||||||
|
res = true
|
||||||
|
} else {
|
||||||
|
res = false
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
res, _ = strconv.ParseBool(vtype)
|
||||||
|
default:
|
||||||
|
res, _ = strconv.ParseBool(string(vtype.([]byte)))
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func (this *StarResult) MustBytes(name string) []byte {
|
||||||
|
num, ok := this.columnref[name]
|
||||||
|
if !ok {
|
||||||
|
return []byte{}
|
||||||
|
}
|
||||||
|
res := this.Result[num].([]byte)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarRows) Rescan() {
|
||||||
|
this.parserows()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarRows) Col(name string) *StarResultCol {
|
||||||
|
result := new(StarResultCol)
|
||||||
|
if _, ok := this.columnref[name]; !ok {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
var rescol []interface{}
|
||||||
|
for _, v := range this.result {
|
||||||
|
rescol = append(rescol, v[this.columnref[name]])
|
||||||
|
}
|
||||||
|
result.Result = rescol
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarRows) Row(id int) *StarResult {
|
||||||
|
result := new(StarResult)
|
||||||
|
if id+1 > len(this.result) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
result.Result = this.result[id]
|
||||||
|
result.Columns = this.Columns
|
||||||
|
result.ColumnsType = this.ColumnsType
|
||||||
|
result.columnref = this.columnref
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarRows) Close() error {
|
||||||
|
return this.Rows.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarRows) parserows() {
|
||||||
|
this.result = [][]interface{}{}
|
||||||
|
this.columnref = make(map[string]int)
|
||||||
|
this.StringResult = []map[string]string{}
|
||||||
|
this.Columns, _ = this.Rows.Columns()
|
||||||
|
types, _ := this.Rows.ColumnTypes()
|
||||||
|
for _, v := range types {
|
||||||
|
this.ColumnsType = append(this.ColumnsType, v.ScanType())
|
||||||
|
}
|
||||||
|
scanArgs := make([]interface{}, len(this.Columns))
|
||||||
|
values := make([]interface{}, len(this.Columns))
|
||||||
|
for i, _ := range values {
|
||||||
|
this.columnref[this.Columns[i]] = i
|
||||||
|
scanArgs[i] = &values[i]
|
||||||
|
}
|
||||||
|
for this.Rows.Next() {
|
||||||
|
if err := this.Rows.Scan(scanArgs...); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
record := make(map[string]string)
|
||||||
|
var rescopy []interface{}
|
||||||
|
for i, col := range values {
|
||||||
|
rescopy = append(rescopy, col)
|
||||||
|
switch vtype := col.(type) {
|
||||||
|
case float64:
|
||||||
|
record[this.Columns[i]] = strconv.FormatFloat(vtype, 'f', -1, 64)
|
||||||
|
case int64:
|
||||||
|
record[this.Columns[i]] = strconv.FormatInt(vtype, 10)
|
||||||
|
case string:
|
||||||
|
record[this.Columns[i]] = vtype
|
||||||
|
case nil:
|
||||||
|
record[this.Columns[i]] = ""
|
||||||
|
default:
|
||||||
|
record[this.Columns[i]] = string(vtype.([]byte))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.result = append(this.result, rescopy)
|
||||||
|
this.StringResult = append(this.StringResult, record)
|
||||||
|
}
|
||||||
|
this.Length = len(this.StringResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
||||||
|
var err error
|
||||||
|
effect := new(StarRows)
|
||||||
|
if err = this.DB.Ping(); err != nil {
|
||||||
|
return effect, err
|
||||||
|
}
|
||||||
|
if len(args) == 0 {
|
||||||
|
return effect, errors.New("no args")
|
||||||
|
}
|
||||||
|
if len(args) == 1 {
|
||||||
|
sql := args[0]
|
||||||
|
if this.Rows, err = this.DB.Query(sql.(string)); err != nil {
|
||||||
|
return effect, err
|
||||||
|
}
|
||||||
|
effect.Rows = this.Rows
|
||||||
|
effect.parserows()
|
||||||
|
return effect, nil
|
||||||
|
}
|
||||||
|
sql := args[0]
|
||||||
|
stmt, err := this.DB.Prepare(sql.(string))
|
||||||
|
if err != nil {
|
||||||
|
return effect, err
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
var para []interface{}
|
||||||
|
for k, v := range args {
|
||||||
|
if k != 0 {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
default:
|
||||||
|
para = append(para, vtype)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if this.Rows, err = stmt.Query(para...); err != nil {
|
||||||
|
return effect, err
|
||||||
|
}
|
||||||
|
effect.Rows = this.Rows
|
||||||
|
effect.parserows()
|
||||||
|
return effect, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarDB) Open(Method, ConnStr string) error {
|
||||||
|
var err error
|
||||||
|
this.DB, err = sql.Open(Method, ConnStr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.DB.Ping()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarDB) Close() error {
|
||||||
|
if err := this.DB.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.DB.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
||||||
|
var err error
|
||||||
|
var effect sql.Result
|
||||||
|
if err = this.DB.Ping(); err != nil {
|
||||||
|
return effect, err
|
||||||
|
}
|
||||||
|
if len(args) == 0 {
|
||||||
|
return effect, errors.New("no args")
|
||||||
|
}
|
||||||
|
if len(args) == 1 {
|
||||||
|
sql := args[0]
|
||||||
|
if _, err = this.DB.Exec(sql.(string)); err != nil {
|
||||||
|
return effect, err
|
||||||
|
}
|
||||||
|
return effect, nil
|
||||||
|
}
|
||||||
|
sql := args[0]
|
||||||
|
stmt, err := this.DB.Prepare(sql.(string))
|
||||||
|
defer stmt.Close()
|
||||||
|
if err != nil {
|
||||||
|
return effect, err
|
||||||
|
}
|
||||||
|
var para []interface{}
|
||||||
|
for k, v := range args {
|
||||||
|
if k != 0 {
|
||||||
|
switch vtype := v.(type) {
|
||||||
|
default:
|
||||||
|
para = append(para, vtype)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if effect, err = stmt.Exec(para...); err != nil {
|
||||||
|
return effect, err
|
||||||
|
}
|
||||||
|
return effect, nil
|
||||||
|
}
|
||||||
|
|
||||||
func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) {
|
func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) {
|
||||||
var ii int = 0
|
var ii int = 0
|
||||||
records := make(map[int]map[string]string)
|
records := make(map[int]map[string]string)
|
||||||
@@ -31,6 +586,8 @@ func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) {
|
|||||||
record[columns[i]] = strconv.FormatInt(vtype, 10)
|
record[columns[i]] = strconv.FormatInt(vtype, 10)
|
||||||
case string:
|
case string:
|
||||||
record[columns[i]] = vtype
|
record[columns[i]] = vtype
|
||||||
|
case nil:
|
||||||
|
record[columns[i]] = ""
|
||||||
default:
|
default:
|
||||||
record[columns[i]] = string(vtype.([]byte))
|
record[columns[i]] = string(vtype.([]byte))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,421 @@
|
|||||||
|
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) CurlWithFileByBytes(url string, postdata map[string]string, formname, fname string, data []byte, savepath string, tofile bool) (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, fname)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fpdst.Write(data)
|
||||||
|
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, func(float64) {})
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -159,7 +160,7 @@ jump:
|
|||||||
if ok, _ := regexp.MatchString("^#", v); ok {
|
if ok, _ := regexp.MatchString("^#", v); ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
segfind := regexp.MustCompile(`\[(.*)\]`)
|
segfind := regexp.MustCompile(`^\[(.*)\]`)
|
||||||
if !inseg {
|
if !inseg {
|
||||||
if ok, _ := regexp.MatchString(`(.*?)=(.*)`, v); ok {
|
if ok, _ := regexp.MatchString(`(.*?)=(.*)`, v); ok {
|
||||||
nolabel = true
|
nolabel = true
|
||||||
@@ -195,3 +196,350 @@ jump:
|
|||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StarCfg struct {
|
||||||
|
Data []*CfgSegment
|
||||||
|
segmap map[string]int
|
||||||
|
nodemap map[int]map[string]int
|
||||||
|
}
|
||||||
|
|
||||||
|
type CfgNode struct {
|
||||||
|
Key string
|
||||||
|
Value string
|
||||||
|
Comment string
|
||||||
|
}
|
||||||
|
|
||||||
|
type CfgSegment struct {
|
||||||
|
Name string
|
||||||
|
cmap map[string]int
|
||||||
|
Comment string
|
||||||
|
Node []*CfgNode
|
||||||
|
InsertNode []*CfgNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarCfg) ParseFromFile(filepath string) error {
|
||||||
|
if !Exists(filepath) {
|
||||||
|
return errors.New(filepath + " 不存在")
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
this.Parse(data)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarCfg) WriteToFile(filepath string) error {
|
||||||
|
data := this.Build()
|
||||||
|
return ioutil.WriteFile(filepath, data, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarCfg) Parse(data []byte) {
|
||||||
|
var newnode *CfgNode
|
||||||
|
segint := 0
|
||||||
|
nodeint := 0
|
||||||
|
this.segmap = make(map[string]int)
|
||||||
|
this.nodemap = make(map[int]map[string]int)
|
||||||
|
strdata := string(data)
|
||||||
|
list := strings.Split(strdata, "\n")
|
||||||
|
newseg := new(CfgSegment)
|
||||||
|
newseg.Name = "unnamed"
|
||||||
|
newseg.InsertNode = []*CfgNode{}
|
||||||
|
this.segmap["unnamed"] = 0
|
||||||
|
lastiseg := true
|
||||||
|
for _, v := range list {
|
||||||
|
istrans := false
|
||||||
|
isseg := false
|
||||||
|
isnote := false
|
||||||
|
isequal := false
|
||||||
|
tmp1 := []rune{}
|
||||||
|
tmp2 := []rune{}
|
||||||
|
note := []rune{}
|
||||||
|
v = strings.TrimSpace(v)
|
||||||
|
for k, v2 := range v {
|
||||||
|
if k == 0 {
|
||||||
|
if v2 == '[' {
|
||||||
|
isseg = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if v2 == '=' && (!istrans && !isnote) {
|
||||||
|
isequal = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if v2 == ']' && (!istrans && !isnote) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if v2 == '#' && (!istrans && !isnote) {
|
||||||
|
isnote = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if v2 == '\\' && (!istrans && !isnote) {
|
||||||
|
istrans = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if istrans {
|
||||||
|
istrans = false
|
||||||
|
}
|
||||||
|
if !isnote && (!isequal) {
|
||||||
|
tmp1 = append(tmp1, v2)
|
||||||
|
} else if !isnote && isequal {
|
||||||
|
tmp2 = append(tmp2, v2)
|
||||||
|
} else if isnote {
|
||||||
|
note = append(note, v2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isseg {
|
||||||
|
this.Data = append(this.Data, newseg)
|
||||||
|
newseg = new(CfgSegment)
|
||||||
|
newseg.InsertNode = []*CfgNode{}
|
||||||
|
newseg.Name = strings.TrimSpace(string(tmp1))
|
||||||
|
if isnote {
|
||||||
|
newseg.Comment = strings.TrimSpace(string(note))
|
||||||
|
}
|
||||||
|
segint++
|
||||||
|
this.segmap[newseg.Name] = segint
|
||||||
|
nodeint = 0
|
||||||
|
lastiseg = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if isequal {
|
||||||
|
newnode = new(CfgNode)
|
||||||
|
newnode.Key = strings.TrimSpace(string(tmp1))
|
||||||
|
newnode.Value = strings.TrimSpace(string(tmp2))
|
||||||
|
if isnote {
|
||||||
|
newnode.Comment = strings.TrimSpace(string(note))
|
||||||
|
}
|
||||||
|
newseg.Node = append(newseg.Node, newnode)
|
||||||
|
if this.nodemap[segint] == nil {
|
||||||
|
this.nodemap[segint] = make(map[string]int)
|
||||||
|
}
|
||||||
|
this.nodemap[segint][newnode.Key] = nodeint
|
||||||
|
nodeint++
|
||||||
|
lastiseg = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if isnote {
|
||||||
|
comment := strings.TrimSpace(string(note))
|
||||||
|
if lastiseg {
|
||||||
|
newseg.Comment += "\n" + comment
|
||||||
|
} else {
|
||||||
|
newnode.Comment += "\n" + comment
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
this.Data = append(this.Data, newseg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarCfg) Seg(name string) *CfgSegment {
|
||||||
|
if _, ok := this.segmap[name]; !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
seg := this.Data[this.segmap[name]]
|
||||||
|
if seg.Name == name {
|
||||||
|
seg.cmap = this.nodemap[this.segmap[name]]
|
||||||
|
return seg
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarCfg) Build() []byte {
|
||||||
|
var res, comm string
|
||||||
|
for _, v := range this.Data {
|
||||||
|
comm = ""
|
||||||
|
if v.Name != "unnamed" {
|
||||||
|
res += `[ ` + this.repkv(v.Name) + ` ]`
|
||||||
|
}
|
||||||
|
if v.Comment != "" {
|
||||||
|
comm = strings.Replace(v.Comment, "\n", "\n#", -1)
|
||||||
|
if comm[0:1] != "\n" {
|
||||||
|
comm = " #" + comm
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
res += comm + "\n"
|
||||||
|
comm = ""
|
||||||
|
for _, v2 := range v.Node {
|
||||||
|
res += this.repkv(v2.Key) + " = " + this.repkv(v2.Value)
|
||||||
|
if v2.Comment != "" {
|
||||||
|
comm = strings.Replace(v2.Comment, "\n", "\n#", -1)
|
||||||
|
if comm[0:1] != "\n" {
|
||||||
|
comm = " #" + comm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += comm + "\n"
|
||||||
|
}
|
||||||
|
comm = ""
|
||||||
|
for _, v2 := range v.InsertNode {
|
||||||
|
res += this.repkv(v2.Key) + " = " + this.repkv(v2.Value)
|
||||||
|
if v2.Comment != "" {
|
||||||
|
comm = strings.Replace(v2.Comment, "\n", "\n#", -1)
|
||||||
|
if comm[0:1] != "\n" {
|
||||||
|
comm = " #" + comm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += comm + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []byte(strings.TrimSpace(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarCfg) repkv(value string) string {
|
||||||
|
value = strings.Replace(value, `\`, `\\`, -1)
|
||||||
|
value = strings.Replace(value, `#`, `\#`, -1)
|
||||||
|
value = strings.Replace(value, `=`, `\=`, -1)
|
||||||
|
value = strings.Replace(value, `[`, `\[`, -1)
|
||||||
|
value = strings.Replace(value, `]`, `\]`, -1)
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) GetComment(key string) string {
|
||||||
|
if v, ok := this.cmap[key]; !ok {
|
||||||
|
for _, v2 := range this.InsertNode {
|
||||||
|
if v2.Key == key {
|
||||||
|
return this.Node[v].Comment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return this.Node[v].Comment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Exist(key string) bool {
|
||||||
|
if _, ok := this.cmap[key]; !ok {
|
||||||
|
for _, v2 := range this.InsertNode {
|
||||||
|
if v2.Key == key {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Get(key string) string {
|
||||||
|
if v, ok := this.cmap[key]; !ok {
|
||||||
|
for _, v2 := range this.InsertNode {
|
||||||
|
if v2.Key == key {
|
||||||
|
return this.Node[v].Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return this.Node[v].Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Int(key string) int {
|
||||||
|
val := this.Get(key)
|
||||||
|
if val == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
res, _ := strconv.Atoi(val)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Int64(key string) int64 {
|
||||||
|
val := this.Get(key)
|
||||||
|
if val == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
res, _ := strconv.ParseInt(val, 10, 64)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Int32(key string) int32 {
|
||||||
|
val := this.Get(key)
|
||||||
|
if val == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
res, _ := strconv.ParseInt(val, 10, 32)
|
||||||
|
return int32(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Float64(key string) float64 {
|
||||||
|
val := this.Get(key)
|
||||||
|
if val == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
res, _ := strconv.ParseFloat(val, 64)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Float32(key string) float32 {
|
||||||
|
val := this.Get(key)
|
||||||
|
if val == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
res, _ := strconv.ParseFloat(val, 32)
|
||||||
|
return float32(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Bool(key string) bool {
|
||||||
|
val := this.Get(key)
|
||||||
|
if val == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
res, _ := strconv.ParseBool(val)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) SetBool(key string, value bool, comment string) error {
|
||||||
|
res := strconv.FormatBool(value)
|
||||||
|
return this.Set(key, res, comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) SetFloat64(key string, prec int, value float64, comment string) error {
|
||||||
|
res := strconv.FormatFloat(value, 'f', prec, 64)
|
||||||
|
return this.Set(key, res, comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) SetFloat32(key string, prec int, value float32, comment string) error {
|
||||||
|
res := strconv.FormatFloat(float64(value), 'f', prec, 32)
|
||||||
|
return this.Set(key, res, comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) SetInt64(key string, value int64, comment string) error {
|
||||||
|
res := strconv.FormatInt(value, 10)
|
||||||
|
return this.Set(key, res, comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) SetInt32(key string, value int32, comment string) error {
|
||||||
|
res := strconv.FormatInt(int64(value), 10)
|
||||||
|
return this.Set(key, res, comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) SetInt(key string, value int, comment string) error {
|
||||||
|
res := strconv.Itoa(value)
|
||||||
|
return this.Set(key, res, comment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *CfgSegment) Set(key, value, comment string) error {
|
||||||
|
if v, ok := this.cmap[key]; !ok {
|
||||||
|
for _, v2 := range this.InsertNode {
|
||||||
|
if v2.Key == key {
|
||||||
|
this.Node[v].Value = value
|
||||||
|
if comment != "" {
|
||||||
|
this.Node[v].Comment = comment
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node := new(CfgNode)
|
||||||
|
node.Key = key
|
||||||
|
node.Value = value
|
||||||
|
node.Comment = comment
|
||||||
|
this.InsertNode = append(this.InsertNode, node)
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
this.Node[v].Value = value
|
||||||
|
if comment != "" {
|
||||||
|
this.Node[v].Comment = comment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package starainrt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_CfgParse(t *testing.T) {
|
||||||
|
data := `#lalala
|
||||||
|
ok =true\# #ok
|
||||||
|
[happy] #ok
|
||||||
|
#kkk
|
||||||
|
me = \=tru
|
||||||
|
sa = p\\k
|
||||||
|
migrate = ok #oooo
|
||||||
|
[siki]
|
||||||
|
sakurs = ssss #[ossk]`
|
||||||
|
//data, _ := ioutil.ReadFile(`c:\Users\Starainrt\Desktop\postgresql.conf`)
|
||||||
|
ini := new(StarCfg)
|
||||||
|
ini.Parse([]byte(data))
|
||||||
|
ini.Seg("happy").SetInt64("sakura", 986787, "")
|
||||||
|
fmt.Println(string(ini.Build()))
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package starainrt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
SecretKey 通信加密Key,不应当被修改
|
||||||
|
*/
|
||||||
|
const SecretKey string = "1996victorique1127B612BTXL"
|
||||||
|
|
||||||
|
var header []byte = []byte{11, 27, 19, 96}
|
||||||
|
var Crypto *StarCrypto
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Crypto = new(StarCrypto)
|
||||||
|
}
|
||||||
|
|
||||||
|
type StarQueue struct {
|
||||||
|
Encode bool
|
||||||
|
Msgid uint16
|
||||||
|
MsgPool []MsgUsed
|
||||||
|
UnFinMsg []byte
|
||||||
|
LastID int //= -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewQueue() *StarQueue {
|
||||||
|
que := new(StarQueue)
|
||||||
|
que.LastID = -1
|
||||||
|
que.Encode = true
|
||||||
|
return que
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarQueue) BuildMessage(str string) []byte {
|
||||||
|
var msg []byte
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
if this.Encode {
|
||||||
|
msg = Crypto.VicqueEncodeV1([]byte(str), SecretKey)
|
||||||
|
} else {
|
||||||
|
msg = []byte(str)
|
||||||
|
}
|
||||||
|
buffer.Write([]byte(Crypto.CRC32(msg)))
|
||||||
|
buffer.Write([]byte{byte(this.Msgid >> 8), byte(this.Msgid)})
|
||||||
|
buffer.Write(msg)
|
||||||
|
lens := len(buffer.Bytes())
|
||||||
|
if lens > 65535 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
msg = make([]byte, lens)
|
||||||
|
copy(msg, buffer.Bytes())
|
||||||
|
buffer.Reset()
|
||||||
|
ulens := uint16(lens)
|
||||||
|
buffer.Write(header)
|
||||||
|
buffer.Write([]byte{byte(ulens >> 8), byte(ulens)})
|
||||||
|
buffer.Write(msg)
|
||||||
|
this.Msgid++
|
||||||
|
return buffer.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
type MsgUsed struct {
|
||||||
|
ID uint16
|
||||||
|
Msg string
|
||||||
|
Crc32 string
|
||||||
|
Conn interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarQueue) ParseMessage(msg []byte, conn interface{}) int {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
buffer.Write(this.UnFinMsg)
|
||||||
|
buffer.Write(msg)
|
||||||
|
msg = buffer.Bytes()
|
||||||
|
if len(msg) <= 6 {
|
||||||
|
this.UnFinMsg = msg
|
||||||
|
return -2
|
||||||
|
}
|
||||||
|
if msg[0] != byte(11) {
|
||||||
|
this.UnFinMsg = []byte{}
|
||||||
|
//resend last
|
||||||
|
return this.LastID + 1
|
||||||
|
}
|
||||||
|
if msg[1] != byte(27) || msg[2] != byte(19) || msg[3] != byte(96) {
|
||||||
|
//resend last
|
||||||
|
this.UnFinMsg = []byte{}
|
||||||
|
return this.LastID + 1
|
||||||
|
}
|
||||||
|
length := uint16(uint(msg[4])<<uint(8) + uint(msg[5]))
|
||||||
|
if 6+length > uint16(len(msg)) {
|
||||||
|
this.UnFinMsg = msg
|
||||||
|
return -2
|
||||||
|
} else {
|
||||||
|
this.UnFinMsg = []byte{}
|
||||||
|
strmsg := msg[6 : length+6]
|
||||||
|
crc := strmsg[0:8]
|
||||||
|
id := strmsg[8:10]
|
||||||
|
strmsg = strmsg[10:]
|
||||||
|
if Crypto.CRC32([]byte(strmsg)) != string(crc) {
|
||||||
|
//resend last
|
||||||
|
this.UnFinMsg = []byte{}
|
||||||
|
return this.LastID + 1
|
||||||
|
} else {
|
||||||
|
if this.Encode {
|
||||||
|
strmsg = Crypto.VicqueDecodeV1(strmsg, SecretKey)
|
||||||
|
}
|
||||||
|
msgs := MsgUsed{uint16(uint(id[0])<<8 + uint(id[1])), string(strmsg), string(crc), conn}
|
||||||
|
this.LastID = int(msgs.ID)
|
||||||
|
this.MsgPool = append(this.MsgPool, msgs)
|
||||||
|
}
|
||||||
|
if 6+length == uint16(len(msg)) {
|
||||||
|
return -2
|
||||||
|
}
|
||||||
|
msg = msg[length+6:]
|
||||||
|
return this.ParseMessage(msg, conn)
|
||||||
|
}
|
||||||
|
return -2
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarQueue) Restore(n int) ([]MsgUsed, error) {
|
||||||
|
if n > len(this.MsgPool) {
|
||||||
|
return nil, errors.New("N is Too Large")
|
||||||
|
}
|
||||||
|
tmp := this.MsgPool[0:n]
|
||||||
|
if n != len(this.MsgPool) {
|
||||||
|
this.MsgPool = this.MsgPool[n:]
|
||||||
|
} else {
|
||||||
|
this.MsgPool = []MsgUsed{}
|
||||||
|
}
|
||||||
|
return tmp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarQueue) RestoreOne() (MsgUsed, error) {
|
||||||
|
if len(this.MsgPool) == 0 {
|
||||||
|
return MsgUsed{}, errors.New("N is Too Large")
|
||||||
|
}
|
||||||
|
tmp := this.MsgPool[0]
|
||||||
|
if 1 != len(this.MsgPool) {
|
||||||
|
this.MsgPool = this.MsgPool[1:]
|
||||||
|
} else {
|
||||||
|
this.MsgPool = []MsgUsed{}
|
||||||
|
}
|
||||||
|
return tmp, nil
|
||||||
|
}
|
||||||
+137
-63
@@ -1,23 +1,43 @@
|
|||||||
package starainrt
|
package starainrt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"errors"
|
||||||
"database/sql"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var HttpNul, HttpNul2 map[string]string
|
var HttpNul, HttpNul2 map[string]string
|
||||||
var HttpTimeOut int64 = 15
|
var HttpTimeOut int64 = 15
|
||||||
var DBRes *sql.DB
|
|
||||||
var DBRows *sql.Rows
|
|
||||||
var ShellRes, ShellErr string
|
var ShellRes, ShellErr string
|
||||||
var ShellExit bool
|
var ShellExit bool
|
||||||
|
|
||||||
//Exits返回指定文件夹/文件是否存在
|
type CircleByteBuffer struct {
|
||||||
|
io.Reader
|
||||||
|
io.Writer
|
||||||
|
io.Closer
|
||||||
|
datas []byte
|
||||||
|
|
||||||
|
start int
|
||||||
|
end int
|
||||||
|
size int
|
||||||
|
isClose bool
|
||||||
|
isEnd bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCircleByteBuffer(len int) *CircleByteBuffer {
|
||||||
|
var e = new(CircleByteBuffer)
|
||||||
|
e.datas = make([]byte, len)
|
||||||
|
e.start = 0
|
||||||
|
e.end = 0
|
||||||
|
e.size = len
|
||||||
|
e.isClose = false
|
||||||
|
e.isEnd = false
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exists 返回指定文件夹/文件是否存在
|
||||||
func Exists(filepath string) bool {
|
func Exists(filepath string) bool {
|
||||||
_, err := os.Stat(filepath)
|
_, err := os.Stat(filepath)
|
||||||
if err != nil && os.IsNotExist(err) {
|
if err != nil && os.IsNotExist(err) {
|
||||||
@@ -46,74 +66,128 @@ func IsFolder(fpath string) bool {
|
|||||||
return s.IsDir()
|
return s.IsDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
//CurlGet发起一个HTTP GET请求
|
func (e *CircleByteBuffer) getLen() int {
|
||||||
func CurlGet(url string) (error, []byte) {
|
if e.start == e.end {
|
||||||
err, _, res, _, _ := Curl(url, "", HttpNul, HttpNul2, "GET")
|
return 0
|
||||||
return err, res
|
} else if e.start < e.end {
|
||||||
}
|
return e.end - e.start
|
||||||
|
|
||||||
//CurlPost发起一个基于表单的HTTP Post请求
|
|
||||||
func CurlPost(url, postdata string) (error, []byte) {
|
|
||||||
err, _, res, _, _ := Curl(url, postdata, HttpNul, HttpNul2, "POST")
|
|
||||||
return err, res
|
|
||||||
}
|
|
||||||
|
|
||||||
//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 {
|
} else {
|
||||||
method = "GET"
|
return e.start - e.end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BytePostData := []byte(postdata)
|
func (e *CircleByteBuffer) getFree() int {
|
||||||
if postdata == "" || len(postdata) == 0 {
|
return e.size - e.getLen()
|
||||||
req, _ = http.NewRequest(method, url, nil)
|
}
|
||||||
|
func (e *CircleByteBuffer) putByte(b byte) error {
|
||||||
|
if e.isClose {
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
e.datas[e.end] = b
|
||||||
|
var pos = e.end + 1
|
||||||
|
for pos == e.start {
|
||||||
|
if e.isClose {
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
time.Sleep(time.Microsecond)
|
||||||
|
}
|
||||||
|
if pos == e.size {
|
||||||
|
e.end = 0
|
||||||
} else {
|
} else {
|
||||||
req, _ = http.NewRequest(method, url, bytes.NewBuffer(BytePostData))
|
e.end = pos
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len(header) == 0 || header == nil) && method == "POST" {
|
func (e *CircleByteBuffer) getByte() (byte, error) {
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
if e.isClose {
|
||||||
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
for k, v := range header {
|
if e.isEnd && e.getLen() <= 0 {
|
||||||
req.Header.Set(k, v)
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
if len(cookie) != 0 {
|
if e.getLen() <= 0 {
|
||||||
for k, v := range cookie {
|
return 0, errors.New("no datas")
|
||||||
req.AddCookie(&http.Cookie{Name: k, Value: v, HttpOnly: true})
|
}
|
||||||
|
var ret = e.datas[e.start]
|
||||||
|
e.start++
|
||||||
|
if e.start == e.size {
|
||||||
|
e.start = 0
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
func (e *CircleByteBuffer) geti(i int) byte {
|
||||||
|
if i >= e.getLen() {
|
||||||
|
panic("out buffer")
|
||||||
|
}
|
||||||
|
var pos = e.start + i
|
||||||
|
if pos >= e.size {
|
||||||
|
pos -= e.size
|
||||||
|
}
|
||||||
|
return e.datas[pos]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*func (e*CircleByteBuffer)puts(bts []byte){
|
||||||
|
for i:=0;i<len(bts);i++{
|
||||||
|
e.put(bts[i])
|
||||||
}
|
}
|
||||||
client := &http.Client{
|
}
|
||||||
Transport: &http.Transport{
|
func (e*CircleByteBuffer)gets(bts []byte)int{
|
||||||
Dial: func(netw, addr string) (net.Conn, error) {
|
if bts==nil {return 0}
|
||||||
deadline := time.Now().Add(time.Duration(HttpTimeOut) * time.Second)
|
var ret=0
|
||||||
c, err := net.DialTimeout(netw, addr, time.Second*time.Duration(HttpTimeOut))
|
for i:=0;i<len(bts);i++{
|
||||||
|
if e.getLen()<=0{break}
|
||||||
|
bts[i]=e.get()
|
||||||
|
ret++
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}*/
|
||||||
|
func (e *CircleByteBuffer) Close() error {
|
||||||
|
e.isClose = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (e *CircleByteBuffer) Read(bts []byte) (int, error) {
|
||||||
|
if e.isClose {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
if bts == nil {
|
||||||
|
return 0, errors.New("bts is nil")
|
||||||
|
}
|
||||||
|
var ret = 0
|
||||||
|
for i := 0; i < len(bts); i++ {
|
||||||
|
b, err := e.getByte()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
if err == io.EOF {
|
||||||
|
return ret, err
|
||||||
}
|
}
|
||||||
c.SetDeadline(deadline)
|
return ret, nil
|
||||||
return c, nil
|
}
|
||||||
},
|
bts[i] = b
|
||||||
}}
|
ret++
|
||||||
resp, err := client.Do(req)
|
}
|
||||||
var rte []*http.Cookie
|
if e.isClose {
|
||||||
|
return ret, io.EOF
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
func (e *CircleByteBuffer) Write(bts []byte) (int, error) {
|
||||||
|
if e.isClose {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
if bts == nil {
|
||||||
|
e.isEnd = true
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
var ret = 0
|
||||||
|
for i := 0; i < len(bts); i++ {
|
||||||
|
err := e.putByte(bts[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err, 0, []byte(""), req.Header, rte
|
fmt.Println("Write bts err:", err)
|
||||||
|
return ret, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
ret++
|
||||||
|
}
|
||||||
statuscode := resp.StatusCode
|
if e.isClose {
|
||||||
hea := resp.Header
|
return ret, io.EOF
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
}
|
||||||
return nil, statuscode, body, hea, resp.Cookies()
|
return ret, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user