update kokan cmd

master
兔子 5 years ago
parent c036036285
commit 941d54b886

@ -9,9 +9,9 @@ import (
"crypto/sha256"
"crypto/sha512"
"database/sql"
"errors"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"hash/crc32"
"io"
@ -19,17 +19,19 @@ import (
"net"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)
var HttpNul, HttpNul2 map[string]string
var HttpTimeOut int64 = 15
var DBRes *sql.DB
var DBRows *sql.Rows
var ShellRes, ShellErr string
var ShellExit bool
func Exists(filepath string) bool {
_, err := os.Stat(filepath)
@ -689,12 +691,12 @@ func AttachFile(source, target, output string) (bool, string) {
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")
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")
return err, res
}
@ -703,7 +705,7 @@ func HttpNulReset() {
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
if method == "" {
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 {
for k, v := range cookie {
cookie1 := &http.Cookie{Name: k, Value: v, HttpOnly: true}
req.AddCookie(cookie1)
req.AddCookie(&http.Cookie{Name: k, Value: v, HttpOnly: true})
}
}
@ -747,23 +748,23 @@ func Curl(url string, postdata string, header map[string]string, cookie map[stri
resp, err := client.Do(req)
var rte []*http.Cookie
if err != nil {
return err, 0, "", req.Header, rte
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, string(body), hea, resp.Cookies()
return nil, statuscode, body, hea, resp.Cookies()
}
func FetchAll(rows *sql.Rows)(error,map[int]map[string]string){
var ii int=0
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
columns, err := rows.Columns()
if err != nil {
return err, records
}
scanArgs := 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]
}
for rows.Next() {
if err := rows.Scan(scanArgs...);err!=nil{
return err,records
if err := rows.Scan(scanArgs...); err != nil {
return err, records
}
record := make(map[string]string)
for i, col := range values {
switch vtype:=col.(type){
switch vtype := col.(type) {
case int64:
record[columns[i]] = strconv.FormatInt(vtype,10)
record[columns[i]] = strconv.FormatInt(vtype, 10)
default:
record[columns[i]] = string(vtype.([]byte))
}
}
records[ii]=record
records[ii] = record
ii++
}
return nil,records
return nil, records
}
func OpenDB(Method,ConnStr string)error{
func OpenDB(Method, ConnStr string) error {
var err error
DBRes,err=sql.Open(Method,ConnStr)
DBRes, err = sql.Open(Method, ConnStr)
return err
}
func CloseDB(){
func CloseDB() {
DBRes.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
records := make(map[int]map[string]string)
if err=DBRes.Ping();err!=nil{
return err,records
if err = DBRes.Ping(); err != nil {
return err, records
}
if len(args)==0 {
return errors.New("no args"),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
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))
if err!=nil{
return err,records
sql := args[0]
stmt, err := DBRes.Prepare(sql.(string))
if err != nil {
return err, records
}
var para []interface{}
for k,v:=range args{
if k!=0{
switch vtype:=v.(type){
for k, v := range args {
if k != 0 {
switch vtype := v.(type) {
default:
para=append(para,vtype)
para = append(para, vtype)
}
}
}
if DBRows,err=stmt.Query(para...);err!=nil{
return err,records
if DBRows, err = stmt.Query(para...); err != nil {
return err, records
}
return FetchAll(DBRows)
}
func DBExec(args ...interface{})(error){
func DBExec(args ...interface{}) error {
var err error
if err=DBRes.Ping();err!=nil{
if err = DBRes.Ping(); err != nil {
return err
}
if len(args)==0 {
if len(args) == 0 {
return errors.New("no args")
}
if(len(args)==1){
sql:=args[0]
if _,err=DBRes.Exec(sql.(string));err!=nil{
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))
if err!=nil{
sql := args[0]
stmt, err := DBRes.Prepare(sql.(string))
if err != nil {
return err
}
var para []interface{}
for k,v:=range args{
if k!=0{
switch vtype:=v.(type){
for k, v := range args {
if k != 0 {
switch vtype := v.(type) {
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 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