root.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. function auth(callback) {
  2. $(".main").fadeOut(500);
  3. setTimeout(() => {
  4. $(".auth").hide().fadeIn(500, () => {
  5. $("#tg_icon").html("");
  6. bodymovin.loadAnimation({
  7. container: document.getElementById("tg_icon"),
  8. renderer: "canvas",
  9. loop: true,
  10. autoplay: true,
  11. path: "https://raw.githubusercontent.com/hikariatama/Hikka/master/assets/noface.json",
  12. rendererSettings: {
  13. clearCanvas: true,
  14. }
  15. });
  16. });
  17. fetch("/web_auth", {
  18. method: "POST",
  19. credentials: "include",
  20. timeout: 300000
  21. })
  22. .then(response => response.text())
  23. .then((response) => {
  24. if (response == "TIMEOUT") {
  25. error_message("Code waiting timeout exceeded. Reload page and try again.");
  26. $(".auth").fadeOut(500);
  27. return
  28. }
  29. if (response.startsWith("hikka_")) {
  30. $.cookie("session", response)
  31. auth_required = false;
  32. $(".authorized").hide().fadeIn(100);
  33. $(".auth").fadeOut(500, () => {
  34. $(".installation").fadeIn(500);
  35. });
  36. callback();
  37. return;
  38. }
  39. })
  40. }, 500);
  41. }
  42. $("#get_started")
  43. .click(() => {
  44. if (auth_required) return auth(() => {
  45. $("#get_started").click();
  46. });
  47. $("#enter_api").fadeOut(500);
  48. $("#get_started").fadeOut(500, () => {
  49. $("#continue_btn").hide().fadeIn(500);
  50. switch_block(_current_block);
  51. });
  52. });
  53. $("#enter_api")
  54. .click(() => {
  55. if (auth_required) return auth(() => {
  56. $("#enter_api").click();
  57. });
  58. $("#get_started").fadeOut(500);
  59. $("#enter_api")
  60. .fadeOut(500, () => {
  61. $("#continue_btn")
  62. .hide()
  63. .fadeIn(500);
  64. switch_block("api_id");
  65. });
  66. });
  67. function isInt(value) {
  68. var x = parseFloat(value);
  69. return !isNaN(value) && (x | 0) === x;
  70. }
  71. function isValidPhone(p) {
  72. var phoneRe = /^[+]?\d{11,13}$/;
  73. return phoneRe.test(p);
  74. }
  75. function finish_login() {
  76. fetch("/finishLogin", {
  77. method: "POST",
  78. credentials: "include"
  79. })
  80. .then(() => {
  81. $(".installation").fadeOut(2000);
  82. setTimeout(() => {
  83. $("#installation_icon").html("");
  84. bodymovin.loadAnimation({
  85. container: document.getElementById("installation_icon"),
  86. renderer: "canvas",
  87. loop: true,
  88. autoplay: true,
  89. path: "https://assets1.lottiefiles.com/animated_stickers/lf_tgs_j7miwfxd.json",
  90. rendererSettings: {
  91. clearCanvas: true,
  92. }
  93. });
  94. $(".finish_block").fadeIn(300);
  95. }, 2000);
  96. })
  97. .catch((err) => {
  98. error_state();
  99. error_message("Login confirmation error: " + err.toString());
  100. });
  101. }
  102. function tg_code() {
  103. fetch("/tgCode", {
  104. method: "POST",
  105. body: `${_tg_pass}\n${_phone}\n${_2fa_pass}`
  106. })
  107. .then((response) => {
  108. if (!response.ok) {
  109. if (response.status == 401) {
  110. $(".auth-code-form").hide().fadeIn(300, () => {
  111. $("#monkey-close").html("");
  112. anim = bodymovin.loadAnimation({
  113. container: document.getElementById("monkey-close"),
  114. renderer: "canvas",
  115. loop: false,
  116. autoplay: true,
  117. path: "https://static.hikari.gay/monkey-close.json",
  118. rendererSettings: {
  119. clearCanvas: true,
  120. }
  121. });
  122. anim.addEventListener("complete", () => {
  123. setTimeout(() => {
  124. anim.goToAndPlay(0);
  125. }, 2000);
  126. })
  127. });
  128. $(".code-input").removeAttr("disabled");
  129. $(".code-input").attr("inputmode", "text");
  130. if($(".enter").hasClass("tgcode"))
  131. $(".enter").removeClass("tgcode");
  132. $(".code-caption").html("Enter your Telegram 2FA password, then press <span style='color: #dc137b;'>Enter</span>");
  133. cnt_btn.setAttribute("current-step", "2fa");
  134. $("#monkey").hide();
  135. $("#monkey-close").hide().fadeIn(100);
  136. _current_block = "2fa";
  137. } else {
  138. $(".code-input").removeAttr("disabled");
  139. response.text().then((text) => {
  140. error_state();
  141. Swal.fire(
  142. "Error",
  143. text,
  144. "error"
  145. );
  146. });
  147. }
  148. } else {
  149. $(".auth-code-form").fadeOut();
  150. switch_block("custom_bot");
  151. }
  152. })
  153. .catch(error => {
  154. Swal.showValidationMessage(
  155. `Auth failed: ${error.toString()}`
  156. )
  157. })
  158. }
  159. function switch_block(block) {
  160. cnt_btn.setAttribute("current-step", block);
  161. try {
  162. $(`#block_${_current_block}`)
  163. .fadeOut(() => {
  164. $(`#block_${block}`)
  165. .hide()
  166. .fadeIn();
  167. });
  168. } catch {
  169. $(`#block_${block}`)
  170. .hide()
  171. .fadeIn();
  172. }
  173. _current_block = block;
  174. }
  175. function error_message(message) {
  176. Swal.fire({
  177. "icon": "error",
  178. "title": message
  179. });
  180. }
  181. function error_state() {
  182. $("body").addClass("red_state");
  183. cnt_btn.disabled = true;
  184. setTimeout(() => {
  185. cnt_btn.disabled = false;
  186. $("body").removeClass("red_state");
  187. }, 1000);
  188. }
  189. var _api_id = "",
  190. _api_hash = "",
  191. _phone = "",
  192. _2fa_pass = "",
  193. _tg_pass = "",
  194. _current_block = skip_creds ? "phone" : "api_id";
  195. const cnt_btn = document.querySelector("#continue_btn");
  196. function process_next() {
  197. let step = cnt_btn.getAttribute("current-step");
  198. if (step == "api_id") {
  199. let api_id = document.querySelector("#api_id").value;
  200. if (api_id.length < 4 || !isInt(api_id)) {
  201. error_state();
  202. return;
  203. }
  204. _api_id = parseInt(api_id, 10);
  205. switch_block("api_hash");
  206. return;
  207. }
  208. if (step == "api_hash") {
  209. let api_hash = document.querySelector("#api_hash").value;
  210. if (api_hash.length != 32) {
  211. error_state();
  212. return;
  213. }
  214. _api_hash = api_hash;
  215. fetch("/setApi", {
  216. method: "PUT",
  217. body: _api_hash + _api_id,
  218. credentials: "include"
  219. })
  220. .then(response => response.text())
  221. .then((response) => {
  222. if (response != "ok") {
  223. error_state();
  224. error_message(response)
  225. } else {
  226. switch_block("phone");
  227. }
  228. })
  229. .catch((err) => {
  230. error_state();
  231. error_message("Error occured while saving credentials: " + err.toString());
  232. });
  233. return;
  234. }
  235. if (step == "phone") {
  236. let phone = document.querySelector("#phone").value;
  237. if (!isValidPhone(phone)) {
  238. error_state();
  239. return;
  240. }
  241. _phone = phone;
  242. fetch("/sendTgCode", {
  243. method: "POST",
  244. body: _phone,
  245. credentials: "include"
  246. })
  247. .then((response) => {
  248. if (!response.ok) {
  249. response.text().then((text) => {
  250. error_state();
  251. error_message(text);
  252. });
  253. } else {
  254. $(".auth-code-form").hide().fadeIn(300, () => {
  255. $("#monkey").html("");
  256. anim2 = bodymovin.loadAnimation({
  257. container: document.getElementById("monkey"),
  258. renderer: "canvas",
  259. loop: false,
  260. autoplay: true,
  261. path: "https://static.hikari.gay/monkey.json",
  262. rendererSettings: {
  263. clearCanvas: true,
  264. }
  265. });
  266. anim2.addEventListener("complete", () => {
  267. setTimeout(() => {
  268. anim2.goToAndPlay(0);
  269. }, 2000);
  270. })
  271. });
  272. $(".code-input").removeAttr("disabled");
  273. $(".enter").addClass("tgcode");
  274. $(".code-caption").text("Enter the code you recieved in Telegram");
  275. cnt_btn.setAttribute("current-step", "code");
  276. _current_block = "code";
  277. }
  278. })
  279. .catch((err) => {
  280. error_state();
  281. error_message("Code send failed: " + err.toString());
  282. });
  283. }
  284. if (step == "2fa") {
  285. let _2fa = document.querySelector("#_2fa").value;
  286. _2fa_pass = _2fa;
  287. tg_code();
  288. return
  289. }
  290. if (step == "custom_bot") {
  291. let custom_bot = document.querySelector("#custom_bot").value;
  292. if (custom_bot != "" && (!custom_bot.toLowerCase().endsWith("bot") || custom_bot.length < 5)) {
  293. Swal.fire({
  294. "icon": "error",
  295. "title": "Bot username invalid",
  296. "text": "It must end with `bot` and be at least 5 symbols in length"
  297. })
  298. return
  299. }
  300. if (custom_bot == "") {
  301. finish_login();
  302. return
  303. }
  304. fetch("/custom_bot", {
  305. method: "POST",
  306. credentials: "include",
  307. body: custom_bot
  308. })
  309. .then(response => response.text())
  310. .then((response) => {
  311. if (response == "OCCUPIED") {
  312. Swal.fire({
  313. "icon": "error",
  314. "title": "This bot username is already occupied!"
  315. })
  316. return;
  317. }
  318. finish_login();
  319. })
  320. .catch((err) => {
  321. error_state();
  322. error_message("Custom bot setting error: " + err.toString());
  323. });
  324. return
  325. }
  326. }
  327. cnt_btn.onclick = () => {
  328. if (cnt_btn.disabled) return;
  329. if (auth_required) return auth(() => {
  330. cnt_btn.click();
  331. });
  332. process_next();
  333. }
  334. $(".installation input").on("keyup", (e) => {
  335. if (cnt_btn.disabled) return;
  336. if (auth_required) return auth(() => {
  337. cnt_btn.click();
  338. });
  339. if (e.key === "Enter" || e.keyCode === 13) {
  340. process_next();
  341. }
  342. });
  343. $(".code-input").on("keyup", (e) => {
  344. if (_current_block == "code" && $(".code-input").val().length == 5) {
  345. _tg_pass = $(".code-input").val();
  346. $(".code-input").attr("disabled", "true");
  347. $(".code-input").val("");
  348. tg_code();
  349. } else if (_current_block == "2fa" && (e.key === "Enter" || e.keyCode === 13)) {
  350. let _2fa = $(".code-input").val();
  351. _2fa_pass = _2fa;
  352. $(".code-input").attr("disabled", "true");
  353. $(".code-input").val("");
  354. tg_code();
  355. }
  356. });
  357. $(".enter").on("click", () => {
  358. if (_current_block == "2fa") {
  359. let _2fa = $(".code-input").val();
  360. _2fa_pass = _2fa;
  361. $(".code-input").attr("disabled", "true");
  362. $(".code-input").val("");
  363. tg_code();
  364. }
  365. });