-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
160 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/go-playground/validator/v10" | ||
"github.com/gorcon/rcon" | ||
) | ||
|
||
func openRcon(i openConnInfo) (*rcon.Conn, error) { | ||
// basic validation so we can return more verbose error messages | ||
err := i.validateConnectionInfo() | ||
if err != nil { | ||
return nil, ErrInvalidConnectionDetails | ||
} | ||
|
||
//actually open | ||
conn, err := rcon.Dial(i.Server, i.Password) | ||
if err != nil { | ||
switch err { | ||
case rcon.ErrAuthNotRCON: | ||
fallthrough | ||
case rcon.ErrInvalidAuthResponse: | ||
return nil, ErrInvalidResponseFromRcon | ||
case rcon.ErrAuthFailed: | ||
fallthrough | ||
default: | ||
return nil, ErrInvalidConnectionDetails | ||
} | ||
} | ||
return conn, nil | ||
} | ||
|
||
func (i *openConnInfo) validateConnectionInfo() error { | ||
validate := validator.New(validator.WithRequiredStructEnabled()) | ||
err := validate.Var(i.Server, "required,hostname_port") | ||
if err != nil { | ||
return err | ||
} | ||
dialer := &net.Dialer{Timeout: time.Second * 1} | ||
_, err = dialer.Dial("tcp", i.Server) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |