func (mux *Mux) Serve() error {
	tlsStr := ""
	if mux.tlsConfig != nil {
		tlsStr = "TLS "
	}
	mux.Logger.Printf("%smux serving on %s, advertising %s", tlsStr, mux.ln.Addr().String(), mux.addr)
	for {
		conn, err := mux.ln.Accept()
		if err, ok := err.(interface {
			Temporary() bool
		}); ok && err.Temporary() {
			continue
		}
		if err != nil {
			mux.wg.Wait()
			for _, ln := range mux.m {
				close(ln.c)
			}
			return err
		}
		mux.wg.Add(1)
		go mux.handleConn(conn)
	}
}
Serve handles connections from ln and multiplexes then across registered listener.
package tcp
import (
	"crypto/tls"
	"crypto/x509"
	"errors"
	"expvar"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"os"
	"strconv"
	"sync"
	"time"
)
const (
	DefaultTimeout = 30 * time.Second
)
var stats *expvar.Map
const (
	numConnectionsHandled   = "num_connections_handled"
	numUnregisteredHandlers = "num_unregistered_handlers"
)
func init() {
	stats = expvar.NewMap("mux")
	stats.Add(numConnectionsHandled, 0)
	stats.Add(numUnregisteredHandlers, 0)
}
type Layer struct {
	ln   net.Listener
	addr net.Addr
	dialer *Dialer
	nodeX509CACert string
	tlsConfig      *tls.Config
}
func (l *Layer) Dial(addr string, timeout time.Duration) (net.Conn, error) {
	return l.dialer.Dial(addr, timeout)
}
func (l *Layer) Accept() (net.Conn, error) { return l.ln.Accept() }
func (l *Layer) Close() error { return l.ln.Close() }
func (l *Layer) Addr() net.Addr {
	return l.addr
}
type Mux struct {
	ln   net.Listener
	addr net.Addr
	m    map[byte]*listener
	wg sync.WaitGroup
	remoteEncrypted bool
	Timeout time.Duration
	Logger *log.Logger
	x509CACert string
	x509Cert string
	x509Key string
	InsecureSkipVerify bool
	tlsConfig *tls.Config
}
func NewMux(ln net.Listener, adv net.Addr) (*Mux, error) {
	addr := adv
	if addr == nil {
		addr = ln.Addr()
	}
	return &Mux{
		ln:      ln,
		addr:    addr,
		m:       make(map[byte]*listener),
		Timeout: DefaultTimeout,
		Logger:  log.New(os.Stderr, "[mux] ", log.LstdFlags),
	}, nil
}
func NewTLSMux(ln net.Listener, adv net.Addr, cert, key, caCert string) (*Mux, error) {
	mux, err := NewMux(ln, adv)
	if err != nil {
		return nil, err
	}
	mux.tlsConfig, err = createTLSConfig(cert, key, caCert)
	if err != nil {
		return nil, err
	}
	mux.ln = tls.NewListener(ln, mux.tlsConfig)
	mux.remoteEncrypted = true
	mux.x509CACert = caCert
	mux.x509Cert = cert
	mux.x509Key = key
	return mux, nil
}