andelf / go-curl

golang curl(libcurl) binding.
Apache License 2.0
487 stars 130 forks source link

运行一会儿报错 #19

Open millken opened 10 years ago

millken commented 10 years ago
goroutine 246 [syscall]:
github.com/andelf/go-curl._Cfunc_curl_easy_perform(0x7f9aac05caa0, 0x7f9af4b64ad8)
    github.com/andelf/go-curl/_obj/_cgo_defun.c:132 +0x31
github.com/andelf/go-curl.(*CURL).Perform(0xc2119ad3c0, 0x4e2b, 0x63a2c0)
    /home/golang/ab/src/github.com/andelf/go-curl/easy.go:281 +0x2a
main.(*Curlx).Send(0xc2119b02d0, 0x0, 0x0)
    /home/golang/ab/curl.go:123 +0x734
main.fastcc(0x6b57c0, 0x3, 0xc21001fb40, 0x42, 0x0, ...)
    /home/golang/ab/cc.go:132 +0x6c9
created by main.workerConfig
    /home/golang/ab/worker.go:143 +0xe26

goroutine 385 [syscall]:
github.com/andelf/go-curl._Cfunc_curl_easy_perform(0x7f9ad80008c0, 0x7f9af4b62ad8)
    github.com/andelf/go-curl/_obj/_cgo_defun.c:132 +0x31
github.com/andelf/go-curl.(*CURL).Perform(0xc2119c5780, 0x4e2b, 0x63a2c0)
    /home/golang/ab/src/github.com/andelf/go-curl/easy.go:281 +0x2a
main.(*Curlx).Send(0xc21197ad20, 0x0, 0x0)
    /home/golang/ab/curl.go:123 +0x734
main.fastcc(0x6b57c0, 0x3, 0xc21001fb40, 0x42, 0x0, ...)
    /home/golang/ab/cc.go:132 +0x6c9
created by main.workerConfig
    /home/golang/ab/worker.go:143 +0xe26
andelf commented 10 years ago

麻烦贴下源代码

millken commented 10 years ago

curl.go

package main

import (
    "bytes"
    "errors"
    curl "github.com/andelf/go-curl"
    "net/url"
    //"os"
    "fmt"
    //"time"
)

type Curlx struct {
    Debug          bool //调试开关
    Url            string
    Headers        map[string]string
    ClientIp       string //本机外网IP,可选
    TargetIp       string
    ProxyUrl       string //代理URL
    PostData       string
    ConnectTimeout int          //连接超时时间,秒
    Result         bytes.Buffer //保存结果
    Err            error        //报错信息
}

func NewCurlx() (c *Curlx) {
    headers := make(map[string]string)
    headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36"
    reqUrl := "http://localhost/"
    return &Curlx{
        Url:            reqUrl,
        Headers:        headers,
        ConnectTimeout: 10,
        Err:            nil,
        Debug:          false,
    }
}

//设置url
func (c *Curlx) SetUrl(url string) {
    c.Url = url
}

//设置连接超时时间
func (c *Curlx) SetConnectTimeout(second int) {
    c.ConnectTimeout = second
}

//添加header
func (c *Curlx) AddHeader(key, value string) {
    c.Headers[key] = value
}

//指定出口ip
func (c *Curlx) SetClientIp(ip string) {
    c.ClientIp = ip
}

//指定目标ip
func (c *Curlx) SetTargetIp(ip string) {
    c.TargetIp = ip
}

//打开调试模式
func (c *Curlx) SetDebug() {
    c.Debug = true
}

//设置post数据
func (c *Curlx) SetPostString(data string) {
    c.PostData = data
}

//转换header map为字符数组
func (c *Curlx) HeaderString() []string {
    headers := []string{}
    for k, v := range c.Headers {
        headers = append(headers, k+": "+v)
    }
    return headers
}

//获取返回body
func (c *Curlx) GetBody() (body string, err error) {
    body = c.Result.String()
    err = c.Err
    return
}

func (c *Curlx) Send() error {
    c.Result.Reset()
    easy := curl.EasyInit()
    defer easy.Cleanup()
    if easy != nil {
        if c.Debug == true {
            easy.Setopt(curl.OPT_VERBOSE, true)
        }
        if c.ClientIp != "" {
            easy.Setopt(curl.OPT_INTERFACE, c.ClientIp)
        }
        if c.TargetIp != "" {
            u, err := url.Parse(c.Url)
            if err != nil {
                c.Err = errors.New(err.Error() + " target ip is " + c.TargetIp)
                return c.Err
            }
            RawQuery := ""
            if u.RawQuery != "" {
                RawQuery = "?" + u.RawQuery
            }
            c.Url = fmt.Sprintf("%s://%s%s%s", u.Scheme, c.TargetIp, u.Path, RawQuery)
            c.AddHeader("Host", u.Host)
        }
        easy.Setopt(curl.OPT_URL, c.Url)
        //easy.Setopt(curl.OPT_PORT, 7891)
        easy.Setopt(curl.OPT_CONNECTTIMEOUT, c.ConnectTimeout)
        easy.Setopt(curl.OPT_HTTPHEADER, c.HeaderString())
        easy.Setopt(curl.OPT_WRITEFUNCTION, func(buf []byte, userdata interface{}) bool { c.Result.Write(buf); return true })

        if c.PostData != "" {
            easy.Setopt(curl.OPT_POSTFIELDS, c.PostData)
        }
        c.Err = easy.Perform()

    }
    return c.Err
}

/*

func main() {
    c := NewCurlx()
    c.Url = "http://www.baidu.com/"

    c.ClientIp = "192.168.3.203"
    //c.SetTargetIp("220.181.111.86")
    c.SetPostString("a=b&c=d")
    c.AddHeader("Referer", "localhost")
    c.AddHeader("Accept", "text/html")
    c.Send()
    //c.Result.WriteTo(os.Stdout)
    body, err := c.GetBody()
    if err != nil {
        fmt.Println("Error : ", err.Error())
    } else {

        fmt.Println(body)
    }
}
*/

fastcc.go

...
    for {
        select {
        case <-time.After(time.Duration(w.SleepTime) * time.Millisecond):
            for uid = 0; uid < url_len; uid++ {

                if w.Random == 0 {
                    surl = urls[uid]
                } else {
                    i1 := int(float32(url_len) * rand.Float32())
                    surl = urls[i1]
                }
                surls := strings.Split(surl, " ")
                if len(surls) > 1 {
                    surl = surls[0]
                    dial_host = strings.Trim(surls[1], " ")
                } else {
                    u, _ := url.Parse(surl)
                    dial_host = u.Host
                }
                log.Debugf("%s", dial_host)
                surl = replace_random_string(surl)

                curlx := NewCurlx()
                curlx.SetUrl(surl)
                curlx.SetTargetIp(dial_host)
                curlx.SetClientIp(w.LocalIp)
                header := makeHeaderSlice()
                for k, v := range header {
                    curlx.AddHeader(k, v)
                }
                curlx.Send()//这里是上面的132行
                bodyString, err := curlx.GetBody()
...

在aws上运行,一下就崩溃了。出错信息同上。