📄 gateway.java
字号:
package Hiisi;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Display;
public class Gateway extends Thread {
boolean running = true;
private Calendar cal = null;
private int year = 0;
private String month = null;
private int day_of_month = 0;
private int hour = 0;
private int minute = 0;
private int second = 0;
private ServerSocketConnection ssc = null;
private SocketConnection sc = null;
private DataInputStream input = null;
private DataOutputStream output = null;
private StreamConnection bc = null;
private DataInputStream bluetoothInput = null;
private DataOutputStream bluetoothOutput = null;
private HttpConnection hc = null;
private DataInputStream httpInput = null;
private DataOutputStream httpOutput = null;
private String requestMethod = null;
private String requestURI = null;
private String requestProtocol = null;
private String requestHost = null;
private int requestPort = 80;
private Hashtable requestHeaderField = new Hashtable();
private byte[] postData = null;
private String responseProtocol = null;
private String responseCode = null;
private String responseMessage = null;
private Hashtable responseHeaderField = new Hashtable();
private int byteTranfered = 0;
Gateway() {
}
private void closeBluetooth() {
try{
if(bluetoothInput != null) {
bluetoothInput.close();
bluetoothInput=null;
}
} catch(IOException e) {
}
try{
if(bluetoothOutput != null) {
bluetoothOutput.close();
bluetoothOutput=null;
}
} catch(IOException e) {
}
try{
if(bc != null){
bc.close();
bc = null;
}
} catch(IOException e){
}
}
private void closeHttp() {
try{
if(httpInput != null) {
httpInput.close();
httpInput=null;
}
} catch(IOException e) {
}
try{
if(httpOutput != null) {
httpOutput.close();
httpOutput=null;
}
} catch(IOException e) {
}
try{
if(hc != null){
hc.close();
hc = null;
}
} catch(IOException e){
}
}
private void closeInput() {
try{
if(input != null){
input.close();
input = null;
}
} catch(IOException e) {
}
}
private void closeOutput() {
try{
if(output != null){
output.close();
output = null;
}
} catch(IOException e) {
}
}
private void closeSocket() {
try{
if(sc != null){
sc.close();
sc = null;
}
} catch(IOException e) {
}
}
private void closeServerSocket() {
try {
if(ssc != null) {
ssc.close();
ssc = null;
}
} catch(IOException e){
}
}
void exitGateway() {
closeInput();
closeBluetooth();
closeHttp();
closeOutput();
closeSocket();
closeServerSocket();
}
public void run() {
try {
ssc = (ServerSocketConnection)Connector.open("socket://:1234");
} catch(IOException e) {
}
while(running){
HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString() + " is idling...");
connectingProcess();
}
exitGateway();
}
private void connectingProcess() {
try{
sc = (SocketConnection)ssc.acceptAndOpen();
sc.setSocketOption(SocketConnection.DELAY, 0);
sc.setSocketOption(SocketConnection.LINGER, 0);
sc.setSocketOption(SocketConnection.KEEPALIVE, 0);
sc.setSocketOption(SocketConnection.RCVBUF, 8192);
sc.setSocketOption(SocketConnection.SNDBUF, 8192);
HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString() + " is running...");
requestMethod = "";
requestURI = "";
requestProtocol = "";
requestHeaderField.clear();
requestHost = "";
requestPort = 80;
input = sc.openDataInputStream();
readRequestHeader();
closeInput();
if(HiisiMIDlet.settingForm.isFilter() && (requestURI.indexOf(HiisiMIDlet.settingForm.getFilterURL()) == -1)) {
requestURI = HiisiMIDlet.settingForm.getFilterURL() + "?_ucb_l=1&_ucb_k=1&_ucb_u=" + requestURI;
output = sc.openDataOutputStream();
output.write(("HTTP/1.1 302 Moved Temporarily\r\n").getBytes());
output.write(("Location: " + requestURI + "\r\n\r\n").getBytes());
output.flush();
closeOutput();
closeSocket();
return;
}
if(requestURI.indexOf("http://ime.nu/") != -1) {
requestURI = "http://" + requestURI.substring(14);
output = sc.openDataOutputStream();
output.write(("HTTP/1.1 302 Moved Temporarily\r\n").getBytes());
output.write(("Location: " + requestURI + "\r\n\r\n").getBytes());
output.flush();
closeOutput();
closeSocket();
return;
}
if(HiisiMIDlet.settingForm.getGatewayMode() == HiisiMIDlet.settingForm.BLUETOOTH_MODE) {
bc = (StreamConnection)Connector.open(HiisiMIDlet.bluetoothConnection.getUrl());
bluetoothOutput = bc.openDataOutputStream();
connectViaBluetooth();
} else if (HiisiMIDlet.settingForm.getGatewayMode() == HiisiMIDlet.settingForm.WAP_MODE) {
hc = (HttpConnection)Connector.open(requestURI, 1);
ConnectViaWap();
}
responseProtocol = "";
responseCode = "";
responseMessage = "";
responseHeaderField.clear();
byteTranfered = 0;
if(HiisiMIDlet.settingForm.getGatewayMode() == HiisiMIDlet.settingForm.BLUETOOTH_MODE) {
bluetoothInput = bc.openDataInputStream();
readResponseHeaderViaBluetooth();
output = sc.openDataOutputStream();
writeResponseFromBluetooth();
closeBluetooth();
} else if(HiisiMIDlet.settingForm.getGatewayMode() == HiisiMIDlet.settingForm.WAP_MODE) {
httpInput = hc.openDataInputStream();
readResponseHeaderViaWap();
output = sc.openDataOutputStream();
writeResponseFromWap();
closeHttp();
}
HiisiMIDlet.logForm.totalLog(HiisiMIDlet.logForm.getSentByte(), HiisiMIDlet.logForm.getRecvdByte());
HiisiMIDlet.logForm.log("localhost - - [" + getAccessTime()+ "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode + " " + byteTranfered + "\n");
closeOutput();
closeSocket();
} catch(IOException e) {
// HiisiMIDlet.mainForm.log(e);
} catch(InterruptedException e) {
// HiisiMIDlet.mainForm.log(e);
} finally{
closeInput();
closeBluetooth();
closeHttp();
closeOutput();
closeSocket();
}
}
private void readRequestHeader() throws IOException {
StringBuffer sb = new StringBuffer();
int c;
int line = 0;
while((c = input.read()) != -1){
if((char)c == '\r') {
if(sb.length() == 0) {
break;
} else {
processRequestHeader(sb.toString(), line);
sb.delete(0, sb.length());
line++;
}
} else if((char)c == '\n') {
} else {
sb.append((char)c);
}
}
sb = null;
if(requestMethod.equals("POST")) {
int dummy = input.read();
if(requestHeaderField.get("content-length") != null) {
int contentLength = Integer.valueOf((String)requestHeaderField.get("content-length")).intValue();
postData = new byte[contentLength];
input.read(postData, 0, contentLength);
HiisiMIDlet.logForm.addSentByte(contentLength);
}
}
}
private void processRequestHeader(String str, int line) throws IOException {
str = str.trim();
if(line == 0){
int delimitPos = str.indexOf(' ');
requestMethod = (str.substring(0, delimitPos)).trim();
str = (str.substring(delimitPos + 1)).trim();
delimitPos = str.indexOf(' ');
requestURI = (str.substring(0, delimitPos)).trim();
requestProtocol = (str.substring(delimitPos + 1)).trim();
} else {
int delimitPos = str.indexOf(':');
String key = ((str.substring(0, delimitPos)).trim()).toLowerCase();
String value = (str.substring(delimitPos + 1)).trim();
if(key.equals("host")) {
delimitPos = value.indexOf(':');
if(delimitPos == -1) {
requestHost = value;
} else {
requestHost = value.substring(0, delimitPos);
requestPort = Integer.valueOf(value.substring(delimitPos + 1)).intValue();
}
}
requestHeaderField.put(key, value);
}
}
private void connectViaBluetooth() throws IOException {
String ua = "";
String wap = "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -