MCPcopy
hub / github.com/electerm/electerm / startTrackingServer

Function startTrackingServer

test/unit-ci/session-ssh-password-first.spec.js:100–165  ·  view source on GitHub ↗

* Start an SSH server that tracks which auth methods are attempted. * @param {string} publicKey - The public key to accept for publickey auth * @returns {{ port: number, authAttempts: string[], close: () => Promise }}

(publicKey)

Source from the content-addressed store, hash-verified

98 * @returns {{ port: number, authAttempts: string[], close: () => Promise<void> }}
99 */
100async function startTrackingServer (publicKey) {
101 const clients = new Set()
102 const authAttempts = []
103
104 const server = new Server({
105 hostKeys: [HOST_KEY.private]
106 }, (client) => {
107 clients.add(client)
108 const cleanup = () => clients.delete(client)
109
110 client.on('close', cleanup)
111 client.on('end', cleanup)
112 client.on('authentication', (ctx) => {
113 authAttempts.push(ctx.method)
114
115 if (ctx.method === 'none') {
116 return ctx.reject(['publickey', 'password'])
117 }
118
119 // Accept password auth
120 if (ctx.method === 'password' && ctx.username === USERNAME && ctx.password === PASSWORD) {
121 return ctx.accept()
122 }
123
124 // Accept publickey auth
125 if (ctx.method === 'publickey' && ctx.username === USERNAME && matchesPublicKey(ctx, publicKey)) {
126 return ctx.accept()
127 }
128
129 ctx.reject(['publickey', 'password'])
130 })
131 client.on('ready', () => {
132 client.on('session', (accept) => {
133 const sshSession = accept()
134 sshSession.on('env', (accept) => accept())
135 sshSession.on('pty', (accept) => accept())
136 sshSession.on('shell', (accept) => {
137 const stream = accept()
138 stream.write('electerm ready\n')
139 })
140 })
141 })
142 })
143
144 server.listen(0, '127.0.0.1')
145 await once(server, 'listening')
146
147 return {
148 port: server.address().port,
149 authAttempts,
150 async close () {
151 for (const client of clients) {
152 client.end()
153 }
154 await new Promise((resolve, reject) => {
155 server.close((err) => {
156 if (err) {
157 reject(err)

Calls 5

matchesPublicKeyFunction · 0.70
onceFunction · 0.70
addMethod · 0.45
onMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected