示例#1
0
        /// <summary>
        /// 收到消息方法
        /// </summary>
        /// <param name="endPoint">远程网络终结点</param>
        /// <param name="Context">消息内容</param>
        /// <param name="Length">长度</param>
        private void MmapMessager(EndPoint endPoint, byte[] Context, int Length, Mmaper mmaper)
        {//MmapInfo 180.1.1.5:6666->192.168.1.10:25565 XXXXXXXXXXXX(Base64)
            // 要发送的数据包字符串 指令       客户端IP:端口                监听IP                        :          端口                                转为Base64的数据
            string Messages = "MmapInfo " + endPoint.ToString() + "->" + mmaper.LocalhostIP.ToString() + ":" + mmaper.LocalhostPort.Port + " " + Convert.ToBase64String(Context);

            SendMessage(new IPEndPoint(mmaper.LocalhostIP, mmaper.LocalhostPort.Port), Messages);
        }
示例#2
0
        /// <summary>
        /// 客户端断开连接事件
        /// </summary>
        /// <param name="endPoint">远程网络终结点</param>
        /// <param name="mmaper">映射规则</param>
        private void MmapTcpError(EndPoint endPoint, Mmaper mmaper)
        {//TcpError 180.1.1.5:6666->192.168.1.10:25565
            // 要发送的数据包字符串 指令       客户端IP:端口                监听IP                        :          端口
            string Messages = "TcpError " + endPoint.ToString() + "->" + mmaper.LocalhostIP.ToString() + ":" + mmaper.LocalhostPort.Port;

            SendMessage(new IPEndPoint(mmaper.LocalhostIP, mmaper.LocalhostPort.Port), Messages);
        }
示例#3
0
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="mmaper">映射规则</param>
 /// <param name="flow">剩余流量</param>
 public Mmaps(Mmaper mmaper, Flow flow)
 {
     IP         = mmaper.LocalhostIP;
     Port       = mmaper.LocalhostPort;
     speed      = mmaper.MmapSpend;
     Flow       = flow;
     ThisMmaper = mmaper;
 }
示例#4
0
        /// <summary>
        /// 映射流量耗尽事件
        /// </summary>
        /// <param name="mmaper">映射规则</param>
        private void MmapFlowEnd(Mmaper mmaper)
        {//MmapFlowEnd 192.168.1.10:25565
            string Messages = "MmapFlowEnd " + mmaper.LocalhostIP.ToString() + ":" + mmaper.LocalhostPort.Port;

            SendMessage(new IPEndPoint(mmaper.LocalhostIP, mmaper.LocalhostPort.Port), Messages);
        }
