void mWorkerForwarder_DoWork(object sender, DoWorkEventArgs e) { int tries = 0; int port = (int)((object[])e.Argument)[0]; bool adding = (bool)((object[])e.Argument)[1]; retry: try { tries++; if (!UPnP.Discover()) { e.Result = 0; } else if (adding) { UPnP.ForwardPort(port, ProtocolType.Tcp, Server.SoftwareName + "Server"); e.Result = 1; } else { UPnP.DeleteForwardingRule(port, ProtocolType.Tcp); e.Result = 3; } } catch { if (tries < 2) { goto retry; } e.Result = 2; } }
private void OnStart(int port, int ws_port) { ListenerPort = port; timer = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(0, 5000, Context.Self, new Timer(), ActorRefs.NoSender); if ((port > 0 || ws_port > 0) && localAddresses.All(p => !p.IsIPv4MappedToIPv6 || IsIntranetAddress(p)) && UPnP.Discover()) { try { localAddresses.Add(UPnP.GetExternalIP()); if (port > 0) { UPnP.ForwardPort(port, ProtocolType.Tcp, "Zoro"); } if (ws_port > 0) { UPnP.ForwardPort(ws_port, ProtocolType.Tcp, "Zoro WebSocket"); } } catch { } } if (port > 0) { tcp_manager.Tell(new Tcp.Bind(Self, new IPEndPoint(IPAddress.Any, port), options: new[] { new Inet.SO.ReuseAddress(true) })); } if (ws_port > 0) { ws_host = new WebHostBuilder().UseKestrel().UseUrls($"http://*:{ws_port}").Configure(app => app.UseWebSockets().Run(ProcessWebSocketAsync)).Build(); ws_host.Start(); } }
private static bool DiscoverNeo() { if (UPnP.Discover()) { Console.WriteLine("You have an UPnP-enabled router and your IP is: " + UPnP.ExternalIp); UPnP.ForwardPort(_neoTcpPort, ProtocolType.Tcp, "NEO Tcp"); return(true); } return(false); }
private void OnStart(ChannelsConfig config) { ListenerTcpPort = config.Tcp?.Port ?? 0; ListenerWsPort = config.WebSocket?.Port ?? 0; MinDesiredConnections = config.MinDesiredConnections; MaxConnections = config.MaxConnections; MaxConnectionsPerAddress = config.MaxConnectionsPerAddress; // schedule time to trigger `OnTimer` event every TimerMillisecondsInterval ms timer = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(0, 5000, Context.Self, new Timer(), ActorRefs.NoSender); if ((ListenerTcpPort > 0 || ListenerWsPort > 0) && localAddresses.All(p => !p.IsIPv4MappedToIPv6 || IsIntranetAddress(p)) && UPnP.Discover()) { try { localAddresses.Add(UPnP.GetExternalIP()); if (ListenerTcpPort > 0) { UPnP.ForwardPort(ListenerTcpPort, ProtocolType.Tcp, "NEO Tcp"); } if (ListenerWsPort > 0) { UPnP.ForwardPort(ListenerWsPort, ProtocolType.Tcp, "NEO WebSocket"); } } catch { } } if (ListenerTcpPort > 0) { tcp_manager.Tell(new Tcp.Bind(Self, config.Tcp, options: new[] { new Inet.SO.ReuseAddress(true) })); } if (ListenerWsPort > 0) { var host = "*"; if (!config.WebSocket.Address.GetAddressBytes().SequenceEqual(IPAddress.Any.GetAddressBytes())) { // Is not for all interfaces host = config.WebSocket.Address.ToString(); } ws_host = new WebHostBuilder().UseKestrel().UseUrls($"http://{host}:{ListenerWsPort}").Configure(app => app.UseWebSockets().Run(ProcessWebSocketAsync)).Build(); ws_host.Start(); } }
static Peer() { localAddresses.UnionWith(NetworkInterface.GetAllNetworkInterfaces().SelectMany(p => p.GetIPProperties().UnicastAddresses).Select(p => p.Address.Unmap())); try { isUPnPDiscovered = UPnP.Discover(); if (isUPnPDiscovered) { localAddresses.Add(UPnP.GetExternalIP()); } } catch { } IPAddress myIPAddress = ZoroChainSystem.Singleton.MyIPAddress; if (myIPAddress != null) { localAddresses.Add(myIPAddress); } }
void mWorkerForwarder_DoWork(object sender, DoWorkEventArgs e) { bool adding = (bool)e.Argument; try { if (!UPnP.Discover()) { e.Result = 0; } else if (adding) { UPnP.ForwardPort(port, ProtocolType.Tcp, Server.SoftwareName + "Server"); e.Result = 1; } else { UPnP.DeleteForwardingRule(port, ProtocolType.Tcp); e.Result = 3; } } catch (Exception ex) { Logger.LogError("Unexpected UPnP error", ex); e.Result = 2; } }