update kokan cmd

master
兔子 6 years ago
parent c036036285
commit 941d54b886

@ -9,9 +9,9 @@ import (
"crypto/sha256" "crypto/sha256"
"crypto/sha512" "crypto/sha512"
"database/sql" "database/sql"
"errors"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"hash/crc32" "hash/crc32"
"io" "io"
@ -19,17 +19,19 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"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 DBRes *sql.DB
var DBRows *sql.Rows var DBRows *sql.Rows
var ShellRes, ShellErr string
var ShellExit bool
func Exists(filepath string) bool { func Exists(filepath string) bool {
_, err := os.Stat(filepath) _, err := os.Stat(filepath)
@ -689,12 +691,12 @@ func AttachFile(source, target, output string) (bool, string) {
return true, strconv.FormatInt(filesize, 10) return true, strconv.FormatInt(filesize, 10)
} }
func CurlGet(url string) (error, string) { func CurlGet(url string) (error, []byte) {
err, _, res, _, _ := Curl(url, "", HttpNul, HttpNul2, "GET") err, _, res, _, _ := Curl(url, "", HttpNul, HttpNul2, "GET")
return err, res return err, res
} }
func CurlPost(url, postdata string) (error, string) { func CurlPost(url, postdata string) (error, []byte) {
err, _, res, _, _ := Curl(url, postdata, HttpNul, HttpNul2, "POST") err, _, res, _, _ := Curl(url, postdata, HttpNul, HttpNul2, "POST")
return err, res return err, res
} }
@ -703,7 +705,7 @@ func HttpNulReset() {
HttpNul, HttpNul2 = tmp, tmp HttpNul, HttpNul2 = tmp, tmp
} }
func Curl(url string, postdata string, header map[string]string, cookie map[string]string, method string) (error, int, string, http.Header, []*http.Cookie) { 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 var req *http.Request
if method == "" { if method == "" {
if len(postdata) != 0 { if len(postdata) != 0 {
@ -727,8 +729,7 @@ func Curl(url string, postdata string, header map[string]string, cookie map[stri
} }
if len(cookie) != 0 { if len(cookie) != 0 {
for k, v := range cookie { for k, v := range cookie {
cookie1 := &http.Cookie{Name: k, Value: v, HttpOnly: true} req.AddCookie(&http.Cookie{Name: k, Value: v, HttpOnly: true})
req.AddCookie(cookie1)
} }
} }
@ -747,14 +748,14 @@ func Curl(url string, postdata string, header map[string]string, cookie map[stri
resp, err := client.Do(req) resp, err := client.Do(req)
var rte []*http.Cookie var rte []*http.Cookie
if err != nil { if err != nil {
return err, 0, "", req.Header, rte return err, 0, []byte(""), req.Header, rte
} }
defer resp.Body.Close() defer resp.Body.Close()
statuscode := resp.StatusCode statuscode := resp.StatusCode
hea := resp.Header hea := resp.Header
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
return nil, statuscode, string(body), hea, resp.Cookies() return nil, statuscode, body, hea, resp.Cookies()
} }
@ -808,7 +809,7 @@ func Query(args ...interface{})(error,map[int]map[string]string){
if len(args) == 0 { if len(args) == 0 {
return errors.New("no args"), records return errors.New("no args"), records
} }
if(len(args)==1){ if len(args) == 1 {
sql := args[0] sql := args[0]
if DBRows, err = DBRes.Query(sql.(string)); err != nil { if DBRows, err = DBRes.Query(sql.(string)); err != nil {
return err, records return err, records
@ -836,7 +837,7 @@ func Query(args ...interface{})(error,map[int]map[string]string){
} }
func DBExec(args ...interface{})(error){ func DBExec(args ...interface{}) error {
var err error var err error
if err = DBRes.Ping(); err != nil { if err = DBRes.Ping(); err != nil {
return err return err
@ -844,7 +845,7 @@ func DBExec(args ...interface{})(error){
if len(args) == 0 { if len(args) == 0 {
return errors.New("no args") return errors.New("no args")
} }
if(len(args)==1){ if len(args) == 1 {
sql := args[0] sql := args[0]
if _, err = DBRes.Exec(sql.(string)); err != nil { if _, err = DBRes.Exec(sql.(string)); err != nil {
return err return err
@ -871,3 +872,110 @@ func DBExec(args ...interface{})(error){
return nil 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
}

Loading…
Cancel
Save