Path: blob/dev/cmd/integration-test/websocket.go
2070 views
package main12import (3"net"4"strings"56"github.com/gobwas/ws/wsutil"78"github.com/projectdiscovery/nuclei/v3/pkg/testutils"9)1011var websocketTestCases = []TestCaseInfo{12{Path: "protocols/websocket/basic.yaml", TestCase: &websocketBasic{}},13{Path: "protocols/websocket/cswsh.yaml", TestCase: &websocketCswsh{}},14{Path: "protocols/websocket/no-cswsh.yaml", TestCase: &websocketNoCswsh{}},15{Path: "protocols/websocket/path.yaml", TestCase: &websocketWithPath{}},16}1718type websocketBasic struct{}1920// Execute executes a test case and returns an error if occurred21func (h *websocketBasic) Execute(filePath string) error {22connHandler := func(conn net.Conn) {23for {24msg, op, _ := wsutil.ReadClientData(conn)25if string(msg) != "hello" {26return27}28_ = wsutil.WriteServerMessage(conn, op, []byte("world"))29}30}31originValidate := func(origin string) bool {32return true33}34ts := testutils.NewWebsocketServer("", connHandler, originValidate)35defer ts.Close()3637results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)38if err != nil {39return err40}4142return expectResultsCount(results, 1)43}4445type websocketCswsh struct{}4647// Execute executes a test case and returns an error if occurred48func (h *websocketCswsh) Execute(filePath string) error {49connHandler := func(conn net.Conn) {5051}52originValidate := func(origin string) bool {53return true54}55ts := testutils.NewWebsocketServer("", connHandler, originValidate)56defer ts.Close()5758results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)59if err != nil {60return err61}6263return expectResultsCount(results, 1)64}6566type websocketNoCswsh struct{}6768// Execute executes a test case and returns an error if occurred69func (h *websocketNoCswsh) Execute(filePath string) error {70connHandler := func(conn net.Conn) {7172}73originValidate := func(origin string) bool {74return origin == "https://google.com"75}76ts := testutils.NewWebsocketServer("", connHandler, originValidate)77defer ts.Close()7879results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)80if err != nil {81return err82}8384return expectResultsCount(results, 0)85}8687type websocketWithPath struct{}8889// Execute executes a test case and returns an error if occurred90func (h *websocketWithPath) Execute(filePath string) error {91connHandler := func(conn net.Conn) {9293}94originValidate := func(origin string) bool {95return origin == "https://google.com"96}97ts := testutils.NewWebsocketServer("/test", connHandler, originValidate)98defer ts.Close()99100results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)101if err != nil {102return err103}104105return expectResultsCount(results, 0)106}107108109