/* Copyright © 2024 Raul */ package cmd import ( "bufio" "fmt" "github.com/spf13/cobra" "net" "strings" "time" ) // serverCmd represents the server command var serverCmd = &cobra.Command{ Use: "server", 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`, 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") } var receiverIsStarted bool = false type chatter struct { Username string IP string } 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) masterChannel := make(chan string, 20) slaveChannel := make(chan string, 20) go masterReceiver(slaveChannel, masterChannel) // TODO: get channels properly working // receivingChannel := make(chan string) // sendingChannel := make(chan string) for { conn, err := ln.Accept() cobra.CheckErr(err) numOfClients++ go handleConn(conn, slaveChannel, masterChannel) } } var numOfClients int = 0 func masterReceiver(slaveChannel chan<- string, masterChannel <-chan string) { for { message := <-masterChannel for i := 0; i < numOfClients; i++ { slaveChannel <- message } } } func handleConn(conn net.Conn, slaveChannel <-chan string, masterChannel chan<- string) { defer conn.Close() go func() { for { message := <-slaveChannel conn.Write([]byte(message)) } }() IP := getIP(conn) t := time.Now() date := fmt.Sprintf("%d-%02d-%02d | %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) final_message_connect := fmt.Sprintf("[%v] Received connection from %v!\n", date, IP) fmt.Print(final_message_connect) conn.Write([]byte("What's your name?\nName: ")) name, err := bufio.NewReader(conn).ReadString('\n') if err != nil { numOfClients-- return } var NewUser = new(chatter) NewUser.Username = strings.TrimRight(name, "\n") NewUser.IP = IP for { message, err := bufio.NewReader(conn).ReadString('\n') if err != nil { fmt.Printf(NewUser.Username + " disconnected!\n") numOfClients-- return } finalMessage := fmt.Sprint("{" + NewUser.IP + "} " + NewUser.Username + ": " + message) fmt.Print(finalMessage) masterChannel <- finalMessage //conn.Write([]byte(message)) } } func getIP(conn net.Conn) (IP string) { if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok { IP = fmt.Sprintf("%v", addr) } return IP }