Пример #1
0
        private void SetNodeName()
        {
            int p = HostName.IndexOf('.');

            if (p < 0)
            {
                NodeName = HostName;
                NbtName  = HostName; // If there are no dots, assume it's a netbios name
            }
            else // if it has dot's assume it's a dns name
            {
                NodeName = HostName.Substring(0, p);

                NbtName = NodeName; // assume the nodename is a dns one

                if (NbtName.Length > 15)
                {
                    NbtName = NbtName.Substring(0, 15); // truncate down to 15 if needed
                }
                // Pad upto 15 if needed, and add to 16
                //fNBTName.PadRight(16, (char)0x20);

                while (NbtName.Length < 16)
                {
                    byte pad = 0x20;

                    NbtName += (char)pad;
                }
            }
        }
Пример #2
0
        public void Connect(string userName, string userPassword, string userResource, string serverName = null, string oAuthToken = null)
        {
            Jid            = XmppJid.Empty;
            UserName       = userName;
            UserPassword   = userPassword;
            UserResource   = userResource;
            UserOAuthToken = oAuthToken;
            ServerName     = string.IsNullOrWhiteSpace(serverName) ? HostName.Substring(HostName.IndexOf('.') + 1) : serverName;

            if (engine == null)
            {
                engine = new Thread(new ThreadStart(StartEngine));
                engine.IsBackground = true;
                engine.SetApartmentState(ApartmentState.STA);
            }

            engine.Start();
        }
        public void Validate(ValidationResult result)
        {
            if (EntityHeader.IsNullOrEmpty(ListenerType))
            {
                result.AddUserError("Listener Type is a Required Field.");
                return;
            }

            switch (ListenerType.Value)
            {
            case ListenerTypes.AMQP:
                if (string.IsNullOrEmpty(HostName))
                {
                    result.AddUserError("Host Name is required for Connecting to an AMQP Server.");
                }
                if (!Anonymous)
                {
                    if (string.IsNullOrEmpty(UserName))
                    {
                        result.AddUserError("User Name is Required to connect to your AMQP server for non-anonymous connections.");
                    }
                    if (string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(SecurePasswordId))
                    {
                        result.AddUserError("Password is Required to connect to your AMQP server for non-anonymous connections.");
                    }
                }
                else
                {
                    UserName = null;
                    Password = null;
                }

                if (!AmqpSubscriptions.Any())
                {
                    result.AddUserError("Please ensure you provide at least one subscription (including wildcards * and #) that will be monitored for incoming messages.");
                }
                break;

            case ListenerTypes.AzureEventHub:
                if (HostName != null && HostName.ToLower().StartsWith("sb://"))
                {
                    HostName = HostName.Substring(5);
                }
                if (string.IsNullOrEmpty(HostName))
                {
                    result.AddUserError("Host Name is required for Azure Event Hubs, this is the host name of your Event Hub without the sb:// protocol.");
                }
                if (string.IsNullOrEmpty(HubName))
                {
                    result.AddUserError("Hub Name is Required for an Azure Event Hub Listener.");
                }
                if (string.IsNullOrEmpty(AccessKey) && string.IsNullOrEmpty(SecureAccessKeyId))
                {
                    result.AddUserError("Access Key is Required for an Azure Event Hub listener.");
                }
                if (!string.IsNullOrEmpty(AccessKey) && !Utils.StringValidationHelper.IsBase64String(AccessKey))
                {
                    result.AddUserError("Access Key does not appear to be a Base 64 string and is likely incorrect.");
                }
                break;

            case ListenerTypes.AzureIoTHub:
                if (HostName != null && HostName.ToLower().StartsWith("sb://"))
                {
                    HostName = HostName.Substring(5);
                }
                if (string.IsNullOrEmpty(HostName))
                {
                    result.AddUserError("Host Name is required for Azure IoT Hub, this is found in the Event Hub-compatible endpoint field on the Azure Portal.");
                }
                if (string.IsNullOrEmpty(ResourceName))
                {
                    result.AddUserError("Resource Name is require for Azure IoT Hub, this is found in the Event Hub-compatible name field on the Azure Portal.");
                }
                if (string.IsNullOrEmpty(AccessKeyName))
                {
                    result.AddUserError("Access Key Name is a required field for an Azure Serice Bus Listener.");
                }
                if (string.IsNullOrEmpty(AccessKey) && string.IsNullOrEmpty(SecureAccessKeyId))
                {
                    result.AddUserError("Access Key is Required for Azure IoT Event Hub.");
                }
                if (!string.IsNullOrEmpty(AccessKey) && !Utils.StringValidationHelper.IsBase64String(AccessKey))
                {
                    result.AddUserError("Access Key does not appear to be a Base 64 string and is likely incorrect.");
                }
                break;

            case ListenerTypes.AzureServiceBus:
                if (string.IsNullOrEmpty(HostName))
                {
                    result.AddUserError("Host Name is required for an Azure Service Bus Listener, this is the host name of your Event Hub without the sb:// protocol.");
                }
                if (HostName != null && HostName.ToLower().StartsWith("sb://"))
                {
                    HostName = HostName.Substring(5);
                }
                if (string.IsNullOrEmpty(Queue))
                {
                    result.AddUserError("Queue Name is required for an Azure Service Bus Listener.");
                }
                if (string.IsNullOrEmpty(AccessKeyName))
                {
                    result.AddUserError("Access Key Name is a required field for an Azure Serice Bus Listener.");
                }
                if (string.IsNullOrEmpty(AccessKey) && string.IsNullOrEmpty(SecureAccessKeyId))
                {
                    result.AddUserError("Access Key is Required for an Azure IoT Service Bus Listener.");
                }
                if (!string.IsNullOrEmpty(AccessKey) && !Utils.StringValidationHelper.IsBase64String(AccessKey))
                {
                    result.AddUserError("Access Key does not appear to be a Base 64 string and is likely incorrect.");
                }

                break;

            case ListenerTypes.MQTTClient:
                if (string.IsNullOrEmpty(HostName))
                {
                    result.AddUserError("Host Name is required for Connecting to an MQTT Server.");
                }
                if (!Anonymous)
                {
                    if (string.IsNullOrEmpty(UserName))
                    {
                        result.AddUserError("User Name is Required to connect to your MQTT Broker for non-anonymous connections.");
                    }
                    if (string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(SecurePasswordId))
                    {
                        result.AddUserError("Password is Required to connect to your MQTT Broker for non-anonymous connections.");
                    }
                }
                else
                {
                    UserName = null;
                    Password = null;
                }
                if (!ConnectToPort.HasValue)
                {
                    result.AddUserError("Please provide a port that your MQTT Client will connect, usually 1883 or 8883 (SSL).");
                }
                MqttSubscriptions.RemoveAll(sub => string.IsNullOrEmpty(sub.Topic));
                if (!MqttSubscriptions.Any())
                {
                    result.AddUserError("Please ensure you provide at least one subscription (including wildcards + and #) that will be monitored for incoming messages.");
                }

                break;

            case ListenerTypes.WebSocket:
                if (string.IsNullOrEmpty(HostName))
                {
                    result.AddUserError("Host Name is required for an Azure Service Bus Listener, this is the host name of your Event Hub without the sb:// protocol.");
                }
                if (HostName != null && HostName.ToLower().StartsWith("wss://"))
                {
                    HostName = HostName.Substring(5);
                }
                if (HostName != null && HostName.ToLower().StartsWith("ws://"))
                {
                    HostName = HostName.Substring(4);
                }
                if (!string.IsNullOrEmpty(Path) && !Path.StartsWith("/"))
                {
                    Path = "/" + Path;
                }

                if (!Anonymous)
                {
                    if (string.IsNullOrEmpty(UserName))
                    {
                        result.AddUserError("User Name is Required to be used to authenticate with your Web Socket Server.");
                    }
                    if (string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(SecurePasswordId))
                    {
                        result.AddUserError("Password is Required to be used to authenticate with your Web Socket Server..");
                    }
                }
                else
                {
                    UserName = null;
                    Password = null;
                }

                break;

            case ListenerTypes.MQTTListener:
                if (!Anonymous)
                {
                    if (string.IsNullOrEmpty(UserName))
                    {
                        result.AddUserError("User Name is Required to be used to authenticate MQTT clients authenticating with your broker.");
                    }
                    if (string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(SecurePasswordId))
                    {
                        result.AddUserError("Password is Required to be used to authenticate MQTT clients authenticating with your broker.");
                    }
                }
                else
                {
                    UserName = null;
                    Password = null;
                }
                if (!ListenOnPort.HasValue)
                {
                    result.AddUserError("Please provide a port that your MQTT listenr will listen for incoming messages, usually 1883.");
                }
                break;

            /*
             * case ListenerTypes.MQTTBroker:
             * break;
             * case ListenerTypes.RabbitMQ:
             * break;
             * case ListenerTypes.RabbitMQClient:
             * break;*/
            case ListenerTypes.RawTCP:
                if (!ListenOnPort.HasValue)
                {
                    result.AddUserError("Please provide a port that your TCP listenr will listen for incoming messages.");
                }
                break;

            case ListenerTypes.RawUDP:
                if (!ListenOnPort.HasValue)
                {
                    result.AddUserError("Please provide a port that your UDP listenr will listen for incoming messages.");
                }
                break;

            case ListenerTypes.Rest:
                if (!ListenOnPort.HasValue)
                {
                    result.AddUserError("Please provide a port that your REST listenr will listen for incoming messages, this is usually port 80 for HTTP and 443 for HTTPS.");
                }
                if (EntityHeader.IsNullOrEmpty(RestServerType))
                {
                    result.AddUserError("Allowable Connection Type is required for an REST Listener.");
                }
                if (!Anonymous)
                {
                    if (string.IsNullOrEmpty(UserName))
                    {
                        result.AddUserError("Anonymous connections are not enabled, you must provide a user name that will be used to connect to your REST endpoint.");
                    }
                    if (string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(SecurePasswordId))
                    {
                        result.AddUserError("Anonymous connections are not enabled, you must provide a password that will be used to connect to your REST endpoint.");
                    }
                }
                else
                {
                    UserName = null;
                    Password = null;
                }


                break;
            }
        }
