Exemplo n.º 1
0
        public static void version_handshake(Stream stream, thinCLIProtocol tCLIprotocol)
        {
            /* Check for the initial magic string */
            byte[] magic = Types.unmarshal_n(stream, (uint)tCLIprotocol.magic_string.Length);
            for (int i = 0; i < tCLIprotocol.magic_string.Length; i++)
            {
                if (magic[i] != tCLIprotocol.magic_string[i])
                {
                    tCLIprotocol.dGlobalError("Failed to find a server on " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port);
                    tCLIprotocol.dGlobalUsage();
                    tCLIprotocol.dExit(1);
                }
            }
            /* Read the remote version numbers */
            int remote_major = Types.unmarshal_int(stream);
            int remote_minor = Types.unmarshal_int(stream);

            tCLIprotocol.dGlobalDebug("Remote host has version " + remote_major + "." + remote_minor, tCLIprotocol);
            tCLIprotocol.dGlobalDebug("Client has version " + tCLIprotocol.major + "." + tCLIprotocol.minor, tCLIprotocol);
            if (tCLIprotocol.major != remote_major)
            {
                tCLIprotocol.dGlobalError("Protocol version mismatch talking to server on " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port);
                tCLIprotocol.dGlobalUsage();
                tCLIprotocol.dExit(1);
            }
            /* Tell the server our version numbers */
            for (int i = 0; i < tCLIprotocol.magic_string.Length; i++)
            {
                stream.WriteByte((byte)tCLIprotocol.magic_string[i]);
            }
            Types.marshal_int(stream, tCLIprotocol.major);
            Types.marshal_int(stream, tCLIprotocol.minor);
        }
Exemplo n.º 2
0
        public static void performCommand(string Body, thinCLIProtocol tCLIprotocol)
        {
            string body = Body;

            body += "username="******"\n";
            body += "password="******"\n";
            if (body.Length != 0)
            {
                body = body.Substring(0, body.Length - 1); // strip last "\n"
            }

            string header         = "POST /cli HTTP/1.0\r\n";
            string content_length = "content-length: " + Encoding.UTF8.GetBytes(body).Length + "\r\n";
            string tosend         = header + content_length + "\r\n" + body;

            try
            {
                Stream stream = Transport.connect(tCLIprotocol, tCLIprotocol.conf.hostname, tCLIprotocol.conf.port);
                if (stream == null)
                {
                    // The SSL functions already tell us what happened
                    tCLIprotocol.dExit(1);
                }
                byte[] message = Encoding.UTF8.GetBytes(tosend);
                stream.Write(message, 0, message.Length);
                stream.Flush();
                Messages.version_handshake(stream, tCLIprotocol);
                interpreter(stream, tCLIprotocol);
            }
            catch (SocketException)
            {
                tCLIprotocol.dGlobalError("Connection to " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port + " refused.");
                tCLIprotocol.dExit(1);
            }
            catch (Exception e)
            {
                if (tCLIprotocol.conf.debug)
                {
                    throw e;
                }
                tCLIprotocol.dGlobalError("Caught exception: " + e.Message);
                tCLIprotocol.dExit(1);
            }
        }
Exemplo n.º 3
0
        public static Stream doRPC(String method, Uri uri, thinCLIProtocol tCLIprotocol, params string[] headers)
        {
            Stream http      = Transport.connect(tCLIprotocol, uri.Host, uri.Port);
            String startLine = string.Format("{0} {1} HTTP/1.0", method, uri.PathAndQuery);

            writeLine(http, startLine);
            foreach (string h in headers)
            {
                writeLine(http, h);
            }
            writeLine(http, "");

            String response = readLine(http);
            int    code     = getResultCode(response);

            switch (code)
            {
            case 200:
                break;

            case 302:
                string url = "";
                while (true)
                {
                    response = readLine(http);
                    if (response.StartsWith("Location: "))
                    {
                        url = response.Substring(10);
                    }
                    if (response.Equals("\r\n") || response.Equals(""))
                    {
                        break;
                    }
                }
                Uri redirect = new Uri(url.Trim());
                tCLIprotocol.conf.hostname = redirect.Host;
                http.Close();
                return(doRPC(method, redirect, tCLIprotocol, headers));

            default:
                tCLIprotocol.dGlobalError(string.Format("Received error code {0} from the server doing an HTTP {1}", code, method));
                http.Close();
                return(null);
            }

            while (true)
            {
                response = readLine(http);
                if (response.Equals("\r\n") || response.Equals(""))
                {
                    break;
                }
            }
            // Stream should be positioned after the headers
            return(http);
        }
