Search Unity

WebGL getIPAdress

Discussion in 'Web' started by ncortesp, Nov 30, 2018.

  1. ncortesp

    ncortesp

    Joined:
    Oct 28, 2016
    Posts:
    42
    How can I get the IpAdress from the device with WebGL platform?

    If I do this;

    Code (CSharp):
    1. foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
    2.         {
    3.             if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
    4.             {
    5.                 foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
    6.                 {
    7.                     if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    8.                     {
    9.                         //do what you want with the IP here... add it to a list, just get the first and break out. Whatever.
    10.                         SGO.Log(ip.Address.ToString());
    11.                     }
    12.                 }
    13.             }
    14.         }
    It throws an error like this;

    Code (CSharp):
    1. Uncaught abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/kripken/emscripten/wiki/Linking") at Error
    2.     at jsStackTrace (blob:http://localhost:52755/bc54a72d-88e3-4b57-97f8-8fc94d90a997:1156:12)
    3.     at stackTrace (blob:http://localhost:52755/bc54a72d-88e3-4b57-97f8-8fc94d90a997:1170:11)
    4.     at abort (blob:http://localhost:52755/bc54a72d-88e3-4b57-97f8-8fc94d90a997:22759:43)
    5.     at _dlopen (blob:http://localhost:52755/bc54a72d-88e3-4b57-97f8-8fc94d90a997:8661:2)
    6.     at __ZN6il2cpp2os13LibraryLoader18LoadDynamicLibraryERKNS_5utils10StringViewIcEEi [il2cpp::os::LibraryLoader::LoadDynamicLibrary(il2cpp::utils::StringView<char> const&, int)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2609632:8)
    7.     at __ZN6il2cpp2os13LibraryLoader18LoadDynamicLibraryERKNS_5utils10StringViewIcEE [il2cpp::os::LibraryLoader::LoadDynamicLibrary(il2cpp::utils::StringView<char> const&)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4219917:9)
    8.     at __ZN6il2cpp2vm13LibraryLoader11LoadLibraryENS_5utils10StringViewIcEE [il2cpp::vm::LibraryLoader::LoadLibrary(il2cpp::utils::StringView<char>)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3704735:8)
    9.     at __ZN6il2cpp2vm14PlatformInvoke7ResolveERK16PInvokeArguments [il2cpp::vm::PlatformInvoke::Resolve(PInvokeArguments const&)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:1131463:10)
    10.     at __Z30il2cpp_codegen_resolve_pinvokeIPFiiELj5ELj6EET_RAT0__KcRAT1__S3_20Il2CppCallConvention13Il2CppCharSetib [int (*il2cpp_codegen_resolve_pinvoke<int (*)(int), 5u, 6u>(char const (&) [5u], char const (&) [6u], Il2CppCallConvention, Il2CppCharSet, int, bool))(int)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3585555:7)
    11.     at _Platform_uname_m3913857930 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3732133:8)
    12.     at _Platform_CheckOS_m1308309308 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2817692:12)
    13.     at _Platform_get_IsMacOS_m1085619612 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3903635:3)
    14.     at _NetworkInterfaceFactory_Create_m1912302726 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2862528:9)
    15.     at _SystemNetworkInterface__cctor_m2883236336 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3985874:7)
    16.     at __Z31RuntimeInvoker_Void_t1185182177PFvvEPK10MethodInfoPvPS4_ [RuntimeInvoker_Void_t1185182177(void (*)(), MethodInfo const*, void*, void**)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4233615:32)
    17.     at dynCall_iiiii (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4237711:41)
    18.     at invoke_iiiii (blob:http://localhost:52755/bc54a72d-88e3-4b57-97f8-8fc94d90a997:17418:33)
    19.     at __ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException [il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3471659:8)
    20.     at __ZN6il2cpp2vm7Runtime9ClassInitEP11Il2CppClass [il2cpp::vm::Runtime::ClassInit(Il2CppClass*)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:1979889:5)
    21.     at _NetworkInterface_GetAllNetworkInterfaces_m1398684982 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3829284:92)
    22.     at _APIManager_SetUserAgent_m1005549515 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:1401216:7)
    23.     at _APIManager__ctor_m620182926 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3276668:2)
    24.     at _GameManager_Awake_m2466247815 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:1934690:3)
    25.     at __Z31RuntimeInvoker_Void_t1185182177PFvvEPK10MethodInfoPvPS4_ [RuntimeInvoker_Void_t1185182177(void (*)(), MethodInfo const*, void*, void**)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4233615:32)
    26.     at dynCall_iiiii (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4237711:41)
    27.     at invoke_iiiii (blob:http://localhost:52755/bc54a72d-88e3-4b57-97f8-8fc94d90a997:17418:33)
    28.     at __ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException [il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3471659:8)
    29.     at _il2cpp_runtime_invoke (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4010585:9)
    30.     at __Z23scripting_method_invoke18ScriptingMethodPtr18ScriptingObjectPtrR18ScriptingArgumentsP21ScriptingExceptionPtrb [scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3699688:8)
    31.     at __ZN19ScriptingInvocation6InvokeEP21ScriptingExceptionPtrb [ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2626758:7)
    32.     at __ZN19ScriptingInvocation13InvokeCheckedEP21ScriptingExceptionPtr [ScriptingInvocation::InvokeChecked(ScriptingExceptionPtr*)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3181007:7)
    33.     at __ZN13MonoBehaviour9CallAwakeEv [MonoBehaviour::CallAwake()] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2318623:2)
    34.     at __ZN13MonoBehaviour12AddToManagerEv [MonoBehaviour::AddToManager()] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2208589:3)
    35.     at __ZN9Behaviour13AwakeFromLoadE17AwakeFromLoadMode [Behaviour::AwakeFromLoad(AwakeFromLoadMode)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3595677:71)
    36.     at __ZN13MonoBehaviour13AwakeFromLoadE17AwakeFromLoadMode [MonoBehaviour::AwakeFromLoad(AwakeFromLoadMode)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:1662020:5)
    37.     at __ZN18AwakeFromLoadQueue13AwakeFromLoadE17AwakeFromLoadModeb [AwakeFromLoadQueue::AwakeFromLoad(AwakeFromLoadMode, bool)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2321284:85)
    38.     at __Z29AwakeAndActivateClonedObjectsPP6ObjectRK10vector_mapIiiNSt3__24lessIiEE13stl_allocatorINS3_4pairIiiEEL18MemLabelIdentifier1ELi16EEE [AwakeAndActivateClonedObjects(Object**, vector_map<int, int, std::__2::less<int>, stl_allocator<std::__2::pair<int, int>, (MemLabelIdentifier)1, 16> > const&)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2524071:2)
    39.     at __Z11CloneObjectR6Object [CloneObject(Object&)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2544167:2)
    40.     at __Z34Object_CUSTOM_Internal_CloneSingleP12Il2CppObject [Object_CUSTOM_Internal_CloneSingle(Il2CppObject*)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3529959:54)
    41.     at _Object_Internal_CloneSingle_m1021157572 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4080976:37)
    42.     at _Object_Instantiate_m3403600534 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3745720:9)
    43.     at _GameManager_get_instance_m1062694977 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2582205:8)
    44.     at _SignalRManager_Awake_m3414226069 (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3246055:8)
    45.     at __Z31RuntimeInvoker_Void_t1185182177PFvvEPK10MethodInfoPvPS4_ [RuntimeInvoker_Void_t1185182177(void (*)(), MethodInfo const*, void*, void**)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4233615:32)
    46.     at dynCall_iiiii (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4237711:41)
    47.     at invoke_iiiii (blob:http://localhost:52755/bc54a72d-88e3-4b57-97f8-8fc94d90a997:17418:33)
    48.     at __ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException [il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3471659:8)
    49.     at _il2cpp_runtime_invoke (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:4010585:9)
    50.     at __Z23scripting_method_invoke18ScriptingMethodPtr18ScriptingObjectPtrR18ScriptingArgumentsP21ScriptingExceptionPtrb [scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:3699688:8)
    51.     at __ZN19ScriptingInvocation6InvokeEP21ScriptingExceptionPtrb [ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool)] (blob:http://localhost:52755/b00026ac-2faa-4652-94b2-73cabbe9ba3c:2626758:7)
    I've tried this way too;

    Code (CSharp):
    1. System.Net.IPAddress[] addr = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList;
    2.         string ipV4 = "";
    3.  
    4.         foreach (System.Net.IPAddress ip in addr)
    5.         {
    6.             //SGO.Log("*****Network address: " + ip);
    7.             //SGO.Log("is IPV4: " + (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork));
    8.             if (string.IsNullOrEmpty(ipV4) && ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    9.                 ipV4 = ip.ToString();
    10.         }
    And still get an error.
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    System.Net.* is not supported on WebGL. See here for more info.
     
  3. ncortesp

    ncortesp

    Joined:
    Oct 28, 2016
    Posts:
    42
    You still haven't answered my question :/.
     
  4. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
  5. QuincyC

    QuincyC

    Joined:
    Jun 5, 2018
    Posts:
    10
    wow roka nice response very helpful for everyone coming here from google
     
    nick_tw likes this.
  6. Rugbug_Redfern

    Rugbug_Redfern

    Joined:
    Jun 2, 2017
    Posts:
    20
    Wow this is a super helpful response for me when I'm not hosting my game on a server but instead a static website with no php support
     
  7. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Then it is likely a case of "s**t outta luck", no? :(

    Remember, just because someone helps someone else in a way that does not help you, it is not much of use to turn sarcastic towards that help.

    The root cause here is that web browsers do not allow web pages to obtain access to the IP address of the visitor. That would be a massive web tracking and privacy security hole.

    However web servers routinely have access to the IP address of a visitor. They just do not expose that to the web browser. On a server backend, it is possible to develop all kinds of visitor tracking, banning etc. operations, but that requires developing a server backend in the first place. PHP is one such lightweight server code execution model, where one can manipulate the IP without necessarily insecurely exposing it back to the client web browser.

    If you still want to get the visitors IP address to the client web browser in Unity, you will need to either
    1. develop such a web backend that captures the visitor's IP address, and exposes it back via some API that the Unity client can invoke a UnityWebRequest to, in order to ask "what is the caller's IP address?".
    2. or if the Unity game resides on the same server than such a custom backend, use a custom .html / .js request response handler that injects the IP address to that content (which is what the PHP solution is doing)
    3. or use some public third party service that provides the IP address of the caller. E.g. https://dev.to/adityathebe/a-handy-way-to-know-your-public-ip-address-with-dns-servers-4nmn shows one such service is https://api.ipify.org/ . Fire a UnityWebRequest GET query to that address, and it will return back your IP address. This solution has the security problem of forfeiting privacy to ipfy.org, and letting them track all visitors to your site, but that is the tradeoff between developing your own API (1. or 2. above) and using someone else's.

    Hopefully that can help you forward!
     
  8. nick_tw

    nick_tw

    Joined:
    Oct 24, 2018
    Posts:
    4
  9. Kai-Baier

    Kai-Baier

    Joined:
    Jan 18, 2018
    Posts:
    2
    Feel bad to say ... was hoping for a way to find a unique identifier to make usernaming easy ....... well
     
  10. snowmobeetle

    snowmobeetle

    Joined:
    May 23, 2017
    Posts:
    7

    THANK YOU!!! I know it's years later, but this post helped me immensely. I have my own apache server that did have php installed, so I just set up a simple php file, added a www request, and it worked! Just wasn't thinking far enough outside of the webGL box...

    Unity code:
    Code (CSharp):
    1.             WWWForm form4 = new WWWForm();
    2.             form4.AddField("domainName", possibleDomainName);
    3.  
    4.             WWW www4 = new WWW("htt...com/phpFileLocation/GetIpFromDomain.php", form4);
    5.             yield return www4;
    6.             Debug.Log("www.text: " + www4.text);
    7.             if (www4.text != null && www4.text != "")
    8.             {
    9.                 Debug.Log("Attempting to connect to IP: " + www4.text);
    10.                 NetworkClient.Connect(www4.text);
    11.  
    12.                 OnStartClient();
    13.             }
    14.             else
    15.             {
    16.                 Debug.Log("Invalid response from DNS check");
    17.                 yield break;
    18.             }

    PHP file GetIpFromDomain.php :
    Code (JavaScript):
    1. <?php
    2.    if($_SERVER["REQUEST_METHOD"] == "POST") {
    3.          
    4.       $domainName = mysqli_real_escape_string($db,$_POST['domainName']);  
    5.  
    6.         echo (gethostbyname($domainName));
    7.       }
    8.    }
    9. ?>