0
点赞
收藏
分享

微信扫一扫

MySQL-分库分表详解(二)

念川LNSC 2023-07-14 阅读 55

目的

目的是为了在网关上转发udp数据和tcp数据。对于网络里面隔离的内网来说,有一台可以上网的服务器,那么通过两块网卡就可以转发出去,在服务器上进行数据的转发,有tcp和udp两种,udp已经写过了,这次使用了c# 来制作一个windows程序来转发tcp 数据。实际上,使用的是windows的netsh命令。
程序简单,好用,非常稳定。

udp 转发桥

udp转发服务桥

tcp转发桥

在桥上制定规则,注意,这里是使用的windows自带的服务做的,投机取巧,比较简单。界面如下:
可以自由增加规则和删除规则
在这里插入图片描述

基本数据结构

对于网络,方向是有进和出两个方向,所以定义了Direction

    public class ProxyRule
    {
        public string Direction { get; set; }
        public string Listenaddress { get; set; }
        public string Listenport { get; set; }
        public string Connectaddress { get; set; }
        public string Connectport { get; set; }
        public string Protocol { get; set; }

        public override bool Equals(Object obj)
        {
            ProxyRule that = obj as ProxyRule;
            if (that == null)
                return false;
            return Direction == that.Direction && Listenaddress == that.Listenaddress && Listenport == that.Listenport;
        }
        public override int GetHashCode()
        {
            return (Direction+Listenaddress+Listenport).GetHashCode();
        }
        public override string ToString()
        {
            return Direction + " listenaddress=" + Listenaddress + " listenport=" + Listenport + " connectaddress=" + Connectaddress + " connectport=" + Connectport + " protocol=" + Protocol;
        }
        public string ToShortString()
        {
            return Direction + " listenaddress=" + Listenaddress + " listenport=" + Listenport;
        }
        public string[] ToSubitems()
        {
            return new string[] { Direction, Listenaddress, Listenport,Connectaddress, Connectport, Protocol };
        }
        public static ProxyRule FromSubitems(string[] items)
        {
            ProxyRule rule = new ProxyRule();
            rule.Direction = items[0];
            rule.Listenaddress = items[1];
            rule.Listenport = items[2];
            rule.Connectaddress = items[3];
            rule.Connectport = items[4];
            rule.Protocol = items[5];
            return rule;
        }
    }

返回结果

    class ExecResult
    {
        public ExecResult()
        {
        }
        public ExecResult(int code,string output,string error):this()
        {
            this.code = code;
            this.output = output;
            this.error = error;
        }
        public int code { get; set; }
        public string output { get; set; }
        public string error { get; set; }
    }

原理

实际上,这里就是使用了windows 的netsh命令来进行网络数据的转发,
netsh interface portproxy add 是用来增加规则
netsh interface portproxy delete 是用来删除规则
netsh interface portproxy set 设定
netsh interface portproxy reset 重置

获取所有

这里举一个获取所有规则得例子

public List<ProxyRule> GetRules()
{
     List<ProxyRule> rules = new List<ProxyRule>();
     foreach (string direction in directions)
     {
                ExecResult result = ExecCommand("netsh", "interface portproxy show " + direction);
                Match m = regex.Match(result.output);
                while (m.Success)
                {
                    ProxyRule rule = new ProxyRule();
                    rule.Direction = direction;
                    rule.Listenaddress = m.Groups[1].Captures[0].Value;
                    rule.Listenport = m.Groups[2].Captures[0].Value;
                    rule.Connectaddress = m.Groups[3].Captures[0].Value;
                    rule.Connectport = m.Groups[4].Captures[0].Value;
                    rule.Protocol = "tcp";
                    rules.Add(rule);
                    Console.WriteLine(rule.ToString());
                    m = m.NextMatch();
                }
      }
      return rules;
}

下载

举报

相关推荐

0 条评论