-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpsServerMainThread.java
More file actions
180 lines (165 loc) · 8.63 KB
/
Copy pathHttpsServerMainThread.java
File metadata and controls
180 lines (165 loc) · 8.63 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import Engines.DDOS.Interface;
import LoadBalancer.Configs;
import Server.HttpListener;
import Server.Reqandres.Request.Request;
import Server.Reqandres.Senders.QuickSender;
import Server.Utils.*;
import Server.Utils.Configs.Perms;
import Server.Utils.Configs.SSLConfigs;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.regex.*;
public class HttpsServerMainThread{
private final String host;
public final boolean lb;
public HttpsServerMainThread(int port,String host,boolean lb){
this.lb = lb;
this.host = host;
new HTTPS();
new HTTP();
}
private class HTTP extends Thread{
public HTTP(){
this.start();
}
private class Redirect extends Thread{
private final Socket sock;
public Redirect(Socket s){
this.sock = s;
try{
if (Perms.isIPAllowed(s.getInetAddress().getHostAddress())){
if (Interface.checkIP(s.getInetAddress().getHostAddress(),host))
this.start();
else{
Logger.glog(s.getInetAddress().getHostAddress() + " request rejected due to DDOS protection." + " ; id = " + 0 , host);
s.getOutputStream().write(HTMLGen.genTooManyRequests(s.getInetAddress().getHostAddress()).getBytes());
s.getOutputStream().flush();
s.getOutputStream().close();
}
}else{
Logger.glog(s.getInetAddress().getHostAddress() + " request rejected due to ip ban." + " ; id = " + " ; id = " + 0 , host);
s.getOutputStream().write(HTMLGen.genIPBan(s.getInetAddress().getHostAddress()).getBytes());
s.getOutputStream().flush();
s.getOutputStream().close();
}
}catch (Exception ex){
Logger.logException(ex);
}
}
@Override
public void run(){
try{
BufferedReader bfr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String firstLine = bfr.readLine();
if (firstLine != null && firstLine.length() > 5){
Pattern pathPattern = Pattern.compile(" /[^ ]*");
Matcher pathMatcher = pathPattern.matcher(firstLine);
if (pathMatcher.find()){
String path = pathMatcher.group().trim();
String upHeader;
boolean hostFound = false;
boolean upFound = false;
String line = "";
while (!hostFound){
line = bfr.readLine();
if (line != null){
if (line.startsWith("Host"))
hostFound = true;
else if (line.startsWith("Upgrade-Insecure-Requests"))
upFound = true;
else if (line.length() < 5)
break;
}else
break;
}
if (hostFound){
String hostHeader = line.split(":")[1].trim();
if (hostHeader.equals(host)){
if (!upFound){
while (!upFound){
line = bfr.readLine();
if (line != null){
if (line.startsWith("Upgrade-Insecure-Requests"))
upFound = true;
else if (line.length() < 5)
break;
}else
break;
}
}
if (upFound){
upHeader = line.split(":")[1].trim();
if (upHeader.equals("1"))
this.red("https://" + host + path);
else{
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeBytes("HTTP/1.1 426 Upgrade Required\nServer: " + basicUtils.Zako + "\nDate: " + new java.util.Date().toString() + "\nConnection: upgrade\nUpgrade: TLS/1.3, HTTP/1.1\r\n\r\n");
}
}else
this.red("https://" + host + path);
}else {
Interface.addWarning(sock.getInetAddress().getHostAddress(),host);
new QuickSender(new Request(sock)).sendBadReq("\"Host\" header conflict.");
}
}else {
Interface.addWarning(sock.getInetAddress().getHostAddress(),host);
new QuickSender(new Request(sock)).sendBadReq("\"Host\" header not found.");
}
}else{
Interface.addWarning(sock.getInetAddress().getHostAddress(),host);
new QuickSender(new Request(sock)).sendBadReq("Path not found in first line.");
}
}
}catch(Exception ex){
Logger.logException(ex);
}
}
private void red(String path){
try{
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeBytes("HTTP/1.1 301 Moved Permanently\nServer: " + basicUtils.Zako + "\nDate: " + new java.util.Date().toString() + "\nLocation: " + URLDecoder.decode(path, StandardCharsets.UTF_8) + "\r\n\r\n");
}catch (Exception ex){
Logger.logException(ex);
}
}
}
@Override
public void run(){
try{
ServerSocket ss = new ServerSocket(80);
Logger.ilog("Http thread is running on port 80 for redirection ...");
System.out.println("Http thread is running on port 80 for redirection ...");
while (true) new Redirect(ss.accept());
}catch (Exception ex) {
Logger.logException(ex);
}
}
}
private class HTTPS extends Thread{
public HTTPS(){
this.start();
}
@Override
public void run () {
try {
System.setProperty("javax.net.ssl.keyStore",lb ? Configs.ssl.jks : SSLConfigs.getJKS(host));
System.setProperty("javax.net.ssl.keyStorePassword",lb ? Configs.ssl.pss : SSLConfigs.getPass(host));
SSLServerSocketFactory sslServerSocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketfactory.createServerSocket(443);
Logger.ilog("Https server thread is now running (jks : " + (lb ? Configs.ssl.jks : SSLConfigs.getJKS(host)) + " ,, jks pass : " + (lb ? Configs.ssl.pss : SSLConfigs.getPass(host)) + ") ..." + (lb ? "(Load balancer)" : ""));
System.out.println("Https server thread is now running (jks : " + (lb ? Configs.ssl.jks : SSLConfigs.getJKS(host)) + " ,, jks pass : " + (lb ? Configs.ssl.pss : SSLConfigs.getPass(host)) + ") ..." + (lb ? "(Load balancer)" : ""));
while (true) new HttpListener((SSLSocket) sslServerSocket.accept(),host,lb);
} catch (Exception ex) {
Logger.logException(ex);
}
Logger.ilog("Server is shutting down ...");
System.out.println("Server is shutting down ...");
}
}
}