Exemplo n.º 4
0
 public static Stream connect(thinCLIProtocol tCLIprotocol, String hostname, int port)
 {
     if (port != 443)
     {
         TcpClient client = new TcpClient(hostname, port);
         Stream    stream = client.GetStream();
         return(stream);
     }
     else
     {
         TcpClient client = new TcpClient(hostname, port);
         // Create an SSL stream that will close the client's stream.
         SslStream sslStream = new SslStream(
             client.GetStream(),
             false,
             new RemoteCertificateValidationCallback(ValidateServerCertificate),
             null
             );
         try
         {
             sslStream.AuthenticateAsClient("", null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true);
         }
         catch (AuthenticationException e) {
             if (tCLIprotocol.conf.debug)
             {
                 throw e;
             }
             tCLIprotocol.dGlobalError("Authentication failed - closing the connection.");
             client.Close();
             return(null);
         } catch (Exception e) {
             if (tCLIprotocol.conf.debug)
             {
                 throw e;
             }
             tCLIprotocol.dGlobalError("Exception during SSL auth - closing the connection.");
             client.Close();
             return(null);
         }
         return(sslStream);
     }
 }
Exemplo n.º 5
0
        public static void http_get(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol)
        {
            try
            {
                if (File.Exists(filename))
                {
                    throw new Exception(string.Format("The file '{0}' already exists", filename));
                }

                using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
                {
                    using (Stream http = HTTP.doRPC("GET", uri, tCLIprotocol))
                    {
                        if (http == null)
                        {
                            tCLIprotocol.dGlobalError("Server rejected request");
                            marshal_response(stream, tag.Failed);
                            return;
                        }
                        byte[] block = new byte[tCLIprotocol.conf.block_size];
                        while (true)
                        {
                            int n = http.Read(block, 0, block.Length);
                            if (n == 0)
                            {
                                break;
                            }
                            fs.Write(block, 0, n);
                        }
                        marshal_response(stream, tag.OK);
                    }
                }
            }
            catch (Exception e)
            {
                tCLIprotocol.dGlobalError("Received exception: " + e.Message);
                tCLIprotocol.dGlobalError("Unable to write output file: " + filename);
                marshal_response(stream, tag.Failed);
            }
        }
Exemplo n.º 6
0
 public static void http_put(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol)
 {
     try
     {
         using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
         {
             using (Stream http = HTTP.doRPC("PUT", uri, tCLIprotocol,
                                             string.Format("Content-Length: {0}", fs.Length)))
             {
                 if (http == null)
                 {
                     marshal_response(stream, tag.Failed);
                     return;
                 }
                 byte[] block = new byte[tCLIprotocol.conf.block_size];
                 while (true)
                 {
                     int n = fs.Read(block, 0, block.Length);
                     if (n == 0)
                     {
                         break;
                     }
                     http.Write(block, 0, n);
                 }
                 marshal_response(stream, tag.OK);
             }
         }
     }
     catch (FileNotFoundException)
     {
         tCLIprotocol.dGlobalError("File not found");
         marshal_response(stream, tag.Failed);
     }
     catch (Exception e)
     {
         tCLIprotocol.dGlobalError(string.Format("Received exception: {0}", e.Message));
         marshal_response(stream, tag.Failed);
     }
 }
Exemplo n.º 7
0
        /// <param name="addr">The target URI, including scheme, hostname and path.</param>
        public static Stream doRPC(String method, Uri uri, thinCLIProtocol tCLIprotocol)
        {
            Stream http   = Transport.connect(tCLIprotocol, uri.Host, uri.Port);
            String header = method + " " + uri.PathAndQuery + " HTTP/1.0\r\n\r\n";

            writeLine(http, header);
            String response = readLine(http);
            int    code     = getResultCode(response);

            switch (code)
            {
            case 200:
                break;

            case 302:
                string url = "";
                while (true)
                {
                    response = readLine(http);
                    if (response.StartsWith("Location: "))
                    {
                        url = response.Substring(10);
                    }
                    if (response.Equals("\r\n") || response.Equals(""))
                    {
                        break;
                    }
                }
                Uri redirect = new Uri(url.Trim());
                tCLIprotocol.conf.hostname = redirect.Host;
                http.Close();
                return(doRPC(method, redirect, tCLIprotocol));

            default:
                tCLIprotocol.dGlobalError("Received an error message from the server doing an HTTP " + method + " " + uri.PathAndQuery + " : " + response);
                http.Close();
                return(null);
            }

            while (true)
            {
                response = readLine(http);
                if (response.Equals("\r\n") || response.Equals(""))
                {
                    break;
                }
            }
            // Stream should be positioned after the headers
            return(http);
        }
