@@ -3,25 +3,86 @@ package main
33import (
44 "context"
55 "flag"
6+ "fmt"
67 "log"
78 "net/http"
89 "os"
910 "time"
1011
1112 "github.com/ba0f3/MCP-Kali-Server/pkg/executor"
1213 "github.com/ba0f3/MCP-Kali-Server/pkg/handlers"
14+ "github.com/ba0f3/MCP-Kali-Server/pkg/service"
1315 "github.com/modelcontextprotocol/go-sdk/mcp"
1416)
1517
18+ // handleServiceInstall installs the server as a system service
19+ func handleServiceInstall (serviceName , servicePort string ) error {
20+ config := service .GetDefaultConfig ()
21+ config .Name = serviceName
22+
23+ // Set service arguments to use HTTP mode with the specified port
24+ config .Args = []string {
25+ "-http" , servicePort ,
26+ "-timeout" , "900" ,
27+ }
28+
29+ fmt .Printf ("Installing %s service...\n " , serviceName )
30+ fmt .Printf ("Service will listen on port: %s\n " , servicePort )
31+ fmt .Printf ("Executable: %s\n " , config .Executable )
32+ fmt .Printf ("Working directory: %s\n " , config .WorkingDir )
33+
34+ if err := service .InstallService (config ); err != nil {
35+ return fmt .Errorf ("installation failed: %w" , err )
36+ }
37+
38+ fmt .Printf ("\n Service '%s' installed successfully!\n " , serviceName )
39+ fmt .Println ("\n To start the service:" )
40+ fmt .Printf (" Windows: sc start %s\n " , serviceName )
41+ fmt .Printf (" Linux: sudo systemctl start %s\n " , serviceName )
42+ fmt .Printf (" macOS: sudo launchctl start %s\n " , serviceName )
43+
44+ return nil
45+ }
46+
47+ // handleServiceUninstall removes the installed service
48+ func handleServiceUninstall (serviceName string ) error {
49+ fmt .Printf ("Uninstalling %s service...\n " , serviceName )
50+
51+ if err := service .UninstallService (serviceName ); err != nil {
52+ return fmt .Errorf ("uninstallation failed: %w" , err )
53+ }
54+
55+ fmt .Printf ("Service '%s' uninstalled successfully!\n " , serviceName )
56+ return nil
57+ }
1658
1759func main () {
1860 var (
1961 debug = flag .Bool ("debug" , false , "Enable debug logging" )
2062 httpAddr = flag .String ("http" , "" , "if set, use streamable HTTP at this address, instead of stdin/stdout" )
2163 timeout = flag .Int ("timeout" , 900 , "Command execution timeout in seconds" )
64+ installService = flag .Bool ("install-service" , false , "Install the server as a system service" )
65+ uninstallService = flag .Bool ("uninstall-service" , false , "Uninstall the system service" )
66+ serviceName = flag .String ("service-name" , "mcp-kali-server" , "Name of the service" )
67+ servicePort = flag .String ("service-port" , ":8080" , "Port for the service to listen on (used with -install-service)" )
2268 )
2369 flag .Parse ()
2470
71+ // Handle service installation/uninstallation
72+ if * installService {
73+ if err := handleServiceInstall (* serviceName , * servicePort ); err != nil {
74+ log .Fatalf ("Failed to install service: %v" , err )
75+ }
76+ return
77+ }
78+
79+ if * uninstallService {
80+ if err := handleServiceUninstall (* serviceName ); err != nil {
81+ log .Fatalf ("Failed to uninstall service: %v" , err )
82+ }
83+ return
84+ }
85+
2586 // Set the global command timeout
2687 executor .SetGlobalTimeout (time .Duration (* timeout ) * time .Second )
2788
0 commit comments