diff options
| author | Shulhan <ms@kilabit.info> | 2021-09-27 02:47:15 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-09-27 02:47:15 +0700 |
| commit | e1f38286eab416a3f00b47e1b4d919a74285094e (patch) | |
| tree | 3c07d06404f1b9eecc61bf076dc87091a8022525 /http_server.go | |
| parent | 2d3e5d9f2bb36135472732f591ac0843cbe70e09 (diff) | |
| download | gorankusu-e1f38286eab416a3f00b47e1b4d919a74285094e.tar.xz | |
all: implement WebSocket API
The WebSocket API replace the HTTP APIs for running and canceling
attack. Later, it will use to notify the result of attack.
Diffstat (limited to 'http_server.go')
| -rw-r--r-- | http_server.go | 125 |
1 files changed, 2 insertions, 123 deletions
diff --git a/http_server.go b/http_server.go index 96fcb83..fb437ac 100644 --- a/http_server.go +++ b/http_server.go @@ -11,7 +11,6 @@ import ( libhttp "github.com/shuLhan/share/lib/http" "github.com/shuLhan/share/lib/memfs" - "github.com/shuLhan/share/lib/mlog" ) // List of HTTP APIs provided by Trunks HTTP server. @@ -23,19 +22,6 @@ var ( ResponseType: libhttp.ResponseTypeJSON, } - apiTargetAttack = &libhttp.Endpoint{ - Method: libhttp.RequestMethodPost, - Path: "/_trunks/api/target/attack", - RequestType: libhttp.RequestTypeJSON, - ResponseType: libhttp.ResponseTypeJSON, - } - apiTargetAttackCancel = &libhttp.Endpoint{ - Method: libhttp.RequestMethodDelete, - Path: "/_trunks/api/target/attack", - RequestType: libhttp.RequestTypeNone, - ResponseType: libhttp.ResponseTypeJSON, - } - apiTargetAttackResultDelete = &libhttp.Endpoint{ Method: libhttp.RequestMethodDelete, Path: "/_trunks/api/target/attack/result", @@ -77,7 +63,7 @@ func (trunks *Trunks) initHttpServer(isDevelopment bool) (err error) { Options: memfs.Options{ Root: "_www", Includes: []string{ - `.*\.(js|png|html|ico)$`, + `.*\.(js|html|ico|png)$`, }, Development: isDevelopment, }, @@ -96,18 +82,6 @@ func (trunks *Trunks) initHttpServer(isDevelopment bool) (err error) { return fmt.Errorf("%s: %w", logp, err) } - apiTargetAttack.Call = trunks.apiTargetAttack - err = trunks.Httpd.RegisterEndpoint(apiTargetAttack) - if err != nil { - return fmt.Errorf("%s: %w", logp, err) - } - - apiTargetAttackCancel.Call = trunks.apiTargetAttackCancel - err = trunks.Httpd.RegisterEndpoint(apiTargetAttackCancel) - if err != nil { - return fmt.Errorf("%s: %w", logp, err) - } - apiTargetAttackResultDelete.Call = trunks.apiTargetAttackResultDelete err = trunks.Httpd.RegisterEndpoint(apiTargetAttackResultDelete) if err != nil { @@ -149,82 +123,6 @@ func (trunks *Trunks) apiEnvironmentGet(epr *libhttp.EndpointRequest) (resbody [ return json.Marshal(&res) } -// -// apiTargetAttack run the load testing on HTTP endpoint with target and -// options defined in request. -// -func (trunks *Trunks) apiTargetAttack(epr *libhttp.EndpointRequest) (resbody []byte, err error) { - if trunks.Env.isAttackRunning() { - return nil, errAttackConflict(trunks.Env.getRunningAttack()) - } - - logp := "apiTargetAttack" - req := &RunRequest{} - - err = json.Unmarshal(epr.RequestBody, req) - if err != nil { - return nil, errInternal(err) - } - - origTarget := trunks.getTargetByID(req.Target.ID) - if origTarget == nil { - return nil, errInvalidTarget(req.Target.ID) - } - - origHttpTarget := origTarget.getHttpTargetByID(req.HttpTarget.ID) - if origTarget == nil { - return nil, errInvalidHttpTarget(req.HttpTarget.ID) - } - - if !origHttpTarget.AllowAttack { - return nil, errAttackNotAllowed() - } - - req = generateRunRequest(trunks.Env, req, origTarget, origHttpTarget) - - req.result, err = newAttackResult(trunks.Env, req) - if err != nil { - return nil, err - } - - trunks.attackq <- req - - msg := fmt.Sprintf("Attacking %s%s with %d RPS for %s seconds", - req.Target.BaseUrl, req.HttpTarget.Path, - req.Target.Opts.RatePerSecond, req.Target.Opts.Duration) - - mlog.Outf("%s: %s\n", logp, msg) - - res := libhttp.EndpointResponse{} - res.Code = http.StatusOK - res.Name = "OK_ATTACK" - res.Message = msg - - return json.Marshal(res) -} - -func (trunks *Trunks) apiTargetAttackCancel(epr *libhttp.EndpointRequest) (resbody []byte, err error) { - res := &libhttp.EndpointResponse{} - - rr := trunks.Env.getRunningAttack() - if rr == nil { - res.Code = http.StatusNotFound - res.Message = "No attack is currently running." - res.Name = "ERR_ATTACK_CANCEL_NOT_FOUND" - return nil, res - } - - trunks.cancelq <- true - - res.Code = http.StatusOK - res.Name = "OK_ATTACK_CANCEL" - res.Message = fmt.Sprintf(`Attack on target "%s / %s" has been canceled`, - rr.Target.Name, rr.HttpTarget.Name) - res.Data = rr - - return json.Marshal(res) -} - func (trunks *Trunks) apiTargetAttackResultDelete(epr *libhttp.EndpointRequest) (resbody []byte, err error) { name := epr.HttpRequest.Form.Get(paramNameName) if len(name) == 0 { @@ -277,26 +175,7 @@ func (trunks *Trunks) apiTargetRunHttp(epr *libhttp.EndpointRequest) ([]byte, er return nil, errInternal(err) } - origTarget := trunks.getTargetByID(req.Target.ID) - if origTarget == nil { - return nil, errInvalidTarget(req.Target.ID) - } - - origHttpTarget := origTarget.getHttpTargetByID(req.HttpTarget.ID) - if origHttpTarget == nil { - return nil, errInvalidHttpTarget(req.HttpTarget.ID) - } - - var res *RunResponse - - if origHttpTarget.Run == nil { - req.Target.BaseUrl = origTarget.BaseUrl - req.Target.Name = origTarget.Name - res, err = trunks.runHttpTarget(req) - } else { - req := generateRunRequest(trunks.Env, req, origTarget, origHttpTarget) - res, err = req.HttpTarget.Run(req) - } + res, err := trunks.RunHttp(req) if err != nil { return nil, errInternal(err) } |