示例#5
0
 /// <summary>
 /// 消息处理方法
 /// </summary>
 /// <param name="endPoint">远程网络终结点</param>
 /// <param name="Context">内容</param>
 private void MessagePack(EndPoint endPoint, string[] Context)
 {
     for (int ic = 0; ic < Context.Length; ic = ic + 1)                                     // 遍历每一个包
     {
         string[] Message      = Context[ic].Split(' ');                                    // 以空格分割为数组
         Connet   ThisConneter = null;
         if (None.ContainsKey(endPoint))
         {
             ThisConneter = None[endPoint];
         }
         if (Login.ContainsKey(endPoint))
         {
             ThisConneter = Login[endPoint];
         }
         if (Mmap.ContainsKey(endPoint))
         {
             ThisConneter = Mmap[endPoint];
         }
         if (Message[0] == "Login")                                                         // 鉴别为登录命令
         {
             string User = Message[1];
             string Pwd  = Message[2];
             if (SelectPassword(User) == Pwd && Pwd != "" && Pwd != null)            // 验证用户名密码
             {
                 SendMessage(endPoint, "Login OK 200");                              // 发送登录成功代码
                 Login.Add(endPoint, None[endPoint]);                                // 在字典添加
                 Login[endPoint].ConnetType = ConnetType.Login;                      // 设为登录状态
                 None.Remove(endPoint);                                              // 从无状态字典内移除
             }
             else
             {
                 SendMessage(endPoint, "Login Error 400");                                  // 发送登录失败信息
                 ServerTcp.Close(endPoint);                                                 // 关闭连接
             }
         }
         if (Message[0] == "Mmap") // 鉴别为请求映射命令
         {                         // TCP,192.168.1.10,25565
             if (ThisConneter.ConnetType != ConnetType.None)
             {
                 string[] Mmap = Message[1].Split(',');                                     // 分割数据
                 for (int i = 0; i < Mmap.Length; i = i + 1)
                 {
                     if (SelectMmap(ThisConneter.User, Mmap[1], Mmap[2]))            // 遍历数据,检查权限
                     {
                         // 创建新映射对象并且实例化     用户名               IP地址                      端口                            查询映射速度        用户名        ip      端口
                         Mmaper NewMmap = new Mmaper(ThisConneter.User, IPAddress.Parse(Mmap[1]), new Ports(int.Parse(Mmap[2])), SelectMmapSpeed(ThisConneter.User, Mmap[1], Mmap[2]));
                         MmapDict.Add(NewMmap);                                                                 // 添加到映射规则集合
                                                                                                                // 创建映射对象并且实例化,映射规则    查询剩余流量    用户名           ip       端口
                         Mmaps mmaps = new Mmaps(NewMmap, SelectFlow(ThisConneter.User, Mmap[1], Mmap[2]));
                         MmapsDict.Add(new IPEndPoint(NewMmap.LocalhostIP, NewMmap.LocalhostPort.Port), mmaps); // 添加到映射规则字典
                         mmaps.Messages += MmapMessager;                                                        // 绑定接收消息事件
                         mmaps.Conneter += MmapConneter;                                                        // 绑定收到连接事件
                         mmaps.FlowEnd  += MmapFlowEnd;                                                         // 绑定流量耗尽事件
                         mmaps.TcpError += MmapTcpError;                                                        // 绑定Tcp连接终止事件
                     }
                     else
                     {
                         SendMessage(endPoint, "Mmap Error " + Mmap[1] + ":" + Mmap[2] + "无映射权限");// 返回无权限信息
                     }
                 }
             }
             else
             {
                 SendMessage(endPoint, "Mmap Error 未登录");                                // 返回无权限信息
             }
         }
         if (Message[0] == "Select")                                                        // 鉴别为查询映射权限命令
         {
             if (ThisConneter.ConnetType != ConnetType.None)
             {
                 string Return = "MmapSelectReturn " + SelectMmapAll(ThisConneter.User); // 获取用户名下的所有映射规则
                 SendMessage(endPoint, Return);                                          // 发送给客户端
             }
             else
             {
                 SendMessage(endPoint, "Select Error 未登录");                              // 返回无权限信息
             }
         }
         if (Message[0] == "MmapInfo") // 鉴别为映射数据传输
         {                             //MmapInfo 180.1.1.5:6666->192.168.1.10:25565 XXXXXXXXXXXX(Base64)
             if (ThisConneter.ConnetType != ConnetType.None)
             {
                 IPAddress  ClientiPAddress  = IPAddress.Parse(Message[1].Split("->")[0].Split(":")[0]);      // 获取客户端IP
                 Ports      ClientPort       = new Ports(int.Parse(Message[1].Split("->")[0].Split(":")[1])); // 获取客户端端口
                 IPAddress  ServeriPAddress  = IPAddress.Parse(Message[1].Split("->")[1].Split(":")[0]);      // 获取服务器IP
                 Ports      ServerPort       = new Ports(int.Parse(Message[1].Split("->")[1].Split(":")[1])); // 获取服务器端口
                 IPEndPoint ClientiPEndPoint = new IPEndPoint(ClientiPAddress, ClientPort.Port);              // 创建客户端网络终结点
                 IPEndPoint ServeriPEndPoint = new IPEndPoint(ServeriPAddress, ServerPort.Port);              // 创建服务器网络终结点
                 if (MmapsDict.ContainsKey(ServeriPEndPoint))
                 {
                     MmapsDict[ServeriPEndPoint].Send(ClientiPEndPoint, Convert.FromBase64String(Message[2]));// 发送数据
                 }
                 else
                 {
                     SendMessage(endPoint, "MmapInfo Error 未找到映射");                   // 返回未找到映射
                 }
             }
             else
             {
                 SendMessage(endPoint, "Select Error 未登录");                              // 返回无权限信息
             }
         }
         if (Message[0] == "TcpClose") // 鉴别为关闭连接
         {                             //TcpClose 180.1.1.5:6666->192.168.1.10:25565
             if (ThisConneter.ConnetType != ConnetType.None)
             {
                 IPAddress  ClientiPAddress  = IPAddress.Parse(Message[1].Split("->")[0].Split(":")[0]);      // 获取客户端IP
                 Ports      ClientPort       = new Ports(int.Parse(Message[1].Split("->")[0].Split(":")[1])); // 获取客户端端口
                 IPAddress  ServeriPAddress  = IPAddress.Parse(Message[1].Split("->")[1].Split(":")[0]);      // 获取服务器IP
                 Ports      ServerPort       = new Ports(int.Parse(Message[1].Split("->")[1].Split(":")[1])); // 获取服务器端口
                 IPEndPoint ClientiPEndPoint = new IPEndPoint(ClientiPAddress, ClientPort.Port);              // 创建客户端网络终结点
                 IPEndPoint ServeriPEndPoint = new IPEndPoint(ServeriPAddress, ServerPort.Port);              // 创建服务器网络终结点
                 if (MmapsDict.ContainsKey(ServeriPEndPoint))
                 {
                     MmapsDict[ServeriPEndPoint].Close(ClientiPEndPoint);                   // 关闭连接
                 }
                 else
                 {
                     SendMessage(endPoint, "TcpClose Error 未找到映射");                    // 返回未找到映射
                 }
             }
             else
             {
                 SendMessage(endPoint, "Select Error 未登录");                              // 返回无权限信息
             }
         }
         if (Message[0] == "MmapStop") // 鉴别为停止映射
         {                             //MmapStop 192.168.1.10:25565
             if (ThisConneter.ConnetType != ConnetType.None)
             {
                 IPAddress  ServeriPAddress  = IPAddress.Parse(Message[1].Split("->")[1].Split(":")[0]);      // 获取服务器IP
                 Ports      ServerPort       = new Ports(int.Parse(Message[1].Split("->")[1].Split(":")[1])); // 获取服务器端口
                 IPEndPoint ServeriPEndPoint = new IPEndPoint(ServeriPAddress, ServerPort.Port);              // 创建服务器网络终结点
                 if (MmapsDict.ContainsKey(ServeriPEndPoint))
                 {
                     Flow   flow = MmapsDict[ServeriPEndPoint].Stop();                      // 映射并且获取剩余流量
                     double MB   = flow.MB;                                                 // 获取MB数值
                     if (flow.IsInfinity)
                     {
                         WriteSpend(ThisConneter.User, ServeriPAddress.ToString(), ServerPort.Port.ToString(), "∞");
                     }
                     else
                     {
                         WriteSpend(ThisConneter.User, ServeriPAddress.ToString(), ServerPort.Port.ToString(), MB.ToString());
                     }
                 }
                 else
                 {
                     SendMessage(endPoint, "TcpClose Error 未找到映射");                    // 返回未找到映射
                 }
             }
             else
             {
                 SendMessage(endPoint, "Select Error 未登录");                              // 返回无权限信息
             }
         }
     }
 }