Exemplo n.º 8
0
 public static void protocol_failure(string msg, tag t, thinCLIProtocol tCLIprotocol)
 {
     tCLIprotocol.dGlobalError("Protocol failure: Reading " + msg + ": unexpected tag: " + t);
     tCLIprotocol.dExit(1);
 }
Exemplo n.º 9
0
 public static void version_handshake(Stream stream, thinCLIProtocol tCLIprotocol)
 {
     /* Check for the initial magic string */
     byte[] magic = Types.unmarshal_n(stream, (uint)tCLIprotocol.magic_string.Length);
     for (int i = 0; i < tCLIprotocol.magic_string.Length; i++)
     {
         if (magic[i] != tCLIprotocol.magic_string[i])
         {
             tCLIprotocol.dGlobalError("Failed to find a server on " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port);
             tCLIprotocol.dGlobalUsage();
             tCLIprotocol.dExit(1);
         }
     }
     /* Read the remote version numbers */
     int remote_major = Types.unmarshal_int(stream);
     int remote_minor = Types.unmarshal_int(stream);
     tCLIprotocol.dGlobalDebug("Remote host has version " + remote_major + "." + remote_minor, tCLIprotocol);
     tCLIprotocol.dGlobalDebug("Client has version " + tCLIprotocol.major + "." + tCLIprotocol.minor, tCLIprotocol);
     if (tCLIprotocol.major != remote_major)
     {
         tCLIprotocol.dGlobalError("Protocol version mismatch talking to server on " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port);
         tCLIprotocol.dGlobalUsage();
         tCLIprotocol.dExit(1);
     }
     /* Tell the server our version numbers */
     for (int i = 0; i < tCLIprotocol.magic_string.Length; i++)
     {
         stream.WriteByte((byte)tCLIprotocol.magic_string[i]);
     }
     Types.marshal_int(stream, tCLIprotocol.major);
     Types.marshal_int(stream, tCLIprotocol.minor);
 }
Exemplo n.º 10
0
        public static void performCommand(string Body, thinCLIProtocol tCLIprotocol)
        {
            string body = Body;
            body += "username="******"\n";
            body += "password="******"\n";
            if (body.Length != 0)
            {
                body = body.Substring(0, body.Length - 1); // strip last "\n"
            }

            string header = "POST /cli HTTP/1.0\r\n";
            string content_length = "content-length: " + Encoding.UTF8.GetBytes(body).Length + "\r\n";
            string tosend = header + content_length + "\r\n" + body;

            try
            {
                Stream stream = Transport.connect(tCLIprotocol, tCLIprotocol.conf.hostname, tCLIprotocol.conf.port);
                if (stream == null)
                {
                    // The SSL functions already tell us what happened
                    tCLIprotocol.dExit(1);
                }
                byte[] message = Encoding.UTF8.GetBytes(tosend);
                stream.Write(message, 0, message.Length);
                stream.Flush();
                Messages.version_handshake(stream, tCLIprotocol);
                interpreter(stream, tCLIprotocol);
            }
            catch (SocketException)
            {
                tCLIprotocol.dGlobalError("Connection to " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port + " refused.");
                tCLIprotocol.dExit(1);
            }
            catch (Exception e)
            {
                if (tCLIprotocol.conf.debug) throw e;
                tCLIprotocol.dGlobalError("Caught exception: " + e.Message);
                tCLIprotocol.dExit(1);
            }
        }