Пример #4
0
        /// <summary>
        /// The Jones client constructor
        /// </summary>
        /// <param name="service">Service Name</param>
        /// <param name="hostname">Hostname</param>
        /// <param name="zkclient">Zookeeper Client connection object</param>
        /// <param name="callback">Callback function for any changes to the config tree</param>
        public SimpleJonesClient(string service, string hostname = null, IZookClient zkclient = null, Action <Dictionary <string, string> > callback = null)
        {
            // initialize the hostname
            if (String.IsNullOrEmpty(hostname))
            {
                // get FDQN
                // http://stackoverflow.com/questions/804700/how-to-find-fqdn-of-local-machine-in-c-net
                // http://support.microsoft.com/kb/303902
                HostName = System.Net.Dns.GetHostEntry("LocalHost").HostName.ToLower();

                int idx = HostName.IndexOf(".");
                if (idx > 0)
                {
                    HostName = HostName.Substring(0, idx);
                }
            }

            if (zkclient != null)
            {
                _zkclient = zkclient;
            }

            // initialize the dictionary to hold the configurations
            Config = new Dictionary <string, string>();

            // set the service name
            ServiceName = service;

            // this is our callback for Config View ZNode changes, to refresh our
            // this.Config
            ConfigViewNodeChangeWatcher = new WatcherWrapper(e =>
            {
                _zkclient.GetData(e.Path, ConfigViewNodeChangeWatcher).ContinueWith(d =>
                {
                    string conf_data = Encoding.UTF8.GetString(d.Result.Data);

                    Config = DeserializeNodeMap(conf_data);

                    if (callback != null)
                    {
                        callback.Invoke(Config);
                    }
                });
            });

            NodeMapPath = String.Format(@"/services/{0}/nodemaps", ServiceName);

            #region get the current Config from the view

            string current_conf_path = "";
            var    tnm = _zkclient.GetData(NodeMapPath, false).ContinueWith(c =>
            {
                string conf_path_json = Encoding.UTF8.GetString(c.Result.Data);

                try
                {
                    current_conf_path = DeserializeNodeMap(conf_path_json)[HostName];
                }
                catch (Exception)
                {
                    // fail-safe default configs when there's no machine-specific config is found
                    current_conf_path = String.Format(@"/services/{0}/conf", ServiceName);
                }

                var td = _zkclient.GetData(current_conf_path, ConfigViewNodeChangeWatcher).ContinueWith(d =>
                {
                    // this part is important so this.Config will not be empty on constructor completion!
                    string conf_data = Encoding.UTF8.GetString(d.Result.Data);

                    Config = DeserializeNodeMap(conf_data);
                });

                td.Wait();     // synchronous wait
            });

            tnm.Wait(); // synchronous wait

            #endregion
        }
