Skip to content

Commit fdba5ae

Browse files
committed
last known working version
1 parent 73cd750 commit fdba5ae

File tree

84 files changed

+8321
-748
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+8321
-748
lines changed

src/main/java/sockslib/DoIt.java

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import sockslib.client.SSLSocks5;
44
import sockslib.client.Socks5;
5+
import sockslib.client.SocksProxy;
56
import sockslib.client.SocksSocket;
67
import sockslib.common.KeyStoreInfo;
78
import sockslib.common.SSLConfiguration;
9+
import sockslib.common.UsernamePasswordCredentials;
810

911
import java.io.BufferedReader;
1012
import java.io.IOException;
@@ -16,14 +18,86 @@
1618
public class DoIt {
1719
public static void main(String[] args)
1820
throws IOException {
19-
// Socket socket = test1(new Socks5(new InetSocketAddress("localhost", 1030)));
20-
Socket socket = test1(new SSLSocks5(new InetSocketAddress("localhost", 1030),
21-
new SSLConfiguration(null, new KeyStoreInfo("client-trust-keystore.jks", "123456", "JKS"))));
21+
Socket socketAnonymous = createSocketPlainAnonymous();
22+
Socket socketAuth = createSocketAuthenticated();
23+
Socket socketSSLAnonymous = createSocketSSL();
24+
Socket socketSSLAuth = createSocketSSLAuthenticated();
2225

23-
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
26+
if (socketAnonymous != null) {
27+
System.out.println("socketAnonymous: " + socketAnonymous);
28+
testSocket(socketAnonymous);
29+
}
30+
if (socketAuth != null) {
31+
System.out.println("socketAuth: " + socketAuth);
32+
testSocket(socketAuth);
33+
}
34+
if (socketSSLAnonymous != null) {
35+
System.out.println("socketSSLAnonymous: " + socketSSLAnonymous);
36+
testSocket(socketSSLAnonymous);
37+
}
38+
if (socketSSLAuth != null) {
39+
System.out.println("socketSSLAuth: " + socketSSLAuth);
40+
testSocket(socketSSLAuth);
41+
}
42+
}
43+
44+
private static Socket createSocketSSLAuthenticated() {
45+
try {
46+
SSLSocks5 socksProxySSLAuth = new SSLSocks5(new InetSocketAddress("localhost", 1030),
47+
new SSLConfiguration(null, new KeyStoreInfo("client-trust-keystore.jks", "123456", "JKS")));
48+
socksProxySSLAuth.setCredentials(new UsernamePasswordCredentials("PANCAKE", "letmein"));
49+
return new SocksSocket(socksProxySSLAuth, new InetSocketAddress("whois.internic.net", 43));
50+
} catch (Exception e) {
51+
System.out.println("socketSSLAuth failed");
52+
System.out.println(e.getMessage());
53+
}
54+
return null;
55+
}
56+
57+
private static Socket createSocketSSL() {
58+
try {
59+
SSLSocks5 socksProxySSLAnonymous = new SSLSocks5(new InetSocketAddress("localhost", 1030),
60+
new SSLConfiguration(null, new KeyStoreInfo("client-trust-keystore.jks", "123456", "JKS")));
61+
return new SocksSocket(socksProxySSLAnonymous, new InetSocketAddress("whois.internic.net", 43));
62+
} catch (Exception e) {
63+
System.out.println("socketSSLAnonymous failed");
64+
System.out.println(e.getMessage());
65+
}
66+
return null;
67+
}
68+
69+
private static Socket createSocketAuthenticated() {
70+
try {
71+
SocksProxy proxyAuth = new Socks5(new InetSocketAddress("localhost", 1080));
72+
proxyAuth.setCredentials(new UsernamePasswordCredentials("PANCAKE", "letmein"));
73+
Socket socketAuth = new SocksSocket(
74+
proxyAuth); // refactor to: new SocksSocket(proxy1, new InetSocketAddress("whois.internic.net", 43))
75+
socketAuth.connect(new InetSocketAddress("whois.internic.net", 43)); // refactor out (see line above)
76+
return socketAuth;
77+
} catch (Exception e) {
78+
System.out.println("socketAuth failed");
79+
System.out.println(e.getMessage());
80+
}
81+
return null;
82+
}
83+
84+
private static Socket createSocketPlainAnonymous() {
85+
try {
86+
Socks5 socksProxyAnonymous = new Socks5(new InetSocketAddress("localhost", 1030));
87+
return new SocksSocket(socksProxyAnonymous, new InetSocketAddress("whois.internic.net", 43));
88+
} catch (Exception e) {
89+
System.out.println("socketAnonymous failed");
90+
System.out.println(e.getMessage());
91+
}
92+
return null;
93+
}
94+
95+
private static void testSocket(Socket socketToTest)
96+
throws IOException {
97+
InputStreamReader isr = new InputStreamReader(socketToTest.getInputStream());
2498
BufferedReader in = new BufferedReader(isr);
2599

26-
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
100+
PrintWriter out = new PrintWriter(socketToTest.getOutputStream(), true);
27101
out.println("google.com");
28102

29103
String line;
@@ -32,8 +106,4 @@ public static void main(String[] args)
32106
}
33107
}
34108

