Warning: session_start(): open(/var/lib/php/sessions/sess_evcqcjb29fn1uqdd7bues74taj, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
abstract sclass StandaloneHttpProxy implements AutoCloseable {
debug = false;
int port = 8443;
new Set blockedIPs;
abstract ServerSocket makeServerSocket();
ServerSocket serverSocket;
void start {
serverSocket = new ServerSocket(port);
Thread thread = startThread("HTTP Proxy Accept Port " + port, r acceptLoop);
print("HTTP proxy on port " + port + " started.");
}
void acceptLoop {
while (true) {
try {
Socket s = serverSocket.accept();
new Request(s).run();
} catch (SocketTimeoutException e) {
}
}
}};
S rewriteHeaderLine(S line) {
/*if (startsWithIgnoreCase(line, "Host:"))
line = "Host: " + forwardServer + ":" + forwardPort;*/
ret line;
}
svoid headerSend(OutputStream out, S header) ctex {
print("Sending header: " + header);
out.write((header + "\r\n").getBytes("US-ASCII"));
}
close { dispose serverSocket; }
class Request {
Socket s; // client socket
S client;
bool blocked;
*(Socket *s) {}
run {
client = dropPrefix("/", s.getInetAddress().toString()); // Hmm... how to get the actual IP properly?
S threadName = "PROXY: Handling client " + client;
if (blocked = blockedIPs.contains(client)) {
print("Blocked IP!! " + client);
pcall { s.close(); }
ret;
}
Thread t2 = new Thread(threadName) {
public void run() ctex {
Socket outSocket = null;
final new AtomicLong transmitted;
final new AtomicLong outTransmitted;
try {
print("HTTP Proxy Incoming!");
final OutputStream out = s.getOutputStream();
final InputStream in = s.getInputStream();
// collect headers
new ByteArrayOutputStream headersOut;
//BufferedReader headerReader = new BufferedReader(new InputStreamReader(in, "US-ASCII"), 1);
boolean forwardedFor = false;
S host = null;
while (true) {
S line = readHttpHeaderLine(in);
if (line == null) ret;
print("Header line read: " + quote(line));
if (startsWithIgnoreCase(line, "Host:"))
host = dropPrefixIgnoreCase("Host:", line).trim();
else {
S rewritten;
if (startsWithIgnoreCase(line, "X-Forwarded-For:")) {
forwardedFor = true;
rewritten = line + ", " + client;
} else
rewritten = empty(line) ? line : rewriteHeaderLine(line);
if (!eq(rewritten, line))
print("Rewritten as: " + quote(rewritten));
if (nempty(rewritten)) {
byte[] bytes = (rewritten + "\r\n").getBytes("US-ASCII");
//print("Sending: " + bytesToHex(bytes));
headersOut.write(bytes);
}
}
if (l(line) == 0)
break;
}
int port = getForwardPort(host);
outSocket = new Socket(forwardServer, port);
final Socket outS = outSocket;
print("Connected to " + forwardServer + ":" + port);
final OutputStream outOut = outSocket.getOutputStream();
final InputStream outIn = outSocket.getInputStream();
outOut.write(headersOut.toByteArray());
if (!forwardedFor)
headerSend(outOut, "X-Forwarded-For: " + client);
headerSend(outOut, "Host: " + addPortToHost(host, port));
headerSend(outOut, ""); // end of headers
outOut.flush();
// forward content in both directions
//copyStream(outIn, out);
Thread inThread = new Thread("Proxy In") {
public void run() ctex {
try {
byte[] buf = new byte[1];
while (true) {
int n = outIn.read(buf);
if (n <= 0) return;
out.write(buf, 0, n);
long t = transmitted.addAndGet(n);
if (t % 100000 == 0) print("Transmitted to client: " + t);
}
} finally {
print("Shutting down client output.");
s.shutdownOutput();
}
}
};
inThread.start();
Thread outThread = new Thread("Proxy Out") {
public void run() ctex {
try {
print("Proxy Out started.");
byte[] buf = new byte[1];
while (true) {
int n = in.read(buf);
if (n <= 0) return;
outOut.write(buf, 0, n);
if (outTransmitted.get() == 0)
print("Proxy Out first byte!");
long t = outTransmitted.addAndGet(n);
if (t % (debug ? 10 : 100000) == 0) print("Transmitted to server: " + t);
}
} finally {
print("Shutting down server output.");
outS.shutdownOutput();
}
}
};
outThread.start();
inThread.join();
outThread.join();
} catch (IOException e) {
print("[internal] " + e);
} finally {
print("Proxy request done, got " + transmitted.get() + ", sent " + outTransmitted.get() + " + headers");
pcall { s.close(); }
pcall { if (outSocket != null) outSocket.close(); }
}
}
}; // Thread t2
t2.setDaemon(true);
t2.start();
} // Request.run
}
}