You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
starainrt/starainrt.go

109 lines
2.5 KiB
Go

6 years ago
package starainrt
import (
"bytes"
"database/sql"
"io/ioutil"
"net"
"net/http"
"os"
"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)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
func IsFile(fpath string) bool {
s, err := os.Stat(fpath)
if err != nil {
return false
}
return !s.IsDir()
}
func IsFolder(fpath string) bool {
s, err := os.Stat(fpath)
if err != nil {
return false
}
return s.IsDir()
}
func CurlGet(url string) (error, []byte) {
err, _, res, _, _ := Curl(url, "", HttpNul, HttpNul2, "GET")
return err, res
}
func CurlPost(url, postdata string) (error, []byte) {
err, _, res, _, _ := Curl(url, postdata, HttpNul, HttpNul2, "POST")
return err, res
}
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()
}