예제 #1
0
 static ISocketConnector GetProxy(String file)
 {
     using (TextReader reader = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)))
     {
         while (true)
         {
             String proxyString = reader.ReadLine();
             if (proxyString == null)
             {
                 break;
             }
             if (proxyString.Length > 0)
             {
                 proxyString = proxyString.Trim();
                 return(ConnectorParser.ParseProxy("gateway:" + proxyString));
             }
         }
     }
     return(null);
 }
예제 #2
0
        static Int32 Main(string[] args)
        {
            TunnelOptions optionsParser = new TunnelOptions();
            List <String> nonOptionArgs = optionsParser.Parse(args);

            if (nonOptionArgs.Count <= 0)
            {
                Console.WriteLine("Must give at least one listener");
                optionsParser.PrintUsage();
                return(-1);
            }

            SelectControl selectControl = new SelectControl(false);

            //
            // Parse listeners
            //
            int arg = 0;

            do
            {
                //
                // Get Command Line Arguments
                //
                String connector = nonOptionArgs[arg];
                arg++;
                if (arg >= nonOptionArgs.Count)
                {
                    Console.WriteLine("EndPoint '{0}' is missing a protocol 'tcp|udp' and a listen port set", connector);
                    optionsParser.PrintUsage();
                    return(-1);
                }
                //
                // Parse End Point
                //
                HostWithOptionalProxy forwardHost = ConnectorParser.ParseConnectorWithPortAndOptionalProxy(connector);

                //
                // Parse Protocol and Port Set
                //
                String protocolAndPortSet = nonOptionArgs[arg++];

                Boolean isTcp;
                if (protocolAndPortSet.StartsWith("tcp", StringComparison.CurrentCultureIgnoreCase))
                {
                    isTcp = true;
                }
                else if (protocolAndPortSet.StartsWith("udp", StringComparison.CurrentCultureIgnoreCase))
                {
                    isTcp = false;
                }
                else
                {
                    throw new InvalidOperationException(String.Format("Unknown protocol '{0}', expected 'tcp' or 'udp'",
                                                                      (protocolAndPortSet.Length < 3) ? protocolAndPortSet : protocolAndPortSet.Remove(3)));
                }

                if (protocolAndPortSet.Length < 4)
                {
                    Console.WriteLine("ProtocolAndPortSet '{0}' Protocol '{1}' is missing a listen port set", connector, protocolAndPortSet);
                    optionsParser.PrintUsage();
                    return(-1);
                }

                String portSetString = protocolAndPortSet.Substring(3);
                if (String.IsNullOrEmpty(portSetString))
                {
                    Console.WriteLine("EndPoint '{0}' Protocol '{1}' is missing a listen port set", connector, protocolAndPortSet);
                    optionsParser.PrintUsage();
                    return(-1);
                }

                PortSet portSet = ParseUtilities.ParsePortSet(portSetString);

                if (isTcp)
                {
                    TcpCallback tcpCallback = new TcpCallback(forwardHost);

                    for (int i = 0; i < portSet.Length; i++)
                    {
                        Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        listenSocket.Bind(new IPEndPoint(IPAddress.Any, portSet[i]));
                        listenSocket.Listen(optionsParser.socketBackLog.ArgValue);
                        selectControl.AddListenSocket(listenSocket, tcpCallback.HandleAccept);
                    }
                }
                else
                {
                    if (forwardHost.proxy == null)
                    {
                        IPEndPoint        serverEndPoint = forwardHost.directEndPoint.GetOrResolveToIPEndPoint();
                        DirectUdpCallback udpCallback    = new DirectUdpCallback(serverEndPoint, 30 * 60); // 30 minutes
                        for (int i = 0; i < portSet.Length; i++)
                        {
                            Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            udpSocket.Bind(new IPEndPoint(IPAddress.Any, portSet[i]));
                            selectControl.AddReceiveSocket(udpSocket, udpCallback.ReceiveHandler);
                        }
                    }
                    else
                    {
                        throw new NotImplementedException();

                        /*
                         * UdpThroughProxyCallback udpCallback = new UdpThroughProxyCallback(serverEndPoint, proxyConnector,
                         *  1000 * 60 * 4, 1000 * 60 * 10);
                         * for (int i = 0; i < portSet.Length; i++)
                         * {
                         *  udpListenerList.Add(new UdpSelectListener(new IPEndPoint(IPAddress.Any, portSet[i]), udpCallback));
                         * }
                         */
                    }
                }
            } while (arg < nonOptionArgs.Count);

            SelectServer selectServer = new SelectServer(selectControl, new Buf(8192));

            selectServer.Run();

            return(-1);
        }