Search Unity

Resolved How to get ip from player?

Discussion in 'Netcode for GameObjects' started by misanurmuhametov, Feb 10, 2023.

  1. misanurmuhametov

    misanurmuhametov

    Joined:
    Nov 20, 2019
    Posts:
    8
    I need to ban player, i have idea to get player ip, and create list with banned players, if list contain player ip his get disconnected, but i idk how to get player ip, have only client id, in documentation i find nothing, in google too, do something know way to get player ip?


    Code (CSharp):
    1. #if UNITY_SERVER || UNITY_EDITOR
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5.  
    6. public class BanManager : MonoBehaviour
    7. {
    8.     public static BanManager main;
    9.     public List<string> bannedIps;
    10.     public void Awake()
    11.     {
    12.         main = this;
    13.     }
    14.     public void Start()
    15.     {
    16.         NetworkManager.Singleton.ConnectionApprovalCallback = ApprovalConnectionForBanCheck;
    17.     }
    18.     public static void BanPlayerIp(ulong clientId)
    19.     {
    20.         main.bannedIps.Add("127.0.0.1"); // i idk how to get ip, then this i add local ip
    21.         NetworkManager.Singleton.DisconnectClient(clientId);
    22.     }
    23.     private void ApprovalConnectionForBanCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
    24.     {
    25.         string cachedIp = "127.0.0.1"; // i idk how to get ip, for example just use local ip
    26.         bool approved = true;
    27.         foreach (string s in bannedIps)
    28.         {
    29.             if(s == cachedIp)
    30.             {
    31.                 approved = false;
    32.             }
    33.         }
    34.         response.Approved = approved;
    35.         response.Pending = false;
    36.     }
    37.  
    38. }
    39. #endif
    40.  
     
    Last edited: Feb 10, 2023
  2. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    I believe most players in the world connect via a dynamic IP address which means they can easily get another address and you potentially ban someone else.
     
  3. Evil-Otaku

    Evil-Otaku

    Joined:
    Oct 17, 2012
    Posts:
    72
    You would want to use the Authentication Service then you can just ban the Player ID linked to their accounts
     
    RikuTheFuffs and lavagoatGG like this.
  4. misanurmuhametov

    misanurmuhametov

    Joined:
    Nov 20, 2019
    Posts:
    8
    Yes, most player uses dynamic IP address, but dynamic ip address don't changes every day, he's changes every 7-14 days (i idk how days, but approximately is 7-14 days), it's have temporarly effeciency
     
  5. misanurmuhametov

    misanurmuhametov

    Joined:
    Nov 20, 2019
    Posts:
    8
    Netcode by default don't provide player auth id, i need manually provide from client in rpc, but i include decompile my game, cheater, when decompile game and change it, can replace id's, generate always new random or replace manually it, all information provided in rpc can be replaced, if cheater cannot overrides method's i can just push bytes from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography (DWORD parametr "MachineGuid") and ban it by hardware, but, i'm earler say, cheater can replace values, values provide by unity netcode and don't get aviablity replaced are ip and clientId (get changing by re-join on the server), but i provide playerId, not for ban, this exist for getting saves, i can delete saves, but if cheater replace all variables, i have only playerId with his saves and ip (ip i'm need to got, i already idk how to get it), you can say 'Use Il2CPP', but with il2cpp cheater can override methods with harmony or something of that, i idk how its works in Il2CPP, but i'm sure, bypasses exists
     
  6. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    Generally speaking if a solution doesn't solve the problem it is a less than optimal solution. You are describing a case where you want to ban an annoying player and he only has to be smart enough to get a new IP address or use a VPN.

    It is a poor solution to your problem.
     
  7. misanurmuhametov

    misanurmuhametov

    Joined:
    Nov 20, 2019
    Posts:
    8
    Cheaters often uses free vpn, free vpn with restrictions, such a 'Temporarly testing using for 24 hours' or '10 GB Free Vpn, Next GB's Pay', he's create new accounts, this creating can be for a long time, for change ip addres, such a pull out and insert the internet cable, this can use lot of time, player often is lazy, his like just change player id and player, and, maybe cheater don't install vpn? Maybe his toooo lazy to install it, cheater can be stupid and don't know about vpn, or don't remember it, ip banning uses on most games, and, i just need to get ip, all solutions contains alternatives solutions or unprofitability reply like 'Player Can Use Vpn' or 'Ip often dynamic and it's change', i just need valid reply's
     
  8. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    Ok tell you what check for the IP address and add it to a banned list. :)
     
  9. misanurmuhametov

    misanurmuhametov

    Joined:
    Nov 20, 2019
    Posts:
    8
    If you know way how to get ip from player, please say it
     
  10. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    There's a free Unity Service for that, have you had a look at it? it would solve your problem as Evil-Otaku mentioned

    EDIT: sorry, I originally wrote "Cerestorm" but the answer was from Evil-Otaku
     
    Last edited: Feb 14, 2023
  11. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    That was Evil-Otaku :) It might be useful to have the IP passed in on client approval for those not wanting to use an authentication service. Is there a good reason for not including it?
     
    RikuTheFuffs-U and lavagoatGG like this.
  12. misanurmuhametov

    misanurmuhametov

    Joined:
    Nov 20, 2019
    Posts:
    8
    I mean if i connect this unity service, i don't get from NetworkClient, OnClientConnected callback args or something of that, i need to provide it manually from rpc, but, cheaters with harmony or something of that can override methods, he can rewrite method to send fake player id, but cheater cannot rewrite clientId, getted from ServerRpcParams or OnClientConnected callback args, in UNet i can get player ip without providing ip from rpc by same player
     
  13. misanurmuhametov

    misanurmuhametov

    Joined:
    Nov 20, 2019
    Posts:
    8
    ConnectionApprovalRequest don't provide player ip, NetworkClient getted by ConnectedClients dictionary too
     
  14. misanurmuhametov

    misanurmuhametov

    Joined:
    Nov 20, 2019
    Posts:
    8
    I invented method, he's stupid, but it's work, and something different unsuitable for me, i use additionally other network library (Mirror), and first connect player to mirror server. When player connect to mirror server, i get ip (I know how to get ip from mirror) and reject connection to netcode (main) server, you can say 'Cheater can rewrite method and directly connect to netcode server', but, i create 'token list', contain 25 random number from 1 to 9 converted to string, if player connect to mirror server, token create, add to token list, provide to player by rpc sending and send to netcode server, netcode server add this token to token list too, if player connect to netcode server, before spawning player need provide token by rpc, when token provided, netcode server search this token in list, if token doesn't finded, netcode server just kicking player, else netcode server spawn player, i change thread status with 'resolved' (i answered myself lol)
     
  15. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    I know, it was more a question to the gallery. :D

    That sounds like a painful workaround, it might be worth creating a feature request on the NGO github for visibility on the client IP. Maybe first we can trouble @simon-lemay-unity and ask if he thinks it's something that realistically would be added to the NGO Unity Transport wrapper.
     
  16. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    It would indeed make a great feature request for the Unity Transport wrapper. The low level transport package already exposes this information so it would be pretty easy to expose it through the NGO wrapper.