From 007d20e56029d2bf28a2573809a7b6ff0ff2f38f Mon Sep 17 00:00:00 2001 From: Hans Moog <hm@mkjc.net> Date: Wed, 19 Feb 2020 01:20:37 +0100 Subject: [PATCH] Feat: added a plugin for the port checks --- go.mod | 2 +- main.go | 2 + plugins/autopeering/autopeering.go | 23 -------- plugins/gossip/gossip.go | 37 ++---------- plugins/portcheck/plugin.go | 90 ++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 57 deletions(-) create mode 100644 plugins/portcheck/plugin.go diff --git a/go.mod b/go.mod index d8ea9480..e5b295a4 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/iotaledger/hive.go v0.0.0-20200217140357-8f1ea1f52085 github.com/iotaledger/iota.go v1.0.0-beta.14 github.com/labstack/echo v3.3.10+incompatible - github.com/labstack/gommon v0.3.0 // indirect + github.com/labstack/gommon v0.3.0 github.com/magiconair/properties v1.8.1 github.com/mattn/go-colorable v0.1.4 // indirect github.com/mattn/go-isatty v0.0.11 // indirect diff --git a/main.go b/main.go index 352f263f..7329f0cf 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/gracefulshutdown" "github.com/iotaledger/goshimmer/plugins/logger" "github.com/iotaledger/goshimmer/plugins/metrics" + "github.com/iotaledger/goshimmer/plugins/portcheck" "github.com/iotaledger/goshimmer/plugins/remotelog" "github.com/iotaledger/goshimmer/plugins/tangle" "github.com/iotaledger/goshimmer/plugins/webapi" @@ -37,6 +38,7 @@ func main() { autopeering.PLUGIN, tangle.PLUGIN, gossip.PLUGIN, + portcheck.PLUGIN, gracefulshutdown.PLUGIN, analysis.PLUGIN, diff --git a/plugins/autopeering/autopeering.go b/plugins/autopeering/autopeering.go index 390bc68e..a7525b2e 100644 --- a/plugins/autopeering/autopeering.go +++ b/plugins/autopeering/autopeering.go @@ -16,9 +16,7 @@ import ( "github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/node" - "github.com/iotaledger/goshimmer/packages/netutil" "github.com/iotaledger/goshimmer/plugins/autopeering/local" - "github.com/iotaledger/goshimmer/plugins/banner" "github.com/iotaledger/goshimmer/plugins/config" "github.com/iotaledger/goshimmer/plugins/gossip" ) @@ -92,11 +90,6 @@ func start(shutdownSignal <-chan struct{}) { log.Fatalf("Error resolving %s: %v", local.CFG_BIND, err) } - // check that discovery is working and the port is open - log.Info("Testing service ...") - checkConnection(localAddr, &lPeer.Peer) - log.Info("Testing service ... done") - conn, err := net.ListenUDP(peeringAddr.Network(), localAddr) if err != nil { log.Fatalf("Error listening: %v", err) @@ -159,19 +152,3 @@ func parseEntryNodes() (result []*peer.Peer, err error) { return result, nil } - -func checkConnection(localAddr *net.UDPAddr, self *peer.Peer) { - peering := self.Services().Get(service.PeeringKey) - remoteAddr, err := net.ResolveUDPAddr(peering.Network(), peering.String()) - if err != nil { - panic(err) - } - - // do not check the address as a NAT may change them for local connections - err = netutil.CheckUDP(localAddr, remoteAddr, false, true) - if err != nil { - log.Errorf("Error testing service: %s", err) - log.Panicf("Please check that %s is publicly reachable at %s/%s", - banner.AppName, peering.String(), peering.Network()) - } -} diff --git a/plugins/gossip/gossip.go b/plugins/gossip/gossip.go index ab00f21d..e294cf2e 100644 --- a/plugins/gossip/gossip.go +++ b/plugins/gossip/gossip.go @@ -4,9 +4,7 @@ import ( "fmt" "net" "strconv" - "sync" - "github.com/iotaledger/hive.go/autopeering/peer" "github.com/iotaledger/hive.go/autopeering/peer/service" "github.com/iotaledger/hive.go/logger" @@ -14,7 +12,6 @@ import ( gp "github.com/iotaledger/goshimmer/packages/gossip" "github.com/iotaledger/goshimmer/packages/gossip/server" "github.com/iotaledger/goshimmer/plugins/autopeering/local" - "github.com/iotaledger/goshimmer/plugins/banner" "github.com/iotaledger/goshimmer/plugins/config" "github.com/iotaledger/goshimmer/plugins/tangle" ) @@ -22,6 +19,7 @@ import ( var ( log *logger.Logger mgr *gp.Manager + Srv *server.TCP ) func configureGossip() { @@ -66,15 +64,10 @@ func start(shutdownSignal <-chan struct{}) { } defer listener.Close() - srv := server.ServeTCP(lPeer, listener, log) - defer srv.Close() + Srv = server.ServeTCP(lPeer, listener, log) + defer Srv.Close() - //check that the server is working and the port is open - log.Info("Testing service ...") - checkConnection(srv, &lPeer.Peer) - log.Info("Testing service ... done") - - mgr.Start(srv) + mgr.Start(Srv) defer mgr.Close() log.Infof("%s started: Address=%s/%s", name, gossipAddr.String(), gossipAddr.Network()) @@ -83,28 +76,6 @@ func start(shutdownSignal <-chan struct{}) { log.Info("Stopping " + name + " ...") } -func checkConnection(srv *server.TCP, self *peer.Peer) { - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - conn, err := srv.AcceptPeer(self) - if err != nil { - return - } - _ = conn.Close() - }() - conn, err := srv.DialPeer(self) - if err != nil { - log.Errorf("Error testing: %s", err) - addr := self.Services().Get(service.GossipKey) - log.Panicf("Please check that %s is publicly reachable at %s/%s", - banner.AppName, addr.String(), addr.Network()) - } - _ = conn.Close() - wg.Wait() -} - func getTransaction(transactionId transaction.Id) (bytes []byte, err error) { log.Debugw("get tx from db", "id", transactionId.String()) diff --git a/plugins/portcheck/plugin.go b/plugins/portcheck/plugin.go new file mode 100644 index 00000000..89dd6bc2 --- /dev/null +++ b/plugins/portcheck/plugin.go @@ -0,0 +1,90 @@ +package portcheck + +import ( + "net" + "sync" + + "github.com/iotaledger/goshimmer/packages/gossip/server" + "github.com/iotaledger/goshimmer/packages/netutil" + "github.com/iotaledger/goshimmer/plugins/autopeering/local" + "github.com/iotaledger/goshimmer/plugins/banner" + "github.com/iotaledger/goshimmer/plugins/config" + "github.com/iotaledger/goshimmer/plugins/gossip" + + "github.com/iotaledger/hive.go/autopeering/peer" + "github.com/iotaledger/hive.go/autopeering/peer/service" + "github.com/iotaledger/hive.go/logger" + "github.com/iotaledger/hive.go/node" +) + +var PLUGIN = node.NewPlugin("Port Check", node.Enabled, run) + +var log *logger.Logger + +func run(ctx *node.Plugin) { + log = logger.NewLogger("Tangle") + + lPeer := local.GetInstance() + + // use the port of the peering service + peeringAddr := lPeer.Services().Get(service.PeeringKey) + _, peeringPort, err := net.SplitHostPort(peeringAddr.String()) + if err != nil { + panic(err) + } + + // resolve the bind address + address := net.JoinHostPort(config.NodeConfig.GetString(local.CFG_BIND), peeringPort) + localAddr, err := net.ResolveUDPAddr(peeringAddr.Network(), address) + if err != nil { + log.Fatalf("Error resolving %s: %v", local.CFG_BIND, err) + } + + // check that discovery is working and the port is open + log.Info("Testing service ...") + checkAutopeeringConnection(localAddr, &lPeer.Peer) + log.Info("Testing service ... done") + + //check that the server is working and the port is open + log.Info("Testing service ...") + checkGossipConnection(gossip.Srv, &lPeer.Peer) + log.Info("Testing service ... done") +} + +func checkAutopeeringConnection(localAddr *net.UDPAddr, self *peer.Peer) { + peering := self.Services().Get(service.PeeringKey) + remoteAddr, err := net.ResolveUDPAddr(peering.Network(), peering.String()) + if err != nil { + panic(err) + } + + // do not check the address as a NAT may change them for local connections + err = netutil.CheckUDP(localAddr, remoteAddr, false, true) + if err != nil { + log.Errorf("Error testing service: %s", err) + log.Panicf("Please check that %s is publicly reachable at %s/%s", + banner.AppName, peering.String(), peering.Network()) + } +} + +func checkGossipConnection(srv *server.TCP, self *peer.Peer) { + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + conn, err := srv.AcceptPeer(self) + if err != nil { + return + } + _ = conn.Close() + }() + conn, err := srv.DialPeer(self) + if err != nil { + log.Errorf("Error testing: %s", err) + addr := self.Services().Get(service.GossipKey) + log.Panicf("Please check that %s is publicly reachable at %s/%s", + banner.AppName, addr.String(), addr.Network()) + } + _ = conn.Close() + wg.Wait() +} -- GitLab