连接过滤器

关键字: 屏蔽IP, 禁止IP, 屏蔽连接, 连接过滤,黑名单,白名单

SuperSocket 中的连接过滤器是一个用于过滤客户端连接的接口。 通过连接过滤器,你可以允许或者禁止从某些地址过来的连接。

连接过滤器的接口定义如下:

  1. /// <summary>
  2. /// The basic interface of connection filter
  3. /// </summary>
  4. public interface IConnectionFilter
  5. {
  6. /// <summary>
  7. /// Initializes the connection filter
  8. /// </summary>
  9. /// <param name="name">The name.</param>
  10. /// <param name="appServer">The app server.</param>
  11. /// <returns></returns>
  12. bool Initialize(string name, IAppServer appServer);
  13. /// <summary>
  14. /// Gets the name of the filter.
  15. /// </summary>
  16. string Name { get; }
  17. /// <summary>
  18. /// Whether allows the connect according the remote endpoint
  19. /// </summary>
  20. /// <param name="remoteAddress">The remote address.</param>
  21. /// <returns></returns>
  22. bool AllowConnect(IPEndPoint remoteAddress);
  23. }

bool Initialize(string name, IAppServer appServer);

此方法用于初始化连接过滤器

string Name {get;}

返回连接过滤器名称

bool AllowConnect (IPEndPoint remoteAddress);

这个方法通过判断客户端的地址来决定是否允许该连接。

下面的代码实现了只允许来自指定IP地址范围的连接的功能:

  1. public class IPConnectionFilter : IConnectionFilter
  2. {
  3. private Tuple<long, long>[] m_IpRanges;
  4. public bool Initialize(string name, IAppServer appServer)
  5. {
  6. Name = name;
  7. var ipRange = appServer.Config.Options.GetValue("ipRange");
  8. string[] ipRangeArray;
  9. if (string.IsNullOrEmpty(ipRange)
  10. || (ipRangeArray = ipRange.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)).Length <= 0)
  11. {
  12. throw new ArgumentException("The ipRange doesn't exist in configuration!");
  13. }
  14. m_IpRanges = new Tuple<long, long>[ipRangeArray.Length];
  15. for (int i = 0; i < ipRangeArray.Length; i++)
  16. {
  17. var range = ipRangeArray[i];
  18. m_IpRanges[i] = GenerateIpRange(range);
  19. }
  20. return true;
  21. }
  22. private Tuple<long, long> GenerateIpRange(string range)
  23. {
  24. var ipArray = range.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
  25. if(ipArray.Length != 2)
  26. throw new ArgumentException("Invalid ipRange exist in configuration!");
  27. return new Tuple<long, long>(ConvertIpToLong(ipArray[0]), ConvertIpToLong(ipArray[1]));
  28. }
  29. private long ConvertIpToLong(string ip)
  30. {
  31. var points = ip.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
  32. if(points.Length != 4)
  33. throw new ArgumentException("Invalid ipRange exist in configuration!");
  34. long value = 0;
  35. long unit = 1;
  36. for (int i = points.Length - 1; i >= 0; i--)
  37. {
  38. value += unit * points[i].ToInt32();
  39. unit *= 256;
  40. }
  41. return value;
  42. }
  43. public string Name { get; private set; }
  44. public bool AllowConnect(IPEndPoint remoteAddress)
  45. {
  46. var ip = remoteAddress.Address.ToString();
  47. var ipValue = ConvertIpToLong(ip);
  48. for (var i = 0; i < m_IpRanges.Length; i++)
  49. {
  50. var range = m_IpRanges[i];
  51. if (ipValue > range.Item2)
  52. return false;
  53. if (ipValue < range.Item1)
  54. return false;
  55. }
  56. return true;
  57. }
  58. }

然后你需要更新配置文件来启用这个连接过滤器:

1) 添加配置节点 "connectionFilters":

  1. <connectionFilters>
  2. <add name="IpRangeFilter"
  3. type="SuperSocket.QuickStart.ConnectionFilter.IPConnectionFilter, SuperSocket.QuickStart.ConnectionFilter" />
  4. </connectionFilters>

2) 为server实例添加connectionFilter属性:

  1. <server name="EchoServer"
  2. serverTypeName="EchoService" ip="Any" port="2012"
  3. connectionFilter="IpRangeFilter"
  4. ipRange="127.0.1.0-127.0.1.255">
  5. </server>

3) 完整的配置文件如下:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <section name="superSocket" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/>
  5. </configSections>
  6. <appSettings>
  7. <add key="ServiceName" value="EchoService"/>
  8. </appSettings>
  9. <superSocket>
  10. <servers>
  11. <server name="EchoServer"
  12. serverTypeName="EchoService"
  13. ip="Any" port="2012"
  14. connectionFilter="IpRangeFilter"
  15. ipRange="127.0.1.0-127.0.1.255">
  16. </server>
  17. </servers>
  18. <serverTypes>
  19. <add name="EchoService"
  20. type="SuperSocket.QuickStart.EchoService.EchoServer, SuperSocket.QuickStart.EchoService" />
  21. </serverTypes>
  22. <connectionFilters>
  23. <add name="IpRangeFilter"
  24. type="SuperSocket.QuickStart.ConnectionFilter.IPConnectionFilter, SuperSocket.QuickStart.ConnectionFilter" />
  25. </connectionFilters>
  26. </superSocket>
  27. <startup>
  28. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  29. </startup>
  30. </configuration>