diff --git a/app/cmd/server.go b/app/cmd/server.go index 6221cc6..f898a78 100644 --- a/app/cmd/server.go +++ b/app/cmd/server.go @@ -181,13 +181,20 @@ type serverConfigMasqueradeProxy struct { RewriteHost bool `mapstructure:"rewriteHost"` } +type serverConfigMasqueradeString struct { + Content string `mapstructure:"content"` + Headers map[string]string `mapstructure:"headers"` + StatusCode int `mapstructure:"statusCode"` +} + type serverConfigMasquerade struct { - Type string `mapstructure:"type"` - File serverConfigMasqueradeFile `mapstructure:"file"` - Proxy serverConfigMasqueradeProxy `mapstructure:"proxy"` - ListenHTTP string `mapstructure:"listenHTTP"` - ListenHTTPS string `mapstructure:"listenHTTPS"` - ForceHTTPS bool `mapstructure:"forceHTTPS"` + Type string `mapstructure:"type"` + File serverConfigMasqueradeFile `mapstructure:"file"` + Proxy serverConfigMasqueradeProxy `mapstructure:"proxy"` + String serverConfigMasqueradeString `mapstructure:"string"` + ListenHTTP string `mapstructure:"listenHTTP"` + ListenHTTPS string `mapstructure:"listenHTTPS"` + ForceHTTPS bool `mapstructure:"forceHTTPS"` } func (c *serverConfig) fillConn(hyConfig *server.Config) error { @@ -613,6 +620,25 @@ func (c *serverConfig) fillMasqHandler(hyConfig *server.Config) error { w.WriteHeader(http.StatusBadGateway) }, } + case "string": + if c.Masquerade.String.Content == "" { + return configError{Field: "masquerade.string.content", Err: errors.New("empty string content")} + } + if c.Masquerade.String.StatusCode != 0 && + (c.Masquerade.String.StatusCode < 200 || c.Masquerade.String.StatusCode > 599) { + return configError{Field: "masquerade.string.statusCode", Err: errors.New("invalid status code (must be 200-599)")} + } + handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + for k, v := range c.Masquerade.String.Headers { + w.Header().Set(k, v) + } + if c.Masquerade.String.StatusCode != 0 { + w.WriteHeader(c.Masquerade.String.StatusCode) + } else { + w.WriteHeader(http.StatusOK) // Use 200 OK by default + } + _, _ = w.Write([]byte(c.Masquerade.String.Content)) + }) default: return configError{Field: "masquerade.type", Err: errors.New("unsupported masquerade type")} } diff --git a/app/cmd/server_test.go b/app/cmd/server_test.go index 73bce1c..73cb666 100644 --- a/app/cmd/server_test.go +++ b/app/cmd/server_test.go @@ -136,6 +136,14 @@ func TestServerConfig(t *testing.T) { URL: "https://some.site.net", RewriteHost: true, }, + String: serverConfigMasqueradeString{ + Content: "aint nothin here", + Headers: map[string]string{ + "content-type": "text/plain", + "custom-haha": "lol", + }, + StatusCode: 418, + }, ListenHTTP: ":80", ListenHTTPS: ":443", ForceHTTPS: true, diff --git a/app/cmd/server_test.yaml b/app/cmd/server_test.yaml index 27e09f0..f1a42d9 100644 --- a/app/cmd/server_test.yaml +++ b/app/cmd/server_test.yaml @@ -102,6 +102,12 @@ masquerade: proxy: url: https://some.site.net rewriteHost: true + string: + content: aint nothin here + headers: + content-type: text/plain + custom-haha: lol + statusCode: 418 listenHTTP: :80 listenHTTPS: :443 forceHTTPS: true