2024-03-29 09:04:20 +01:00
|
|
|
/*
|
|
|
|
Copyright © 2024 Raul
|
|
|
|
*/
|
2024-04-01 12:28:22 +02:00
|
|
|
|
2024-03-29 09:04:20 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2024-04-01 12:28:22 +02:00
|
|
|
"github.com/spf13/cobra"
|
2024-03-29 09:04:20 +01:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// serverCmd represents the server command
|
|
|
|
var serverCmd = &cobra.Command{
|
|
|
|
Use: "server",
|
2024-04-01 12:28:22 +02:00
|
|
|
Short: "Main chat server",
|
|
|
|
Long: `You can connect to this server by running the following command from
|
|
|
|
a client:
|
|
|
|
nc $SERVER_IP $PORT
|
|
|
|
|
|
|
|
Assuming your IP is 192.168.0.30 and the server port is left on default the
|
|
|
|
command would be as follows:
|
|
|
|
nc 192.168.0.30 1302`,
|
2024-03-29 09:04:20 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
server(cmd)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(serverCmd)
|
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
|
|
// and all subcommands, e.g.:
|
|
|
|
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
|
|
// is called directly, e.g.:
|
|
|
|
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
serverCmd.Flags().String("port", "1302", "Port to listen on")
|
|
|
|
}
|
|
|
|
|
2024-04-01 12:28:22 +02:00
|
|
|
var receiverIsStarted bool = false
|
|
|
|
|
|
|
|
type chatter struct {
|
|
|
|
Username string
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:04:20 +01:00
|
|
|
func server(cmd *cobra.Command) {
|
|
|
|
var lport string
|
|
|
|
port, _ := cmd.Flags().GetString("port")
|
|
|
|
if port != "" {
|
|
|
|
lport = port
|
|
|
|
} else {
|
|
|
|
lport = "1302"
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", ":"+lport)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
fmt.Printf("Listening on port %v...\n", lport)
|
2024-04-01 12:28:22 +02:00
|
|
|
masterChannel := make(chan string, 20)
|
|
|
|
|
|
|
|
slaveChannel := make(chan string, 20)
|
|
|
|
go masterReceiver(slaveChannel, masterChannel)
|
2024-03-29 14:57:02 +01:00
|
|
|
// TODO: get channels properly working
|
2024-04-01 12:28:22 +02:00
|
|
|
// receivingChannel := make(chan string)
|
|
|
|
// sendingChannel := make(chan string)
|
2024-03-29 09:04:20 +01:00
|
|
|
for {
|
2024-03-29 09:40:02 +01:00
|
|
|
conn, err := ln.Accept()
|
|
|
|
cobra.CheckErr(err)
|
2024-04-01 12:28:22 +02:00
|
|
|
numOfClients++
|
|
|
|
go handleConn(conn, slaveChannel, masterChannel)
|
2024-03-29 09:04:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-01 12:28:22 +02:00
|
|
|
var numOfClients int = 0
|
|
|
|
|
|
|
|
func masterReceiver(slaveChannel chan<- string, masterChannel <-chan string) {
|
|
|
|
for {
|
|
|
|
message := <-masterChannel
|
|
|
|
for i := 0; i < numOfClients; i++ {
|
|
|
|
slaveChannel <- message
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 09:04:20 +01:00
|
|
|
}
|
|
|
|
|
2024-04-01 12:28:22 +02:00
|
|
|
// func receiver(conn net.Conn, ch chan string) {
|
|
|
|
// fmt.Println("THE RECEIVER HAS BEEN STARTED, YOU CAN ONLY SEE THIS MESSAGE ONCE")
|
|
|
|
// receiverIsStarted = false
|
|
|
|
// for {
|
|
|
|
// select {
|
|
|
|
// case otherMessage := <-ch:
|
|
|
|
// conn.Write([]byte(otherMessage))
|
|
|
|
// default:
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
func handleConn(conn net.Conn, slaveChannel <-chan string, masterChannel chan<- string) {
|
2024-03-29 09:04:20 +01:00
|
|
|
defer conn.Close()
|
2024-04-01 12:28:22 +02:00
|
|
|
//var otherMessage string
|
2024-03-29 14:57:02 +01:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2024-04-01 12:28:22 +02:00
|
|
|
//select {
|
|
|
|
message := <-slaveChannel
|
|
|
|
conn.Write([]byte(message))
|
2024-03-29 14:57:02 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-03-29 09:04:20 +01:00
|
|
|
IP := getIP(conn)
|
|
|
|
t := time.Now()
|
2024-04-01 12:28:22 +02:00
|
|
|
date := fmt.Sprintf("%d-%02d-%02d | %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
2024-03-29 14:57:02 +01:00
|
|
|
final_message_connect := fmt.Sprintf("[%v] Received connection from %v!\n", date, IP)
|
|
|
|
fmt.Print(final_message_connect)
|
2024-03-29 09:04:20 +01:00
|
|
|
conn.Write([]byte("What's your name?\nName: "))
|
|
|
|
name, err := bufio.NewReader(conn).ReadString('\n')
|
2024-03-29 09:36:03 +01:00
|
|
|
if err != nil {
|
2024-04-01 12:28:22 +02:00
|
|
|
numOfClients--
|
2024-03-29 09:36:03 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-29 09:04:20 +01:00
|
|
|
var NewUser = new(chatter)
|
|
|
|
NewUser.Username = strings.TrimRight(name, "\n")
|
|
|
|
NewUser.IP = IP
|
|
|
|
for {
|
|
|
|
message, err := bufio.NewReader(conn).ReadString('\n')
|
2024-03-29 09:36:03 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(NewUser.Username + " disconnected!\n")
|
2024-04-01 12:28:22 +02:00
|
|
|
numOfClients--
|
2024-03-29 09:36:03 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-29 14:57:02 +01:00
|
|
|
finalMessage := fmt.Sprint("{" + NewUser.IP + "} " + NewUser.Username + ": " + message)
|
|
|
|
fmt.Print(finalMessage)
|
2024-04-01 12:28:22 +02:00
|
|
|
masterChannel <- finalMessage
|
2024-03-29 14:57:02 +01:00
|
|
|
//conn.Write([]byte(message))
|
2024-03-29 09:04:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIP(conn net.Conn) (IP string) {
|
|
|
|
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
|
|
|
IP = fmt.Sprintf("%v", addr)
|
|
|
|
}
|
|
|
|
return IP
|
|
|
|
}
|