Esempio n. 1
0
 public override void init()
 {
     try {
         io = new IO();
         if (lport == -1)
         {
             Type c = Type.GetType(target);
             IForwardedTCPIPDaemon daemon = (IForwardedTCPIPDaemon)Activator.CreateInstance(c);
             daemon.setChannel(this);
             Object[] foo = getPort(session, rport);
             daemon.setArg((Object[])foo[3]);
             new ThreadAux(daemon).start();
             connected = true;
             return;
         }
         else
         {
             TcpSocket socket = (factory == null) ?
                                new TcpSocket(target, lport) :
                                factory.createSocket(target, lport);
             socket.setTcpNoDelay(true);
             io.setInputStream(socket.getInputStream());
             io.setOutputStream(socket.getOutputStream());
             connected = true;
         }
     }
     catch (Exception e) {
         Console.WriteLine("target={0},port={1}", target, lport);
         Console.WriteLine(e);
     }
 }
Esempio n. 2
0
        public void connect(ISocketFactory socket_factory, String host, int port, int timeout)
        {
            try {
                if (socket_factory == null)
                {
                    socket = TcpSocketCreator.CreateSocket(proxy_host, proxy_port, timeout);
                    ins    = socket.getInputStream();
                    outs   = socket.getOutputStream();
                }
                else
                {
                    socket = socket_factory.createSocket(proxy_host, proxy_port);
                    ins    = socket_factory.getInputStream(socket);
                    outs   = socket_factory.getOutputStream(socket);
                }
                if (timeout > 0)
                {
                    socket.setSoTimeout(timeout);
                }
                socket.setTcpNoDelay(true);

                StreamAux.write(outs, StringAux.getBytesUTF8("CONNECT " + host + ":" + port + " HTTP/1.0\r\n"));

                if (user != null && passwd != null)
                {
                    byte[] _code = StringAux.getBytesUTF8((user + ":" + passwd));
                    _code = StringAux.toBase64(_code, 0, _code.Length);
                    StreamAux.write(outs, StringAux.getBytesUTF8("Proxy-Authorization: Basic "));
                    StreamAux.write(outs, _code);
                    StreamAux.write(outs, StringAux.getBytesUTF8("\r\n"));
                }

                StreamAux.write(outs, StringAux.getBytesUTF8("\r\n"));
                StreamAux.flush(outs);

                int foo = 0;

                StringBuilder sb = new StringBuilder();
                while (foo >= 0)
                {
                    foo = ins.ReadByte();
                    if (foo != 13)
                    {
                        sb.Append((char)foo);
                        continue;
                    }
                    foo = ins.ReadByte();
                    if (foo != 10)
                    {
                        continue;
                    }
                    break;
                }
                if (foo < 0)
                {
                    throw new System.IO.IOException();
                }

                String response = sb.ToString();
                String reason   = "Unknow reason";
                int    code     = -1;
                try {
                    foo = response.IndexOf(' ');
                    int bar = response.IndexOf(' ', foo + 1);
                    code   = int.Parse(response.Substring(foo + 1, bar - (foo + 1)));
                    reason = response.Substring(bar + 1);
                }
                catch                //(JException e)
                {
                }
                if (code != 200)
                {
                    throw new System.IO.IOException("proxy error: " + reason);
                }

                /*
                 * while(foo>=0){
                 * foo=in.read(); if(foo!=13) continue;
                 * foo=in.read(); if(foo!=10) continue;
                 * foo=in.read(); if(foo!=13) continue;
                 * foo=in.read(); if(foo!=10) continue;
                 * break;
                 * }
                 */

                int count = 0;
                while (true)
                {
                    count = 0;
                    while (foo >= 0)
                    {
                        foo = ins.ReadByte();
                        if (foo != 13)
                        {
                            count++;
                            continue;
                        }
                        foo = ins.ReadByte();
                        if (foo != 10)
                        {
                            continue;
                        }
                        break;
                    }
                    if (foo < 0)
                    {
                        throw new System.IO.IOException();
                    }
                    if (count == 0)
                    {
                        break;
                    }
                }
            }
            catch (RuntimeException e) {
                try {
                    if (socket != null)
                    {
                        socket.close();
                    }
                }
                catch                //(JException eee)
                {
                }
                String message = "ProxyHTTP: " + e.ToString();
                throw e;
            }
        }