private IPEndPoint GetRealyDest(RelayHeader relayHeader) { IPEndPoint relayDest = null; if (relayHeader.Type == RelayType.IP) { relayDest = new IPEndPoint(IPAddress.Parse(relayHeader.Name), Config.serverPort); } else if (relayHeader.Type == RelayType.Node) { relayDest = DHTLikeNet.GetNodeIP(relayHeader.Name); } else { log.Error(String.Format("The relayHeader.Type={0} is not defined.", relayHeader.Type)); return(null); } if (relayDest != null && IPAddress.IsLoopback(relayDest.Address) && relayDest.Port == Config.serverPort) { this.isEndPoint = true; relayDest = GetIPFromHeader(); } return(relayDest); }
public TcpRelay(byte[] header, Socket inSocket) { if (header == null || inSocket == null) { return; } this.isStartPoint = true; this.relayHeader = new RelayHeader().FromBytes(header); this.inSocket = inSocket; }
public void Relay() { try { log.Info("Receive Relay request from: " + inSocket.RemoteEndPoint); if (!isStartPoint) { this.relayHeader = new RelayHeader().FromSocket(inSocket); } IPEndPoint relayDest = GetRealyDest(this.relayHeader); if (relayDest == null) { log.Info("Failed to get ip."); inSocket.Close(); return; } outSocket = new TcpClient(); // Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); AsyncRelay(relayDest); } catch (Exception e) { log.Info(e); } }
public void serverRun() { _server.Start(); while (_isRunning) { try { Socket newClient = _server.AcceptSocket(); IPEndPoint remoteIp = (IPEndPoint)newClient.RemoteEndPoint; // log.Info("connection from: " + remoteIp); if (!IPAddress.IsLoopback(remoteIp.Address)) { log.Info("Unknow danger connection from: " + remoteIp); continue; } byte[] headerbyts = new RelayHeader(RelayType.Node, this.relayNode).ToBytes(); new Thread(() => new TcpRelay(headerbyts, newClient).Relay()).Start(); } catch (IOException e) { Console.WriteLine(e); } } }