Genymobile / scrcpy

Display and control your Android device
Apache License 2.0
108.89k stars 10.48k forks source link

Ssh tunnel not works #5254

Open toto6976 opened 2 weeks ago

toto6976 commented 2 weeks ago

Good morning, I cannot connect to my smartphone when I am outside my home. I read the doc on 'ssh tunnel' but it doesn't work. I suppose i don't understand. Can you tell me what to do ?: the commands I pass must be wrong.

My smartphone is identified on the local network at 192.168.1.14 I have an ssh server open on 192.168.1.72. The PC with which I want to access the smartphone is in 192.168.0.251 in a other town. I open dyndns adresse on livebox to my ssh serveur.

rom1v commented 2 weeks ago

First, independently of scrcpy, can you access your remote computer (on which the remote device is connected) from your local computer using ssh?

toto6976 commented 2 weeks ago

Yes i can access the remote computer from my local computer via ssh. Remote house : pc with ssh server, smartphone. Local house : pc with ssh client.

And scrcpy works in the remote house from pc (witch one having the ssh server) to smartphone.

rom1v commented 2 weeks ago

OK, the describe what you did and what errors you got. Only use IP for know (don't use DNS to avoid additional possible issues).

toto6976 commented 2 weeks ago

I am trying to understand how the ssh tunnel works

# local  5038 --> remote  5037
# local 27183 <-- remote 27183
ssh -CN -L5038:localhost:5037 -R27183:localhost:27183 your_remote_computer

1/ i don't understand the ports. 2/ in my situation, what is remote computer and the localhost.

From thé pc 192.168.0.251 I tried ssh -CN -L5038:192.168.0.251:5037 -R27183:192.168.0.251:27183 mysshserveur@dyndns.net pxxxx

hostname contains iinvalid characters

rom1v commented 2 weeks ago

1/ i don't understand the ports.

5037 is the port adb is listening on on your remote computer If you create a local port redirection (-L5038:localhost:5037), this means that accessing your local port 5038 will redirect you to localhost:5037 from the other end of the tunnel (so on your remote computer). In other words, from your local computer, localhost:5038 is your_remote_computer:5037.

2/ in my situation, what is remote computer and the localhost.

My smartphone is identified on the local network at 192.168.1.14 I have an ssh server open on 192.168.1.72.

What is the public address IP if your ssh server (as viewed from your client computer, 192.168.1.72 is the local address, not the public one)? That IP is your_remote_computer.

localhost is localhost, nothing to change.

toto6976 commented 2 weeks ago

Hi 192.168.1.72 is the local ip adress of my pc on wich my ssh server is located. Of course it have an ip public : 65.xx.xxx.yyy. Is this PC the remote PC ? Is 192.168.0.251 the localhost ip ? I think i don't really understand port redirection. So i keep 5037. Then here is the new code ssh -CN -L5037:192.168.0.251:5037 -R27183:192.168.0.251:27183 65.xx.xxx.yyy pxxxx Is it OK ?

toto6976 commented 2 weeks ago

5037 is the port adb is listening on on your remote computer

So the 'remote computer' is the one witch the ssh server is installed. It means on the same LAN as the smartphone ?

rom1v commented 2 weeks ago

Your server: 65.xx.xxx.yyy (machine with local ip: 192.168.1.72). Your client: localhost (machine with the local ip: 192.168.0.251).

I assume both computers are on Linux.

From the client, execute:

ssh -CN -L5038:localhost:5037 -R27183:localhost:27183 65.xx.xxx.yyy
# keep this open

From another terminal on the client:

export ADB_SERVER_SOCKET=tcp:localhost:5038
scrcpy

I think i don't really understand port redirection. So i keep 5037.

One problem is that if your local computer also has an adb server running, 5037 is already used locally.

ssh -CN -L5037:192.168.0.251:5037 -R27183:192.168.0.251:27183 65.xx.xxx.yyy pxxxx Is it OK ?

No, you must keep localhost. The remote direction will not work on another interface by default.

And what is your pxxxx? If your ssh server listens on a port different of the default one (22), you can pass an option -pxxxx (the - is missing, probably a typo in your comment).

toto6976 commented 2 weeks ago

Yes , it's -pxxxx sorry.

One problem is that if your local computer also has an adb server running, 5037 is already used locally.

On the localhost computer (192.168.0.251) ? So an 'adb kill-server is OK ?

toto6976 commented 2 weeks ago

One problem is that if your local computer also has an adb server running, 5037 is already used locally.

'Local computer' it means localhost computer (192.168.0.251) ?

rom1v commented 2 weeks ago

Yes, the client.

("localhost computer" is confusing, since every computer is localhost/127.0.0.1)

toto6976 commented 2 weeks ago

From another terminal on the client:

export ADB_SERVER_SOCKET=tcp:localhost:5038
scrcpy

I write "scrcpy" or "scrcpy --tcpip 192.168.1.14:5555 ?

toto6976 commented 2 weeks ago

OK i think i understand.

A last question : can i keep the smartphone screen off ? What should i add to the command line you gave me ?

rom1v commented 2 weeks ago

I write "scrcpy" or "scrcpy --tcpip 192.168.1.14:5555 ?

As you like (it depends if it is connected over USB or not). From the client, you can use adb devices to list the remote devices (and adb connect, etc. as usual).

can i keep the smartphone screen off ?

Yes: scrcpy --turn-screen-off.

toto6976 commented 2 weeks ago

It is not connected over usb. So i write scrcpy --tcpip 192.168.1.14:5555 ?

toto6976 commented 2 weeks ago

as I am not in the house today where the smartphone is located, will it ask me to associate the 'localhost' PC or everything happens as if I am using the PC (remote) with which I am using it? had associated?

rom1v commented 2 weeks ago

So i write scrcpy --tcpip 192.168.1.14:5555 ?

Yes. From a terminal where ADB_SERVER_SOCKET is correctly defined:

export ADB_SERVER_SOCKET=tcp:localhost:5038
toto6976 commented 2 weeks ago

OK. I trie today.

toto6976 commented 1 week ago

Everything is ok.

I tried with the dyndns adress and it seams ok too.

can we use scrcpy on 4G if the wifi connection cuts out for one reason or another?

Le lun. 2 sept. 2024 à 09:40, Romain Vimont @.***> a écrit :

Yes. From a terminal where ADB_SERVER_SOCKET is correctly defined:

export ADB_SERVER_SOCKET=tcp:localhost:5038

— Reply to this email directly, view it on GitHub https://github.com/Genymobile/scrcpy/issues/5254#issuecomment-2324032065, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMWAAOQ7H5AXUQM6Z4KN47LZUQJABAVCNFSM6AAAAABNO4K626VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRUGAZTEMBWGU . You are receiving this because you authored the thread.Message ID: @.***>

toto6976 commented 6 days ago

Hi, This morning, i can't connect adb !! adb: failed to check server version: protocol fault (couldn't read status): Connection reset by peer

