Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using System.Net.Sockets;
  6. using Server.Network;
  7. namespace Server.Misc
  8. {
  9. public class ServerList
  10. {
  11. /*
  12. * The default setting for Address, a value of 'null', will use your local IP address. If all of your local IP addresses
  13. * are private network addresses and AutoDetect is 'true' then ServUO will attempt to discover your public IP address
  14. * for you automatically.
  15. *
  16. * If you do not plan on allowing clients outside of your LAN to connect, you can set AutoDetect to 'false' and leave
  17. * Address set to 'null'.
  18. *
  19. * If your public IP address cannot be determined, you must change the value of Address to your public IP address
  20. * manually to allow clients outside of your LAN to connect to your server. Address can be either an IP address or
  21. * a hostname that will be resolved when ServUO starts.
  22. *
  23. * If you want players outside your LAN to be able to connect to your server and you are behind a router, you must also
  24. * forward TCP port 2593 to your private IP address. The procedure for doing this varies by manufacturer but generally
  25. * involves configuration of the router through your web browser.
  26. *
  27. * ServerList will direct connecting clients depending on both the address they are connecting from and the address and
  28. * port they are connecting to. If it is determined that both ends of a connection are private IP addresses, ServerList
  29. * will direct the client to the local private IP address. If a client is connecting to a local public IP address, they
  30. * will be directed to whichever address and port they initially connected to. This allows multihomed servers to function
  31. * properly and fully supports listening on multiple ports. If a client with a public IP address is connecting to a
  32. * locally private address, the server will direct the client to either the AutoDetected IP address or the manually entered
  33. * IP address or hostname, whichever is applicable. Loopback clients will be directed to loopback.
  34. *
  35. * If you would like to listen on additional ports (i.e. 22, 23, 80, for clients behind highly restrictive egress
  36. * firewalls) or specific IP adddresses you can do so by modifying the file SocketOptions.cs found in this directory.
  37. */
  38. public static readonly string Address = null;
  39. public static readonly string ServerName = "My Shard";
  40. public static readonly bool AutoDetect = true;
  41. private static IPAddress m_PublicAddress;
  42. public static void Initialize()
  43. {
  44. if (Address == null)
  45. {
  46. if (AutoDetect)
  47. AutoDetection();
  48. }
  49. else
  50. {
  51. Resolve(Address, out m_PublicAddress);
  52. }
  53. EventSink.ServerList += new ServerListEventHandler(EventSink_ServerList);
  54. }
  55. private static void EventSink_ServerList(ServerListEventArgs e)
  56. {
  57. try
  58. {
  59. NetState ns = e.State;
  60. Socket s = ns.Socket;
  61. IPEndPoint ipep = (IPEndPoint)s.LocalEndPoint;
  62. IPAddress localAddress = ipep.Address;
  63. int localPort = ipep.Port;
  64. if (IsPrivateNetwork(localAddress))
  65. {
  66. ipep = (IPEndPoint)s.RemoteEndPoint;
  67. if (!IsPrivateNetwork(ipep.Address) && m_PublicAddress != null)
  68. localAddress = m_PublicAddress;
  69. }
  70. e.AddServer(ServerName, new IPEndPoint(localAddress, localPort));
  71. }
  72. catch
  73. {
  74. e.Rejected = true;
  75. }
  76. }
  77. private static void AutoDetection()
  78. {
  79. if (!HasPublicIPAddress())
  80. {
  81. Console.Write("ServerList: Auto-detecting public IP address...");
  82. m_PublicAddress = FindPublicAddress();
  83. if (m_PublicAddress != null)
  84. Console.WriteLine("done ({0})", m_PublicAddress.ToString());
  85. else
  86. Console.WriteLine("failed");
  87. }
  88. }
  89. private static void Resolve(string addr, out IPAddress outValue)
  90. {
  91. if (IPAddress.TryParse(addr, out outValue))
  92. return;
  93. try
  94. {
  95. IPHostEntry iphe = Dns.GetHostEntry(addr);
  96. if (iphe.AddressList.Length > 0)
  97. outValue = iphe.AddressList[iphe.AddressList.Length - 1];
  98. }
  99. catch
  100. {
  101. }
  102. }
  103. private static bool HasPublicIPAddress()
  104. {
  105. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  106. foreach (NetworkInterface adapter in adapters)
  107. {
  108. IPInterfaceProperties properties = adapter.GetIPProperties();
  109. foreach (IPAddressInformation unicast in properties.UnicastAddresses)
  110. {
  111. IPAddress ip = unicast.Address;
  112. if (!IPAddress.IsLoopback(ip) && ip.AddressFamily != AddressFamily.InterNetworkV6 && !IsPrivateNetwork(ip))
  113. return true;
  114. }
  115. }
  116. return false;
  117. /*
  118. IPHostEntry iphe = Dns.GetHostEntry( Dns.GetHostName() );
  119. IPAddress[] ips = iphe.AddressList;
  120. for ( int i = 0; i < ips.Length; ++i )
  121. {
  122. if ( ips[i].AddressFamily != AddressFamily.InterNetworkV6 && !IsPrivateNetwork( ips[i] ) )
  123. return true;
  124. }
  125. return false;
  126. */
  127. }
  128. private static bool IsPrivateNetwork(IPAddress ip)
  129. {
  130. // 10.0.0.0/8
  131. // 172.16.0.0/12
  132. // 192.168.0.0/16
  133. if (ip.AddressFamily == AddressFamily.InterNetworkV6)
  134. return false;
  135. if (Utility.IPMatch("192.168.*", ip))
  136. return true;
  137. else if (Utility.IPMatch("10.*", ip))
  138. return true;
  139. else if (Utility.IPMatch("172.16-31.*", ip))
  140. return true;
  141. else
  142. return false;
  143. }
  144. private static IPAddress FindPublicAddress()
  145. {
  146. try
  147. {
  148. String ip = "";
  149. WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
  150. request.Timeout = 15000;
  151. using (WebResponse response = request.GetResponse())
  152. {
  153. using (StreamReader stream = new StreamReader(response.GetResponseStream()))
  154. {
  155. ip = stream.ReadToEnd();
  156. }
  157. }
  158. int first = ip.IndexOf("Address: ") + 9;
  159. int last = ip.LastIndexOf("</body>");
  160. ip = ip.Substring(first, last - first);
  161. return IPAddress.Parse(ip);
  162. }
  163. catch
  164. {
  165. return null;
  166. }
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement