|
|
@ -16,6 +16,7 @@ import (
|
|
|
|
"expvar"
|
|
|
|
"expvar"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"math/big"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
@ -47,6 +48,9 @@ type Server struct {
|
|
|
|
packetsRecv, bytesRecv expvar.Int
|
|
|
|
packetsRecv, bytesRecv expvar.Int
|
|
|
|
packetsDropped expvar.Int
|
|
|
|
packetsDropped expvar.Int
|
|
|
|
accepts expvar.Int
|
|
|
|
accepts expvar.Int
|
|
|
|
|
|
|
|
curClients expvar.Int
|
|
|
|
|
|
|
|
curHomeClients expvar.Int // ones with preferred
|
|
|
|
|
|
|
|
unknownFrames expvar.Int
|
|
|
|
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
mu sync.Mutex
|
|
|
|
closed bool
|
|
|
|
closed bool
|
|
|
@ -137,13 +141,14 @@ func (s *Server) registerClient(c *sclient) {
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
old := s.clients[c.key]
|
|
|
|
old := s.clients[c.key]
|
|
|
|
if old == nil {
|
|
|
|
if old == nil {
|
|
|
|
s.logf("derp: %s: client %x: adding connection", c.nc.RemoteAddr(), c.key)
|
|
|
|
c.logf("adding connection")
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
old.nc.Close()
|
|
|
|
old.nc.Close()
|
|
|
|
s.logf("derp: %s: client %x: adding connection, replacing %s", c.nc.RemoteAddr(), c.key, old.nc.RemoteAddr())
|
|
|
|
c.logf("adding connection, replacing %s", old.nc.RemoteAddr())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.clients[c.key] = c
|
|
|
|
s.clients[c.key] = c
|
|
|
|
s.clientsEver[c.key] = true
|
|
|
|
s.clientsEver[c.key] = true
|
|
|
|
|
|
|
|
s.curClients.Add(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// unregisterClient removes a client from the server.
|
|
|
|
// unregisterClient removes a client from the server.
|
|
|
@ -152,9 +157,14 @@ func (s *Server) unregisterClient(c *sclient) {
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
cur := s.clients[c.key]
|
|
|
|
cur := s.clients[c.key]
|
|
|
|
if cur == c {
|
|
|
|
if cur == c {
|
|
|
|
s.logf("derp: %s: client %x: removing connection", c.nc.RemoteAddr(), c.key)
|
|
|
|
c.logf("removing connection")
|
|
|
|
delete(s.clients, c.key)
|
|
|
|
delete(s.clients, c.key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.curClients.Add(-1)
|
|
|
|
|
|
|
|
if c.preferred {
|
|
|
|
|
|
|
|
s.curHomeClients.Add(-1)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) accept(nc net.Conn, brw *bufio.ReadWriter) error {
|
|
|
|
func (s *Server) accept(nc net.Conn, brw *bufio.ReadWriter) error {
|
|
|
@ -172,14 +182,24 @@ func (s *Server) accept(nc net.Conn, brw *bufio.ReadWriter) error {
|
|
|
|
return fmt.Errorf("client %x rejected: %v", clientKey, err)
|
|
|
|
return fmt.Errorf("client %x rejected: %v", clientKey, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lim := rate.Inf
|
|
|
|
|
|
|
|
if s.BytesPerSecond != 0 {
|
|
|
|
|
|
|
|
lim = rate.Limit(s.BytesPerSecond)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const burstBytes = 1 << 20 // generous bandwidth delay product? must be over 64k max packet size.
|
|
|
|
|
|
|
|
limiter := rate.NewLimiter(lim, burstBytes)
|
|
|
|
|
|
|
|
|
|
|
|
// At this point we trust the client so we don't time out.
|
|
|
|
// At this point we trust the client so we don't time out.
|
|
|
|
nc.SetDeadline(time.Time{})
|
|
|
|
nc.SetDeadline(time.Time{})
|
|
|
|
|
|
|
|
|
|
|
|
c := &sclient{
|
|
|
|
c := &sclient{
|
|
|
|
key: clientKey,
|
|
|
|
s: s,
|
|
|
|
nc: nc,
|
|
|
|
key: clientKey,
|
|
|
|
br: br,
|
|
|
|
nc: nc,
|
|
|
|
bw: bw,
|
|
|
|
br: br,
|
|
|
|
|
|
|
|
bw: bw,
|
|
|
|
|
|
|
|
limiter: limiter,
|
|
|
|
|
|
|
|
logf: logger.WithPrefix(s.logf, fmt.Sprintf("derp client %v/%x: ", nc.RemoteAddr(), clientKey)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if clientInfo != nil {
|
|
|
|
if clientInfo != nil {
|
|
|
|
c.info = *clientInfo
|
|
|
|
c.info = *clientInfo
|
|
|
@ -199,64 +219,96 @@ func (s *Server) accept(nc net.Conn, brw *bufio.ReadWriter) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer s.unregisterClient(c)
|
|
|
|
defer s.unregisterClient(c)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return c.run()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *sclient) run() error {
|
|
|
|
|
|
|
|
s := c.s
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
defer cancel()
|
|
|
|
go s.sendClientKeepAlives(ctx, c)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lim := rate.Inf
|
|
|
|
go s.sendClientKeepAlives(ctx, c)
|
|
|
|
if s.BytesPerSecond != 0 {
|
|
|
|
|
|
|
|
lim = rate.Limit(s.BytesPerSecond)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const burstBytes = 1 << 20 // generous bandwidth delay product? must be over 64k max packet size.
|
|
|
|
|
|
|
|
limiter := rate.NewLimiter(lim, burstBytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
ft, fl, err := readFrameHeader(c.br)
|
|
|
|
ft, fl, err := readFrameHeader(c.br)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("client %x: readFrameHeader: %v", c.key, err)
|
|
|
|
return fmt.Errorf("client %x: readFrameHeader: %v", c.key, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ft != frameSendPacket {
|
|
|
|
switch ft {
|
|
|
|
// TODO: nothing else yet supported
|
|
|
|
case frameNotePreferred:
|
|
|
|
return fmt.Errorf("client %x: unsupported frame %v", c.key, ft)
|
|
|
|
err = c.handleFrameNotePreferred(ft, fl)
|
|
|
|
|
|
|
|
case frameSendPacket:
|
|
|
|
|
|
|
|
err = c.handleFrameSendPacket(ctx, ft, fl)
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
err = c.handleUnknownFrame(ctx, ft, fl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dstKey, contents, err := s.recvPacket(ctx, c.br, fl, limiter)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("client %x: recvPacket: %v", c.key, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
func (c *sclient) handleUnknownFrame(ctx context.Context, ft frameType, fl uint32) error {
|
|
|
|
dst := s.clients[dstKey]
|
|
|
|
if err := c.limiter.WaitN(ctx, int(fl)); err != nil {
|
|
|
|
s.mu.Unlock()
|
|
|
|
return fmt.Errorf("rate limit: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := io.CopyN(ioutil.Discard, c.br, int64(fl))
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if dst == nil {
|
|
|
|
func (c *sclient) handleFrameNotePreferred(ft frameType, fl uint32) error {
|
|
|
|
s.packetsDropped.Add(1)
|
|
|
|
if fl != 1 {
|
|
|
|
if debug {
|
|
|
|
return fmt.Errorf("frameNotePreferred wrong size")
|
|
|
|
s.logf("derp: %s: client %x: dropping packet for unknown %x", nc.RemoteAddr(), c.key, dstKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v, err := c.br.ReadByte()
|
|
|
|
continue
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return fmt.Errorf("frameNotePreferred ReadByte: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
c.setPreferred(v != 0)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *sclient) handleFrameSendPacket(ctx context.Context, ft frameType, fl uint32) error {
|
|
|
|
|
|
|
|
s := c.s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dstKey, contents, err := s.recvPacket(ctx, c.br, fl, c.limiter)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return fmt.Errorf("client %x: recvPacket: %v", c.key, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
|
|
|
|
dst := s.clients[dstKey]
|
|
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if dst == nil {
|
|
|
|
|
|
|
|
s.packetsDropped.Add(1)
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
|
|
|
|
c.logf("dropping packet for unknown %x", dstKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dst.mu.Lock()
|
|
|
|
dst.mu.Lock()
|
|
|
|
err = s.sendPacket(dst.bw, &dst.info, c.key, contents)
|
|
|
|
err = s.sendPacket(dst.bw, &dst.info, c.key, contents)
|
|
|
|
dst.mu.Unlock()
|
|
|
|
dst.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
s.logf("derp: %s: client %x: dropping packet for %x: %v", nc.RemoteAddr(), c.key, dstKey, err)
|
|
|
|
c.logf("write error sending packet to %x: %v", dstKey, err)
|
|
|
|
|
|
|
|
|
|
|
|
// If we cannot send to a destination, shut it down.
|
|
|
|
// If we cannot send to a destination, shut it down.
|
|
|
|
// Let its receive loop do the cleanup.
|
|
|
|
// Let its receive loop do the cleanup.
|
|
|
|
s.mu.Lock()
|
|
|
|
s.mu.Lock()
|
|
|
|
if s.clients[dstKey] == dst {
|
|
|
|
if s.clients[dstKey] == dst {
|
|
|
|
s.clients[dstKey].nc.Close()
|
|
|
|
s.clients[dstKey].nc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) sendClientKeepAlives(ctx context.Context, c *sclient) {
|
|
|
|
func (s *Server) sendClientKeepAlives(ctx context.Context, c *sclient) {
|
|
|
|
if err := c.keepAliveLoop(ctx); err != nil {
|
|
|
|
if err := c.keepAliveLoop(ctx); err != nil {
|
|
|
|
s.logf("derp: %s: client %x: keep alive failed: %v", c.nc.RemoteAddr(), c.key, err)
|
|
|
|
c.logf("keep alive failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -392,18 +444,34 @@ func (s *Server) recvPacket(ctx context.Context, br *bufio.Reader, frameLen uint
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// (The "s" prefix is to more explicitly distinguish it from Client in derp_client.go)
|
|
|
|
// (The "s" prefix is to more explicitly distinguish it from Client in derp_client.go)
|
|
|
|
type sclient struct {
|
|
|
|
type sclient struct {
|
|
|
|
nc net.Conn
|
|
|
|
s *Server
|
|
|
|
key key.Public
|
|
|
|
nc net.Conn
|
|
|
|
info clientInfo
|
|
|
|
key key.Public
|
|
|
|
|
|
|
|
info clientInfo
|
|
|
|
|
|
|
|
logf logger.Logf
|
|
|
|
|
|
|
|
limiter *rate.Limiter
|
|
|
|
|
|
|
|
|
|
|
|
keepAliveTimer *time.Timer
|
|
|
|
keepAliveTimer *time.Timer
|
|
|
|
keepAliveReset chan struct{}
|
|
|
|
keepAliveReset chan struct{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
preferred bool
|
|
|
|
|
|
|
|
|
|
|
|
mu sync.Mutex // mu guards writing to bw
|
|
|
|
mu sync.Mutex // mu guards writing to bw
|
|
|
|
br *bufio.Reader
|
|
|
|
br *bufio.Reader
|
|
|
|
bw *bufio.Writer
|
|
|
|
bw *bufio.Writer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *sclient) setPreferred(v bool) {
|
|
|
|
|
|
|
|
if c.preferred == v {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if v {
|
|
|
|
|
|
|
|
c.s.curHomeClients.Add(1)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
c.s.curHomeClients.Add(-1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *sclient) keepAliveLoop(ctx context.Context) error {
|
|
|
|
func (c *sclient) keepAliveLoop(ctx context.Context) error {
|
|
|
|
jitterMs, err := crand.Int(crand.Reader, big.NewInt(5000))
|
|
|
|
jitterMs, err := crand.Int(crand.Reader, big.NewInt(5000))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -449,13 +517,15 @@ func (s *Server) expVarFunc(f func() interface{}) expvar.Func {
|
|
|
|
// ExpVar returns an expvar variable suitable for registering with expvar.Publish.
|
|
|
|
// ExpVar returns an expvar variable suitable for registering with expvar.Publish.
|
|
|
|
func (s *Server) ExpVar() expvar.Var {
|
|
|
|
func (s *Server) ExpVar() expvar.Var {
|
|
|
|
m := new(metrics.Set)
|
|
|
|
m := new(metrics.Set)
|
|
|
|
m.Set("gauge_current_connnections", s.expVarFunc(func() interface{} { return len(s.netConns) }))
|
|
|
|
|
|
|
|
m.Set("counter_unique_clients_ever", s.expVarFunc(func() interface{} { return len(s.clientsEver) }))
|
|
|
|
m.Set("counter_unique_clients_ever", s.expVarFunc(func() interface{} { return len(s.clientsEver) }))
|
|
|
|
|
|
|
|
m.Set("gauge_current_connnections", &s.curClients)
|
|
|
|
|
|
|
|
m.Set("gauge_current_home_connnections", &s.curHomeClients)
|
|
|
|
m.Set("accepts", &s.accepts)
|
|
|
|
m.Set("accepts", &s.accepts)
|
|
|
|
m.Set("bytes_received", &s.bytesRecv)
|
|
|
|
m.Set("bytes_received", &s.bytesRecv)
|
|
|
|
m.Set("bytes_sent", &s.bytesSent)
|
|
|
|
m.Set("bytes_sent", &s.bytesSent)
|
|
|
|
m.Set("packets_dropped", &s.packetsDropped)
|
|
|
|
m.Set("packets_dropped", &s.packetsDropped)
|
|
|
|
m.Set("packets_sent", &s.packetsSent)
|
|
|
|
m.Set("packets_sent", &s.packetsSent)
|
|
|
|
m.Set("packets_received", &s.packetsRecv)
|
|
|
|
m.Set("packets_received", &s.packetsRecv)
|
|
|
|
|
|
|
|
m.Set("unknown_frames", &s.unknownFrames)
|
|
|
|
return m
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|