Skip to content

Commit

Permalink
Merge pull request #3 from PondWader/fix-ci
Browse files Browse the repository at this point in the history
Fix CI
  • Loading branch information
PondWader authored May 16, 2024
2 parents ae5d625 + dd1a937 commit ef8c680
Show file tree
Hide file tree
Showing 5 changed files with 771 additions and 772 deletions.
23 changes: 11 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ name: CI

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
test:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]
node-version: [20.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install yarn
- run: npx yarn install
- run: npm test
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install yarn
- run: npx yarn install
- run: npm test
11 changes: 6 additions & 5 deletions src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Socks5Connection {
public destAddress?: string;
public destPort?: number;
public command?: keyof typeof Socks5ConnectionCommand;
private errorHandler = () => {};
private errorHandler = () => { };
public metadata: any = {};

constructor(server: Socks5Server, socket: Duplex) {
Expand Down Expand Up @@ -52,7 +52,7 @@ export class Socks5Connection {
const authMethods = await this.readBytes(authMethodsAmount);

const authMethodByteCode = this.server.authHandler ? 0x02 : 0x00;

if (!authMethods.includes(authMethodByteCode)) {
// The chosen authentication method is not available
this.socket.write(Buffer.from([
Expand Down Expand Up @@ -127,7 +127,7 @@ export class Socks5Connection {
const addrType = (await this.readBytes(1)).readUInt8();

let address = '';
switch(addrType) {
switch (addrType) {
case 1:
// IPv4
address = (await this.readBytes(4)).join('.');
Expand All @@ -153,7 +153,7 @@ export class Socks5Connection {
this.socket.destroy(); // No valid address type provided
return;
}

const port = (await this.readBytes(2)).readUInt16BE();

if (!this.server.supportedCommands.has(command)) {
Expand Down Expand Up @@ -184,7 +184,7 @@ export class Socks5Connection {
0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00
]));
]));
this.socket.destroy();
}

Expand All @@ -199,6 +199,7 @@ export class Socks5Connection {

this.server.connectionHandler(this as InitialisedSocks5Connection, (status) => {
if (Socks5ConnectionStatus[status] === undefined) throw new Error(`"${status}" is not a valid status.`);
console.log(`Sending ${status}`)

// We can just send 0x00 for bound address stuff
this.socket.write(Buffer.from([
Expand Down
5 changes: 3 additions & 2 deletions src/connectionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import net from "net";
export default function (connection: InitialisedSocks5Connection, sendStatus: (status: keyof typeof Socks5ConnectionStatus) => void) {
if (connection.command !== 'connect') return sendStatus('COMMAND_NOT_SUPPORTED');

connection.socket.on('error', () => {}); // Ignore errors
connection.socket.on('error', () => { }); // Ignore errors

const stream = net.createConnection({
host: connection.destAddress,
port: connection.destPort
});

let streamOpened = false;
stream.on('error', (err: Error & {code: string}) => {
stream.on('error', (err: Error & { code: string }) => {
if (!streamOpened) {
switch (err.code) {
case 'EINVAL':
case 'ENOENT':
case 'ENOTFOUND':
case 'ETIMEDOUT':
Expand Down
30 changes: 14 additions & 16 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function handleTestConnection(socks5Port, initialData, stagedData = []) {
host: '127.0.0.1',
port: socks5Port
})

let opened = false
socket.on('ready', () => opened = true)

Expand Down Expand Up @@ -37,13 +37,13 @@ function handleTestConnection(socks5Port, initialData, stagedData = []) {
}

async function httpTest(withAuth) {
const httpServer = await createHttpTestServer()
const httpServer = await createHttpTestServer()

const username = randomString(11)
const password = randomString(9)
const { server, port } = await createSocks5TestServer(withAuth ? {
auth: {
username,
username,
password
}
} : undefined)
Expand All @@ -52,12 +52,12 @@ async function httpTest(withAuth) {
portBuf.writeUInt16BE(httpServer.port)

try {
const expectedBytes = withAuth ?
const expectedBytes = withAuth ?
[
...socks5Buffers.server.serverChoice(2),
...socks5Buffers.server.authenticationRespose(0),
...socks5Buffers.server.connectionResponse(0, [1, 0, 0, 0, 0], [0, 0]),
...Buffer.from('HTTP/1.1 200 OK\r\nContent-Length: 13\r\nDate: \r\nConnection: close\r\n\r\nHello, world!')
...Buffer.from('HTTP/1.1 200 OK\r\nContent-Length: 13\r\nDate: \r\nConnection: close\r\n\r\nHello, world!')
] :
[
...socks5Buffers.server.serverChoice(0),
Expand All @@ -71,24 +71,22 @@ async function httpTest(withAuth) {
socks5Buffers.client.connectionRequest(0x01, 0x01, Buffer.from([127, 0, 0, 1]), portBuf)
]), [{
after: withAuth ? 13 : 11,
data: 'GET / HTTP/1.1\r\nConnection: close\r\n\r\n'
data: 'GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n'
}])]).toEqual(expectedBytes)
} catch(err) {
} catch (err) {
throw err
} finally {
server.close()
httpServer.server.close()
throw err
}

server.close()
httpServer.server.close()
}

test('HTTP request through socks5 proxy server', () => httpTest(false))
test('HTTP request through socks5 proxy server with user-pass authentication', () => httpTest(true))

test('Connection fails to invalid address', async () => {
const { server, port } = await createSocks5TestServer()

expect([...await handleTestConnection(port, Buffer.concat([
socks5Buffers.client.greeting([0x00]),
socks5Buffers.client.connectionRequest(0x01, 0x03, Buffer.from([7, ...'invalid.test']), Buffer.from([1, 1]))
Expand All @@ -107,7 +105,7 @@ test('Connection to user-password protected server with no auth fails', async ()
password: randomString(5)
}
})

expect([...await handleTestConnection(port, Buffer.concat([
socks5Buffers.client.greeting([0x00])
]))]).toEqual([
Expand All @@ -124,7 +122,7 @@ test('Connection to user-password protected server with invalid auth fails', asy
password: randomString(5)
}
})

expect([...await handleTestConnection(port, Buffer.concat([
socks5Buffers.client.greeting([0x02]),
socks5Buffers.client.authenticationRequest(randomString(6), randomString(6))
Expand All @@ -143,7 +141,7 @@ test('Connection to server with invalid auth option fails', async () => {
password: randomString(5)
}
})

expect([...await handleTestConnection(port, Buffer.concat([
socks5Buffers.client.greeting([0x99])
]))]).toEqual([
Expand All @@ -159,7 +157,7 @@ test('Connection ruleset validator works', async () => {
server.setRulesetValidator((conn) => {
return conn.destPort !== 25
})

expect([...await handleTestConnection(port, Buffer.concat([
socks5Buffers.client.greeting([0x00]),
socks5Buffers.client.connectionRequest(0x01, 0x03, Buffer.from([10, ...'google.com']), Buffer.from([0, 25]))
Expand Down
Loading

0 comments on commit ef8c680

Please sign in to comment.