From c4ce2306df402e03b7fd0f2f10e07cf21b2babc2 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sat, 25 Feb 2017 15:39:26 +0300 Subject: [PATCH] Split long lines --- src/cypherpunks.ru/govpn/client/client.go | 16 +- src/cypherpunks.ru/govpn/client/tcp.go | 24 ++- .../govpn/cmd/govpn-client/main.go | 8 +- .../govpn/cmd/govpn-server/action.go | 1 + .../govpn/cmd/govpn-server/conf.go | 15 +- .../govpn/cmd/govpn-server/main.go | 24 ++- src/cypherpunks.ru/govpn/common.go | 4 +- src/cypherpunks.ru/govpn/identity.go | 21 ++- src/cypherpunks.ru/govpn/peer.go | 32 +++- src/cypherpunks.ru/govpn/server/common.go | 7 +- src/cypherpunks.ru/govpn/server/server.go | 65 ++++++-- src/cypherpunks.ru/govpn/server/tcp.go | 90 ++++++++-- src/cypherpunks.ru/govpn/server/udp.go | 154 +++++++++++++++--- src/cypherpunks.ru/govpn/stats.go | 15 +- 14 files changed, 399 insertions(+), 77 deletions(-) diff --git a/src/cypherpunks.ru/govpn/client/client.go b/src/cypherpunks.ru/govpn/client/client.go index 4ebd8a9..ef832af 100644 --- a/src/cypherpunks.ru/govpn/client/client.go +++ b/src/cypherpunks.ru/govpn/client/client.go @@ -48,7 +48,11 @@ type Configuration struct { // Validate returns an error if a configuration is invalid func (c *Configuration) Validate() error { if c.Peer.MTU > govpn.MTUMax { - return errors.Errorf("Invalid MTU %d, maximum allowable is %d", c.Peer.MTU, govpn.MTUMax) + return errors.Errorf( + "Invalid MTU %d, maximum allowable is %d", + c.Peer.MTU, + govpn.MTUMax, + ) } if len(c.RemoteAddress) == 0 { return errors.New("Missing RemoteAddress") @@ -78,7 +82,9 @@ func (c *Configuration) LogFields() logrus.Fields { f[prefix+"proxy"] = c.ProxyAddress } if c.FileDescriptor > 0 { - f[prefix+"remote"] = fmt.Sprintf("fd:%d(%s)", c.FileDescriptor, c.RemoteAddress) + f[prefix+"remote"] = fmt.Sprintf( + "fd:%d(%s)", c.FileDescriptor, c.RemoteAddress, + ) } else { f[prefix+"remote"] = c.RemoteAddress } @@ -154,7 +160,11 @@ func (c *Client) KnownPeers() *govpn.KnownPeers { func (c *Client) MainCycle() { var err error l := c.logger.WithFields(logrus.Fields{"func": logFuncPrefix + "Client.MainCycle"}) - l.WithFields(c.LogFields()).WithFields(c.config.LogFields()).Info("Starting...") + l.WithFields( + c.LogFields(), + ).WithFields( + c.config.LogFields(), + ).Info("Starting...") // if available, run PreUp, it might create interface if c.config.Peer.PreUp != nil { diff --git a/src/cypherpunks.ru/govpn/client/tcp.go b/src/cypherpunks.ru/govpn/client/tcp.go index eca4852..0832e11 100644 --- a/src/cypherpunks.ru/govpn/client/tcp.go +++ b/src/cypherpunks.ru/govpn/client/tcp.go @@ -103,7 +103,9 @@ HandshakeCycle: } n, err = conn.Read(buf[prev:]) if err != nil { - c.logger.WithFields(fields).WithFields( + c.logger.WithFields( + fields, + ).WithFields( c.LogFields(), ).Debug("Packet timeouted") c.timeouted <- struct{}{} @@ -113,7 +115,9 @@ HandshakeCycle: prev += n _, err = c.idsCache.Find(buf[:prev]) if err != nil { - c.logger.WithFields(fields).WithFields( + c.logger.WithFields( + fields, + ).WithFields( c.LogFields(), ).WithError(err).Debug("Can't find peer in ids") continue @@ -121,7 +125,11 @@ HandshakeCycle: peer, err = hs.Client(buf[:prev]) prev = 0 if err != nil { - c.logger.WithFields(fields).WithError(err).WithFields( + c.logger.WithFields( + fields, + ).WithError( + err, + ).WithFields( c.LogFields(), ).Debug("Can't create new peer") continue @@ -157,7 +165,9 @@ TransportCycle: default: } if prev == len(buf) { - c.logger.WithFields(c.LogFields()).Debug("Packet timeouted") + c.logger.WithFields( + c.LogFields(), + ).Debug("Packet timeouted") c.timeouted <- struct{}{} break TransportCycle } @@ -167,7 +177,11 @@ TransportCycle: } n, err = conn.Read(buf[prev:]) if err != nil { - c.logger.WithError(err).WithFields(c.LogFields()).Debug("Connection timeouted") + c.logger.WithError( + err, + ).WithFields( + c.LogFields(), + ).Debug("Connection timeouted") c.timeouted <- struct{}{} break TransportCycle } diff --git a/src/cypherpunks.ru/govpn/cmd/govpn-client/main.go b/src/cypherpunks.ru/govpn/cmd/govpn-client/main.go index bd8b067..a3399ab 100644 --- a/src/cypherpunks.ru/govpn/cmd/govpn-client/main.go +++ b/src/cypherpunks.ru/govpn/cmd/govpn-client/main.go @@ -80,7 +80,13 @@ func main() { } if protocol, err = govpn.NewProtocolFromString(*proto); err != nil { - logger.WithError(err).WithFields(fields).WithField("proto", *proto).Fatal("Invalid protocol") + logger.WithError( + err, + ).WithFields( + fields, + ).WithField( + "proto", *proto, + ).Fatal("Invalid protocol") } if *proxyAddr != "" && protocol == govpn.ProtocolUDP { diff --git a/src/cypherpunks.ru/govpn/cmd/govpn-server/action.go b/src/cypherpunks.ru/govpn/cmd/govpn-server/action.go index f5fde8b..751081a 100644 --- a/src/cypherpunks.ru/govpn/cmd/govpn-server/action.go +++ b/src/cypherpunks.ru/govpn/cmd/govpn-server/action.go @@ -1,6 +1,7 @@ /* GoVPN -- simple secure free software virtual private network daemon Copyright (C) 2014-2017 Sergey Matveev + 2016-2017 Bruno Clermont This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/src/cypherpunks.ru/govpn/cmd/govpn-server/conf.go b/src/cypherpunks.ru/govpn/cmd/govpn-server/conf.go index 80581ab..d357b25 100644 --- a/src/cypherpunks.ru/govpn/cmd/govpn-server/conf.go +++ b/src/cypherpunks.ru/govpn/cmd/govpn-server/conf.go @@ -125,7 +125,12 @@ func confRefresh() error { return errors.Wrap(err, "confRead") } confs = *newConfs - logger.WithFields(fields).WithField("newConfs", len(confs)).Debug("idsCache.Update") + logger.WithFields( + fields, + ).WithField( + "newConfs", + len(confs), + ).Debug("idsCache.Update") if err = idsCache.Update(newConfs); err != nil { return errors.Wrap(err, "idsCache.Update") } @@ -138,13 +143,17 @@ func confInit() { err := confRefresh() fields := logrus.Fields{"func": "confInit"} if err != nil { - logger.WithError(err).WithFields(fields).Fatal("Couldn't perform initial configuration read") + logger.WithError(err).WithFields( + fields, + ).Fatal("Couldn't perform initial configuration read") } go func() { for { time.Sleep(refreshRate) if err = confRefresh(); err != nil { - logger.WithError(err).WithFields(fields).Error("Couldn't refresh configuration") + logger.WithError(err).WithFields( + fields, + ).Error("Couldn't refresh configuration") } } }() diff --git a/src/cypherpunks.ru/govpn/cmd/govpn-server/main.go b/src/cypherpunks.ru/govpn/cmd/govpn-server/main.go index 2c70267..a60e5a0 100644 --- a/src/cypherpunks.ru/govpn/cmd/govpn-server/main.go +++ b/src/cypherpunks.ru/govpn/cmd/govpn-server/main.go @@ -58,12 +58,18 @@ func main() { logger, err = govpn.NewLogger(*logLevel, *syslog) if err != nil { - logrus.WithFields(fields).WithError(err).Fatal("Couldn't initialize logging") + logrus.WithFields( + fields, + ).WithError(err).Fatal("Couldn't initialize logging") } govpn.SetLogger(logger) if *egdPath != "" { - logger.WithField("egd_path", *egdPath).WithFields(fields).Debug("Init EGD") + logger.WithField( + "egd_path", *egdPath, + ).WithFields( + fields, + ).Debug("Init EGD") govpn.EGDInit(*egdPath) } @@ -75,13 +81,23 @@ func main() { Timeout: govpn.TimeoutDefault, } if serverConfig.Protocol, err = govpn.NewProtocolFromString(*proto); err != nil { - logger.WithError(err).WithFields(fields).WithField("proto", *proto).Fatal("Invalid protocol") + logger.WithError(err).WithFields( + fields, + ).WithField( + "proto", *proto, + ).Fatal("Invalid protocol") } if err = serverConfig.Validate(); err != nil { logger.WithError(err).WithFields(fields).Fatal("Invalid configuration") } - srv := server.NewServer(serverConfig, confs, idsCache, logger, govpn.CatchSignalShutdown()) + srv := server.NewServer( + serverConfig, + confs, + idsCache, + logger, + govpn.CatchSignalShutdown(), + ) if *stats != "" { go govpn.StatsProcessor(*stats, srv.KnownPeers()) diff --git a/src/cypherpunks.ru/govpn/common.go b/src/cypherpunks.ru/govpn/common.go index 3fe3165..684491c 100644 --- a/src/cypherpunks.ru/govpn/common.go +++ b/src/cypherpunks.ru/govpn/common.go @@ -140,7 +140,9 @@ func NewProtocolFromString(p string) (Protocol, error) { index++ } - return Protocol(-1), errors.Errorf("Invalid protocol %q: %s", p, strings.Join(choices, ",")) + return Protocol(-1), errors.Errorf( + "Invalid protocol %q: %s", p, strings.Join(choices, ","), + ) } // SliceZero zeros each byte. diff --git a/src/cypherpunks.ru/govpn/identity.go b/src/cypherpunks.ru/govpn/identity.go index 81066bc..db338f0 100644 --- a/src/cypherpunks.ru/govpn/identity.go +++ b/src/cypherpunks.ru/govpn/identity.go @@ -86,7 +86,12 @@ func (mc *MACCache) Update(peers *map[PeerID]*PeerConf) error { delete(mc.cache, pid) } } - logger.WithFields(fields).WithField("size", mc.Length()).Debug("Cleaned, add/update new key") + logger.WithFields( + fields, + ).WithField( + "size", + mc.Length(), + ).Debug("Cleaned, add/update new key") for pid, pc := range *peers { if _, exists := mc.cache[pid]; exists { logger.WithFields(fields).WithFields( @@ -133,7 +138,12 @@ func AddTimeSync(ts int, data []byte) { for i := 0; i < 8; i++ { data[i] ^= buf[i] } - logger.WithFields(fields).WithField("after", hex.EncodeToString(data)).Debug("Done") + logger.WithFields( + fields, + ).WithField( + "after", + hex.EncodeToString(data), + ).Debug("Done") } // Find tries to find peer's identity (that equals to MAC) @@ -166,7 +176,12 @@ func (mc *MACCache) Find(data []byte) (*PeerID, error) { mt.l.Unlock() return nil, errors.Wrap(err, "mt.mac.Write") } - logger.WithFields(fields).WithField("buf", hex.EncodeToString(buf[:0])).Debug("mt.mac.Sum") + logger.WithFields( + fields, + ).WithField( + "buf", + hex.EncodeToString(buf[:0]), + ).Debug("mt.mac.Sum") mt.mac.Sum(sum[:0]) mt.l.Unlock() diff --git a/src/cypherpunks.ru/govpn/peer.go b/src/cypherpunks.ru/govpn/peer.go index b466161..e914cd6 100644 --- a/src/cypherpunks.ru/govpn/peer.go +++ b/src/cypherpunks.ru/govpn/peer.go @@ -294,14 +294,17 @@ func newPeer(isClient bool, addr string, conn io.Writer, conf *PeerConf, key *[S func (p *Peer) EthProcess(data []byte) error { const paddingSize = 1 if len(data) > p.MTU-paddingSize { - logger.WithFields(p.LogFields()).WithFields( + logger.WithFields( + p.LogFields(), + ).WithFields( p.ConfigurationLogFields(), ).WithFields( logrus.Fields{ "func": logFuncPrefix + "Peer.EthProcess", "padding": paddingSize, "packet_size": len(data), - }).Warning("Ignore padded data packet larger than MTU") + }, + ).Warning("Ignore padded data packet larger than MTU") return nil } p.BusyT.Lock() @@ -362,8 +365,13 @@ func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool { "data": len(data), } if len(data) < MinPktLength { - logger.WithFields(p.LogFields()).WithFields(fields).WithField( - "minimum_packet_Length", MinPktLength, + logger.WithFields( + p.LogFields(), + ).WithFields( + fields, + ).WithField( + "minimum_packet_Length", + MinPktLength, ).Debug("Ignore packet smaller than allowed minimum") return false } @@ -377,7 +385,9 @@ func (p *Peer) PktProcess(data []byte, tap io.Writer, reorderable bool) bool { var err error out, err = EnclessDecode(p.key, p.nonceR, data[:len(data)-NonceSize]) if err != nil { - logger.WithFields(p.LogFields()).WithError(err).Debug("Failed to decode encless") + logger.WithFields( + p.LogFields(), + ).WithError(err).Debug("Failed to decode encless") p.FramesUnauth++ p.BusyR.Unlock() return false @@ -504,7 +514,9 @@ func PeerTapProcessor(peer *Peer, tap *TAP, terminator chan struct{}) { } case data = <-tap.Sink: if err = peer.EthProcess(data); err != nil { - logger.WithFields(fields).WithFields( + logger.WithFields( + fields, + ).WithFields( peer.LogFields(), ).WithError(err).Warn("Can't process ethernet packet") } @@ -520,7 +532,9 @@ func PeerTapProcessor(peer *Peer, tap *TAP, terminator chan struct{}) { break CPRProcessor case data = <-tap.Sink: if err = peer.EthProcess(data); err != nil { - logger.WithFields(fields).WithFields( + logger.WithFields( + fields, + ).WithFields( peer.LogFields(), ).WithError(err).Warn("Can't process ethernet packet") } @@ -528,7 +542,9 @@ func PeerTapProcessor(peer *Peer, tap *TAP, terminator chan struct{}) { } if data == nil { if err = peer.EthProcess(nil); err != nil { - logger.WithFields(fields).WithFields( + logger.WithFields( + fields, + ).WithFields( peer.LogFields(), ).WithError(err).Warn("Can't process nil ethernet packet") } diff --git a/src/cypherpunks.ru/govpn/server/common.go b/src/cypherpunks.ru/govpn/server/common.go index 76ccfed..9a5554b 100644 --- a/src/cypherpunks.ru/govpn/server/common.go +++ b/src/cypherpunks.ru/govpn/server/common.go @@ -42,7 +42,12 @@ func (s *Server) callUp(peer *govpn.Peer, proto govpn.Protocol) (*govpn.TAP, err fields["func"] = logFuncPrefix + "Server.callUp" if !isConfigIface && conf.PreUp == nil { - return nil, errors.Wrapf(errMisconfiguredTap, "interface:%q, PreUp:%q", conf.Iface, conf.PreUp) + return nil, errors.Wrapf( + errMisconfiguredTap, + "interface:%q, PreUp:%q", + conf.Iface, + conf.PreUp, + ) } if conf.PreUp != nil { diff --git a/src/cypherpunks.ru/govpn/server/server.go b/src/cypherpunks.ru/govpn/server/server.go index 76f8ee3..ef53b8f 100644 --- a/src/cypherpunks.ru/govpn/server/server.go +++ b/src/cypherpunks.ru/govpn/server/server.go @@ -28,12 +28,14 @@ import ( "cypherpunks.ru/govpn" ) -// PeerConfigurer is used by a GoVPN server to figure the configuration of a single peer +// PeerConfigurer is used by a GoVPN server to figure the configuration +// of a single peer type PeerConfigurer interface { Get(govpn.PeerID) *govpn.PeerConf } -// MACPeerFinder is used by GoVPN server to figure the PeerID from handshake data +// MACPeerFinder is used by GoVPN server to figure the PeerID from +// handshake data type MACPeerFinder interface { Find([]byte) (*govpn.PeerID, error) } @@ -117,7 +119,8 @@ func (s *Server) KnownPeers() *govpn.KnownPeers { return &s.knownPeers } -// NewServer return a configured GoVPN server, to listen network connection MainCycle must be executed +// NewServer return a configured GoVPN server, to listen network +// connection MainCycle must be executed func NewServer(serverConf Configuration, peerConfs PeerConfigurer, idsCache MACPeerFinder, logger *logrus.Logger, termSignal chan interface{}) *Server { govpn.SetLogger(logger) return &Server{ @@ -154,7 +157,13 @@ func (s *Server) MainCycle() { } fields := logrus.Fields{"func": logFuncPrefix + "Server.MainCycle"} - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Info("Starting...") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + s.configuration.LogFields(), + ).Info("Starting...") var needsDeletion bool var err error @@ -165,13 +174,27 @@ MainCycle: for { select { case <-s.termSignal: - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Info("Terminating") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + s.configuration.LogFields(), + ).Info("Terminating") for _, ps := range s.peers { if err = s.callDown(ps); err != nil { - s.logger.WithFields(fields).WithError(err).WithFields(ps.peer.LogFields()).Error("Failed to run callDown") + s.logger.WithFields( + fields, + ).WithError(err).WithFields( + ps.peer.LogFields(), + ).Error("Failed to run callDown") } if err = ps.tap.Close(); err != nil { - logrus.WithError(err).WithFields(fields).WithFields(ps.peer.LogFields()).Error("Couldn't close TAP") + logrus.WithError(err).WithFields( + fields, + ).WithFields( + ps.peer.LogFields(), + ).Error("Couldn't close TAP") } } // empty value signals that everything is fine @@ -183,7 +206,11 @@ MainCycle: s.hsLock.Lock() for addr, hs := range s.handshakes { if hs.LastPing.Add(s.configuration.Timeout).Before(now) { - logrus.WithFields(fields).WithFields(hs.LogFields()).Debug("handshake is expired, delete") + logrus.WithFields( + fields, + ).WithFields( + hs.LogFields(), + ).Debug("handshake is expired, delete") hs.Zero() delete(s.handshakes, addr) } @@ -193,18 +220,32 @@ MainCycle: s.kpLock.Lock() for addr, ps := range s.peers { ps.peer.BusyR.Lock() - needsDeletion = ps.peer.LastPing.Add(s.configuration.Timeout).Before(now) + needsDeletion = ps.peer.LastPing.Add( + s.configuration.Timeout, + ).Before(now) ps.peer.BusyR.Unlock() if needsDeletion { - logrus.WithFields(fields).WithFields(ps.peer.LogFields()).Info("Delete peer") + logrus.WithFields( + fields, + ).WithFields( + ps.peer.LogFields(), + ).Info("Delete peer") delete(s.peers, addr) delete(s.knownPeers, addr) delete(s.peersByID, *ps.peer.ID) if err = s.callDown(ps); err != nil { - logrus.WithError(err).WithFields(fields).WithFields(ps.peer.LogFields()).Error("Couldn't execute callDown") + logrus.WithError(err).WithFields( + fields, + ).WithFields( + ps.peer.LogFields(), + ).Error("Couldn't execute callDown") } if err = ps.tap.Close(); err != nil { - logrus.WithError(err).WithFields(fields).WithFields(ps.peer.LogFields()).Error("Couldn't close TAP") + logrus.WithError(err).WithFields( + fields, + ).WithFields( + ps.peer.LogFields(), + ).Error("Couldn't close TAP") } ps.terminator <- struct{}{} } diff --git a/src/cypherpunks.ru/govpn/server/tcp.go b/src/cypherpunks.ru/govpn/server/tcp.go index 28528a0..59eda53 100644 --- a/src/cypherpunks.ru/govpn/server/tcp.go +++ b/src/cypherpunks.ru/govpn/server/tcp.go @@ -44,12 +44,22 @@ func (s *Server) startTCP() { "func": logFuncPrefix + "Server.startTCP", "bind": bind.String(), } - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Info("Listen") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + s.configuration.LogFields(), + ).Info("Listen") go func() { for { conn, err := listener.AcceptTCP() if err != nil { - s.logger.WithError(err).WithFields(fields).WithFields(s.LogFields()).Error("Failed to accept TCP connection") + s.logger.WithError(err).WithFields( + fields, + ).WithFields( + s.LogFields(), + ).Error("Failed to accept TCP connection") continue } go s.handleTCP(conn) @@ -86,13 +96,23 @@ func (s *Server) handleTCP(conn net.Conn) { } n, err = conn.Read(buf[prev:]) if err != nil { - s.logger.WithFields(fields).WithFields(s.LogFields()).WithError(err).Debug("Can't read connection: either EOFed or timeouted") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithError( + err, + ).Debug("Can't read connection: either EOFed or timeouted") break } prev += n peerID, err := s.idsCache.Find(buf[:prev]) if err != nil { - s.logger.WithFields(fields).WithFields(s.LogFields()).WithError(err).Debug("Couldn't lookup for peer in ids") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithError(err).Debug("Couldn't lookup for peer in ids") continue } if peerID == nil { @@ -102,14 +122,24 @@ func (s *Server) handleTCP(conn net.Conn) { if hs == nil { conf = s.confs.Get(*peerID) if conf == nil { - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Error("Configuration get failed") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + s.configuration.LogFields(), + ).Error("Configuration get failed") break } hs = govpn.NewHandshake(addr, conn, conf) } peer, err = hs.Server(buf[:prev]) if err != nil { - s.logger.WithFields(fields).WithError(err).WithFields(s.LogFields()).Error("Can't create new peer") + s.logger.WithFields( + fields, + ).WithError(err).WithFields( + s.LogFields(), + ).Error("Can't create new peer") continue } prev = 0 @@ -117,7 +147,13 @@ func (s *Server) handleTCP(conn net.Conn) { continue } - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Info("Handshake completed") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + peer.LogFields(), + ).Info("Handshake completed") hs.Zero() s.peersByIDLock.RLock() @@ -145,11 +181,23 @@ func (s *Server) handleTCP(conn net.Conn) { s.peersLock.Unlock() s.peersByIDLock.Unlock() s.kpLock.Unlock() - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Debug("Rehandshake completed") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + peer.LogFields(), + ).Debug("Rehandshake completed") } else { tap, err = s.callUp(peer, govpn.ProtocolTCP) if err != nil { - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).WithError(err).Error("TAP failed") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + peer.LogFields(), + ).WithError(err).Error("TAP failed") peer = nil break } @@ -169,7 +217,13 @@ func (s *Server) handleTCP(conn net.Conn) { s.peersLock.Unlock() s.peersByIDLock.Unlock() s.kpLock.Unlock() - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Info("Peer created") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + peer.LogFields(), + ).Info("Peer created") } break } @@ -193,7 +247,13 @@ func (s *Server) handleTCP(conn net.Conn) { } n, err = conn.Read(buf[prev:]) if err != nil { - s.logger.WithFields(fields).WithFields(s.LogFields()).WithError(err).Debug("Can't read connection: either EOFed or timeouted") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithError( + err, + ).Debug("Can't read connection: either EOFed or timeouted") break } prev += n @@ -206,7 +266,13 @@ func (s *Server) handleTCP(conn net.Conn) { continue } if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) { - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Warn("Packet unauthenticated") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + peer.LogFields(), + ).Warn("Packet unauthenticated") break } copy(buf, buf[i+govpn.NonceSize:prev]) diff --git a/src/cypherpunks.ru/govpn/server/udp.go b/src/cypherpunks.ru/govpn/server/udp.go index a2d92af..c579301 100644 --- a/src/cypherpunks.ru/govpn/server/udp.go +++ b/src/cypherpunks.ru/govpn/server/udp.go @@ -56,7 +56,13 @@ func (s *Server) startUDP() { "func": logFuncPrefix + "Server.startUDP", "bind": bind.String(), } - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Info("Listen") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + s.configuration.LogFields(), + ).Info("Listen") udpBufs <- make([]byte, govpn.MTUMax) go func() { var buf []byte @@ -75,18 +81,30 @@ func (s *Server) startUDP() { buf = <-udpBufs n, raddr, err = conn.ReadFromUDP(buf) if err != nil { - s.logger.WithFields(fields).WithFields(s.LogFields()).WithError(err).Debug("Receive failure") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithError(err).Debug("Receive failure") break } addr = raddr.String() loopFields := logrus.Fields{"addr": addr} - s.logger.WithFields(fields).WithFields(loopFields).Debug("Got UDP buffer, check if peer exists") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).Debug("Got UDP buffer, check if peer exists") s.peersLock.RLock() ps, exists = s.peers[addr] s.peersLock.RUnlock() if exists { - s.logger.WithFields(fields).WithFields(loopFields).Debug("Already known peer, PktProcess") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).Debug("Already known peer, PktProcess") go func(peer *govpn.Peer, tap *govpn.TAP, buf []byte, n int) { peer.PktProcess(buf[:n], tap, true) udpBufs <- buf @@ -99,15 +117,31 @@ func (s *Server) startUDP() { hs, exists = s.handshakes[addr] s.hsLock.RUnlock() if !exists { - logrus.WithFields(fields).WithFields(loopFields).Debug("No handshake yet, try to figure peer ID") + logrus.WithFields( + fields, + ).WithFields( + loopFields, + ).Debug("No handshake yet, try to figure peer ID") peerID, err = s.idsCache.Find(buf[:n]) if err != nil { - s.logger.WithFields(fields).WithFields(loopFields).WithFields(s.LogFields()).WithError(err).Debug("Couldn't lookup for peer in ids") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).WithFields( + s.LogFields(), + ).WithError(err).Debug("Couldn't lookup for peer in ids") udpBufs <- buf continue } if peerID == nil { - s.logger.WithFields(fields).WithFields(loopFields).WithFields(s.LogFields()).Debug("Identity unknown") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).WithFields( + s.LogFields(), + ).Debug("Identity unknown") udpBufs <- buf continue } @@ -116,12 +150,24 @@ func (s *Server) startUDP() { s.logger.WithFields(fields).WithFields(loopFields).Debug("Found peer ID") conf = s.confs.Get(*peerID) if conf == nil { - s.logger.WithFields(loopFields).WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Error("Peer try to connect, but not configured") + s.logger.WithFields( + loopFields, + ).WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + s.configuration.LogFields(), + ).Error("Peer try to connect, but not configured") udpBufs <- buf continue } - s.logger.WithFields(loopFields).WithFields(fields).Debug("Got configuration, perform handshake") + s.logger.WithFields( + loopFields, + ).WithFields( + fields, + ).Debug("Got configuration, perform handshake") hs = govpn.NewHandshake( addr, udpSender{conn: conn, addr: raddr}, @@ -130,10 +176,22 @@ func (s *Server) startUDP() { _, err := hs.Server(buf[:n]) udpBufs <- buf if err != nil { - s.logger.WithFields(loopFields).WithFields(fields).WithError(err).WithFields(s.LogFields()).Error("Can't create new peer: handshake failed") + s.logger.WithFields( + loopFields, + ).WithFields( + fields, + ).WithError(err).WithFields( + s.LogFields(), + ).Error("Can't create new peer: handshake failed") continue } - s.logger.WithFields(loopFields).WithFields(fields).WithFields(s.LogFields()).Info("Hashshake started, continue next packet") + s.logger.WithFields( + loopFields, + ).WithFields( + fields, + ).WithFields( + s.LogFields(), + ).Info("Hashshake started, continue next packet") s.hsLock.Lock() s.handshakes[addr] = hs @@ -141,20 +199,44 @@ func (s *Server) startUDP() { continue } - logrus.WithFields(fields).WithFields(loopFields).Debug("Already go handshake, finish it") + logrus.WithFields( + fields, + ).WithFields( + loopFields, + ).Debug("Already go handshake, finish it") peer, err := hs.Server(buf[:n]) if err != nil { - s.logger.WithFields(fields).WithFields(loopFields).WithError(err).WithFields(s.LogFields()).Error("Can't create new peer: handshake failed") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).WithError(err).WithFields( + s.LogFields(), + ).Error("Can't create new peer: handshake failed") udpBufs <- buf continue } if peer == nil { - s.logger.WithFields(fields).WithFields(loopFields).WithFields(s.LogFields()).Error("Couldn't continue handshake") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).WithFields( + s.LogFields(), + ).Error("Couldn't continue handshake") udpBufs <- buf continue } - s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(loopFields).WithFields(peer.LogFields()).Info("Handshake completed") + s.logger.WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + loopFields, + ).WithFields( + peer.LogFields(), + ).Info("Handshake completed") hs.Zero() s.hsLock.Lock() @@ -170,7 +252,11 @@ func (s *Server) startUDP() { s.peersByIDLock.RUnlock() if exists { - s.logger.WithFields(fields).WithFields(loopFields).Debug("Peer already exists") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).Debug("Peer already exists") s.peersLock.Lock() s.peers[addrPrev].terminator <- struct{}{} psNew := &PeerState{ @@ -197,13 +283,33 @@ func (s *Server) startUDP() { s.peersByIDLock.Unlock() s.kpLock.Unlock() - s.logger.WithFields(fields).WithFields(loopFields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Debug("Rehandshake completed") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).WithFields( + s.LogFields(), + ).WithFields( + peer.LogFields(), + ).Debug("Rehandshake completed") } else { go func(addr string, peer *govpn.Peer) { - s.logger.WithFields(fields).WithFields(loopFields).Debug("Peer do not already exists") + s.logger.WithFields( + fields, + ).WithFields( + loopFields, + ).Debug("Peer do not already exists") tap, err := s.callUp(peer, govpn.ProtocolUDP) if err != nil { - s.logger.WithFields(loopFields).WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).WithError(err).Error("TAP failed") + s.logger.WithFields( + loopFields, + ).WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + peer.LogFields(), + ).WithError(err).Error("TAP failed") return } psNew := &PeerState{ @@ -226,7 +332,15 @@ func (s *Server) startUDP() { s.peersLock.Unlock() s.peersByIDLock.Unlock() s.kpLock.Unlock() - s.logger.WithFields(loopFields).WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Info("Peer initialized") + s.logger.WithFields( + loopFields, + ).WithFields( + fields, + ).WithFields( + s.LogFields(), + ).WithFields( + peer.LogFields(), + ).Info("Peer initialized") }(addr, peer) } udpBufs <- buf diff --git a/src/cypherpunks.ru/govpn/stats.go b/src/cypherpunks.ru/govpn/stats.go index 2fa90e4..2f6c517 100644 --- a/src/cypherpunks.ru/govpn/stats.go +++ b/src/cypherpunks.ru/govpn/stats.go @@ -46,7 +46,9 @@ func StatsProcessor(stats string, peers *KnownPeers) { "port": stats, } - logger.WithFields(fields).WithField( + logger.WithFields( + fields, + ).WithField( "port", stats, ).Debug("Stats are going to listen") statsPort, err := net.Listen("tcp", stats) @@ -65,8 +67,11 @@ func StatsProcessor(stats string, peers *KnownPeers) { } deadLine := time.Now().Add(rwTimeout) if err = conn.SetDeadline(deadLine); err != nil { - logger.WithFields(fields).WithField( - "deadline", deadLine.String(), + logger.WithFields( + fields, + ).WithField( + "deadline", + deadLine.String(), ).WithError(err).Error("Can't set deadline") } else if _, err = conn.Read(buf); err != nil { logger.WithFields(fields).WithError(err).Error("Can't read buffer") @@ -78,7 +83,9 @@ func StatsProcessor(stats string, peers *KnownPeers) { peersList = append(peersList, *peer) } if err = json.NewEncoder(conn).Encode(peersList); err != nil { - logger.WithFields(fields).WithField( + logger.WithFields( + fields, + ).WithField( "peers", len(peersList), ).WithError(err).Error("Can't encode to JSON") } -- 2.44.0