Пример #5
0
        //�f�[�^�擾�i����f�[�^�́A�����������j
        public bool Recv(Logger logger, SockTcp tcpObj, int timeout, ILife iLife)
        {
            var buf = tcpObj.LineRecv(timeout, iLife);

            if (buf == null)
            {
                return(false);
            }
            buf = Inet.TrimCrlf(buf);

            _urlEncoding = MLang.GetEncoding(buf);//URL�G���R�[�h�̌`����ۑ�����

            //Ver5.9.8
            if (_urlEncoding == null)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < buf.Length; i++)
                {
                    sb.Append(String.Format("0x{0:X},", buf[i]));
                }
                logger.Set(LogKind.Error, tcpObj, 9999, String.Format("_urlEncoding==null buf.Length={0} buf={1}", buf.Length, sb.ToString()));
                //���̂܂ܗ�O�֓˓�������
            }

            var str = _urlEncoding.GetString(buf);

            // ���\�b�h�EURI�E�o�[�W�����ɕ���
            //"GET http://hostname:port@user:pass/path/filename.ext?param HTTP/1.1"
            RequestStr = str;

            //(�󔒂ŕ�������)�@"GET <=> http://hostname:port@user:pass/path/filename.ext?param HTTP/1.1"
            var index = str.IndexOf(' ');

            if (index < 0) //Ver5.0.0-a8
            {
                return(false);
            }

            //(�O��) "GET"
            var methodStr = str.Substring(0, index);

            foreach (HttpMethod m in Enum.GetValues(typeof(HttpMethod)))
            {
                if (methodStr.ToUpper() == m.ToString().ToUpper())
                {
                    HttpMethod = m;
                    break;
                }
            }
            if (HttpMethod == HttpMethod.Unknown)
            {
                logger.Set(LogKind.Secure, tcpObj, 1, string.Format("{0}", RequestStr));//�T�|�[�g�O�̃��\�b�h�ł��i������p���ł��܂���j
                return(false);
            }
            if (HttpMethod == HttpMethod.Connect)
            {
                Protocol = ProxyProtocol.Ssl;
                Port     = 443;//�f�t�H���g�̃|�[�g�ԍ���443�ɂȂ�
            }

            //(�㔼) "http://*****:*****@user:pass/path/filename.ext?param HTTP/1.1"
            str = str.Substring(index + 1);


            //(�󔒂ŕ�������)�@"http://*****:*****@user:pass/path/filename.ext?param <=> HTTP/1.1"
            index = str.IndexOf(' ');
            if (index < 0) //Ver5.0.0-a8
            {
                return(false);
            }
            //(�㔼) "HTTP/1.1"
            HttpVer = str.Substring(index + 1);

            if (HttpVer != "HTTP/0.9" && HttpVer != "HTTP/1.0" && HttpVer != "HTTP/1.1")
            {
                logger.Set(LogKind.Secure, tcpObj, 2, RequestStr);//�T�|�[�g�O�̃o�[�W�����ł��i������p���ł��܂���j
                return(false);
            }

            //(�O��) "http://*****:*****@user:pass/path/filename.ext?param"
            str = str.Substring(0, index);

            if (Protocol == ProxyProtocol.Unknown)  //�v���g�R���擾
            //("://"�������)�@"http <=> hostname:port@user:pass/path/filename.ext?param <=> HTTP/1.1"
            {
                index = str.IndexOf("://");
                if (index < 0) //Ver5.0.0-a8
                {
                    return(false);
                }
                //(�O��) "http"
                var protocolStr = str.Substring(0, index);

                if (protocolStr.ToLower() == "ftp")
                {
                    Protocol = ProxyProtocol.Ftp; //�v���g�R����FTP�ɏC��
                    Port     = 21;                //FTP�ڑ��̃f�t�H���g�̃|�[�g�ԍ���21�ɂȂ�
                }
                else if (protocolStr.ToLower() != "http")
                {
                    //Ver5.6.7
                    //Msg.Show(MsgKind.Error,"�݌v�G���[�@Request.Recv()");
                    //�G���[�\����|�b�v�A�b�v���烍�O�ɕύX
                    logger.Set(LogKind.Error, tcpObj, 29, string.Format("protocolStr={0}", protocolStr));
                    return(false);
                }
                else
                {
                    Protocol = ProxyProtocol.Http;
                }
                //(�㔼) "hostname:port@user:pass/path/filename.ext?param"
                str = str.Substring(index + 3);
            }
            //(�ŏ���"/"�ŕ�������)�@"hostname:port@user:pass <=> /path/filename.ext?param"
            index    = str.IndexOf('/');
            HostName = str;
            if (0 <= index)
            {
                //(�O��) ""hostname:port@user:pass"
                HostName = str.Substring(0, index);

                //(�㔼) "/path/filename.ext?param"
                str = str.Substring(index);
            }
            else
            {
                // GET http://hostname HTTP/1.0 �̂悤�ɁA���[�g�f�B���N�g�����w�肳��Ă��Ȃ��ꍇ�̑Ώ�
                str = "/";
            }

            //�z�X�g�������Ƀ��[�U���F�p�X���[�h�������Ă���ꍇ�̏���
            index = HostName.IndexOf("@");
            if (0 <= index)
            {
                var userpass = HostName.Substring(0, index);

                //���[�U���F�p�X���[�h��j������
                HostName = HostName.Substring(index + 1);

                var i = userpass.IndexOf(':');
                if (i == -1)
                {
                    User = userpass;
                }
                else
                {
                    User = userpass.Substring(0, i);
                    Pass = userpass.Substring(i + 1);
                }
            }
            //Ver5.1.2 IPv6�A�h���X�\�L�̃z�X�g���ɑΉ�
            var tmp = HostName.Split(new[] { '[', ']' });

            if (tmp.Length == 3) //IPv6�A�h���X�\�L�ł���Ɣ��f����
            {
                HostName = string.Format("[{0}]", tmp[1]);
                index    = tmp[2].IndexOf(":");
                if (0 <= index)
                {
                    var s = tmp[2].Substring(index + 1);
                    Port = Convert.ToInt32(s);
                }
            }
            else
            {
                //�z�X�g�������Ƀ|�[�g�ԍ��������Ă���ꍇ�̏���
                index = HostName.IndexOf(":");
                if (0 <= index)
                {
                    var s = HostName.Substring(index + 1);
                    Port     = Convert.ToInt32(s);
                    HostName = HostName.Substring(0, index);
                }
            }

            Uri = str;

            //CGI����
            if (-1 != Uri.LastIndexOf('?'))
            {
                Cgi = true;
            }

            //�g���q�擾
            if (!Cgi)
            {
                index = Uri.LastIndexOf('/');
                if (index != -1)
                {
                    str = Uri.Substring(index + 1);
                }
                index = str.LastIndexOf('.');
                if (index != -1)
                {
                    Ext = str.Substring(index + 1);
                }
            }
            return(true);
        }