-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
89 lines (76 loc) 路 2.88 KB
/
Main.java
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
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
Properties config = loadConfig();
if (config == null) {
System.err.println("Failed to load config file.");
return;
}
int port = Integer.parseInt(config.getProperty("proxyPort"));
String targetAddress = config.getProperty("targetAddress");
int targetPort = Integer.parseInt(config.getProperty("targetPort"));
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Proxy server started on port " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(() -> handleClient(clientSocket, targetAddress, targetPort)).start();
}
} catch (IOException e) {
System.err.println("Error occurred: " + e.getMessage());
}
}
private static Properties loadConfig() {
Path configPath = Path.of("config.properties");
Properties config = new Properties();
try (InputStream is = Files.newInputStream(configPath)) {
config.load(is);
return config;
} catch (IOException e) {
System.err.println("Failed to load config file: " + e.getMessage());
return null;
}
}
private static void handleClient(Socket clientSocket, String targetAddress, int targetPort) {
System.out
.println("Ping, from " + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());
try (Socket targetSocket = new Socket(targetAddress, targetPort)) {
System.out.println("Incoming connection from " + clientSocket.getInetAddress().getHostAddress() + ":"
+ clientSocket.getPort());
Thread forwardThread = new Thread(() -> forwardData(clientSocket, targetSocket));
forwardThread.start();
forwardData(targetSocket, clientSocket);
forwardThread.join();
} catch (IOException | InterruptedException e) {
System.err.println("Error occurred during connection handling: " + e.getMessage());
} finally {
System.out.println(
"Closing, from " + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());
closeQuietly(clientSocket);
}
}
private static void forwardData(Socket source, Socket destination) {
byte[] buffer = new byte[8192];
int bytesRead;
try (InputStream input = source.getInputStream();
OutputStream output = destination.getOutputStream()) {
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
System.err.println("Error occurred during data forwarding: " + e.getMessage());
} finally {
closeQuietly(destination);
}
}
private static void closeQuietly(Closeable closeable) {
try {
closeable.close();
} catch (IOException e) {
System.err.println("Error occurred during closing: " + e.getMessage());
}
}
}