aboutsummaryrefslogtreecommitdiff
path: root/soyuz/library.cc
blob: 5d10f83eb71adc3db2e55cd9f3c9afc7a0e7f3ff (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (C) 2021-2023 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

#include <soyuz/library.hh>

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


#include <soyuz/soyuz.hh>
#include <soyuz/tray.hh>
#include <soyuz/windows.hh>

namespace soyuz {

fmt::ostream log_file = fmt::output_file("soyuz.log");

static auto CALLBACK 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;
  DWORD pid;

  EnumWindows(enum_windows_proc, (LPARAM)&window);

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

  GetWindowText(window, title, length);

  GetWindowThreadProcessId(window, &pid);

  return pid;
}

auto delete_handle(DWORD pid) -> int {
  HANDLE lunar =
      OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE, FALSE, pid);
  ULONG size = 1 << 10;
  std::unique_ptr<BYTE[]> buffer;

  if (!lunar) {
    soyuz::log(soyuz::log_level::LOG_LEVEL_WARN,
               fmt::format("could not open handle to lunar client: {}",
                           GetLastError()));

    return 1;
  }

  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(soyuz::log_level::LOG_LEVEL_DEBUG,
               "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;
    BYTE name_buffer[1 << 10];
    WCHAR target_name[256];
    DWORD session_id;

    if (!DuplicateHandle(lunar, h, GetCurrentProcess(), &target, 0, FALSE,
                         DUPLICATE_SAME_ACCESS)) {
      continue;
    }

    NTSTATUS status = NtQueryObject(target, ObjectNameInformation, name_buffer,
                                    sizeof(name_buffer), nullptr);

    CloseHandle(target);

    if (!NT_SUCCESS(status)) {
      continue;
    }

    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(soyuz::log_level::LOG_LEVEL_INFO,
                 "found lunar client's discord ipc named pipe");
      DuplicateHandle(lunar, h, GetCurrentProcess(), &target, 0, FALSE,
                      DUPLICATE_CLOSE_SOURCE);
      CloseHandle(target);
      soyuz::log(soyuz::log_level::LOG_LEVEL_INFO,
                 "closed lunar client's discord ipc named pipe");

      return 0;
    }
  }

  return 0;
}

auto write_log_file(const std::string &message) -> void {
  log_file.print("{}\n", message);
}

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(soyuz::log_level::LOG_LEVEL_DEBUG, "opened 'soyuz.log'");
}

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

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

  close_log_file();
  std::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;
}

auto log_t::to_colorref() const -> COLORREF {
  switch (this->level) {
  case LOG_LEVEL_TRACE: {
    return 0x00FF0000;
  } // blue
  case LOG_LEVEL_DEBUG: {
    return 0x0000FF00;
  } // green
  case LOG_LEVEL_INFO: {
    return 0x00000000;
  } // black
  case LOG_LEVEL_WARN: {
    return 0x000080FF;
  } // orange
  case LOG_LEVEL_ERROR: {
    return 0x000000FF;
  } // red
  default: {
    return 0x00000000;
  } // black
  }
}

} // namespace soyuz