Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get ip from connect socket? #8

Open
sonygod opened this issue Jul 31, 2019 · 14 comments
Open

how to get ip from connect socket? #8

sonygod opened this issue Jul 31, 2019 · 14 comments

Comments

@sonygod
Copy link

sonygod commented Jul 31, 2019

how to get ip from connect socket?

@Tezar
Copy link
Contributor

Tezar commented Aug 23, 2019

You need to access _socket, but since it's private, you need to create new class e.g. HostAwareWebsocket extending original WebSocket and then create method for returning _socket.host().

@sonygod
Copy link
Author

sonygod commented Aug 24, 2019

thank you ,@Tezar

@sonygod sonygod closed this as completed Aug 24, 2019
@sh-dave
Copy link

sh-dave commented Aug 24, 2019

Or just use @:privateAccess?

@Tezar
Copy link
Contributor

Tezar commented Aug 25, 2019

I am not that well versed in the Haxe, so this solution comed as little surprise. Documentation seems little problematic to google up (at least for me) so quick intro to anyone going to same path.

You can access private vars like this

@:privateAccess var host = ws._socket.host();

Or by specifying @:access before function concerned (seems to require full path regardless of the imports)

     @:access(hx.ws.WebSocket._socket)
     public static function main() {
           ...
           trace(ws._socket.host());
    }

Also note, that host() returns local endpoint, for remote IP call peer()

@ianharrigan
Copy link
Owner

I guess it makes sense to for this to be added into the actual api also? If someone doesnt make a PR ill make one in the next few days.

Cheers guys!
Ian

@ianharrigan ianharrigan reopened this Aug 26, 2019
@sonygod
Copy link
Author

sonygod commented Nov 26, 2019

image

image

WHY my ip looks like some thing like

        ip 16777343

@Tezar @sh-dave

@Tezar
Copy link
Contributor

Tezar commented Nov 26, 2019

Well, it is IP4 address, which is stored as 4bytes (hence the range 0-255, since thats tha maximum value 1 byte can hold). The usual format is just human friendly way of writing it down. IP you send is 01 00 00 7F in hexa, which corresponds to 127.0.0.1 (dont get confused by big/little endian stuff). If you want more on this topic, I recommend to start on wiki and go from there, this stuff is programing language agnostic, so you will benefit from it anywhere.

You can safely use this format for comparsion and other stuff, but for output expected by you, you can try to call host.toString()

@sonygod
Copy link
Author

sonygod commented Nov 26, 2019

it's seem well

  public function intToIp(ipInt:Int) :String{
	var ipArr:Array<Int> = [];
	for (i in 0...4) {
		ipArr[3 - i] ^= ipInt & 255;
		ipInt >>>= 8;
	}
	return ipArr.join('.');
}

@sonygod sonygod closed this as completed Nov 26, 2019
@sonygod
Copy link
Author

sonygod commented Nov 27, 2019

host = ws._socket.host()

the result is server ip instead of client ip

@sonygod sonygod reopened this Nov 27, 2019
@Tezar
Copy link
Contributor

Tezar commented Nov 27, 2019

#8 (comment)

Also note, that host() returns local endpoint, for remote IP call peer()

@sonygod
Copy link
Author

sonygod commented Nov 27, 2019

👍 @Tezar

@sonygod sonygod closed this as completed Nov 27, 2019
@sonygod
Copy link
Author

sonygod commented Feb 16, 2020

hi @Tezar ,ip is right.but port is wrong

function toPort(hander:IHander):Int {
	var ws:WebSocketCommon = cast hander;
	var socket:Socket = @:privateAccess ws._socket;
	var h = socket.peer();

	return h.port;
}

my port is 12011,and got the result is 50444?

@sonygod sonygod reopened this Feb 16, 2020
@Tezar
Copy link
Contributor

Tezar commented Feb 20, 2020

Hi @sonygod,
that's strange, I have tried and it works as intended (remote WSS returns 443). What peer server are you using? Can you confirm that you are really connected to the endpoint via system tools? (tcpview on windows, netstat on linux, youareonyourown on mac)

@Tezar
Copy link
Contributor

Tezar commented May 17, 2021

@sonygod any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants