2018-02-08 14:16:27 +00:00
|
|
|
#include "server.h"
|
2018-01-22 10:22:31 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-15 21:59:21 +00:00
|
|
|
#include <inttypes.h>
|
2018-02-01 15:36:50 +00:00
|
|
|
#include <stdint.h>
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-15 21:59:21 +00:00
|
|
|
#include <SDL2/SDL_assert.h>
|
2018-02-13 09:10:18 +00:00
|
|
|
|
2018-02-13 10:55:12 +00:00
|
|
|
#include "config.h"
|
2018-02-13 09:10:18 +00:00
|
|
|
#include "log.h"
|
2018-02-16 10:11:07 +00:00
|
|
|
#include "net.h"
|
2018-01-22 10:22:31 +00:00
|
|
|
|
2018-01-23 14:46:34 +00:00
|
|
|
#define SOCKET_NAME "scrcpy"
|
|
|
|
|
2018-02-16 14:19:35 +00:00
|
|
|
#ifdef OVERRIDE_SERVER_PATH
|
|
|
|
# define DEFAULT_SERVER_PATH OVERRIDE_SERVER_PATH
|
2018-02-13 10:55:12 +00:00
|
|
|
#else
|
2018-02-16 14:19:35 +00:00
|
|
|
# define DEFAULT_SERVER_PATH PREFIX PREFIXED_SERVER_PATH
|
2018-02-13 10:55:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *get_server_path(void) {
|
2018-02-16 14:19:35 +00:00
|
|
|
const char *server_path = getenv("SCRCPY_SERVER_PATH");
|
2018-02-02 08:31:44 +00:00
|
|
|
if (!server_path) {
|
2018-02-16 14:19:35 +00:00
|
|
|
server_path = DEFAULT_SERVER_PATH;
|
2018-01-23 14:46:34 +00:00
|
|
|
}
|
2018-02-13 10:55:12 +00:00
|
|
|
return server_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_bool push_server(const char *serial) {
|
|
|
|
process_t process = adb_push(serial, get_server_path(), "/data/local/tmp/scrcpy-server.jar");
|
2018-02-08 14:16:27 +00:00
|
|
|
return process_check_success(process, "adb push");
|
2018-01-23 14:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 14:16:27 +00:00
|
|
|
static SDL_bool enable_tunnel(const char *serial, Uint16 local_port) {
|
|
|
|
process_t process = adb_reverse(serial, SOCKET_NAME, local_port);
|
|
|
|
return process_check_success(process, "adb reverse");
|
2018-01-23 14:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 14:16:27 +00:00
|
|
|
static SDL_bool disable_tunnel(const char *serial) {
|
|
|
|
process_t process = adb_reverse_remove(serial, SOCKET_NAME);
|
|
|
|
return process_check_success(process, "adb reverse --remove");
|
2018-01-23 14:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 14:16:27 +00:00
|
|
|
static process_t execute_server(const char *serial, Uint16 max_size, Uint32 bit_rate) {
|
2018-02-01 11:18:06 +00:00
|
|
|
char max_size_string[6];
|
2018-02-01 15:36:50 +00:00
|
|
|
char bit_rate_string[11];
|
|
|
|
sprintf(max_size_string, "%"PRIu16, max_size);
|
|
|
|
sprintf(bit_rate_string, "%"PRIu32, bit_rate);
|
2018-01-22 10:22:31 +00:00
|
|
|
const char *const cmd[] = {
|
|
|
|
"shell",
|
2018-02-02 08:31:44 +00:00
|
|
|
"CLASSPATH=/data/local/tmp/scrcpy-server.jar",
|
2018-01-22 10:22:31 +00:00
|
|
|
"app_process",
|
2018-01-29 16:06:44 +00:00
|
|
|
"/", // unused
|
2018-01-29 14:40:33 +00:00
|
|
|
"com.genymobile.scrcpy.ScrCpyServer",
|
2018-02-01 11:18:06 +00:00
|
|
|
max_size_string,
|
2018-02-01 15:36:50 +00:00
|
|
|
bit_rate_string,
|
2018-01-22 10:22:31 +00:00
|
|
|
};
|
|
|
|
return adb_execute(serial, cmd, sizeof(cmd) / sizeof(cmd[0]));
|
|
|
|
}
|
|
|
|
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-15 21:59:21 +00:00
|
|
|
static socket_t listen_on_port(Uint16 port) {
|
|
|
|
#define IPV4_LOCALHOST 0x7F000001
|
|
|
|
return net_listen(IPV4_LOCALHOST, port, 1);
|
2018-02-08 14:16:27 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 13:55:33 +00:00
|
|
|
static void close_socket(socket_t *socket) {
|
|
|
|
SDL_assert(*socket != INVALID_SOCKET);
|
|
|
|
if (!net_close(*socket)) {
|
|
|
|
LOGW("Cannot close socket");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*socket = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2018-02-08 14:16:27 +00:00
|
|
|
void server_init(struct server *server) {
|
|
|
|
*server = (struct server) SERVER_INITIALIZER;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_bool server_start(struct server *server, const char *serial, Uint16 local_port,
|
|
|
|
Uint16 max_size, Uint32 bit_rate) {
|
|
|
|
if (!push_server(serial)) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enable_tunnel(serial, local_port)) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the application level, the device part is "the server" because it
|
|
|
|
// serves video stream and control. However, at network level, the client
|
|
|
|
// listens and the server connects to the client. That way, the client can
|
|
|
|
// listen before starting the server app, so there is no need to try to
|
|
|
|
// connect until the server socket is listening on the device.
|
|
|
|
|
|
|
|
server->server_socket = listen_on_port(local_port);
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-15 21:59:21 +00:00
|
|
|
if (server->server_socket == INVALID_SOCKET) {
|
2018-02-13 09:10:18 +00:00
|
|
|
LOGE("Could not listen on port %" PRIu16, local_port);
|
2018-02-08 14:16:27 +00:00
|
|
|
disable_tunnel(serial);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// server will connect to our server socket
|
|
|
|
server->process = execute_server(serial, max_size, bit_rate);
|
|
|
|
if (server->process == PROCESS_NONE) {
|
2018-02-16 13:55:33 +00:00
|
|
|
close_socket(&server->server_socket);
|
2018-02-08 14:16:27 +00:00
|
|
|
disable_tunnel(serial);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->adb_reverse_enabled = SDL_TRUE;
|
|
|
|
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-15 21:59:21 +00:00
|
|
|
socket_t server_connect_to(struct server *server, const char *serial, Uint32 timeout_ms) {
|
|
|
|
server->device_socket = net_accept(server->server_socket);
|
|
|
|
if (server->device_socket == INVALID_SOCKET) {
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
2018-02-08 16:38:38 +00:00
|
|
|
|
2018-02-08 14:16:27 +00:00
|
|
|
// we don't need the server socket anymore
|
2018-02-16 13:55:33 +00:00
|
|
|
close_socket(&server->server_socket);
|
2018-02-08 16:38:38 +00:00
|
|
|
|
|
|
|
// we don't need the adb tunnel anymore
|
|
|
|
disable_tunnel(serial); // ignore failure
|
|
|
|
server->adb_reverse_enabled = SDL_FALSE;
|
|
|
|
|
2018-02-08 14:16:27 +00:00
|
|
|
return server->device_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
void server_stop(struct server *server, const char *serial) {
|
|
|
|
SDL_assert(server->process != PROCESS_NONE);
|
2018-02-16 10:11:07 +00:00
|
|
|
|
|
|
|
if (server->device_socket != INVALID_SOCKET) {
|
|
|
|
// shutdown the socket to finish the device process gracefully
|
2018-02-16 13:55:33 +00:00
|
|
|
if (!net_shutdown(server->device_socket, SHUT_RDWR)) {
|
|
|
|
LOGW("Cannot shutdown socket");
|
|
|
|
}
|
2018-02-16 10:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("Waiting the server to complete execution on the device...");
|
|
|
|
cmd_simple_wait(server->process, NULL); // ignore exit code
|
|
|
|
LOGD("Server terminated");
|
2018-02-08 14:16:27 +00:00
|
|
|
|
|
|
|
if (server->adb_reverse_enabled) {
|
|
|
|
// ignore failure
|
|
|
|
disable_tunnel(serial);
|
|
|
|
}
|
|
|
|
}
|
2018-02-09 11:59:36 +00:00
|
|
|
|
|
|
|
void server_destroy(struct server *server) {
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-15 21:59:21 +00:00
|
|
|
if (server->server_socket != INVALID_SOCKET) {
|
2018-02-16 13:55:33 +00:00
|
|
|
close_socket(&server->server_socket);
|
2018-02-09 11:59:36 +00:00
|
|
|
}
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-15 21:59:21 +00:00
|
|
|
if (server->device_socket != INVALID_SOCKET) {
|
2018-02-16 13:55:33 +00:00
|
|
|
close_socket(&server->device_socket);
|
2018-02-09 11:59:36 +00:00
|
|
|
}
|
|
|
|
}
|