update
This commit is contained in:
@@ -10,8 +10,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
host = flag.String("host", "localhost", "Server host")
|
||||
port = flag.String("port", "26666", "Server port")
|
||||
host = flag.String("h", "localhost", "Server host")
|
||||
port = flag.String("p", "26666", "Server port")
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -62,4 +62,4 @@ func main() {
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Printf("Error reading input: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
45
server.go
45
server.go
@@ -7,17 +7,52 @@ import (
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
port = flag.String("port", "26666", "Port to listen on")
|
||||
logFile = flag.String("logfile", "server.log", "Log file path")
|
||||
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 {
|
||||
@@ -59,7 +94,7 @@ func handleConnection(conn net.Conn) {
|
||||
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",
|
||||
connInfo := fmt.Sprintf("New connection from IP: %s, Port: %d, Timestamp: %s",
|
||||
remoteAddr.IP, remoteAddr.Port, timestamp)
|
||||
fmt.Println(connInfo)
|
||||
log.Println(connInfo)
|
||||
@@ -69,7 +104,7 @@ func handleConnection(conn net.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)
|
||||
@@ -86,4 +121,4 @@ func handleConnection(conn net.Conn) {
|
||||
disconnectMsg := fmt.Sprintf("Client %s:%d disconnected", remoteAddr.IP, remoteAddr.Port)
|
||||
fmt.Println(disconnectMsg)
|
||||
log.Println(disconnectMsg)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user