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,23 +748,23 @@ 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()
} }
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)
columns, err:= rows.Columns() columns, err := rows.Columns()
if err!=nil { if err != nil {
return err,records return err, records
} }
scanArgs := make([]interface{}, len(columns)) scanArgs := make([]interface{}, len(columns))
values := make([]interface{}, len(columns)) values := make([]interface{}, len(columns))
@ -771,103 +772,210 @@ func FetchAll(rows *sql.Rows)(error,map[int]map[string]string){
scanArgs[i] = &values[i] scanArgs[i] = &values[i]
} }
for rows.Next() { for rows.Next() {
if err := rows.Scan(scanArgs...);err!=nil{ if err := rows.Scan(scanArgs...); err != nil {
return err,records return err, records
} }
record := make(map[string]string) record := make(map[string]string)
for i, col := range values { for i, col := range values {
switch vtype:=col.(type){ switch vtype := col.(type) {
case int64: case int64:
record[columns[i]] = strconv.FormatInt(vtype,10) record[columns[i]] = strconv.FormatInt(vtype, 10)
default: default:
record[columns[i]] = string(vtype.([]byte)) record[columns[i]] = string(vtype.([]byte))
} }
} }
records[ii]=record records[ii] = record
ii++ ii++
} }
return nil,records return nil, records
} }
func OpenDB(Method,ConnStr string)error{ func OpenDB(Method, ConnStr string) error {
var err error var err error
DBRes,err=sql.Open(Method,ConnStr) DBRes, err = sql.Open(Method, ConnStr)
return err return err
} }
func CloseDB(){ func CloseDB() {
DBRes.Close() DBRes.Close()
DBRows.Close() DBRows.Close()
} }
func Query(args ...interface{})(error,map[int]map[string]string){ func Query(args ...interface{}) (error, map[int]map[string]string) {
var err error var err error
records := make(map[int]map[string]string) records := make(map[int]map[string]string)
if err=DBRes.Ping();err!=nil{ if err = DBRes.Ping(); err != nil {
return err,records return err, records
} }
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
} }
return FetchAll(DBRows) return FetchAll(DBRows)
} }
sql:=args[0] sql := args[0]
stmt,err:=DBRes.Prepare(sql.(string)) stmt, err := DBRes.Prepare(sql.(string))
if err!=nil{ if err != nil {
return err,records return err, records
} }
var para []interface{} var para []interface{}
for k,v:=range args{ for k, v := range args {
if k!=0{ if k != 0 {
switch vtype:=v.(type){ switch vtype := v.(type) {
default: default:
para=append(para,vtype) para = append(para, vtype)
} }
} }
} }
if DBRows,err=stmt.Query(para...);err!=nil{ if DBRows, err = stmt.Query(para...); err != nil {
return err,records return err, records
} }
return FetchAll(DBRows) return FetchAll(DBRows)
} }
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
} }
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
} }
return nil return nil
} }
sql:=args[0] sql := args[0]
stmt,err:=DBRes.Prepare(sql.(string)) stmt, err := DBRes.Prepare(sql.(string))
if err!=nil{ if err != nil {
return err return err
} }
var para []interface{} var para []interface{}
for k,v:=range args{ for k, v := range args {
if k!=0{ if k != 0 {
switch vtype:=v.(type){ switch vtype := v.(type) {
default: default:
para=append(para,vtype) para = append(para, vtype)
} }
} }
} }
if _,err=stmt.Exec(para...);err!=nil{ if _, err = stmt.Exec(para...); err != nil {
return err return err
} }
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