2024-03-23 12:33:49 +01:00
|
|
|
/*
|
|
|
|
Copyright © 2024 Raul
|
|
|
|
*/
|
|
|
|
package cmd
|
2024-03-22 15:38:28 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2024-03-24 10:56:34 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2024-03-22 15:38:28 +01:00
|
|
|
)
|
|
|
|
|
2024-03-23 12:33:49 +01:00
|
|
|
// serverCmd represents the server command
|
|
|
|
var serverCmd = &cobra.Command{
|
|
|
|
Use: "server",
|
|
|
|
Short: "Start the server (default port: 8080)",
|
|
|
|
Long: `A longer description that spans multiple lines and likely contains examples
|
|
|
|
and usage of using your command. For example:
|
|
|
|
|
|
|
|
Cobra is a CLI library for Go that empowers applications.
|
|
|
|
This application is a tool to generate the needed files
|
|
|
|
to quickly create a Cobra application.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
server()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-03-22 15:38:28 +01:00
|
|
|
func server() {
|
|
|
|
ln, err := net.Listen("tcp", ":8080")
|
|
|
|
catchErr(err)
|
2024-03-23 12:33:49 +01:00
|
|
|
fmt.Println("Listening on port 8080...")
|
2024-03-22 15:38:28 +01:00
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
catchErr(err)
|
|
|
|
go handleConn(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleConn(conn net.Conn) {
|
|
|
|
defer conn.Close()
|
|
|
|
message, err := bufio.NewReader(conn).ReadString('\n')
|
2024-03-24 10:56:34 +01:00
|
|
|
message = strings.TrimRight(message, "\n")
|
2024-03-22 15:38:28 +01:00
|
|
|
catchErr(err)
|
|
|
|
conn_IP := getIP(conn)
|
2024-03-24 10:56:34 +01:00
|
|
|
t := time.Now()
|
|
|
|
date := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
|
|
|
|
2024-03-27 13:48:12 +01:00
|
|
|
final_message := fmt.Sprintf("[%v] Received connection from %v with message: %v\n", date, conn_IP, message)
|
2024-03-24 10:56:34 +01:00
|
|
|
writer(final_message)
|
2024-03-22 15:38:28 +01:00
|
|
|
reply_message := fmt.Sprintf("Your message was sent successfully\n")
|
|
|
|
conn.Write([]byte(reply_message))
|
2024-03-27 13:48:12 +01:00
|
|
|
fmt.Print(final_message)
|
2024-03-22 15:38:28 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIP(conn net.Conn) (IP string) {
|
|
|
|
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
|
|
|
IP = fmt.Sprintf("%v", addr)
|
|
|
|
}
|
|
|
|
return IP
|
|
|
|
}
|
|
|
|
|
|
|
|
func catchErr(err error) (errHappened bool) {
|
|
|
|
errHappened = false
|
|
|
|
if err != nil {
|
|
|
|
errHappened = true
|
|
|
|
log.Fatalf("Error: %v\n", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
return errHappened
|
|
|
|
}
|
2024-03-24 10:56:34 +01:00
|
|
|
|
|
|
|
func writer(logstring string) {
|
|
|
|
file, err := os.OpenFile("./connections.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0640)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error occurred: %v\n", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
file.WriteString(logstring)
|
|
|
|
}
|