main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package main
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "flag"
  6. "fmt"
  7. "github.com/EDDYCJY/fake-useragent"
  8. "io"
  9. "io/ioutil"
  10. "math/rand"
  11. "net/http"
  12. "os"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. "net"
  18. "github.com/apoorvam/goterminal"
  19. )
  20. func goFun(postContent string, Referer string, XforwardFor bool, customIP ipArray, wg *sync.WaitGroup) {
  21. defer func() {
  22. if r := recover(); r != nil {
  23. go goFun(postContent, Referer, XforwardFor, customIP, wg)
  24. }
  25. }()
  26. transport := &http.Transport{
  27. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  28. }
  29. for true {
  30. if customIP != nil && len(customIP) > 0 {
  31. dialer := &net.Dialer{
  32. Timeout: 30 * time.Second,
  33. KeepAlive: 30 * time.Second,
  34. }
  35. transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
  36. rand.Seed(time.Now().Unix())
  37. ip := customIP[rand.Intn(len(customIP))]
  38. if strings.HasPrefix(addr, "https") {
  39. addr = ip + ":443"
  40. } else if strings.HasPrefix(addr, "http") {
  41. addr = ip + ":80"
  42. } else {
  43. addr = ip + ":80"
  44. }
  45. return dialer.DialContext(ctx, network, addr)
  46. }
  47. transport.DialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
  48. rand.Seed(time.Now().Unix())
  49. ip := customIP[rand.Intn(len(customIP))]
  50. if strings.HasPrefix(addr, "https") {
  51. addr = ip + ":443"
  52. } else if strings.HasPrefix(addr, "http") {
  53. addr = ip + ":80"
  54. } else {
  55. addr = ip + ":80"
  56. }
  57. return dialer.DialContext(ctx, network, addr)
  58. }
  59. }
  60. var request *http.Request
  61. var err1 error = nil
  62. client := &http.Client{
  63. Transport: transport,
  64. Timeout: time.Second * 10,
  65. }
  66. if len(postContent) > 0 {
  67. request, err1 = http.NewRequest("POST", TargetUrl, strings.NewReader(postContent))
  68. } else {
  69. request, err1 = http.NewRequest("GET", TargetUrl, nil)
  70. }
  71. if err1 != nil {
  72. continue
  73. }
  74. if len(Referer) == 0 {
  75. Referer = TargetUrl
  76. }
  77. request.Header.Add("Cookie", RandStringBytesMaskImpr(12))
  78. request.Header.Add("User-Agent", browser.Random())
  79. request.Header.Add("Referer", Referer)
  80. if XforwardFor {
  81. randomip := generateRandomIPAddress()
  82. request.Header.Add("X-Forwarded-For", randomip)
  83. request.Header.Add("X-Real-IP", randomip)
  84. }
  85. if len(headers) > 0 {
  86. for _, head := range headers {
  87. headKey := head.key
  88. headValue := head.value
  89. if strings.HasPrefix(head.key, "Random") {
  90. count, convErr := strconv.Atoi(strings.ReplaceAll(head.value, "Random", ""))
  91. if convErr == nil {
  92. headKey = RandStringBytesMaskImpr(count)
  93. }
  94. }
  95. if strings.HasPrefix(head.value, "Random") {
  96. count, convErr := strconv.Atoi(strings.ReplaceAll(head.value, "Random", ""))
  97. if convErr == nil {
  98. headValue = RandStringBytesMaskImpr(count)
  99. }
  100. }
  101. request.Header.Del(headKey)
  102. request.Header.Set(headKey, headValue)
  103. }
  104. }
  105. resp, err2 := client.Do(request)
  106. if err2 != nil {
  107. continue
  108. }
  109. io.Copy(ioutil.Discard, resp.Body)
  110. resp.Body.Close()
  111. }
  112. wg.Done()
  113. }
  114. var h = flag.Bool("h", false, "this help")
  115. var count = flag.Int("c", 16, "concurrent thread for download,default 16")
  116. var url = flag.String("s", "", "target url")
  117. var postContent = flag.String("p", "", "post content")
  118. var referer = flag.String("r", "", "referer url")
  119. var detectLocation = flag.Bool("d", false, "detect Real link from the Location in http header")
  120. var xforwardfor = flag.Bool("f", true, "randomized X-Forwarded-For and X-Real-IP address")
  121. var subscribe = flag.String("sub", "", "subscribe url")
  122. var TerminalWriter = goterminal.New(os.Stdout)
  123. var customIP ipArray
  124. var headers headersList
  125. var TargetUrl string
  126. func usage() {
  127. fmt.Fprintf(os.Stderr,
  128. `webBenchmark version: /0.6
  129. Usage: webBenchmark [-c concurrent] [-s target] [-p] [-r refererUrl] [-f] [-i ip]
  130. Options:
  131. `)
  132. flag.PrintDefaults()
  133. fmt.Fprintf(os.Stderr,
  134. `
  135. Advanced Example:
  136. webBenchmark -c 16 -s https://some.website -r https://referer.url -i 10.0.0.1 -i 10.0.0.2
  137. 16 concurrent to benchmark https://some.website with https://referer.url directly to ip 10.0.0.1 and 10.0.0.2
  138. webBenchmark -c 16 -s https://some.website -r https://referer.url
  139. 16 concurrent to benchmark https://some.website with https://referer.url to dns resolved ip address
  140. `)
  141. }
  142. func main() {
  143. flag.Var(&customIP, "i", "custom ip address for that domain, multiple addresses automatically will be assigned randomly")
  144. flag.Var(&headers, "H", "custom header")
  145. //flag.BoolVar(&detectLocation, "d", true, "detect Real link from the Location in http header")
  146. flag.Usage = usage
  147. flag.Parse()
  148. if *h {
  149. flag.Usage()
  150. return
  151. }
  152. routines := *count
  153. if customIP != nil && len(customIP) > 0 && routines < len(customIP) {
  154. routines = len(customIP)
  155. }
  156. // subscribe mode
  157. if len(*subscribe) > 0 {
  158. subs := Subscribe(*subscribe)
  159. if *detectLocation {
  160. location := GetHttpLocation(subs)
  161. if len(location) > 0 {
  162. TargetUrl = location
  163. } else {
  164. TargetUrl = subs
  165. }
  166. } else {
  167. TargetUrl = subs
  168. }
  169. go subscribeUpdate(*subscribe)
  170. }
  171. // local detect location
  172. if len(*subscribe) == 0 && *detectLocation && len(*url) > 0 {
  173. location := GetHttpLocation(*url)
  174. if len(location) > 0 {
  175. TargetUrl = location
  176. } else {
  177. TargetUrl = *url
  178. }
  179. go RefreshHttpLocation(*url)
  180. }
  181. go showStat()
  182. var waitgroup sync.WaitGroup
  183. if routines <= 0 {
  184. routines = 16
  185. }
  186. for i := 0; i < routines; i++ {
  187. waitgroup.Add(1)
  188. go goFun(*postContent, *referer, *xforwardfor, customIP, &waitgroup)
  189. }
  190. waitgroup.Wait()
  191. TerminalWriter.Reset()
  192. }