nat/nat.go
func Map(ctx context.Context, conf *Config, logger log.Logger, m Interface,
	protocol string, extPort, intPort int, name string) error {

	mapUpdateInterval := time.Duration(
		conf.MapUpdateInterval) * time.Millisecond

	mapTimeout := time.Duration(conf.MapTimeout) * time.Millisecond

	logger = logger.Add("proto", protocol, "extPort", extPort,
		"intPort", intPort, "mapUpdateInterval", mapUpdateInterval,
		"mapTimeout", mapTimeout, "name", name)

	if err := m.AddMapping(protocol, extPort, intPort,
		name, mapTimeout); err != nil {
		logger.Error(err.Error())
		return ErrAddMapping
	}
	logger.Info("mapped network port")
	go func() {
		timer := time.NewTimer(mapUpdateInterval)

		defer func() {
			timer.Stop()
			logger.Debug("deleting port mapping")
			m.DeleteMapping(protocol, extPort, intPort)
		}()

		for {
			select {
			case <-ctx.Done():
				return
			case <-timer.C:
				logger.Debug("refreshing port mapping")
				if err := m.AddMapping(protocol, extPort,
					intPort, name, mapTimeout); err != nil {
					logger.Warn("couldn't add" +
						" port mapping, error: " +
						err.Error())
				}
				timer.Reset(mapUpdateInterval)
			}
		}
	}()
	return nil
}
func Map(ctx context.Context, conf *Config, logger log.Logger, m Interface,
	protocol string, extPort, intPort int, name string) error {

	mapUpdateInterval := time.Duration(
		conf.MapUpdateInterval) * time.Millisecond

	mapTimeout := time.Duration(conf.MapTimeout) * time.Millisecond

	logger = logger.Add("proto", protocol, "extPort", extPort,
		"intPort", intPort, "mapUpdateInterval", mapUpdateInterval,
		"mapTimeout", mapTimeout, "name", name)

	if err := m.AddMapping(protocol, extPort, intPort,
		name, mapTimeout); err != nil {
		logger.Error(err.Error())
		return ErrAddMapping
	}
	logger.Info("mapped network port")
	go func() {
		timer := time.NewTimer(mapUpdateInterval)

		defer func() {
			timer.Stop()
			logger.Debug("deleting port mapping")
			m.DeleteMapping(protocol, extPort, intPort)
		}()

		for {
			select {
			case <-ctx.Done():
				return
			case <-timer.C:
				logger.Debug("refreshing port mapping")
				if err := m.AddMapping(protocol, extPort,
					intPort, name, mapTimeout); err != nil {
					logger.Warn("couldn't add" +
						" port mapping, error: " +
						err.Error())
				}
				timer.Reset(mapUpdateInterval)
			}
		}
	}()
	return nil
}
// Map adds a port mapping on NAT interface and keeps it alive until interface
// is closed.