-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
276 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package config | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"github.com/gorcon/rcon-cli/internal/session" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// Config allows to take a remote server address and password from | ||
// the configuration file. This enables not to specify these flags when | ||
// running the CLI. | ||
// | ||
// Example: | ||
// ```yaml | ||
// default: | ||
// address: "127.0.0.1:16260" | ||
// password: "password" | ||
// ```. | ||
type Config map[string]session.Session | ||
|
||
// ReadYamlConfig reads config data from yaml file. | ||
func ReadYamlConfig(path string) (cfg Config, err error) { | ||
file, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return cfg, err | ||
} | ||
|
||
if err := yaml.Unmarshal(file, &cfg); err != nil { | ||
return cfg, err | ||
} | ||
|
||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package rcon | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/gorcon/rcon" | ||
) | ||
|
||
// Execute sends command to Execute to the remote server and returns | ||
// the response. | ||
func Execute(address string, password string, command string) (string, error) { | ||
if command == "" { | ||
return "", errors.New("command is not set") | ||
} | ||
|
||
console, err := rcon.Dial(address, password) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer console.Close() | ||
|
||
return console.Execute(command) | ||
} | ||
|
||
// CheckCredentials sends auth request for remote server. Returns en error if | ||
// address or password is incorrect. | ||
func CheckCredentials(address string, password string) error { | ||
console, err := rcon.Dial(address, password) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return console.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package telnet | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/gorcon/telnet" | ||
) | ||
|
||
// Execute sends command to Execute to the remote server and returns | ||
// the response. | ||
func Execute(address string, password string, command string) (string, error) { | ||
if command == "" { | ||
return "", errors.New("command is not set") | ||
} | ||
|
||
console, err := telnet.Dial(address, password) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer console.Close() | ||
|
||
return console.Execute(command) | ||
} | ||
|
||
// DialInteractive parses commands from input reader, executes them on remote | ||
// server and writes responses to output writer. Password can be empty string. | ||
// In this case password will be prompted in an interactive window. | ||
func Interactive(r io.Reader, w io.Writer, address string, password string) error { | ||
return telnet.DialInteractive(r, w, address, password) | ||
} | ||
|
||
// CheckCredentials sends auth request for remote server. Returns en error if | ||
// address or password is incorrect. | ||
func CheckCredentials(address string, password string) error { | ||
console, err := telnet.Dial(address, password) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return console.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package session | ||
|
||
// Allowed protocols. | ||
const ( | ||
ProtocolRCON = "rcon" | ||
ProtocolTELNET = "telnet" | ||
) | ||
|
||
// DefaultProtocol contains the default protocol for connecting to a | ||
// remote server. | ||
const DefaultProtocol = ProtocolRCON | ||
|
||
// Session contains details for making a request on a remote server. | ||
type Session struct { | ||
Address string `json:"address" yaml:"address"` | ||
Password string `json:"password" yaml:"password"` | ||
Log string `json:"log" yaml:"log"` | ||
Type string `json:"type" yaml:"type"` | ||
} |
Oops, something went wrong.