From 1a8d46f61ebfec1521c156547e0e5fe2e33c68b7 Mon Sep 17 00:00:00 2001 From: ren yuze Date: Fri, 28 Jun 2019 11:11:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=89=B2=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crypto.go | 530 +++++++++++++++++++++++++++++ database.go | 131 ++++++++ ini.go | 135 ++++++++ shell.go | 162 +++++++++ starainrt.go | 927 --------------------------------------------------- 5 files changed, 958 insertions(+), 927 deletions(-) create mode 100644 crypto.go create mode 100644 database.go create mode 100644 ini.go create mode 100644 shell.go diff --git a/crypto.go b/crypto.go new file mode 100644 index 0000000..f0d607f --- /dev/null +++ b/crypto.go @@ -0,0 +1,530 @@ +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" +) + +func 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 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 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 := 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 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 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 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) +} diff --git a/database.go b/database.go new file mode 100644 index 0000000..13902f0 --- /dev/null +++ b/database.go @@ -0,0 +1,131 @@ +package starainrt + +import ( + "database/sql" + "errors" + "strconv" +) + +func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) { + var ii int = 0 + records := make(map[int]map[string]string) + columns, err := rows.Columns() + if err != nil { + return err, records + } + scanArgs := make([]interface{}, len(columns)) + values := make([]interface{}, len(columns)) + for i := range values { + scanArgs[i] = &values[i] + } + for rows.Next() { + if err := rows.Scan(scanArgs...); err != nil { + return err, records + } + record := make(map[string]string) + for i, col := range values { + switch vtype := col.(type) { + case float64: + record[columns[i]] = strconv.FormatFloat(vtype, 'f', -1, 64) + case int64: + record[columns[i]] = strconv.FormatInt(vtype, 10) + case string: + record[columns[i]] = vtype + default: + record[columns[i]] = string(vtype.([]byte)) + } + } + records[ii] = record + ii++ + } + return nil, records +} + +func OpenDB(Method, ConnStr string) error { + var err error + DBRes, err = sql.Open(Method, ConnStr) + if err != nil { + return err + } + err = DBRes.Ping() + return err +} +func CloseDB() { + DBRes.Close() + DBRows.Close() +} + +func Query(args ...interface{}) (error, map[int]map[string]string) { + var err error + records := make(map[int]map[string]string) + if err = DBRes.Ping(); err != nil { + return err, records + } + if len(args) == 0 { + return errors.New("no args"), records + } + if len(args) == 1 { + sql := args[0] + if DBRows, err = DBRes.Query(sql.(string)); err != nil { + return err, records + } + return FetchAll(DBRows) + } + sql := args[0] + stmt, err := DBRes.Prepare(sql.(string)) + defer stmt.Close() + if err != nil { + return err, records + } + var para []interface{} + for k, v := range args { + if k != 0 { + switch vtype := v.(type) { + default: + para = append(para, vtype) + } + } + } + if DBRows, err = stmt.Query(para...); err != nil { + return err, records + } + return FetchAll(DBRows) + +} + +func DBExec(args ...interface{}) error { + var err error + if err = DBRes.Ping(); err != nil { + return err + } + if len(args) == 0 { + return errors.New("no args") + } + if len(args) == 1 { + sql := args[0] + if _, err = DBRes.Exec(sql.(string)); err != nil { + return err + } + return nil + } + sql := args[0] + stmt, err := DBRes.Prepare(sql.(string)) + defer stmt.Close() + if err != nil { + return err + } + var para []interface{} + for k, v := range args { + if k != 0 { + switch vtype := v.(type) { + default: + para = append(para, vtype) + } + } + } + if _, err = stmt.Exec(para...); err != nil { + return err + } + return nil + +} diff --git a/ini.go b/ini.go new file mode 100644 index 0000000..acbaee5 --- /dev/null +++ b/ini.go @@ -0,0 +1,135 @@ +package starainrt + +import ( + "bufio" + "io" + "io/ioutil" + "os" + "regexp" + "strings" +) + +func ReWriteFileByKV(filepath, rem, conn, ret string, kv map[string]string) bool { + var outputstr string + var ataru bool = false + if (!Exists(filepath)) || (!IsFile(filepath)) { + return false + } + fso, err := os.Open(filepath) + if err != nil { + return false + } + rempar := regexp.MustCompile("^" + rem) + buf := bufio.NewReader(fso) + for { + bytxt, _, eof := buf.ReadLine() + if eof == io.EOF { + break + } + txt := strings.TrimSpace(string(bytxt)) + if rempar.Match([]byte(txt)) { + outputstr += txt + ret + continue + } + for k, v := range kv { + keypar := regexp.MustCompile("^" + k) + if keypar.Match([]byte(txt)) { + ataru = true + outputstr += k + conn + v + ret + delete(kv, k) + } + } + if ataru { + ataru = false + continue + } else { + outputstr += txt + ret + } + } + fso.Close() + if len(kv) != 0 { + for k, v := range kv { + outputstr += k + conn + v + ret + delete(kv, k) + } + } + err = ioutil.WriteFile(filepath, []byte(outputstr), 0755) + if err != nil { + return false + } + return true +} + +func GetINIValue(filepath, rem, conn string, kv []string) (bool, map[string]string) { + outputstr := make(map[string]string) + if (!Exists(filepath)) || (!IsFile(filepath)) { + return false, outputstr + } + fso, err := os.Open(filepath) + if err != nil { + return false, outputstr + } + rempar := regexp.MustCompile("^" + rem) + buf := bufio.NewReader(fso) + for { + bytxt, _, eof := buf.ReadLine() + if eof == io.EOF { + break + } + txt := strings.TrimSpace(string(bytxt)) + if rempar.Match([]byte(txt)) { + continue + } + var i int = 0 + var v2 string + for k, v := range kv { + if v == "" { + continue + } + v2 = "" + for _, b := range []byte(v) { + switch string(b) { + case ".": + v2 += "\\." + case "\\": + v2 += "\\" + case "-": + v2 += "\\-" + case "(": + v2 += "\\(" + case ")": + v2 += "\\)" + case "{": + v2 += "\\{" + case "}": + v2 += "\\}" + case "[": + v2 += "\\[" + case "]": + v2 += "\\]" + case "$": + v2 += "\\]$" + case "^": + v2 += "\\^" + default: + v2 += string(b) + } + } + keypar := regexp.MustCompile("^" + v2 + ".*?" + conn + "(.*)") + if keypar.Match([]byte(txt)) { + i++ + kekka := keypar.FindSubmatch([]byte(txt)) + outputstr[v] = string(kekka[1]) + kv[k] = "" + } + } + if i != len(kv) { + for _, v := range kv { + if v != "" { + outputstr[v] = "" + } + } + } + } + return true, outputstr +} diff --git a/shell.go b/shell.go new file mode 100644 index 0000000..74e0966 --- /dev/null +++ b/shell.go @@ -0,0 +1,162 @@ +package starainrt + +import ( + "bufio" + "fmt" + "io" + "os" + "os/exec" + "runtime" + "strings" + "syscall" + "time" +) + +type suncli struct { + outfile io.ReadCloser + infile io.WriteCloser + errfile io.ReadCloser + cmd *exec.Cmd + thread bool + counter int +} + +func (this suncli) IsExit() bool { + return ShellExit +} + +func NewPipeShell(command string, arg ...string) (*suncli, error) { + var err error + lovecli := suncli{} + lovecli.counter = 0 + cmd := exec.Command(command, arg...) + lovecli.cmd = cmd + lovecli.infile, err = lovecli.cmd.StdinPipe() + if err != nil { + return &lovecli, err + } + lovecli.outfile, err = lovecli.cmd.StdoutPipe() + if err != nil { + return &lovecli, err + } + lovecli.errfile, err = lovecli.cmd.StderrPipe() + if err != nil { + return &lovecli, err + } + if err := lovecli.cmd.Start(); err != nil { + return &lovecli, err + } + go func() { + lovecli.cmd.Wait() + }() + ShellExit = false + lovecli.thread = false + return &lovecli, nil +} + +func (this suncli) GetResult(maxtime int) (string, string, bool) { + var stop bool + reader := bufio.NewReader(this.outfile) + erreader := bufio.NewReader(this.errfile) + if !this.thread { + this.thread = true + go func() { + var line2 string + var stack bool = false + stop = false + for { + if !stack { + go func() { + stack = true + if erreader.Size() > 0 { + line2, _ = erreader.ReadString('\n') + ShellErr += line2 + line2 = "" + } + stack = false + }() + } + line, err2 := reader.ReadString('\n') + if err2 != nil || io.EOF == err2 { + stop = true + break + } + this.counter++ + ShellRes += line + } + }() + } + waittm := 0 + for !stop { + time.Sleep(time.Millisecond * 250) + waittm += 1 + if maxtime >= 0 { + if waittm/4 > maxtime { + restr := ShellRes + ShellRes = "" + errstr := ShellErr + ShellErr = "" + return restr, errstr, false + } + } + } + ShellExit = true + this.thread = false + restr := ShellRes + ShellRes = "" + errstr := ShellErr + ShellErr = "" + return restr, errstr, true +} + +func (this suncli) Exec(cmdstr string, maxtime int) (string, string, bool) { + this.infile.Write([]byte(cmdstr + "\n")) + return this.GetResult(maxtime) +} + +func (this suncli) WriteCmd(cmdstr string) { + this.infile.Write([]byte(cmdstr + "\n")) + return +} + +func (this suncli) ExitCode() int { + return this.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() +} + +func MsgBox(text, defaults string) string { + var input string + input = defaults + fmt.Print(text) + fmt.Scanln(&input) + return strings.TrimSpace(input) +} + +func MessageBox(text, defaults string) string { + fmt.Print(text) + inputReader := bufio.NewReader(os.Stdin) + str, _ := inputReader.ReadString('\n') + if runtime.GOOS == "windows" { + str = str[0 : len(str)-2] + } else { + str = str[0 : len(str)-1] + } + if str == "" { + str = defaults + } + return strings.TrimSpace(str) +} + +func YesNo(text string, defaults bool) bool { + res := strings.ToUpper(MsgBox(text, "")) + if res == "" { + return defaults + } + res = res[0:1] + if res == "Y" { + return true + } else if res == "N" { + return false + } else { + return defaults + } +} diff --git a/starainrt.go b/starainrt.go index bc710a6..2bd160c 100644 --- a/starainrt.go +++ b/starainrt.go @@ -1,30 +1,12 @@ package starainrt import ( - "bufio" "bytes" - "crypto/md5" - "crypto/rc4" - "crypto/sha1" - "crypto/sha256" - "crypto/sha512" "database/sql" - "encoding/base64" - "encoding/hex" - "errors" - "fmt" - "hash/crc32" - "io" "io/ioutil" "net" "net/http" "os" - "os/exec" - "regexp" - "runtime" - "strconv" - "strings" - "syscall" "time" ) @@ -56,642 +38,6 @@ func IsFolder(fpath string) bool { } return s.IsDir() } -func ReWriteFileByKV(filepath, rem, conn, ret string, kv map[string]string) bool { - var outputstr string - var ataru bool = false - if (!Exists(filepath)) || (!IsFile(filepath)) { - return false - } - fso, err := os.Open(filepath) - if err != nil { - return false - } - rempar := regexp.MustCompile("^" + rem) - buf := bufio.NewReader(fso) - for { - bytxt, _, eof := buf.ReadLine() - if eof == io.EOF { - break - } - txt := strings.TrimSpace(string(bytxt)) - if rempar.Match([]byte(txt)) { - outputstr += txt + ret - continue - } - for k, v := range kv { - keypar := regexp.MustCompile("^" + k) - if keypar.Match([]byte(txt)) { - ataru = true - outputstr += k + conn + v + ret - delete(kv, k) - } - } - if ataru { - ataru = false - continue - } else { - outputstr += txt + ret - } - } - fso.Close() - if len(kv) != 0 { - for k, v := range kv { - outputstr += k + conn + v + ret - delete(kv, k) - } - } - err = ioutil.WriteFile(filepath, []byte(outputstr), 0755) - if err != nil { - return false - } - return true -} - -func GetINIValue(filepath, rem, conn string, kv []string) (bool, map[string]string) { - outputstr := make(map[string]string) - if (!Exists(filepath)) || (!IsFile(filepath)) { - return false, outputstr - } - fso, err := os.Open(filepath) - if err != nil { - return false, outputstr - } - rempar := regexp.MustCompile("^" + rem) - buf := bufio.NewReader(fso) - for { - bytxt, _, eof := buf.ReadLine() - if eof == io.EOF { - break - } - txt := strings.TrimSpace(string(bytxt)) - if rempar.Match([]byte(txt)) { - continue - } - var i int = 0 - var v2 string - for k, v := range kv { - if v == "" { - continue - } - v2 = "" - for _, b := range []byte(v) { - switch string(b) { - case ".": - v2 += "\\." - case "\\": - v2 += "\\" - case "-": - v2 += "\\-" - case "(": - v2 += "\\(" - case ")": - v2 += "\\)" - case "{": - v2 += "\\{" - case "}": - v2 += "\\}" - case "[": - v2 += "\\[" - case "]": - v2 += "\\]" - case "$": - v2 += "\\]$" - case "^": - v2 += "\\^" - default: - v2 += string(b) - } - } - keypar := regexp.MustCompile("^" + v2 + ".*?" + conn + "(.*)") - if keypar.Match([]byte(txt)) { - i++ - kekka := keypar.FindSubmatch([]byte(txt)) - outputstr[v] = string(kekka[1]) - kv[k] = "" - } - } - if i != len(kv) { - for _, v := range kv { - if v != "" { - outputstr[v] = "" - } - } - } - } - return true, outputstr -} - -func 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 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 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 := 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 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 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 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) -} func CurlGet(url string) (error, []byte) { err, _, res, _, _ := Curl(url, "", HttpNul, HttpNul2, "GET") @@ -760,276 +106,3 @@ func Curl(url string, postdata string, header map[string]string, cookie map[stri return nil, statuscode, body, hea, resp.Cookies() } - -func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) { - var ii int = 0 - records := make(map[int]map[string]string) - columns, err := rows.Columns() - if err != nil { - return err, records - } - scanArgs := make([]interface{}, len(columns)) - values := make([]interface{}, len(columns)) - for i := range values { - scanArgs[i] = &values[i] - } - for rows.Next() { - if err := rows.Scan(scanArgs...); err != nil { - return err, records - } - record := make(map[string]string) - for i, col := range values { - switch vtype := col.(type) { - case float64: - record[columns[i]] = strconv.FormatFloat(vtype, 'f', -1, 64) - case int64: - record[columns[i]] = strconv.FormatInt(vtype, 10) - case string: - record[columns[i]] = vtype - default: - record[columns[i]] = string(vtype.([]byte)) - } - } - records[ii] = record - ii++ - } - return nil, records -} - -func OpenDB(Method, ConnStr string) error { - var err error - DBRes, err = sql.Open(Method, ConnStr) - if err != nil { - return err - } - err = DBRes.Ping() - return err -} -func CloseDB() { - DBRes.Close() - DBRows.Close() -} - -func Query(args ...interface{}) (error, map[int]map[string]string) { - var err error - records := make(map[int]map[string]string) - if err = DBRes.Ping(); err != nil { - return err, records - } - if len(args) == 0 { - return errors.New("no args"), records - } - if len(args) == 1 { - sql := args[0] - if DBRows, err = DBRes.Query(sql.(string)); err != nil { - return err, records - } - return FetchAll(DBRows) - } - sql := args[0] - stmt, err := DBRes.Prepare(sql.(string)) - defer stmt.Close() - if err != nil { - return err, records - } - var para []interface{} - for k, v := range args { - if k != 0 { - switch vtype := v.(type) { - default: - para = append(para, vtype) - } - } - } - if DBRows, err = stmt.Query(para...); err != nil { - return err, records - } - return FetchAll(DBRows) - -} - -func DBExec(args ...interface{}) error { - var err error - if err = DBRes.Ping(); err != nil { - return err - } - if len(args) == 0 { - return errors.New("no args") - } - if len(args) == 1 { - sql := args[0] - if _, err = DBRes.Exec(sql.(string)); err != nil { - return err - } - return nil - } - sql := args[0] - stmt, err := DBRes.Prepare(sql.(string)) - defer stmt.Close() - if err != nil { - return err - } - var para []interface{} - for k, v := range args { - if k != 0 { - switch vtype := v.(type) { - default: - para = append(para, vtype) - } - } - } - if _, err = stmt.Exec(para...); err != nil { - return err - } - return nil - -} - -type suncli struct { - outfile io.ReadCloser - infile io.WriteCloser - errfile io.ReadCloser - cmd *exec.Cmd - thread bool - counter int -} - -func (this suncli) IsExit() bool { - return ShellExit -} - -func NewPipeShell(command string, arg ...string) (*suncli, error) { - var err error - lovecli := suncli{} - lovecli.counter = 0 - cmd := exec.Command(command, arg...) - lovecli.cmd = cmd - lovecli.infile, err = lovecli.cmd.StdinPipe() - if err != nil { - return &lovecli, err - } - lovecli.outfile, err = lovecli.cmd.StdoutPipe() - if err != nil { - return &lovecli, err - } - lovecli.errfile, err = lovecli.cmd.StderrPipe() - if err != nil { - return &lovecli, err - } - if err := lovecli.cmd.Start(); err != nil { - return &lovecli, err - } - go func() { - lovecli.cmd.Wait() - }() - ShellExit = false - lovecli.thread = false - return &lovecli, nil -} - -func (this suncli) GetResult(maxtime int) (string, string, bool) { - var stop bool - reader := bufio.NewReader(this.outfile) - erreader := bufio.NewReader(this.errfile) - if !this.thread { - this.thread = true - go func() { - var line2 string - var stack bool = false - stop = false - for { - if !stack { - go func() { - stack = true - if erreader.Size() > 0 { - line2, _ = erreader.ReadString('\n') - ShellErr += line2 - line2 = "" - } - stack = false - }() - } - line, err2 := reader.ReadString('\n') - if err2 != nil || io.EOF == err2 { - stop = true - break - } - this.counter++ - ShellRes += line - } - }() - } - waittm := 0 - for !stop { - time.Sleep(time.Millisecond * 250) - waittm += 1 - if maxtime >= 0 { - if waittm/4 > maxtime { - restr := ShellRes - ShellRes = "" - errstr := ShellErr - ShellErr = "" - return restr, errstr, false - } - } - } - ShellExit = true - this.thread = false - restr := ShellRes - ShellRes = "" - errstr := ShellErr - ShellErr = "" - return restr, errstr, true -} - -func (this suncli) Exec(cmdstr string, maxtime int) (string, string, bool) { - this.infile.Write([]byte(cmdstr + "\n")) - return this.GetResult(maxtime) -} - -func (this suncli) WriteCmd(cmdstr string) { - this.infile.Write([]byte(cmdstr + "\n")) - return -} - -func (this suncli) ExitCode() int { - return this.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() -} - -func MsgBox(text, defaults string) string { - var input string - input = defaults - fmt.Print(text) - fmt.Scanln(&input) - return strings.TrimSpace(input) -} - -func MessageBox(text, defaults string) string { - fmt.Print(text) - inputReader := bufio.NewReader(os.Stdin) - str, _ := inputReader.ReadString('\n') - if runtime.GOOS == "windows" { - str = str[0 : len(str)-2] - } else { - str = str[0 : len(str)-1] - } - if str == "" { - str = defaults - } - return strings.TrimSpace(str) -} - -func YesNo(text string, defaults bool) bool { - res := strings.ToUpper(MsgBox(text, "")) - if res == "" { - return defaults - } - res = res[0:1] - if res == "Y" { - return true - } else if res == "N" { - return false - } else { - return defaults - } -}