maybe_tls

package module
v0.0.0-...-89c499b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2016 License: Unlicense Imports: 3 Imported by: 1

README

maybe_tls

An implementation of golang's net.Listener interface that accepts both TCP and TLS-over-TCP connections on the same port.

Example

To use, create a net.Listener and a tls.Config object first, then wrap them in a maybe_tls.Listener:

config := tls.Config{Certificates: []tls.Certificate{keypair}}
ln, err := net.Listen("tcp", ":1234")
if err != nil {
  // Handle error
}
mln := maybe_tls.Listener{ln, &config}

Then use the maybe_tls.Listener just as you would a net.Listener. You can detect whether the accepted connection is encrypted or not by testing whether it's a tls.Conn:

conn, err := mln.Accept()
switch conn.(type) {
default:
    // It's a plain TCP connection
case *tls.Conn:
    // It's a TLS connection
}

Restrictions

maybe_tls.Listener works by trying to figure out whether the first few bytes that a client sends look like a TLS ClientHello message. If so, the data from the connection is "rewound" and wrapped in a tls.Conn. If not, the data from the connection is rewound and replayed as is. Because of this detection mechanism, the maybe_tls.Listener can only be used with application protocols where the client is expected to send the first message after a connection is made.

Installation

go get github.com/aaw/maybe_tls

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Wraps a TCP connection and implements the net.Conn interface.

func (Conn) Close

func (c Conn) Close() error

Close closes the connection. Any blocked Read or Write operations will be unblocked and return errors.

func (Conn) LocalAddr

func (c Conn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (Conn) Read

func (c Conn) Read(b []byte) (n int, err error)

Read reads data from the connection. Read can be made to time out and return a Error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.

func (Conn) RemoteAddr

func (c Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (Conn) SetDeadline

func (c Conn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

A deadline is an absolute time after which I/O operations fail with a timeout (see type Error) instead of blocking. The deadline applies to all future I/O, not just the immediately following call to Read or Write.

An idle timeout can be implemented by repeatedly extending the deadline after successful Read or Write calls.

A zero value for t means I/O operations will not time out.

func (Conn) SetReadDeadline

func (c Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future Read calls. A zero value for t means Read will not time out.

func (Conn) SetWriteDeadline

func (c Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future Write calls. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out.

func (Conn) Write

func (c Conn) Write(b []byte) (n int, err error)

Write writes data to the connection. Write can be made to time out and return a Error with Timeout() == true after a fixed time limit; see SetDeadline and SetWriteDeadline.

type Listener

type Listener struct {
	Listener net.Listener
	Config   *tls.Config
}

func (*Listener) Accept

func (s *Listener) Accept() (c net.Conn, err error)

Accept waits for and returns the next connection to the listener.

func (*Listener) Addr

func (s *Listener) Addr() net.Addr

Addr returns the listener's network address.

func (*Listener) Close

func (s *Listener) Close() error

Close closes the listener. Any blocked Accept operations will be unblocked and return errors.

type StreamReplay

type StreamReplay struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL