示例#1
0
// NewServer creates a server that will listen for requests over HTTP and interact with the RCON server specified
// non-/api prefixed routes are served from static files compiled into bindata_assetfs.go
func NewRestServer(c *ServerConfig) {
	var err error
	rcon_client, err = mcrcon.NewClient(c.RCON_address, c.RCON_port, c.RCON_password)

	if err != nil {
		panic(fmt.Errorf("Could not connect to RCON server at %s:%d. (Error was: %s)", c.RCON_address, c.RCON_port, err))
	}

	router := bone.New()

	// Redirect static resources, and then handle the static resources (/gui/) routes with the static asset file
	router.Handle("/", http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
		http.Redirect(response, request, "/gui/", 302)
	}))
	router.Get("/gui/", http.StripPrefix("/gui/", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: ""})))

	// Define the API (JSON) routes
	router.GetFunc("/api", apiRootHandler)
	router.GetFunc("/api/users", usersRootHandler)
	router.GetFunc("/api/users/:username", usernameHandler)

	// TODO: Require a http basic auth username and password if passed in.

	// Start the server
	fmt.Println("Starting server on port", c.Port)
	http.ListenAndServe(fmt.Sprintf(":%d", c.Port), router)
}
示例#2
0
// runREPL takes an address and password, then sest up a connection to the RCON server and presents the user with a
// read-evaluate-print-loop command prompt for the connected RCON server.
func runREPL(address string, port int, password string) {
	client, err := mcrcon.NewClient(address, port, password)

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println("Type \"exit\" to quit")

	inputreader := bufio.NewReader(os.Stdin)

	for {
		fmt.Print("> ")
		input, err := inputreader.ReadString('\n') // this will prompt the user for input

		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		if input == "exit\n" {
			client.Close()
			return
		}

		cmdResponse, rUserErr := client.SendCommand(input)

		if rUserErr != nil {
			fmt.Println("FATAL: ", rUserErr)
		}

		fmt.Println(cmdResponse)
	}

}
示例#3
0
// runCommand takes the options passed in from the command line, prints the output of the command, and terminates.
func runCommand(address string, port int, password string, command string) {
	client, err := mcrcon.NewClient(address, port, password)

	if err != nil {
		jww.FATAL.Println(err)
		os.Exit(1)
	}

	jww.DEBUG.Println(fmt.Sprintf("Connecting to %s:%d, with password %s, using command %s", address, port, password, command))
	fmt.Println("Executing command: ", command)

	cmdResponse, rUserErr := client.SendCommand(command)

	if rUserErr != nil {
		jww.FATAL.Println(rUserErr)
	}

	jww.DEBUG.Println("Response: ", cmdResponse)
	fmt.Println(cmdResponse)

	client.Close()
}