Skip to content

Commit

Permalink
webssh. define default credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
edospadoni committed Mar 15, 2024
1 parent 64586ed commit 81fd428
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 13 additions & 4 deletions api/methods/webssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ import (
)

func GetWebSSH(c *gin.Context) {
// get request fields
// define request fields
var jsonData models.SSHConnect

// set defaults
jsonData.Username = "root"
jsonData.Port = "22"

// get real params
if err := c.BindJSON(&jsonData); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "request fields malformed", "error": err.Error()})
return
Expand Down Expand Up @@ -60,7 +66,7 @@ func GetWebSSH(c *gin.Context) {
keysPath := configuration.Config.DataDir + "/" + username + ".key"

// read key
keyPrivate, err := os.ReadFile(keysPath + "")
keyPrivate, err := os.ReadFile(keysPath)
if err != nil {
c.JSON(http.StatusBadRequest, structs.Map(response.StatusBadRequest{
Code: 400,
Expand Down Expand Up @@ -120,8 +126,8 @@ func GetWebSSH(c *gin.Context) {
// make post request for websocket id
resPost, errPost := client.PostForm(webSSHURL, url.Values{
"hostname": {ipAddress},
"port": {"22"},
"username": {"root"},
"port": {jsonData.Port},
"username": {jsonData.Username},
"privatekey": {string(keyPrivate)},
"passphrase": {jsonData.Passphrase},
"term": {"xterm-256color"},
Expand Down Expand Up @@ -153,6 +159,9 @@ func GetWebSSH(c *gin.Context) {
// parse body response
jsonParsed, _ := gabs.ParseJSON(body)

// add xsrf token
jsonParsed.Set(xsrfToken, "xsrf")

// check if id exists
idFound, _ := jsonParsed.Path("id").Data().(string)
if len(idFound) == 0 || idFound == "null" {
Expand Down
2 changes: 2 additions & 0 deletions api/models/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type SSHGenerate struct {
}

type SSHConnect struct {
Username string `json:"username"`
Port string `json:"port"`
UnitID string `json:"unit_id" binding:"required"`
Passphrase string `json:"passphrase" binding:"required"`
}

0 comments on commit 81fd428

Please sign in to comment.