public MainClient(string configFile, IDependent dependent) { ConfigFile = configFile; Config = new MainConfig(); if (File.Exists(ConfigFile)) { Config = JsonConvert.DeserializeObject <MainConfig>(File.ReadAllText(ConfigFile)); } File.WriteAllText(ConfigFile, JsonConvert.SerializeObject(Config)); selfNodeData.DomainName = Config.TapConfig.SelfName; NetworkManager = new NetworkManager(Config.NetworksSaveConfig); TapMessageInterface = new TapMessageInterface(Config.TapConfig, dependent.GetTapInerface()); DnmpClient = new DnmpClient(TapMessageInterface, new OsUdpProtocol(Config.GeneralConfig.ReceiveBufferSize, Config.GeneralConfig.SendBufferSize), Config.ClientConfig); DnmpClient.OnDisconnected += () => CurrentNetworkId = Guid.Empty; DnmpClient.OnConnectionTimeout += () => CurrentNetworkId = Guid.Empty; WebSocketServer = new ClientWebSocketServer(this); HttpServer = new ClientHttpServer(this); }
public NetworkHandler(DnmpClient realClient, Protocol usedProtocol) { this.realClient = realClient; UsedProtocol = usedProtocol; usedProtocol.OnReceive += ReceiveCallback; }
public void Disconnect() { DnmpClient.Stop(); TapMessageInterface.Stop(); CurrentNetworkId = Guid.Empty; }
public void Dispose() { DnmpClient?.Dispose(); WebSocketServer?.Dispose(); TapMessageInterface?.Dispose(); }
public void Connect(Guid networkId, int sourcePort, bool startAsFirst, IPAddress publicIp, bool useUpnp = true, bool useStun = true) { var networkConnectData = NetworkManager.SavedNetworks[networkId].GetConnectionData(); if (useUpnp) { PortMapperUtils.TryMapPort(sourcePort, Config.StunConfig.PortMappingTimeout).Wait(); } var endPoints = new HashSet <IPEndPoint>(); if (startAsFirst) { IPEndPoint stunnedEndPoint = null; if (useStun) { try { stunnedEndPoint = PortMapperUtils.GetStunnedIpEndPoint(sourcePort, Config.StunConfig.Host, Config.StunConfig.Port); } catch (Exception) { // ignored } if (stunnedEndPoint == null) { WebSocketServer.BroadcastNotification("stun-error"); stunnedEndPoint = new IPEndPoint(IPAddress.Loopback, sourcePort); } } else { stunnedEndPoint = new IPEndPoint(publicIp, sourcePort); } foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces()) { if (!(networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)) { continue; } if (networkInterface.OperationalStatus != OperationalStatus.Up) { continue; } foreach (var ip in networkInterface.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { endPoints.Add(new IPEndPoint(ip.Address, sourcePort)); endPoints.Add(new IPEndPoint(ip.Address, stunnedEndPoint.Port)); } } } foreach (var ip in GetTraceRoute(stunnedEndPoint.Address, 32)) { endPoints.Add(new IPEndPoint(ip, stunnedEndPoint.Port)); endPoints.Add(new IPEndPoint(ip, sourcePort)); } endPoints.Add(stunnedEndPoint); endPoints.Add(new IPEndPoint(IPAddress.Loopback, sourcePort)); foreach (var endPoint in endPoints.Where(x => x.Port > 0 && !x.Address.GetAddressBytes().SequenceEqual(IPAddress.Any.GetAddressBytes()))) { NetworkManager.AddEndPoint(networkId, new RealIPEndPoint(endPoint)); } NetworkManager.SaveNetworks(); WebSocketServer.BroadcastNetworkList(); Task.Run(() => DnmpClient.StartAsFirstNodeAsync(new RealIPEndPoint(new IPEndPoint(IPAddress.Any, sourcePort)), new RealIPEndPoint(stunnedEndPoint), networkConnectData.Item2, new AesSymmetricKey(), selfNodeData.GetBytes())); } else { Task.Run(() => DnmpClient.ConnectManyAsync(networkConnectData.Item1.ToArray(), new RealIPEndPoint(new IPEndPoint(IPAddress.Any, sourcePort)), true, networkConnectData.Item2, new AesSymmetricKey(), selfNodeData.GetBytes())); } CurrentNetworkId = networkId; }