github.com/mholt/caddy-l4@v0.0.0-20241104153248-ec8fae209322/modules/l4echo/echo.go (about)

     1  // Copyright 2020 Matthew Holt
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package l4echo
    16  
    17  import (
    18  	"io"
    19  
    20  	"github.com/caddyserver/caddy/v2"
    21  	"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
    22  
    23  	"github.com/mholt/caddy-l4/layer4"
    24  )
    25  
    26  func init() {
    27  	caddy.RegisterModule(&Handler{})
    28  }
    29  
    30  // Handler is a simple handler that writes what it reads.
    31  type Handler struct{}
    32  
    33  // CaddyModule returns the Caddy module information.
    34  func (*Handler) CaddyModule() caddy.ModuleInfo {
    35  	return caddy.ModuleInfo{
    36  		ID:  "layer4.handlers.echo",
    37  		New: func() caddy.Module { return new(Handler) },
    38  	}
    39  }
    40  
    41  // Handle handles the connection.
    42  func (*Handler) Handle(cx *layer4.Connection, _ layer4.Handler) error {
    43  	_, err := io.Copy(cx, cx)
    44  	return err
    45  }
    46  
    47  // UnmarshalCaddyfile sets up the Handler from Caddyfile tokens. Syntax:
    48  //
    49  //	echo
    50  func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
    51  	_, wrapper := d.Next(), d.Val() // consume wrapper name
    52  
    53  	// No same-line options are supported
    54  	if d.CountRemainingArgs() > 0 {
    55  		return d.ArgErr()
    56  	}
    57  
    58  	// No blocks are supported
    59  	if d.NextBlock(d.Nesting()) {
    60  		return d.Errf("malformed layer4 connection handler '%s': blocks are not supported", wrapper)
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  // Interface guards
    67  var (
    68  	_ caddyfile.Unmarshaler = (*Handler)(nil)
    69  	_ layer4.NextHandler    = (*Handler)(nil)
    70  )