mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-24 05:03:34 -04:00
fix upnp not picking first local ipv4 address
This commit is contained in:
parent
40fde5c892
commit
e2c33d2d8d
@ -78,12 +78,41 @@ namespace MCGalaxy.Core {
|
||||
} while (length > 0);
|
||||
} while (start.Subtract(DateTime.UtcNow) < Timeout);
|
||||
return false;
|
||||
}
|
||||
catch {
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ForwardPort(int port, ProtocolType protocol, string description) {
|
||||
if (String.IsNullOrEmpty(_serviceUrl) )
|
||||
throw new InvalidOperationException("No UPnP service available or Discover() has not been called");
|
||||
|
||||
XmlDocument xdoc = SOAPRequest(_serviceUrl,
|
||||
"<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
|
||||
"<NewRemoteHost></NewRemoteHost>" +
|
||||
"<NewExternalPort>" + port + "</NewExternalPort>" +
|
||||
"<NewProtocol>" + protocol.ToString().ToUpper() + "</NewProtocol>" +
|
||||
"<NewInternalPort>" + port + "</NewInternalPort>" +
|
||||
"<NewInternalClient>" + GetLocalIP() + "</NewInternalClient>" +
|
||||
"<NewEnabled>1</NewEnabled>" +
|
||||
"<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
|
||||
"<NewLeaseDuration>0</NewLeaseDuration>" +
|
||||
"</u:AddPortMapping>", "AddPortMapping");
|
||||
}
|
||||
|
||||
public static void DeleteForwardingRule(int port, ProtocolType protocol) {
|
||||
if (String.IsNullOrEmpty(_serviceUrl) )
|
||||
throw new InvalidOperationException("No UPnP service available or Discover() has not been called");
|
||||
|
||||
XmlDocument xdoc = SOAPRequest(_serviceUrl,
|
||||
"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
|
||||
"<NewRemoteHost></NewRemoteHost>" +
|
||||
"<NewExternalPort>" + port + "</NewExternalPort>" +
|
||||
"<NewProtocol>" + protocol.ToString().ToUpper() + "</NewProtocol>" +
|
||||
"</u:DeletePortMapping>", "DeletePortMapping");
|
||||
}
|
||||
|
||||
|
||||
static string GetServiceUrl(string resp) {
|
||||
#if !DEBUG
|
||||
try {
|
||||
@ -111,48 +140,16 @@ namespace MCGalaxy.Core {
|
||||
return resp.Substring(0, n) + p;
|
||||
}
|
||||
|
||||
public static void ForwardPort(int port, ProtocolType protocol, string description) {
|
||||
if (String.IsNullOrEmpty(_serviceUrl) )
|
||||
throw new InvalidOperationException("No UPnP service available or Discover() has not been called");
|
||||
|
||||
XmlDocument xdoc = SOAPRequest(_serviceUrl,
|
||||
"<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
|
||||
"<NewRemoteHost></NewRemoteHost>" +
|
||||
"<NewExternalPort>" + port.ToString() + "</NewExternalPort>" +
|
||||
"<NewProtocol>" + protocol.ToString().ToUpper() + "</NewProtocol>" +
|
||||
"<NewInternalPort>" + port.ToString() + "</NewInternalPort>" +
|
||||
"<NewInternalClient>" + GetLocalIP() + "</NewInternalClient>" +
|
||||
"<NewEnabled>1</NewEnabled>" +
|
||||
"<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
|
||||
"<NewLeaseDuration>0</NewLeaseDuration>" +
|
||||
"</u:AddPortMapping>", "AddPortMapping");
|
||||
}
|
||||
|
||||
public static void DeleteForwardingRule(int port, ProtocolType protocol) {
|
||||
if (String.IsNullOrEmpty(_serviceUrl) )
|
||||
throw new InvalidOperationException("No UPnP service available or Discover() has not been called");
|
||||
|
||||
XmlDocument xdoc = SOAPRequest(_serviceUrl,
|
||||
"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
|
||||
"<NewRemoteHost>"</NewRemoteHost>" +
|
||||
"<NewExternalPort>" + port + "</NewExternalPort>" +
|
||||
"<NewProtocol>" + protocol.ToString().ToUpper() + "</NewProtocol>" +
|
||||
"</u:DeletePortMapping>", "DeletePortMapping");
|
||||
}
|
||||
|
||||
public static string GetLocalIP() {
|
||||
string localIP = "?";
|
||||
static string GetLocalIP() {
|
||||
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
|
||||
|
||||
foreach (IPAddress ip in host.AddressList) {
|
||||
if (ip.AddressFamily == AddressFamily.InterNetwork) {
|
||||
localIP = ip.ToString();
|
||||
return ip.ToString();
|
||||
}
|
||||
}
|
||||
return localIP;
|
||||
return "?";
|
||||
}
|
||||
|
||||
|
||||
static XmlDocument SOAPRequest(string url, string soap, string function) {
|
||||
string req = "<?xml version=\"1.0\"?>" +
|
||||
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
|
||||
|
Loading…
x
Reference in New Issue
Block a user