diff options
| author | Shulhan <ms@kilabit.info> | 2023-05-16 14:06:13 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-05-16 14:06:13 +0700 |
| commit | 84b8a1de6d109f0ae26f9ee01c318b61584c3160 (patch) | |
| tree | 5abc0d2f7b46f97de0be7dde0bd73c19fed8420f /websocket_server.go | |
| parent | b95e5d973e7e07951dbdb5a6d01a92b5af24a34f (diff) | |
| download | gorankusu-84b8a1de6d109f0ae26f9ee01c318b61584c3160.tar.xz | |
all: remove WebSocket server
Using WebSocket for communication in client require additional
setup, especially if its behind proxy.
For example, if we server the trunks server under domain testing.local
behind proxy, we need to setup route for the WebSocket too.
Diffstat (limited to 'websocket_server.go')
| -rw-r--r-- | websocket_server.go | 138 |
1 files changed, 0 insertions, 138 deletions
diff --git a/websocket_server.go b/websocket_server.go deleted file mode 100644 index 9cff066..0000000 --- a/websocket_server.go +++ /dev/null @@ -1,138 +0,0 @@ -// SPDX-FileCopyrightText: 2021 M. Shulhan <ms@kilabit.info> -// SPDX-License-Identifier: GPL-3.0-or-later - -package trunks - -import ( - "context" - "encoding/base64" - "encoding/json" - "errors" - "fmt" - "log" - "net/http" - - liberrors "github.com/shuLhan/share/lib/errors" - "github.com/shuLhan/share/lib/websocket" -) - -func (trunks *Trunks) wsBroadcastAttackFinish(result *AttackResult) { - if result == nil { - return - } - - logp := "wsBroadcastAttackFinish" - - jsonb, err := json.Marshal(result) - if err != nil { - log.Printf("%s: %s", logp, err) - return - } - - packet, err := websocket.NewBroadcast( - pathApiAttackResult, - base64.StdEncoding.EncodeToString(jsonb), - ) - if err != nil { - log.Printf("%s: %s", logp, err) - return - } - - for _, conn := range trunks.Wsd.Clients.All() { - err = websocket.Send(conn, packet) - if err != nil { - log.Printf("%s: %s", logp, err) - } - } -} - -func (trunks *Trunks) initWebSocketServer() (err error) { - opts := &websocket.ServerOptions{ - Address: trunks.Env.websocketListenAddress, - } - - trunks.Wsd = websocket.NewServer(opts) - - err = trunks.Wsd.RegisterTextHandler( - "POST", - pathApiAttackHttp, - trunks.handleWsAttackHttp, - ) - if err != nil { - return err - } - - err = trunks.Wsd.RegisterTextHandler( - "DELETE", - pathApiAttackHttp, - trunks.handleWsAttackHttpCancel, - ) - if err != nil { - return err - } - - return nil -} - -func (trunks *Trunks) handleWsAttackHttp( - ctx context.Context, - req *websocket.Request, -) (res websocket.Response) { - logp := "handleWsAttackHttp" - - reqBody, err := base64.StdEncoding.DecodeString(req.Body) - if err != nil { - return handleError(logp, err) - } - - runRequest := &RunRequest{} - err = json.Unmarshal(reqBody, runRequest) - if err != nil { - return handleError(logp, err) - } - - err = trunks.AttackHttp(runRequest) - if err != nil { - return handleError(logp, err) - } - - res.Code = http.StatusOK - - return res -} - -func (trunks *Trunks) handleWsAttackHttpCancel( - ctx context.Context, - req *websocket.Request, -) (res websocket.Response) { - logp := "handleWsAttackHttpCancel" - - rr, err := trunks.AttackHttpCancel() - if err != nil { - return handleError(logp, err) - } - - jsonb, err := json.Marshal(rr) - if err != nil { - return handleError(logp, err) - } - - res.Code = http.StatusOK - res.Message = fmt.Sprintf(`Attack on target "%s / %s" has been canceled`, - rr.Target.Name, rr.HttpTarget.Name) - res.Body = base64.StdEncoding.EncodeToString(jsonb) - - return res -} - -func handleError(logp string, err error) (res websocket.Response) { - e := &liberrors.E{} - if errors.As(err, &e) { - res.Code = int32(e.Code) - res.Message = e.Message - return res - } - res.Code = http.StatusInternalServerError - res.Message = fmt.Sprintf("%s: %s", logp, err) - return res -} |