Exemplo n.º 11
0
        public static void http_get(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol)
        {
            try
            {
                if (File.Exists(filename))
                    throw new Exception(string.Format("The file '{0}' already exists", filename));

                using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
                {
                    using (Stream http = HTTP.doRPC("GET", uri, tCLIprotocol))
                    {
                        if (http == null)
                        {
                            tCLIprotocol.dGlobalError("Server rejected request");
                            marshal_response(stream, tag.Failed);
                            return;
                        }
                        byte[] block = new byte[tCLIprotocol.conf.block_size];
                        while (true)
                        {
                            int n = http.Read(block, 0, block.Length);
                            if (n == 0) break;
                            fs.Write(block, 0, n);
                        }
                        marshal_response(stream, tag.OK);
                    }
                }
            }
            catch (Exception e)
            {
                tCLIprotocol.dGlobalError("Received exception: " + e.Message);
                tCLIprotocol.dGlobalError("Unable to write output file: " + filename);
                marshal_response(stream, tag.Failed);
            }
        }
Exemplo n.º 12
0
 public static void http_put(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol)
 {
     try
     {
         using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
         {
             using (Stream http = HTTP.doRPC("PUT", uri, tCLIprotocol))
             {
                 if (http == null)
                 {
                     marshal_response(stream, tag.Failed);
                     return;
                 }
                 byte[] block = new byte[tCLIprotocol.conf.block_size];
                 while (true)
                 {
                     int n = fs.Read(block, 0, block.Length);
                     if (n == 0) break;
                     http.Write(block, 0, n);
                 }
                 marshal_response(stream, tag.OK);
             }
         }
     }
     catch (FileNotFoundException)
     {
         tCLIprotocol.dGlobalError("File not found");
         marshal_response(stream, tag.Failed);
     }
     catch (Exception e)
     {
         tCLIprotocol.dGlobalError(string.Format("Received exception: {0}", e.Message));
         marshal_response(stream, tag.Failed);
     }
 }
Exemplo n.º 13
0
 public static void protocol_failure(string msg, tag t, thinCLIProtocol tCLIprotocol)
 {
     tCLIprotocol.dGlobalError("Protocol failure: Reading " + msg + ": unexpected tag: " + t);
     tCLIprotocol.dExit(1);
 }
Exemplo n.º 14
0
        /// <param name="addr">The target URI, including scheme, hostname and path.</param>
        public static Stream doRPC(String method, Uri uri, thinCLIProtocol tCLIprotocol)
        {
            Stream http = Transport.connect(tCLIprotocol, uri.Host, uri.Port);
            String header = method + " " + uri.PathAndQuery + " HTTP/1.0\r\n\r\n";
            writeLine(http, header);
            String response = readLine(http);
            int code = getResultCode(response);
            switch (code)
            {
                case 200:
                    break;
                case 302:
                    string url = "";
                    while (true)
                    {
                        response = readLine(http);
                        if (response.StartsWith("Location: "))
                            url = response.Substring(10);
                        if (response.Equals("\r\n") || response.Equals("")) break;
                    }
                    Uri redirect = new Uri(url.Trim());
                    tCLIprotocol.conf.hostname = redirect.Host;
                    http.Close();
                    return doRPC(method, redirect, tCLIprotocol);
                default:
                    tCLIprotocol.dGlobalError("Received an error message from the server doing an HTTP " + method + " " + uri.PathAndQuery + " : " + response);
                    http.Close();
                    return null;
            }

            while (true)
            {
                response = readLine(http);
                if (response.Equals("\r\n") || response.Equals("")) break;
            }
            // Stream should be positioned after the headers
            return http;
        }
Exemplo n.º 15
0
        public static Stream connect(thinCLIProtocol tCLIprotocol, String hostname, int port)
        {
		    if (port != 443){
			    TcpClient client = new TcpClient(hostname, port);
			    Stream stream = client.GetStream();
			    return stream;
		    } else {
        	    TcpClient client = new TcpClient(hostname, port);
        	    // Create an SSL stream that will close the client's stream.
        	    SslStream sslStream = new SslStream(
            	    client.GetStream(),
            	    false,
            	    new RemoteCertificateValidationCallback(ValidateServerCertificate),
            	    null
                );
        	    try
        	    {
                    sslStream.AuthenticateAsClient("", null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true);
                }
        	    catch (AuthenticationException e){
        		    if (tCLIprotocol.conf.debug) throw e;
        		    tCLIprotocol.dGlobalError("Authentication failed - closing the connection.");
            	    client.Close();
            	    return null;
                } catch (Exception e) {
            	    if (tCLIprotocol.conf.debug) throw e;
            	    tCLIprotocol.dGlobalError("Exception during SSL auth - closing the connection.");
            	    client.Close();
            	    return null;
                }
			    return sslStream;
		    }
	    }