Esempio n. 1
0
 HandleResult Service_OnReceive(IntPtr connId, byte[] bytes)
 {
     if (isAccess)
     {
         var extraData = this.GetExtra <ExtraData>(connId);
         extraData.Data += Encoding.Default.GetString(bytes);
         if (extraData.Data.Length > 128)
         {
             this.Disconnect(connId);
         }
         else if (extraData.Data.IndexOf('\n') > 0)
         {
             if (extraData.Data.Trim() == extraData.Key)
             {
                 var data = Encoding.Default.GetBytes("已于"
                                                      + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                                                      + "成功授权!\r\n");
                 this.Send(connId, data, data.Length);
                 INIOperation.WriteString("IPList", extraData.IP, "true");
                 extraData.Access = true;
             }
             else
             {
                 this.Disconnect(connId);
                 this.Log(string.Format("[{0}]验证失败:IP={1}", this.Name, extraData.IP));
             }
         }
     }
     else
     {
         var aId = this.GetExtra <IntPtr>(connId);
         this.agent.Send(aId, bytes, bytes.Length);
     }
     return(HandleResult.Ignore);
 }
Esempio n. 2
0
 HandleResult Service_OnClose(TcpServer sender, IntPtr connId, SocketOperation enOperation, int errorCode)
 {
     if (isAccess)
     {
         string ip   = "";
         ushort port = 0;
         this.GetRemoteAddress(connId, ref ip, ref port);
         var result = false;
         foreach (var cid in this.GetAllConnectionIDs())
         {
             if (cid == connId)
             {
                 continue;
             }
             var data = this.GetExtra <ExtraData>(cid);
             if (data.IP == ip && data.Access)
             {
                 result = true;
                 break;
             }
         }
         if (!result)
         {
             INIOperation.WriteString("IPList", ip, "false");
         }
     }
     else
     {
         this.agent.Disconnect(this.GetExtra <IntPtr>(connId));
     }
     this.RemoveExtra(connId);
     return(HandleResult.Ignore);
 }
Esempio n. 3
0
        private HandleResult Service_OnAccept(TcpServer sender, IntPtr connId, IntPtr pClient)
        {
            strPwd = INIOperation.ReadString("Info", "Password");
            var data = Encoding.Default.GetBytes("欢迎连接\r\n请输入密码:");

            this.Send(connId, data, data.Length);
            this.SetExtra(connId, "");
            return(HandleResult.Ok);
        }
Esempio n. 4
0
 HandleResult Service_OnPrepareListen(TcpServer sender, IntPtr soListen)
 {
     isAccess = INIOperation.ReadString("Info", "ServiceName") == this.Name;
     if (isAccess)
     {
         strKey = INIOperation.ReadString("Info", "AccessKey");
         INIOperation.WriteString("IPList", null, null);
     }
     else
     {
         forwardIp   = ConfigurationManager.AppSettings[this.Name + "_ForwardIP"];
         forwardPort = ushort.Parse(ConfigurationManager.AppSettings[this.Name + "_ForwardPort"]);
     }
     return(HandleResult.Ignore);
 }
Esempio n. 5
0
        private HandleResult Service_OnReceive(IntPtr connId, byte[] bytes)
        {
            byte[] data    = null;
            var    strData = this.GetExtra <string>(connId);

            strData += Encoding.Default.GetString(bytes);
            if (strData.Length > 24)
            {
                data = Encoding.Default.GetBytes("密码长度错误!");
                this.Send(connId, data, data.Length);
                Thread.Sleep(100);
                this.Disconnect(connId);
            }
            else if (strData.IndexOf('\n') > 0)
            {
                if (strData.Trim() != strPwd)
                {
                    data = Encoding.Default.GetBytes("密码错误!");
                    this.Send(connId, data, data.Length);
                    Thread.Sleep(100);
                    this.Disconnect(connId);
                    return(HandleResult.Ok);
                }
                foreach (var cId in this.GetAllConnectionIDs())
                {
                    if (cId == connId)
                    {
                        continue;
                    }
                    this.Disconnect(cId);
                }
                string ip   = "";
                ushort port = 0;
                this.GetRemoteAddress(connId, ref ip, ref port);
                INIOperation.WriteString("Info", "IP", ip);
                data = Encoding.Default.GetBytes("设置成功!");
                this.Send(connId, data, data.Length);
                Thread.Sleep(100);
                this.Disconnect(connId);
            }
            else
            {
                this.SetExtra(connId, strData);
            }
            return(HandleResult.Ok);
        }
Esempio n. 6
0
        HandleResult Service_OnAccept(TcpServer sender, IntPtr connId, IntPtr pClient)
        {
            string ip   = "";
            ushort port = 0;

            this.GetRemoteAddress(connId, ref ip, ref port);
            if (isAccess)
            {
                var key     = Guid.NewGuid().ToString();
                var keyData = Encoding.Default.GetBytes(Encrypt.AESEncrypt(key, strKey) + "\r\n");
                this.Send(connId, keyData, keyData.Length);
                this.SetExtra(connId, new ExtraData()
                {
                    Key = key, IP = ip
                });
                return(HandleResult.Ignore);
            }
            if (!bool.Parse(INIOperation.ReadString("IPList", ip, "false")))
            {
                this.Disconnect(connId);
                this.Log(string.Format("[{0}]连接失败:IP={1}", this.Name, ip));
                return(HandleResult.Ignore);
            }
            var aId = IntPtr.Zero;

            if (agent.Connect(forwardIp, forwardPort, ref aId))
            {
                this.SetExtra(connId, aId);
                agent.SetExtra(aId, connId);
            }
            else
            {
                this.Disconnect(connId);
            }
            return(HandleResult.Ignore);
        }