aboutsummaryrefslogtreecommitdiff
path: root/soyuz/library.cc
blob: 5777fe2de41e27122bcb13fb671f25b47554c746 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (C) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

#include <cstdio>
#include <fmt/format.h>
#include <fstream>
#include <memory>
#include <string>
#include <Windows.h>

#include "soyuz/library.hh"
#include "soyuz/soyuz.hh"
#include "soyuz/tray.hh"
#include "soyuz/windows.hh"

namespace soyuz {

std::ofstream log_file("soyuz.log");

static auto enum_windows_proc(HWND hwnd, LPARAM lparam) -> BOOL {
  int length = GetWindowTextLength(hwnd);
  auto title = new CHAR[length + 1];
  GetWindowText(hwnd, title, length);

  if (strstr(title, LUNAR_WINDOW_NAME_BASE)) {
    *((HWND *)lparam) = hwnd;

    delete[] title;

    return FALSE;
  }

  delete[] title;

  return TRUE;
}

auto find_lunar() -> DWORD {
  HWND window = nullptr;
  EnumWindows(enum_windows_proc, (LPARAM)&window);

  int length = GetWindowTextLength(window);
  auto title = new CHAR[length + 1];
  GetWindowText(window, title, length);

  DWORD pid;
  GetWindowThreadProcessId(window, &pid);
  log("hooked lunar client"); log("you may now close this window");
  return pid;
}

auto delete_handle(DWORD pid) -> int {
  HANDLE lunar = OpenProcess(
    PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE,
    FALSE,
    pid
  );
  if (!lunar) {
    soyuz::log(fmt::format("could not open handle to lunar client: {}", GetLastError()));

    return 1;
  }

  ULONG size = 1 << 10;
  std::unique_ptr<BYTE[]> buffer;
  for (;;) {
    buffer = std::make_unique<BYTE[]>(size);

    NTSTATUS status = NtQueryInformationProcess(
      lunar,
      ProcessHandleInformation,
      buffer.get(),
      size,
      &size
    );

    if (NT_SUCCESS(status)) { break; }
    if (status == STATUS_INFO_LENGTH_MISMATCH) { size += 1 << 10; continue; }

    soyuz::log("could not enumerate handle, skipping");

    return 1;
  }

  auto *info = reinterpret_cast<PROCESS_HANDLE_SNAPSHOT_INFORMATION *>(buffer.get());
  for (ULONG i = 0; i < info->NumberOfHandles; ++i) {
    HANDLE h = info->Handles[i].HandleValue;
    HANDLE target;
    if (!DuplicateHandle(
      lunar,
      h,
      GetCurrentProcess(),
      &target,
      0,
      FALSE,
      DUPLICATE_SAME_ACCESS
    )) { continue; }

    BYTE name_buffer[1 << 10];
    NTSTATUS status = NtQueryObject(
      target,
      ObjectNameInformation,
      name_buffer,
      sizeof(name_buffer),
      nullptr
    );
    CloseHandle(target);
    if (!NT_SUCCESS(status)) { continue; }

    WCHAR target_name[256];
    DWORD session_id;
    ProcessIdToSessionId(pid, &session_id);
    swprintf_s(target_name, DISCORD_IPC_NAMED_PIPE_NAME);
    size_t length = wcslen(target_name);

    auto *name = reinterpret_cast<UNICODE_STRING *>(name_buffer);
    if (name->Buffer && _wcsnicmp(name->Buffer, target_name, length) == 0) {
      soyuz::log("found lunar client's discord ipc named pipe");

      DuplicateHandle(
        lunar,
        h,
        GetCurrentProcess(),
        &target,
        0,
        FALSE,
        DUPLICATE_CLOSE_SOURCE
      );
      CloseHandle(target);

      soyuz::log("closed lunar client's discord ipc named pipe");

      return 0;
    }
  }

  return 0;
}

auto write_log_file(const std::string &message) -> void {
  log_file << message << std::endl;
}

auto init_log_file() -> void {
  if (!log_file.is_open()) {
    soyuz::log("could not open 'soyuz.log'");
    soyuz::log("proceeding without logging to file");

    return;
  }

  soyuz::log("opened 'soyuz.log'");
}

auto close_log_file() -> void {
  soyuz::log("closing 'soyuz.log'"); log_file.close();
}

auto exit(int exit_code) -> void {
  if (log_file.is_open()) { close_log_file(); }
  ::exit(exit_code);
}

auto current_date_time() -> std::string {
  time_t now = time(nullptr);
  struct tm t_struct{};
  char buffer[80];
  localtime_s(&t_struct, &now); // t_struct = *localtime(&now);
  strftime(buffer, sizeof(buffer), "%Y-%m-%d.%X", &t_struct);

  return buffer;
}

}