httpProxy.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * HTTP代理管理路由
  3. */
  4. const express = require('express');
  5. const router = express.Router();
  6. const logger = require('../logger');
  7. const { requireLogin } = require('../middleware/auth');
  8. const httpProxyService = require('../services/httpProxyService');
  9. // 获取代理状态
  10. router.get('/proxy/status', requireLogin, async (req, res) => {
  11. try {
  12. const status = httpProxyService.getStatus();
  13. res.json(status);
  14. } catch (error) {
  15. logger.error('获取代理状态失败:', error);
  16. res.status(500).json({
  17. error: '获取代理状态失败',
  18. details: error.message
  19. });
  20. }
  21. });
  22. // 启动代理服务
  23. router.post('/proxy/start', requireLogin, async (req, res) => {
  24. try {
  25. const config = req.body;
  26. await httpProxyService.start(config);
  27. res.json({
  28. success: true,
  29. message: '代理服务已启动',
  30. status: httpProxyService.getStatus()
  31. });
  32. } catch (error) {
  33. logger.error('启动代理服务失败:', error);
  34. res.status(500).json({
  35. error: '启动代理服务失败',
  36. details: error.message
  37. });
  38. }
  39. });
  40. // 停止代理服务
  41. router.post('/proxy/stop', requireLogin, async (req, res) => {
  42. try {
  43. await httpProxyService.stop();
  44. res.json({
  45. success: true,
  46. message: '代理服务已停止',
  47. status: httpProxyService.getStatus()
  48. });
  49. } catch (error) {
  50. logger.error('停止代理服务失败:', error);
  51. res.status(500).json({
  52. error: '停止代理服务失败',
  53. details: error.message
  54. });
  55. }
  56. });
  57. // 重启代理服务
  58. router.post('/proxy/restart', requireLogin, async (req, res) => {
  59. try {
  60. await httpProxyService.stop();
  61. await httpProxyService.start(req.body);
  62. res.json({
  63. success: true,
  64. message: '代理服务已重启',
  65. status: httpProxyService.getStatus()
  66. });
  67. } catch (error) {
  68. logger.error('重启代理服务失败:', error);
  69. res.status(500).json({
  70. error: '重启代理服务失败',
  71. details: error.message
  72. });
  73. }
  74. });
  75. // 更新代理配置
  76. router.put('/proxy/config', requireLogin, async (req, res) => {
  77. try {
  78. const config = req.body;
  79. // 验证配置
  80. if (config.port && (config.port < 1 || config.port > 65535)) {
  81. return res.status(400).json({ error: '端口号必须在1-65535之间' });
  82. }
  83. if (config.enableAuth && (!config.username || !config.password)) {
  84. return res.status(400).json({ error: '启用认证时必须提供用户名和密码' });
  85. }
  86. await httpProxyService.updateConfig(config);
  87. res.json({
  88. success: true,
  89. message: '代理配置已更新',
  90. status: httpProxyService.getStatus()
  91. });
  92. } catch (error) {
  93. logger.error('更新代理配置失败:', error);
  94. res.status(500).json({
  95. error: '更新代理配置失败',
  96. details: error.message
  97. });
  98. }
  99. });
  100. // 获取代理配置
  101. router.get('/proxy/config', requireLogin, async (req, res) => {
  102. try {
  103. const status = httpProxyService.getStatus();
  104. res.json({
  105. success: true,
  106. config: status.config
  107. });
  108. } catch (error) {
  109. logger.error('获取代理配置失败:', error);
  110. res.status(500).json({
  111. error: '获取代理配置失败',
  112. details: error.message
  113. });
  114. }
  115. });
  116. // 测试代理连接
  117. router.post('/proxy/test', requireLogin, async (req, res) => {
  118. try {
  119. const { testUrl = 'http://httpbin.org/ip' } = req.body;
  120. const axios = require('axios');
  121. const status = httpProxyService.getStatus();
  122. if (!status.isRunning) {
  123. return res.status(400).json({ error: '代理服务未运行' });
  124. }
  125. // 通过代理测试连接
  126. const proxyConfig = {
  127. host: status.config.host === '0.0.0.0' ? 'localhost' : status.config.host,
  128. port: status.config.port
  129. };
  130. const startTime = Date.now();
  131. const response = await axios.get(testUrl, {
  132. proxy: proxyConfig,
  133. timeout: 10000
  134. });
  135. const responseTime = Date.now() - startTime;
  136. res.json({
  137. success: true,
  138. message: '代理连接测试成功',
  139. testUrl,
  140. responseTime: `${responseTime}ms`,
  141. statusCode: response.status,
  142. proxyConfig
  143. });
  144. } catch (error) {
  145. logger.error('代理连接测试失败:', error);
  146. res.status(500).json({
  147. error: '代理连接测试失败',
  148. details: error.message
  149. });
  150. }
  151. });
  152. module.exports = router;