xray-api.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import axios from "axios"
  2. import qs from "qs"
  3. import fs from "fs"
  4. import path from "path"
  5. import { v4 as uuidv4 } from "uuid"
  6. import randomstring from "randomstring"
  7. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
  8. export class XrayApi {
  9. constructor(url, username, password) {
  10. this.url = url
  11. this.username = username
  12. this.password = password
  13. this.api = axios.create({
  14. baseURL: url,
  15. withCredentials: true
  16. })
  17. }
  18. async login() {
  19. const { data, headers } = await this.api.post(
  20. "login",
  21. qs.stringify({
  22. username: this.username,
  23. password: this.password
  24. })
  25. )
  26. if (headers["set-cookie"] instanceof Array) {
  27. this.api.defaults.headers.Cookie = headers["set-cookie"][1]
  28. } else {
  29. this.api.defaults.headers.Cookie = headers["set-cookie"]
  30. }
  31. }
  32. async status() {
  33. const { data } = await this.api.post("server/status")
  34. console.log(data)
  35. }
  36. async update() {
  37. try {
  38. const { data } = await this.api.post(
  39. "/panel/xray/update",
  40. qs.stringify({
  41. xraySetting: fs.readFileSync(
  42. path.resolve("dist", "xray_edited.json")
  43. )
  44. })
  45. )
  46. console.log(data)
  47. if (!data.success) {
  48. throw new Error(data.message)
  49. }
  50. } catch (e) {
  51. console.log(e.response.status)
  52. throw new Error(e.response.data)
  53. }
  54. }
  55. async restartXray() {
  56. const { data } = await this.api.post("server/restartXrayService")
  57. console.log(data)
  58. }
  59. async add(name, users) {
  60. const list = await this.list()
  61. const item = list.find(item => item.remark === name)
  62. if (item) {
  63. console.log(`Inbound ${name} already exists`)
  64. await this.del(item.id)
  65. console.log(`Inbound ${name} deleted`)
  66. }
  67. const shortId = randomstring.generate({
  68. length: 8,
  69. charset: "hex"
  70. })
  71. const publicKey = "6jZhBr6PvJDFaU8o0WMeJBzLlwmr-D3hScvT8iM7qW0"
  72. const privateKey = "yGN50gBt8fnx4GLyEnpD1GTjXel5rII7nSVcclC1QzQ"
  73. const port = Math.floor(Math.random() * 10000) + 20000
  74. const clients = users.map(user => {
  75. return {
  76. id: uuidv4(),
  77. flow: "",
  78. email: user,
  79. limitIp: 0,
  80. totalGB: 0,
  81. expiryTime: 0,
  82. enable: true,
  83. tgId: "",
  84. subId: randomstring.generate(16),
  85. reset: 0
  86. }
  87. })
  88. const { data } = await this.api.post(
  89. "panel/inbound/add",
  90. qs.stringify({
  91. up: 0,
  92. down: 0,
  93. total: 0,
  94. remark: name,
  95. enable: true,
  96. expiryTime: 0,
  97. listen: "",
  98. port,
  99. protocol: "vless",
  100. settings: JSON.stringify({
  101. clients,
  102. decryption: "none",
  103. fallbacks: []
  104. }),
  105. streamSettings: JSON.stringify({
  106. network: "tcp",
  107. security: "reality",
  108. externalProxy: [],
  109. realitySettings: {
  110. show: false,
  111. xver: 0,
  112. dest: "yahoo.com:443",
  113. serverNames: ["yahoo.com", "www.yahoo.com"],
  114. privateKey,
  115. minClient: "",
  116. maxClient: "",
  117. maxTimediff: 0,
  118. shortIds: [shortId],
  119. settings: {
  120. publicKey,
  121. fingerprint: "firefox",
  122. serverName: "",
  123. spiderX: "/"
  124. }
  125. },
  126. tcpSettings: {
  127. acceptProxyProtocol: false,
  128. header: {
  129. type: "none"
  130. }
  131. }
  132. }),
  133. sniffing: JSON.stringify({
  134. enabled: true,
  135. destOverride: ["http", "tls", "quic", "fakedns"],
  136. metadataOnly: false,
  137. routeOnly: false
  138. })
  139. })
  140. )
  141. console.log(data)
  142. if (!data.success) {
  143. throw new Error(data.msg)
  144. }
  145. const server = new URL(this.api.defaults.baseURL).hostname
  146. return clients.map(client => {
  147. return {
  148. name: client.email,
  149. config: `{name: ${client.email}, server: ${server}, port: ${port}, reality-opts: {public-key: ${publicKey}, short-id: ${shortId}}, client-fingerprint: chrome, type: vless, uuid: ${client.id}, tls: true, tfo: false, skip-cert-verify: false, servername: yahoo.com, network: tcp}`
  150. }
  151. })
  152. }
  153. async del(id) {
  154. const { data } = await this.api.post(`panel/inbound/del/${id}`)
  155. console.log(data)
  156. }
  157. async list() {
  158. const { data } = await this.api.post("panel/inbound/list")
  159. return data.obj
  160. }
  161. }