Skip to content

Commit 392a41e

Browse files
committed
add test for socks4
1 parent 9110250 commit 392a41e

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

run_tests.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env sh
22

3-
./test_proxy.sh &
4-
./test_proxy.sh username password &
3+
./test_proxy.sh 5 &
4+
./test_proxy.sh 5 username password &
5+
./test_proxy.sh 4 &
56

67
./gradlew test --info

src/main/kotlin/com/theevilroot/asyncsocket/Socks4CoroutineSocket.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.nio.channels.AsynchronousSocketChannel
1010
class Socks4CoroutineSocket(
1111
val socksIsa: InetSocketAddress,
1212
channel: AsynchronousSocketChannel,
13-
userId: String
13+
val userId: String
1414
) : CoroutineSocket(channel) {
1515

1616
lateinit var remoteIsa: InetSocketAddress
@@ -19,7 +19,9 @@ class Socks4CoroutineSocket(
1919
val message = byteArrayOf(0x04, 0x01,
2020
(isa.port shr 8).toByte(),
2121
(isa.port and 0xff).toByte(),
22-
*isa.address.address)
22+
*isa.address.address,
23+
*userId.toByteArray(),
24+
0x00.toByte())
2325
ByteBuffer.wrap(message).let {
2426
val count = super.write(it)
2527
if (count < message.size)
@@ -38,7 +40,7 @@ class Socks4CoroutineSocket(
3840
val bPort = ByteArray(2)
3941
response.position(2)
4042
response.get(bPort, 0, 2)
41-
response.get(bPort, 0, 4)
43+
response.get(addr, 0, 4)
4244
val port = (bPort[0].toUByte().toInt() shl 8) + bPort[1].toUByte().toInt()
4345
return InetSocketAddress(InetAddress.getByAddress(addr), port)
4446
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.theevilroot.asyncsocket
2+
3+
import kotlinx.coroutines.runBlocking
4+
import org.junit.Assert
5+
import org.junit.Before
6+
import org.junit.Test
7+
import java.net.InetSocketAddress
8+
import java.nio.ByteBuffer
9+
import java.nio.channels.AsynchronousSocketChannel
10+
11+
class Socks4Test {
12+
13+
lateinit var channel: AsynchronousSocketChannel
14+
15+
@Before
16+
fun initChannel() {
17+
channel = AsynchronousSocketChannel.open()
18+
}
19+
20+
@Test
21+
fun testSocks4Connect(): Unit = runBlocking {
22+
val socket = Socks4CoroutineSocket(InetSocketAddress("127.0.0.1", 1082), channel, "user")
23+
socket.init()
24+
socket.connect(InetSocketAddress("52.48.142.75", 80))
25+
26+
Assert.assertTrue(socket.isOpened)
27+
Assert.assertTrue(socket.isConnected)
28+
29+
val request = "GET /?format=json HTTP/1.1\r\nConnection: close\r\nHost: api.ipify.org\r\nAccept: */*\r\nUser-Agent: curl/1.1.1\r\n\r\n"
30+
Assert.assertEquals(request.length, socket.write(ByteBuffer.wrap(request.toByteArray())))
31+
32+
33+
// read some data, we don't need much
34+
val response = ByteBuffer.allocate(32)
35+
Assert.assertTrue(socket.read(response) > 0)
36+
37+
socket.close()
38+
}
39+
40+
}

src/test/kotlin/com/theevilroot/asyncsocket/SocksTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class SocksTest {
5050
fun testUserPassAuthIncorrect(): Unit = runBlocking {
5151
val credentials = "username" to "blah"
5252
val socket = SocksCoroutineSocket(InetSocketAddress("localhost", 1080), channel, credentials)
53-
Assert.assertThrows(SocksCoroutineSocket.SocksException::class.java) {
53+
Assert.assertThrows(SocksException::class.java) {
5454
runBlocking { socket.init() }
5555
}
5656
}

test_proxy.sh

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
#!/usr/bin/env sh
22

33
if [ $# -eq 0 ]
4+
then
5+
echo "usage: test_proxy [username password]"
6+
exit 1
7+
fi
8+
9+
if [ $1 = "4" ]
10+
then
11+
protocol=socks4
12+
port=1082
13+
elif [ $1 = "5" ]
14+
then
15+
protocol=socks5
16+
port=1081
17+
else
18+
echo "protocol $1 is invalid. use 5 or 4 instead"
19+
exit 1
20+
fi
21+
22+
23+
if [ $# -eq 1 ]
424
then
525
echo ':: using no auth. port: 1081'
6-
pproxy -l socks5://0.0.0.0:1081
26+
pproxy -l "$protocol://0.0.0.0:$port"
727
exit 0
828
fi
929

10-
if [ $# -eq 2 ]
30+
if [ $# -eq 3 ]
1131
then
1232
echo ':: using user/pass auth. port: 1080'
13-
pproxy -l "socks5://0.0.0.0:1080#$1:$2"
33+
port=1080
34+
pproxy -l "$protocol://0.0.0.0:$port#$1:$2"
1435
exit 0
15-
else
16-
echo "usage: test_proxy [username password]"
17-
exit 1
1836
fi
37+
38+

0 commit comments

Comments
 (0)