File size: 5,955 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const io = require('socket.io-client')

const SERVER_URL = 'http://localhost:8082'
const VALID_TOKEN = 'U2FsdGVkX1+26QCTo4JlGxKnQFcDjOxHyZthx/zucoUMh4CVGc3d4DdBPG2LK52gLuO5RRtmnclYoZESSyQrP7RDoJxh4PLkF6Dm7m3PiO112i1DWxp/xANsRQBzotJ1soPa1ym82BNQRNoziZHbG2RmC/wM953x1zEBgK0wD6LW0I9cQMNIaiO+TV8f/qYt9m4QEoG3UBnLWCN5+oHU0DTOMJQgXBl7zFVwj/9YENt0rN5tBLZSSZv6krM94lmTO7V/mXycUqa/fWJuz7+B0ThlxFNFwzHdizGB+iqTHLoFrDL9Nw0d4oHnBOEkzSmwjIzNysVttfESpjAjNZ6gfmpV7+qERGxt9VHTogWP2cIaT5Dqjkc1ldG63yfYzXCx'
const VALID_SESSION = 'b6374f59-06d3-44a1-8da5-c1ace74cd0bc'

// 所有需要测试的WebSocket路径
const SOCKET_PATHS = [
  '/terminal',
  '/sftp-v2',
  '/docker',
  '/onekey',
  '/server-status',
  '/file-transfer'
]

// 测试用例
const testCases = [
  {
    name: '无Token连接',
    config: {
      auth: {},
      extraHeaders: { cookie: `session=${ VALID_SESSION }` }
    },
    expectedError: 'No Token'
  },
  {
    name: '无Cookie连接',
    config: {
      auth: { token: VALID_TOKEN },
      extraHeaders: {}
    },
    expectedError: 'No Cookie'
  },
  {
    name: '无Session连接',
    config: {
      auth: { token: VALID_TOKEN },
      extraHeaders: { cookie: 'other=value' }
    },
    expectedError: 'No Session Cookie'
  },
  {
    name: '伪造Token连接',
    config: {
      auth: { token: 'fake-invalid-token-12345' },
      extraHeaders: { cookie: `session=${ VALID_SESSION }` }
    },
    expectedError: 'Authentication Failed'
  },
  {
    name: '正常连接',
    config: {
      auth: { token: VALID_TOKEN },
      extraHeaders: { cookie: `session=${ VALID_SESSION }` }
    },
    expectedError: null // 预期成功
  }
]

async function runTest(testCase, path) {
  return new Promise((resolve) => {
    const socket = io(SERVER_URL, {
      path,
      ...testCase.config,
      transports: ['websocket'],
      reconnection: false
    })

    const timeout = setTimeout(() => {
      socket.disconnect()
      console.log('    ⏱️  超时(5秒)')
      resolve({ passed: false, error: 'timeout' })
    }, 5000)

    socket.on('connect', () => {
      clearTimeout(timeout)
      if (testCase.expectedError) {
        console.log(`    ❌ 预期失败但连接成功 (Socket ID: ${ socket.id })`)
        socket.disconnect()
        resolve({ passed: false, error: 'unexpected_success' })
      } else {
        console.log(`    ✅ 连接成功 (Socket ID: ${ socket.id })`)
        socket.disconnect()
        resolve({ passed: true })
      }
    })

    socket.on('connect_error', (error) => {
      clearTimeout(timeout)
      const errorMsg = error.message

      if (testCase.expectedError) {
        const passed = errorMsg.includes(testCase.expectedError)
        if (passed) {
          console.log(`    ✅ 预期错误: ${ errorMsg }`)
        } else {
          console.log('    ❌ 错误不匹配')
          console.log(`       实际: ${ errorMsg }`)
          console.log(`       预期: ${ testCase.expectedError }`)
        }
        resolve({ passed, error: errorMsg })
      } else {
        console.log(`    ❌ 不应该失败: ${ errorMsg }`)
        resolve({ passed: false, error: errorMsg })
      }
    })
  })
}

async function runAllTests() {
  console.log('========================================')
  console.log('WebSocket 全面权限测试')
  console.log('========================================')
  console.log(`服务器地址: ${ SERVER_URL }`)
  console.log(`Session: ${ VALID_SESSION.substring(0, 20) }...`)
  console.log(`Token: ${ VALID_TOKEN.substring(0, 20) }...`)
  console.log('========================================\n')

  const results = {}
  let totalTests = 0
  let totalPassed = 0
  let totalFailed = 0

  for (const path of SOCKET_PATHS) {
    console.log(`\n${ '='.repeat(60) }`)
    console.log(`测试路径: ${ path }`)
    console.log('='.repeat(60))

    results[path] = { passed: 0, failed: 0, tests: [] }

    for (const testCase of testCases) {
      console.log(`\n  测试: ${ testCase.name }`)
      const result = await runTest(testCase, path)

      totalTests++
      results[path].tests.push({
        name: testCase.name,
        ...result
      })

      if (result.passed) {
        results[path].passed++
        totalPassed++
      } else {
        results[path].failed++
        totalFailed++
      }

      // 延迟避免请求过快
      await new Promise(resolve => setTimeout(resolve, 500))
    }

    console.log(`\n  路径 ${ path } 总结: ${ results[path].passed }/${ testCases.length } 通过`)
  }

  // 打印总结
  console.log('\n\n' + '='.repeat(60))
  console.log('测试总结')
  console.log('='.repeat(60))

  console.log('\n各路径测试结果:')
  for (const path of SOCKET_PATHS) {
    const pathResult = results[path]
    const status = pathResult.failed === 0 ? '✅' : '❌'
    console.log(`  ${ status } ${ path }: ${ pathResult.passed }/${ testCases.length } 通过`)
  }

  console.log('\n整体统计:')
  console.log(`  总计测试: ${ totalTests }`)
  console.log(`  通过: ${ totalPassed } ✅`)
  console.log(`  失败: ${ totalFailed } ❌`)
  console.log(`  成功率: ${ ((totalPassed / totalTests) * 100).toFixed(2) }%`)

  console.log('\n' + '='.repeat(60))
  console.log(totalFailed === 0 ? '✅ 所有测试通过!' : '❌ 部分测试失败')
  console.log('='.repeat(60))

  process.exit(totalFailed > 0 ? 1 : 0)
}

// 检查配置
if (VALID_TOKEN === '你的有效token' || VALID_SESSION === '你的有效session') {
  console.error('❌ 错误:请先配置有效的 TOKEN 和 SESSION')
  console.error('\n获取方法:')
  console.error('1. 登录到系统')
  console.error('2. 打开浏览器开发者工具 (F12)')
  console.error('3. Session: Application → Cookies → session')
  console.error('4. Token: Console 中执行 localStorage.getItem("token")')
  console.error('\n然后修改本文件顶部的 VALID_TOKEN 和 VALID_SESSION')
  process.exit(1)
}

runAllTests()