channel 2: open failed: connect failed: Connection refused

toto6976 commented 6 days ago

I start adb on the ssh server. Everything is ok ! Why ???!

rom1v commented 6 days ago

I start adb on the ssh server.

Because your client adb connects to the adb daemon, which normally runs on the same machine, but here it runs on the remote machine (and the client and daemon connects through the ssh tunnel).

In other words, when you run adb devices, normally, your process connects to the ssh daemon (listening on localhost:5037 by default). But here, you specified to connect to localhost:5038, and the local port 5038 is forwarded to the remote port 5037, so your local adb client connects to the remote adb daemon. If the daemon is not running, then there is nothing to connect to.

toto6976 commented 6 days ago

ok. Do I have to reconnect the device via usb? or an adb connect device_ip_address:5555 is enough

rom1v commented 6 days ago

It depends if you rebooted the device. It must be connected over USB at least once after each device reboot to execute (directly or indirectly) adb tcpip 5555.

toto6976 commented 6 days ago

Another thing: I have two devices on two different addresses. They each have a different tcpip port. Device 1 tcpip 5555 Device 2 tcpip 5556 When I change devices, only device 2 requires a usb reconnection to work. The other no?!? I don't know what to do. Device 1 is a normal Samsung, device 2 is an OS 11 lineage.

toto6976 commented 6 days ago

It depends if you rebooted the device. It must be connected over USB at least once after each device reboot to execute (directly or indirectly) adb tcpip 5555.

No, the device is not rebooted

rom1v commented 6 days ago

They each have a different tcpip port

Why do you use different ports? Just use 5555 (the default) for all devices.

When I change devices, only device 2 requires a usb reconnection to work. The other no?!? I don't know what to do.

I'm not sure to understand.

toto6976 commented 6 days ago

I start adb on the ssh server.

Because your client adb connects to the adb daemon, which normally runs on the same machine, but here it runs on the remote machine (and the client and daemon connects through the ssh tunnel).

In other words, when you run adb devices, normally, your process connects to the ssh daemon (listening on localhost:5037 by default). But here, you specified to connect to localhost:5038, and the local port 5038 is forwarded to the remote port 5037, so your local adb client connects to the remote adb daemon. If the daemon is not running, then there is nothing to connect to.

ok. It's not practical at all but hey...Isn't there a way to do it differently?

toto6976 commented 6 days ago

They each have a different tcpip port

Why do you use different ports? Just use 5555 (the default) for all devices.

When I change devices, only device 2 requires a usb reconnection to work. The other no?!? I don't know what to do.

I'm not sure to understand.

https://developer.android.com/tools/adb Each emulator uses a pair of sequential ports — an even-numbered port for console connections and an odd-numbered port for adb connections. For example:

Emulator 1, console: 5554 Emulator 1, adb: 5555 Emulator 2, console: 5556 Emulator 2, adb: 5557 and so on.

rom1v commented 6 days ago

It's not practical at all but hey...Isn't there a way to do it differently?

It's just some hacks to connect to the adb daemon as if it were local (i.e. running on the same machine).

The user-friendly solution would be to add server/client features to scrcpy, which is out-of-scope.

toto6976 commented 6 days ago

ok, so only

adb tcpip 5555 adb connect device_ip_address1:5555 adb connect device_ip_address2:5555

then I can switch from one to the other by simply changing the address: scrcpy --tcpip adresse_ip1:5555 or scrcpy --tcpip adresse_ip2:5555

toto6976 commented 6 days ago

It's not practical at all but hey...Isn't there a way to do it differently?

It's just some hacks to connect to the adb daemon as if it were local (i.e. running on the same machine).

The user-friendly solution would be to add server/client features to scrcpy, which is out-of-scope.

I don't understand absolutely anything! We'll leave it like that for the moment.