📄 doconsole.cpp
字号:
#include "mpdimpl.h"#include <stdio.h>#include "GetStringOpt.h"#include "Translate_Error.h"#include "mpdutil.h"#include <conio.h> /* getch */static void GetPassword(char *question, char *account, char *password){ if (question != NULL) printf(question); else printf("password for %s: ", account); fflush(stdout); HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD dwMode; if (!GetConsoleMode(hStdin, &dwMode)) dwMode = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT; SetConsoleMode(hStdin, dwMode & ~ENABLE_ECHO_INPUT); gets(password); SetConsoleMode(hStdin, dwMode); printf("\n"); fflush(stdout);}void DoConsole(char *host, int port, bool bAskPwd, char *altphrase){ SOCKET sock; char phrase[MPD_PASSPHRASE_MAX_LENGTH+1]; char str[CONSOLE_STR_LENGTH+1]; char *result; int error; easy_socket_init(); ParseRegistry(false); if (host == NULL || host[0] == '\0') host = g_pszHost; if (port == -1) port = g_nPort; if (easy_create(&sock, 0, INADDR_ANY) == SOCKET_ERROR) { error = WSAGetLastError(); Translate_Error(error, str); printf("easy_create failed: %d\n%s\n", error, str); fflush(stdout); return; } if (altphrase != NULL) { strncpy(phrase, altphrase, MPD_PASSPHRASE_MAX_LENGTH); phrase[MPD_PASSPHRASE_MAX_LENGTH] = '\0'; } else if (bAskPwd || !ReadMPDRegistry("phrase", phrase, false)) { printf("please input the passphrase: ");fflush(stdout); HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD dwMode; if (!GetConsoleMode(hStdin, &dwMode)) dwMode = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT; SetConsoleMode(hStdin, dwMode & ~ENABLE_ECHO_INPUT); gets(phrase); SetConsoleMode(hStdin, dwMode); printf("\n");fflush(stdout); } printf("connecting to %s:%d\n", host, port);fflush(stdout); if (easy_connect(sock, host, port) == SOCKET_ERROR) { error = WSAGetLastError(); Translate_Error(error, str); printf("easy_connect failed: %d\n%s\n", error, str);fflush(stdout); easy_closesocket(sock); return; } if (!ReadString(sock, str)) { printf("reading challenge string failed.\n");fflush(stdout); easy_closesocket(sock); return; } if (strlen(phrase) + strlen(str) > MPD_PASSPHRASE_MAX_LENGTH) { printf("unable to process passphrase.\n");fflush(stdout); easy_closesocket(sock); return; } strcat(phrase, str); result = crypt(phrase, MPD_SALT_VALUE); memset(phrase, 0, strlen(phrase)); // zero out the passphrase if (altphrase != NULL) memset(altphrase, 0, strlen(altphrase)); // zero out the passphrase strcpy(str, result); if (WriteString(sock, str) == SOCKET_ERROR) { error = WSAGetLastError(); Translate_Error(error, str); printf("WriteString of the encrypted response string failed: %d\n%s\n", error, str);fflush(stdout); easy_closesocket(sock); return; } if (!ReadString(sock, str)) { printf("reading authentication result failed.\n");fflush(stdout); easy_closesocket(sock); return; } if (strcmp(str, "SUCCESS")) { printf("host authentication failed.\n");fflush(stdout); easy_closesocket(sock); return; } if (WriteString(sock, "console") == SOCKET_ERROR) { error = WSAGetLastError(); Translate_Error(error, str); printf("WriteString('console') failed: %d\n%s\n", error, str);fflush(stdout); easy_closesocket(sock); return; } printf("connected\n");fflush(stdout); while (gets(str)) { if ((strnicmp(str, "getpid ", 7) == 0) || (strnicmp(str, "geterror ", 9) == 0) || (strnicmp(str, "getexitcode ", 12) == 0) || (strnicmp(str, "getexitcodewait ", 16) == 0) || (strnicmp(str, "getexittime ", 12) == 0) || (stricmp(str, "version") == 0) || (stricmp(str, "mpich version") == 0) || (stricmp(str, "config") == 0) || (strnicmp(str, "dbput ", 6) == 0) || (strnicmp(str, "dbget ", 6) == 0) || (stricmp(str, "dbcreate") == 0) || (strnicmp(str, "dbcreate ", 9) == 0) || (strnicmp(str, "dbdestroy ", 10) == 0) || (strnicmp(str, "dbfirst ", 8) == 0) || (strnicmp(str, "dbnext ", 7) == 0) || (stricmp(str, "dbfirstdb") == 0) || (stricmp(str, "dbnextdb") == 0) || (strnicmp(str, "dbdelete ", 9) == 0) || (stricmp(str, "ps") == 0) || (stricmp(str, "forwarders") == 0) || (strnicmp(str, "createtmpfile ", 14) == 0) || (strnicmp(str, "deletetmpfile ", 14) == 0) || (strnicmp(str, "mpich1readint ", 14) == 0) || (strnicmp(str, "freeprocess ", 12) == 0) || (strnicmp(str, "lget ", 5) == 0) || (strnicmp(str, "freecached", 10) == 0) || (strnicmp(str, "setdbgoutput ", 13) == 0) || (strnicmp(str, "canceldbgoutput", 15) == 0) || (stricmp(str, "clrmpduser") == 0) || (stricmp(str, "enablempduser") == 0) || (stricmp(str, "disablempduser") == 0)) { if (WriteString(sock, str) == SOCKET_ERROR) { error = WSAGetLastError(); printf("writing '%s' failed, %d\n", str, error); Translate_Error(error, str); printf("%s\n", str); fflush(stdout); break; } if (ReadStringTimeout(sock, str, MPD_DEFAULT_TIMEOUT)) { printf("%s\n", str);fflush(stdout); } else { printf("timeout waiting for result to return.\n");fflush(stdout); } } else if (strnicmp(str, "launch ", 7) == 0) { char pszPassword[100]; if (GetStringOpt(str, "p", pszPassword)) { char pszStrTemp[300] = ""; char *pszEncoded, *pStr; unsigned int i; pszEncoded = EncodePassword(pszPassword); if (pszEncoded != NULL) { _snprintf(pszStrTemp, 300, " p=%s", pszEncoded); free(pszEncoded); } /* erase the original password */ pStr = strstr(str, "p="); pStr = strstr(pStr, pszPassword); for (i=0; i<strlen(pszPassword); i++) pStr[i] = ' '; while (*pStr != '=') pStr--; *pStr = ' '; while (*pStr != 'p') pStr--; *pStr = ' '; /* append the encoded password */ strcat(str, pszStrTemp); } if (WriteString(sock, str) == SOCKET_ERROR) { error = WSAGetLastError(); printf("writing '%s' failed, %d\n", str, error); Translate_Error(error, str); printf("%s\n", str); fflush(stdout); break; } if (ReadStringTimeout(sock, str, MPD_DEFAULT_TIMEOUT)) { printf("%s\n", str);fflush(stdout); } else { printf("timeout waiting for result to return.\n");fflush(stdout); } } else if ((strnicmp(str, "setmpduser ", 11) == 0) || (stricmp(str, "setmpduser") == 0)) { // get the account char pszAccount[100]; if (!GetStringOpt(str, "a", pszAccount)) { printf("account: "); fflush(stdout); gets(pszAccount); } // get the password char pszPassword[100]; if (!GetStringOpt(str, "p", pszPassword)) { char ch; int index; printf("password: "); fflush(stdout); ch = getch(); index = 0; while (ch != 13)//'\r') { pszPassword[index] = ch; index++; ch = getch(); } pszPassword[index] = '\0'; printf("\n"); } // encode the password char pszStrTemp[300] = ""; char *pszEncoded; pszEncoded = EncodePassword(pszPassword); if (pszEncoded != NULL) { // create the command sprintf(str, "setmpduser a=%s p=%s", pszAccount, pszEncoded); free(pszEncoded); if (WriteString(sock, str) == SOCKET_ERROR) { error = WSAGetLastError(); printf("writing '%s' failed, %d\n", str, error); Translate_Error(error, str); printf("%s\n", str); fflush(stdout); break; } if (ReadStringTimeout(sock, str, MPD_DEFAULT_TIMEOUT)) { printf("%s\n", str);fflush(stdout); } else { printf("timeout waiting for result to return.\n");fflush(stdout); } } else { printf("FAIL - unable to encode the password for transmission.\n"); } } else if (strnicmp(str, "validate ", 9) == 0) { char pszPassword[100]; if (GetStringOpt(str, "p", pszPassword)) { char pszStrTemp[300] = ""; char *pszEncoded, *pStr; unsigned int i; pszEncoded = EncodePassword(pszPassword); if (pszEncoded != NULL) { _snprintf(pszStrTemp, 300, " p=%s", pszEncoded); free(pszEncoded); } /* erase the original password */ pStr = strstr(str, "p="); pStr = strstr(pStr, pszPassword); for (i=0; i<strlen(pszPassword); i++) pStr[i] = ' '; while (*pStr != '=') pStr--; *pStr = ' '; while (*pStr != 'p') pStr--; *pStr = ' '; /* append the encoded password */ strcat(str, pszStrTemp); } if (WriteString(sock, str) == SOCKET_ERROR) { error = WSAGetLastError(); printf("writing '%s' failed, %d\n", str, error); Translate_Error(error, str); printf("%s\n", str); fflush(stdout); break; } if (ReadStringTimeout(sock, str, MPD_DEFAULT_TIMEOUT)) { printf("%s\n", str);fflush(stdout); } else { printf("timeout waiting for result to return.\n");fflush(stdout); } } else if (strnicmp(str, "barrier ", 8) == 0) { if (WriteString(sock, str) == SOCKET_ERROR) { error = WSAGetLastError(); printf("writing '%s' failed, %d\n", str, error); Translate_Error(error, str); printf("%s\n", str); fflush(stdout); break; } if (ReadString(sock, str)) { printf("%s\n", str);fflush(stdout); } else { printf("error waiting for result to return.\n");fflush(stdout); } } else if (stricmp(str, "hosts") == 0) { if (WriteString(sock, str) == SOCKET_ERROR) { error = WSAGetLastError(); Translate_Error(error, str); printf("writing hosts request failed, %d\n%s\n", error, str);fflush(stdout); break; } if (ReadStringTimeout(sock, str, MPD_DEFAULT_TIMEOUT)) { char *p = strstr(str, "result="); if (p != NULL) { printf("%s\n", &p[7]);fflush(stdout); } else { printf("%s\n", str);fflush(stdout); } } else { printf("timeout waiting for result to return\n");fflush(stdout); } } else if (strnicmp(str, "next ", 5) == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -