mirror of
https://github.com/Tiiffi/mcrcon.git
synced 2025-10-27 19:31:07 -04:00
Fixes to Windows utf-8 support + one more Minecraft newline fix
This commit is contained in:
114
mcrcon.c
114
mcrcon.c
@ -33,13 +33,13 @@
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define VERSION "0.8.0"
|
#define VERSION "0.8.0"
|
||||||
@ -61,15 +61,15 @@
|
|||||||
|
|
||||||
// rcon packet structure
|
// rcon packet structure
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int32_t size;
|
int32_t size;
|
||||||
int32_t id;
|
int32_t id;
|
||||||
int32_t cmd;
|
int32_t cmd;
|
||||||
uint8_t data[DATA_BUFFSIZE];
|
uint8_t data[DATA_BUFFSIZE];
|
||||||
// ignoring string2 for now
|
// ignoring string2 for now
|
||||||
} rc_packet;
|
} rc_packet;
|
||||||
|
|
||||||
// ===================================
|
// ===================================
|
||||||
// FUNCTION DEFINITIONS
|
// FUNCTION DEFINITIONS
|
||||||
// ===================================
|
// ===================================
|
||||||
|
|
||||||
// Network related functions
|
// Network related functions
|
||||||
@ -140,9 +140,7 @@ void sighandler(int sig)
|
|||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
global_connection_alive = 0;
|
global_connection_alive = 0;
|
||||||
#ifndef _WIN32
|
exit(EXIT_SUCCESS);
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAX_WAIT_TIME 600
|
#define MAX_WAIT_TIME 600
|
||||||
@ -177,7 +175,7 @@ int main(int argc, char *argv[])
|
|||||||
char *host = getenv("MCRCON_HOST");
|
char *host = getenv("MCRCON_HOST");
|
||||||
char *pass = getenv("MCRCON_PASS");
|
char *pass = getenv("MCRCON_PASS");
|
||||||
char *port = getenv("MCRCON_PORT");
|
char *port = getenv("MCRCON_PORT");
|
||||||
|
|
||||||
if (!port) port = "25575";
|
if (!port) port = "25575";
|
||||||
if (!host) host = "localhost";
|
if (!host) host = "localhost";
|
||||||
|
|
||||||
@ -199,7 +197,7 @@ int main(int argc, char *argv[])
|
|||||||
case 'r': flag_raw_output = 1; break;
|
case 'r': flag_raw_output = 1; break;
|
||||||
case 'w':
|
case 'w':
|
||||||
flag_wait_seconds = mcrcon_parse_seconds(optarg);
|
flag_wait_seconds = mcrcon_parse_seconds(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'v':
|
case 'v':
|
||||||
puts(VER_STR);
|
puts(VER_STR);
|
||||||
@ -239,6 +237,9 @@ int main(int argc, char *argv[])
|
|||||||
old_output_codepage = GetConsoleOutputCP();
|
old_output_codepage = GetConsoleOutputCP();
|
||||||
old_input_codepage = GetConsoleCP();
|
old_input_codepage = GetConsoleCP();
|
||||||
|
|
||||||
|
SetConsoleOutputCP(CP_UTF8);
|
||||||
|
SetConsoleCP(CP_UTF8);
|
||||||
|
|
||||||
// Set the file translation mode to UTF16
|
// Set the file translation mode to UTF16
|
||||||
_setmode(_fileno(stdin), _O_U16TEXT);
|
_setmode(_fileno(stdin), _O_U16TEXT);
|
||||||
#endif
|
#endif
|
||||||
@ -291,8 +292,8 @@ void usage(void)
|
|||||||
puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password -w 5 \"say Server is restarting!\" save-all stop\n");
|
puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password -w 5 \"say Server is restarting!\" save-all stop\n");
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
puts("Press enter to exit.");
|
puts("Press enter to exit.");
|
||||||
getchar();
|
getchar();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
@ -319,10 +320,10 @@ void net_init_WSA(void)
|
|||||||
void net_close(int sd)
|
void net_close(int sd)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
closesocket(sd);
|
closesocket(sd);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
#else
|
#else
|
||||||
close(sd);
|
close(sd);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,16 +342,16 @@ int net_connect(const char *host, const char *port)
|
|||||||
hints.ai_protocol = IPPROTO_TCP;
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
net_init_WSA();
|
net_init_WSA();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int ret = getaddrinfo(host, port, &hints, &server_info);
|
int ret = getaddrinfo(host, port, &hints, &server_info);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
log_error("Name resolution failed.\n");
|
log_error("Name resolution failed.\n");
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
log_error("Error %d: %s", ret, gai_strerror(ret));
|
log_error("Error %d: %s", ret, gai_strerror(ret));
|
||||||
#else
|
#else
|
||||||
log_error("Error %d: %s\n", ret, gai_strerror(ret));
|
log_error("Error %d: %s\n", ret, gai_strerror(ret));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -376,7 +377,7 @@ int net_connect(const char *host, const char *port)
|
|||||||
/* TODO (Tiiffi): Check why windows does not report errors */
|
/* TODO (Tiiffi): Check why windows does not report errors */
|
||||||
log_error("Connection failed.\n");
|
log_error("Connection failed.\n");
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
log_error("Error %d: %s\n", errno, strerror(errno));
|
log_error("Error %d: %s\n", errno, strerror(errno));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
freeaddrinfo(server_info);
|
freeaddrinfo(server_info);
|
||||||
@ -491,9 +492,9 @@ void print_color(int color)
|
|||||||
else return;
|
else return;
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
fputs(colors[color], stdout);
|
fputs(colors[color], stdout);
|
||||||
#else
|
#else
|
||||||
SetConsoleTextAttribute(console_handle, color);
|
SetConsoleTextAttribute(console_handle, color);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -502,20 +503,32 @@ void print_color(int color)
|
|||||||
void packet_print(rc_packet *packet)
|
void packet_print(rc_packet *packet)
|
||||||
{
|
{
|
||||||
uint8_t *data = packet->data;
|
uint8_t *data = packet->data;
|
||||||
|
int i;
|
||||||
|
|
||||||
if (flag_raw_output == 1) {
|
if (flag_raw_output == 1) {
|
||||||
fputs((char *) data, stdout);
|
fputs((char *) data, stdout);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i;
|
// Newline fix for Minecraft
|
||||||
|
if (global_valve_protocol == false) {
|
||||||
|
const char test[] = "Unknown or incomplete command, see below for error";
|
||||||
|
size_t test_size = sizeof test - 1;
|
||||||
|
if (strncmp((char *) data, test, test_size) == 0) {
|
||||||
|
fwrite(data, test_size, 1, stdout);
|
||||||
|
putchar('\n');
|
||||||
|
data = &data[test_size];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int default_color = 0;
|
int default_color = 0;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
CONSOLE_SCREEN_BUFFER_INFO console_info;
|
CONSOLE_SCREEN_BUFFER_INFO console_info;
|
||||||
if (GetConsoleScreenBufferInfo(console_handle, &console_info) != 0) {
|
if (GetConsoleScreenBufferInfo(console_handle, &console_info) != 0) {
|
||||||
default_color = console_info.wAttributes + 0x30;
|
default_color = console_info.wAttributes + 0x30;
|
||||||
} else default_color = 0x37;
|
}
|
||||||
|
else default_color = 0x37;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool slash = false;
|
bool slash = false;
|
||||||
@ -536,6 +549,7 @@ void packet_print(rc_packet *packet)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add missing slashes
|
||||||
if (colors_detected == false && global_minecraft_newline_fix && data[i] == '/') {
|
if (colors_detected == false && global_minecraft_newline_fix && data[i] == '/') {
|
||||||
slash ? putchar('\n') : (slash = true);
|
slash ? putchar('\n') : (slash = true);
|
||||||
}
|
}
|
||||||
@ -587,7 +601,7 @@ bool rcon_auth(int sock, char *passwd)
|
|||||||
return 0; // send failed
|
return 0; // send failed
|
||||||
}
|
}
|
||||||
|
|
||||||
receive:
|
receive:
|
||||||
packet = net_recv_packet(sock);
|
packet = net_recv_packet(sock);
|
||||||
if (packet == NULL)
|
if (packet == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
@ -662,7 +676,7 @@ int rcon_command(int sock, char *command)
|
|||||||
|
|
||||||
if (flag_silent_mode == false) {
|
if (flag_silent_mode == false) {
|
||||||
if (packet->size > 10)
|
if (packet->size > 10)
|
||||||
packet_print(packet);
|
packet_print(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = select(sock + 1, &read_fds, NULL, NULL, &timeout);
|
int result = select(sock + 1, &read_fds, NULL, NULL, &timeout);
|
||||||
@ -693,9 +707,9 @@ int run_commands(int argc, char *argv[])
|
|||||||
|
|
||||||
if (flag_wait_seconds > 0) {
|
if (flag_wait_seconds > 0) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
Sleep(flag_wait_seconds * 1000);
|
Sleep(flag_wait_seconds * 1000);
|
||||||
#else
|
#else
|
||||||
sleep(flag_wait_seconds);
|
sleep(flag_wait_seconds);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -713,8 +727,8 @@ int run_terminal_mode(int sock)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
int len = get_line(command, MAX_COMMAND_LENGTH);
|
int len = get_line(command, MAX_COMMAND_LENGTH);
|
||||||
if (len < 1) continue;
|
if (len < 1) continue;
|
||||||
|
|
||||||
if (strcasecmp(command, "Q") == 0) break;
|
if (strcasecmp(command, "Q") == 0) break;
|
||||||
|
|
||||||
if (len > 0 && global_connection_alive) {
|
if (len > 0 && global_connection_alive) {
|
||||||
@ -725,7 +739,7 @@ int run_terminal_mode(int sock)
|
|||||||
|
|
||||||
/* Special case for "stop" command to prevent server-side bug.
|
/* Special case for "stop" command to prevent server-side bug.
|
||||||
* https://bugs.mojang.com/browse/MC-154617
|
* https://bugs.mojang.com/browse/MC-154617
|
||||||
*
|
*
|
||||||
* NOTE: This is hacky workaround which should be handled better to
|
* NOTE: This is hacky workaround which should be handled better to
|
||||||
* ensure compatibility with other servers using source RCON.
|
* ensure compatibility with other servers using source RCON.
|
||||||
* NOTE: strcasecmp() is POSIX function.
|
* NOTE: strcasecmp() is POSIX function.
|
||||||
@ -739,12 +753,16 @@ int run_terminal_mode(int sock)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
char tmp[1];
|
char tmp[1];
|
||||||
|
#ifdef _WIN32
|
||||||
|
// TODO: More Windows side testing!
|
||||||
|
int result = recv(sock, tmp, sizeof(tmp), MSG_PEEK | 0);
|
||||||
|
#else
|
||||||
int result = recv(sock, tmp, sizeof(tmp), MSG_PEEK | MSG_DONTWAIT);
|
int result = recv(sock, tmp, sizeof(tmp), MSG_PEEK | MSG_DONTWAIT);
|
||||||
|
#endif
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
// Connection closed
|
break; // Connection closed
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
// TODO: Check for errors too!
|
// TODO: Check for return values and errors!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -762,6 +780,9 @@ char *utf8_getline(char *buf, int size, FILE *stream)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wint_t ch;
|
||||||
|
while ((ch = getwchar()) != L'\n' && ch != WEOF);
|
||||||
|
|
||||||
// Calculates UTF-8 buffer size
|
// Calculates UTF-8 buffer size
|
||||||
int required_size = WideCharToMultiByte(CP_UTF8, 0, in, -1, NULL, 0, NULL, NULL);
|
int required_size = WideCharToMultiByte(CP_UTF8, 0, in, -1, NULL, 0, NULL, NULL);
|
||||||
if (size < required_size) {
|
if (size < required_size) {
|
||||||
@ -781,7 +802,11 @@ char *utf8_getline(char *buf, int size, FILE *stream)
|
|||||||
// gets line from stdin and deals with rubbish left in the input buffer
|
// gets line from stdin and deals with rubbish left in the input buffer
|
||||||
int get_line(char *buffer, int bsize)
|
int get_line(char *buffer, int bsize)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
char *ret = utf8_getline(buffer, bsize, stdin);
|
||||||
|
#else
|
||||||
char *ret = fgets(buffer, bsize, stdin);
|
char *ret = fgets(buffer, bsize, stdin);
|
||||||
|
#endif
|
||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
if (ferror(stdin)) {
|
if (ferror(stdin)) {
|
||||||
log_error("Error %d: %s\n", errno, strerror(errno));
|
log_error("Error %d: %s\n", errno, strerror(errno));
|
||||||
@ -793,14 +818,15 @@ int get_line(char *buffer, int bsize)
|
|||||||
|
|
||||||
// remove unwanted characters from the buffer
|
// remove unwanted characters from the buffer
|
||||||
buffer[strcspn(buffer, "\r\n")] = '\0';
|
buffer[strcspn(buffer, "\r\n")] = '\0';
|
||||||
|
|
||||||
int len = strlen(buffer);
|
int len = strlen(buffer);
|
||||||
|
|
||||||
// clean input buffer if needed
|
// clean input buffer if needed
|
||||||
|
#ifndef _WIN32
|
||||||
if (len == bsize - 1) {
|
if (len == bsize - 1) {
|
||||||
int ch;
|
int ch;
|
||||||
while ((ch = getchar()) != '\n' && ch != EOF);
|
while ((ch = getchar()) != '\n' && ch != EOF);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user