Merge pull request #1942 from nspcc-dev/server/announced_port

network: add ability to specify port for P2P version exchange
This commit is contained in:
Roman Khimov 2021-04-30 14:42:25 +03:00 committed by GitHub
commit 8bfb6d7e37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 2 deletions

View file

@ -11,6 +11,7 @@ import (
// ApplicationConfiguration config specific to the node. // ApplicationConfiguration config specific to the node.
type ApplicationConfiguration struct { type ApplicationConfiguration struct {
Address string `yaml:"Address"` Address string `yaml:"Address"`
AnnouncedNodePort uint16 `yaml:"AnnouncedPort"`
AttemptConnPeers int `yaml:"AttemptConnPeers"` AttemptConnPeers int `yaml:"AttemptConnPeers"`
DBConfiguration storage.DBConfiguration `yaml:"DBConfiguration"` DBConfiguration storage.DBConfiguration `yaml:"DBConfiguration"`
DialTimeout time.Duration `yaml:"DialTimeout"` DialTimeout time.Duration `yaml:"DialTimeout"`

View file

@ -1278,8 +1278,14 @@ func (s *Server) broadcastTxLoop() {
} }
} }
// Port returns actual server port. It may differs from that of server.Config. // Port returns a server port that should be used in P2P version exchange. In
// case if `AnnouncedPort` is set in the server.Config, the announced node port
// will be returned (e.g. consider the node running behind NAT). If `AnnouncedPort`
// isn't set, the port returned may still differs from that of server.Config.
func (s *Server) Port() (uint16, error) { func (s *Server) Port() (uint16, error) {
if s.AnnouncedPort != 0 {
return s.ServerConfig.AnnouncedPort, nil
}
var port uint16 var port uint16
_, portStr, err := net.SplitHostPort(s.transport.Address()) _, portStr, err := net.SplitHostPort(s.transport.Address())
if err != nil { if err != nil {

View file

@ -31,7 +31,10 @@ type (
// Address. Example: "127.0.0.1". // Address. Example: "127.0.0.1".
Address string Address string
// Port. Example: 20332. // AnnouncedPort is an announced node port for P2P version exchange.
AnnouncedPort uint16
// Port is the actual node port it is bound to. Example: 20332.
Port uint16 Port uint16
// The network mode the server will operate on. // The network mode the server will operate on.
@ -92,6 +95,7 @@ func NewServerConfig(cfg config.Config) ServerConfig {
return ServerConfig{ return ServerConfig{
UserAgent: cfg.GenerateUserAgent(), UserAgent: cfg.GenerateUserAgent(),
Address: appConfig.Address, Address: appConfig.Address,
AnnouncedPort: appConfig.AnnouncedNodePort,
Port: appConfig.NodePort, Port: appConfig.NodePort,
Net: protoConfig.Magic, Net: protoConfig.Magic,
Relay: appConfig.Relay, Relay: appConfig.Relay,