github.com/openziti/transport@v0.1.5/udp/connection.go (about)

     1  /*
     2  	Copyright NetFoundry, Inc.
     3  
     4  	Licensed under the Apache License, Version 2.0 (the "License");
     5  	you may not use this file except in compliance with the License.
     6  	You may obtain a copy of the License at
     7  
     8  	https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  	Unless required by applicable law or agreed to in writing, software
    11  	distributed under the License is distributed on an "AS IS" BASIS,
    12  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  	See the License for the specific language governing permissions and
    14  	limitations under the License.
    15  */
    16  
    17  package udp
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/x509"
    22  	"fmt"
    23  	"github.com/openziti/transport"
    24  	"github.com/pkg/errors"
    25  	"io"
    26  	"net"
    27  	"time"
    28  )
    29  
    30  type Connection struct {
    31  	detail *transport.ConnectionDetail
    32  	socket net.Conn
    33  	reader io.Reader
    34  }
    35  
    36  func (self *Connection) Detail() *transport.ConnectionDetail {
    37  	return self.detail
    38  }
    39  
    40  func (self *Connection) PeerCertificates() []*x509.Certificate {
    41  	return nil
    42  }
    43  
    44  func (self *Connection) Reader() io.Reader {
    45  	return self.reader
    46  }
    47  
    48  func (self *Connection) Writer() io.Writer {
    49  	return self.socket
    50  }
    51  
    52  func (self *Connection) Conn() net.Conn {
    53  	return self.socket
    54  }
    55  
    56  func (self *Connection) SetReadTimeout(t time.Duration) error {
    57  	return self.socket.SetReadDeadline(time.Now().Add(t))
    58  }
    59  
    60  func (self *Connection) SetWriteTimeout(t time.Duration) error {
    61  	return self.socket.SetWriteDeadline(time.Now().Add(t))
    62  }
    63  
    64  func (self *Connection) ClearReadTimeout() error {
    65  	var zero time.Time
    66  	return self.socket.SetReadDeadline(zero)
    67  }
    68  
    69  func (self *Connection) ClearWriteTimeout() error {
    70  	var zero time.Time
    71  	return self.socket.SetWriteDeadline(zero)
    72  }
    73  
    74  func (self *Connection) Close() error {
    75  	return self.socket.Close()
    76  }
    77  
    78  type loggingWriter struct{ io.Writer }
    79  
    80  func (w loggingWriter) Write(b []byte) (int, error) {
    81  	fmt.Printf("Wrote: %v byte to underlay\n", len(b))
    82  	if n, err := w.Writer.Write(b); err != nil {
    83  		panic(err)
    84  	} else {
    85  		return n, nil
    86  	}
    87  }
    88  
    89  type loggingReader struct{ io.Reader }
    90  
    91  func (w loggingReader) Read(b []byte) (int, error) {
    92  	n, err := w.Reader.Read(b)
    93  	if err != nil {
    94  		return n, err
    95  	}
    96  	fmt.Printf("Read: %v bytes from underlay\n", n)
    97  	magicV2 := []byte{0x03, 0x06, 0x09, 0x0c}
    98  	if len(b) > 20 && bytes.Equal(magicV2, b[:4]) {
    99  		panic(errors.New("no good"))
   100  	}
   101  	return n, err
   102  }