125 lines
3.0 KiB
Go
125 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
port = flag.String("p", "26666", "Port to listen on")
|
|
logFile = flag.String("log", "server.log", "Log file path")
|
|
daemon = flag.Bool("d", false, "Run as daemon")
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
// If daemon flag is set, restart the program in background
|
|
if *daemon {
|
|
// Get the path of the current executable
|
|
executable, err := os.Executable()
|
|
if err != nil {
|
|
log.Fatal("Failed to get executable path:", err)
|
|
}
|
|
|
|
// Prepare command to run the program in background
|
|
// We exclude the -d flag to prevent infinite loop
|
|
args := []string{}
|
|
if *port != "26666" {
|
|
args = append(args, "-p", *port)
|
|
}
|
|
if *logFile != "server.log" {
|
|
args = append(args, "-log", *logFile)
|
|
}
|
|
|
|
cmd := exec.Command(executable, args...)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
|
|
// Start the command in background
|
|
err = cmd.Start()
|
|
if err != nil {
|
|
log.Fatal("Failed to start daemon:", err)
|
|
}
|
|
|
|
// Print the PID of the background process
|
|
fmt.Printf("Server started as daemon with PID %d\n", cmd.Process.Pid)
|
|
return
|
|
}
|
|
|
|
// Open log file
|
|
file, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
log.Fatal("Failed to open log file:", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
// Set log output to both file and console
|
|
log.SetOutput(file)
|
|
|
|
// Start listening on specified port
|
|
listener, err := net.Listen("tcp", ":"+*port)
|
|
if err != nil {
|
|
log.Fatal("Failed to start server:", err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
fmt.Printf("Server listening on port %s\n", *port)
|
|
log.Printf("Server listening on port %s\n", *port)
|
|
|
|
for {
|
|
// Accept incoming connections
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
log.Printf("Failed to accept connection: %v", err)
|
|
continue
|
|
}
|
|
|
|
// Handle each connection in a separate goroutine
|
|
go handleConnection(conn)
|
|
}
|
|
}
|
|
|
|
func handleConnection(conn net.Conn) {
|
|
defer conn.Close()
|
|
|
|
// Get connection info
|
|
remoteAddr := conn.RemoteAddr().(*net.TCPAddr)
|
|
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
// Print and log connection info
|
|
connInfo := fmt.Sprintf("New connection from IP: %s, Port: %d, Timestamp: %s",
|
|
remoteAddr.IP, remoteAddr.Port, timestamp)
|
|
fmt.Println(connInfo)
|
|
log.Println(connInfo)
|
|
|
|
// Create a scanner to read messages from the client
|
|
scanner := bufio.NewScanner(conn)
|
|
for scanner.Scan() {
|
|
message := scanner.Text()
|
|
msgTimestamp := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
// Print and log the received message with timestamp
|
|
logEntry := fmt.Sprintf("[%s] Received: %s", msgTimestamp, message)
|
|
fmt.Println(logEntry)
|
|
log.Println(logEntry)
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
errorMsg := fmt.Sprintf("Error reading from connection: %v", err)
|
|
fmt.Println(errorMsg)
|
|
log.Println(errorMsg)
|
|
}
|
|
|
|
// Log when client disconnects
|
|
disconnectMsg := fmt.Sprintf("Client %s:%d disconnected", remoteAddr.IP, remoteAddr.Port)
|
|
fmt.Println(disconnectMsg)
|
|
log.Println(disconnectMsg)
|
|
}
|