Skip to content

Commit

Permalink
Merge pull request #38 from shatteredsilicon/fix-connection-hijacked-…
Browse files Browse the repository at this point in the history
…problem

Fix 'http: connection has been hijacked' problem
  • Loading branch information
gordan-bobic authored Jan 27, 2025
2 parents b19641a + 8ccf3c6 commit 46ba56c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 5 additions & 5 deletions app/controllers/agent/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c Agent) Cmd(uuid string, conn *websocket.Conn) revel.Result {
// When the agent disconnects, set oN.agent_configs.running=0 for the agent.
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Open(); err != nil {
return c.Error(err, "Agent.Cmd: dbm.Open")
return c.RenderError(fmt.Errorf("Agent.Cmd: dbm.Open: %s", err.Error()))
}
agentHandler := agent.NewMySQLHandler(dbm, instance.NewMySQLHandler(dbm))

Expand Down Expand Up @@ -122,7 +122,7 @@ func (c Agent) Data(conn *websocket.Conn) revel.Result {
dbStats := msgStats // copy
dbm := db.NewMySQLManager()
if err := dbm.Open(); err != nil {
return c.Error(err, "Agent.Data: dbm.Open")
return c.RenderError(fmt.Errorf("Agent.Data: dbm.Open: %s", err.Error()))
}
defer dbm.Close()

Expand All @@ -141,7 +141,7 @@ func (c Agent) Data(conn *websocket.Conn) revel.Result {
case io.EOF:
// We got everything, client disconnected.
default:
return c.Error(err, "Agent.Data: qan.SaveData")
return c.RenderError(fmt.Errorf("Agent.Data: qan.SaveData: %s", err.Error()))
}
}

Expand All @@ -160,7 +160,7 @@ func (c Agent) Log(conn *websocket.Conn) revel.Result {
dbStats := msgStats // copy
dbm := db.NewMySQLManager()
if err := dbm.Open(); err != nil {
return c.Error(err, "Agent.Log: dbm.Open")
return c.RenderError(fmt.Errorf("Agent.Log: dbm.Open: %s", err.Error()))
}
defer dbm.Close()
dbh := agent.NewLogHandler(dbm, &dbStats)
Expand All @@ -176,7 +176,7 @@ func (c Agent) Log(conn *websocket.Conn) revel.Result {
case io.EOF:
revel.TRACE.Printf("%s: done (EOF)", prefix)
default:
return c.Error(err, "Agent.Log: agent.SaveLog")
return c.RenderError(fmt.Errorf("Agent.Log: agent.SaveLog: %s", err.Error()))
}
}

Expand Down
12 changes: 11 additions & 1 deletion app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
queryService "github.com/shatteredsilicon/qan-api/service/query"
"github.com/shatteredsilicon/qan-api/stats"
"github.com/shatteredsilicon/ssm/proto"
"golang.org/x/net/websocket"

// dummy import to keep the package
_ "github.com/revel/modules/testrunner/app/controllers"
Expand Down Expand Up @@ -250,7 +251,16 @@ func authAgent(c *revel.Controller) revel.Result {
revel.ERROR.Printf("auth agent: %s", err)
}
c.Response.Status = int(res.Code)
return c.RenderText(res.Error)
if c.Request.Method == "WS" {
if err := websocket.JSON.Send(c.Request.Websocket, proto.Response{
Code: res.Code,
Error: res.Error,
}); err != nil {
return c.RenderError(err)
}
} else {
return c.RenderText(res.Error)
}
}
c.Args["agentId"] = agentId

Expand Down

0 comments on commit 46ba56c

Please sign in to comment.