35-
private static SocksSocket test1(Socks5 socks5)
36-
throws IOException {
37-
return new SocksSocket(socks5, new InetSocketAddress("whois.internic.net",43));
38-
}
39109
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package sockslib.client;
16+
17+
import sockslib.common.AddressType;
18+
import sockslib.utils.SocksUtil;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
import java.net.InetAddress;
23+
import java.net.InetSocketAddress;
24+
import java.net.SocketAddress;
25+
import java.net.UnknownHostException;
26+
27+
/**
28+
* The class <code>RequestCmdReplyMessage</code> represents the message that
29+
* sent by SOCKS server when client sends a command request.
30+
*
31+
* @author Youchao Feng
32+
* @version 1.0
33+
* @date Mar 23, 2015 5:55:06 PM
34+
*/
35+
public class CommandReplyMessage implements SocksMessage {
36+
37+
/**
38+
* Logger that subclasses also can use.
39+
*/
40+
protected Logger logger = LoggerFactory.getLogger(CommandReplyMessage.class);
41+
42+
/**
43+
* The bytes that received from SOCKS server.
44+
*/
45+
private byte[] replyBytes;
46+
47+
/**
48+
* Constructs an instance of {@link CommandReplyMessage} with an array of
49+
* bytes that received from SOCKS server.
50+
*
51+
* @param replyBytes The bytes that received from SOCKS server.
52+
*/
53+
public CommandReplyMessage(byte[] replyBytes) {
54+
this.replyBytes = replyBytes;
55+
}
56+
57+
/**
58+
* Returns <code>true</code> if the command request is success.
59+
*
60+
* @return If the command request is success, it will return
61+
* <code>true</code>.
62+
*/
63+
public boolean isSuccess() {
64+
if (replyBytes.length < 10) {
65+
return false;
66+
}
67+
return replyBytes[1] == 0;
68+
}
69+
70+
/**
71+
* Gets IP address from the bytes that sent by SOCKS server.
72+
*
73+
* @return IP address.
74+
* @throws UnknownHostException If the host is unknown.
75+
*/
76+
public InetAddress getIp() throws UnknownHostException {
77+
byte[] addressBytes = null;
78+
79+
if (replyBytes[3] == AddressType.IPV4) {
80+
addressBytes = new byte[4];
81+
} else if (replyBytes[3] == AddressType.IPV6) {
82+
addressBytes = new byte[16];
83+
}
84+
85+
System.arraycopy(replyBytes, 4, addressBytes, 0, addressBytes.length);
86+
return InetAddress.getByAddress(addressBytes);
87+
}
88+
89+
/**
90+
* Gets port from bytes that sent by SOCKS server.
91+
*
92+
* @return port.
93+
*/
94+
public int getPort() {
95+
96+
return SocksUtil.bytesToInt(replyBytes[replyBytes.length - 2], replyBytes[replyBytes.length
97+
- 1]);
98+
}
99+
100+
/**
101+
* Returns the bytes that sent by SOCKS server.
102+
*
103+
* @return The bytes that sent by SOCKS server.
104+
*/
105+
public byte[] getReplyBytes() {
106+
return replyBytes;
107+
}
108+
109+
/**
110+
* Sets reply bytes.
111+
*
112+
* @param replyBytes The bytes that sent by SOCKS server.
113+
*/
114+
public void setReplyBytes(byte[] replyBytes) {
115+
this.replyBytes = replyBytes;
116+
}
117+
118+
/**
119+
* Gets the socket address.
120+
*
121+
* @return Socket address.
122+
*/
123+
public SocketAddress getSocketAddress() {
124+
try {
125+
return new InetSocketAddress(getIp(), getPort());
126+
} catch (UnknownHostException e) {
127+
logger.error(e.getMessage(), e);
128+
}
129+
return null;
130+
}
131+
132+
}

0 commit comments

Comments
 (0)