📄 proxy.java
字号:
/*
* Created on Aug 15, 2005
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.GTADS.proxy;
import java.net.*;
import java.util.Hashtable;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.GTADS.client.ClientLoginScreen;
import org.GTADS.client.DSChatClient;
import org.GTADS.client.preferences.PreferencesManager;
import org.GTADS.messenger.MessageAdapter;
import org.GTADS.protocol.MetaData;
/**
* @author sday
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Proxy extends Thread{
public static Proxy instance;
public int proxyPort = PreferencesManager.getInstance().getProxyPort();
public int remoteProxyPort = 8000;
public static boolean isSpecial = true;
private DatagramSocket proxyHostSocket;//= new DatagramSocket(proxyPort);
private DatagramSocket proxyConnectSocket;
private Hashtable proxySockets;
private DatagramSocket allSend;
private boolean isHost;
private InetAddress clientIPAddress;
private InetAddress hostIPAddress;
public static char[] gameInitByte = {0x82,0x00,0xb0,0xfa,0x02,0x00,0x08,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x6c,0x61,0x79,0x01,
0x00,0x0e,0x00,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xa0,0x21,0x8d,0x30,0xed,0x34,0xda,0x11,0x94,0x80,0x00,0x06,0x29,0x36,0xcb,
0x3c,0x40,0x3b,0xdf,0x22,0x12,0x36,0xcf,0x11,0xae,0xc8,0x00,0x00,0xc0,0x66,0x91,0xad,0x04,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0xde,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,0x00,0x00,0x00};
public static byte udpByte = 1;
public static byte tcpByte = 2;
private DirectPlayGameCheck gameCheck;
public static Proxy getInstance() throws IOException{
if (instance == null)
instance = new Proxy();
return instance;
}
public static void clearInstance() {
if (instance != null){
if (instance.getProxySocket() != null){
instance.getProxySocket().close();
}
if (instance.getproxyConnectSocket() != null){
instance.getproxyConnectSocket().close();
}
GTAVirtualPlayerTCP.clearInstance();
GTAVirtualPlayerUDP.clearInstance();
GTAVirtualHostTCP.clearInstance();
GTAVirtualHostUDP.clearInstance();
GTADirectPlayServerTCP.clearInstance();
GTADirectPlayServerUDP.clearInstance();
if (instance.gameCheck != null)
instance.gameCheck.stop();
//instance.stop();
instance = null;
ProxyGUI.getInstance().setStatusLabel(ProxyGUI.PROXY_READY);
}
}
public Proxy() throws IOException{
start();
}
public void run(){
try {
if (ProxyGUI.getInstance().isProxyHosting()){
initHost();
}
else {
initNoHost();
}
} catch (IOException ioe){
return;
}
}
public void initHost() throws IOException{
// Grab and set host information
boolean isInitialState = true;
//proxyConnectSocket = new DatagramSocket();
try {
proxyHostSocket = new DatagramSocket(proxyPort);
} catch (IOException be){
String message = "Port " + proxyPort + " is in use!";
JOptionPane.showMessageDialog(null,message,"Error Message", JOptionPane.ERROR_MESSAGE);
ProxyGUI.getInstance().pressStartProxyButton();
return;
}
// Listen for connection from other proxy
byte buffer[] = new byte[10000];
DatagramPacket initialConnection = new DatagramPacket(buffer, 255);
ProxyGUI.getInstance().setStatusLabel(ProxyGUI.PROXY_LISTENING);
while(isInitialState){
proxyHostSocket.receive(initialConnection);
String connectString = new String(buffer);
connectString = connectString.substring(0,initialConnection.getLength());
proxyHostSocket.getRemoteSocketAddress();
isInitialState = !isInitialState;
ProxyGUI.getInstance().setStatusLabel("REQUEST OK!");
if (!ProxyGUI.getInstance().isStandAlone){
MetaData hostConnected = new MetaData(ClientLoginScreen.getInstance().getUsername(),
MetaData.GAME, MetaData.PROXY_CONNECTED);
MessageAdapter.sendData(null,DSChatClient.ClientSocket,hostConnected,
connectString);
gameCheck = new DirectPlayGameCheck(2301);
}
String s = new String(initialConnection.getData());
clientIPAddress = initialConnection.getAddress();
/*proxyConnectSocket*/proxyHostSocket.connect(clientIPAddress,8000);
}
GTAVirtualPlayerTCP.getInstance();
GTAVirtualPlayerUDP.getInstance();
// Handle host
while(true){
buffer = new byte[3000];
DatagramPacket proxyRecieve = new DatagramPacket(buffer,buffer.length);
proxyHostSocket.receive(proxyRecieve);
buffer = getByteOffset(proxyRecieve.getData(),0,proxyRecieve.getLength());
//String s = new String(buffer, buffer.length);
//System.out.println(buffer.length);
if (buffer[0] == udpByte){
GTAVirtualPlayerUDP.getInstance().processVPlayerUDP(getByteOffset(buffer,1,buffer.length));
}
else if (equalsString(buffer,"DXPLAY,")){
System.out.println("Recieved DirectPlay (UDP) Data from Proxy");
DatagramSocket dxPlaySend = new DatagramSocket(4550);
dxPlaySend.connect(InetAddress.getByName("localhost"),47624);
int lengthOfDxData = 52;
int offset = "DXPLAY,".length();
byte[] dxData = getByteOffset(buffer,"DXPLAY,".length(),buffer.length);//new byte[lengthOfDxData];
displayByte(dxData);
System.out.println();
//for (int i = 0; i< lengthOfDxData; i++){
//dxData[i] = buffer[offset + i];
//}
dxPlaySend.send(new DatagramPacket(dxData, dxData.length));
// Start TCP listening on port 2300
}
else if (equalsString(buffer,"DXPLAYTCP,")){
System.out.println("Recieved DirectPlay (TCP) Data from Proxy");
try {
Socket directPlayConnect = new Socket(InetAddress.getByName("localhost"), 47624);
byte[] dxData = getByteOffset(buffer,"DXPLAYTCP,".length(), buffer.length);
displayByte(dxData);
System.out.println();
directPlayConnect.getOutputStream().write(dxData,0,dxData.length);
System.out.println("Sending DirectPlay TCP Data to localhost length " + (buffer.length - "DXPLAYTCP,".length()));
directPlayConnect.setSoLinger(true, 0);
directPlayConnect.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
else if (equalsString(buffer,"VPLAYERTCP,")){
GTAVirtualPlayerTCP.getInstance().processVPlayerTCP(getByteOffset(buffer,"VPLAYERTCP,".length(),buffer.length));
}
}
// Proxy from Client --> 8000 to 2301 --> 2300
// Proxy from 2300 --> 2301 to 8000 --> Client
}
public DatagramSocket getProxySocket(){
return proxyHostSocket;
}
public DatagramSocket getproxyConnectSocket(){
return proxyConnectSocket;
}
public void initNoHost() throws IOException{
boolean isInitialState = true;
if (ProxyGUI.getInstance().getIpAddress().equals("")){
String message = "Invalid IP Address!";
JOptionPane.showMessageDialog(null,message,"Error Message", JOptionPane.ERROR_MESSAGE);
ProxyGUI.getInstance().pressStartProxyButton();
return;
}
try {
String ipAddress = ProxyGUI.getInstance().getIpAddress();
hostIPAddress = InetAddress.getByName(ipAddress);
DatagramSocket s = new DatagramSocket();
s.connect(hostIPAddress, remoteProxyPort);
} catch (IOException ioe){
if (ioe instanceof UnknownHostException){
String message = "Could not connect to Proxy at " + ProxyGUI.getInstance().getIpAddress();
JOptionPane.showMessageDialog(null,message,"Error Message", JOptionPane.ERROR_MESSAGE);
ProxyGUI.getInstance().pressStartProxyButton();
return;
}
else {
String message = "Disconnected from " + ProxyGUI.getInstance().getIpAddress();
JOptionPane.showMessageDialog(null,message,"Error Message", JOptionPane.ERROR_MESSAGE);
ProxyGUI.getInstance().pressStartProxyButton();
return;
}
}
proxyHostSocket = new DatagramSocket(remoteProxyPort);
proxyHostSocket.connect(hostIPAddress, remoteProxyPort);
// Send initial message
String greetingString = new String();
if (!ProxyGUI.getInstance().isStandAlone){
greetingString = ClientLoginScreen.getInstance().getUsername();
}
else {
greetingString = "Lets Go!";
}
byte[] b = convertStringToByte(greetingString);
byte[] buffer = new byte[10000];
DatagramPacket p = new DatagramPacket(b,b.length);
proxyHostSocket.send(p);
// Setup DirectPlaySvr, and Virtual Host
GTAVirtualHostTCP.getInstance();
GTAVirtualHostUDP.getInstance();
GTADirectPlayServerUDP.getInstance();
GTADirectPlayServerTCP.getInstance();
//proxyConnectSocket = new DatagramSocket(remoteProxyPort);
while (true){
buffer = new byte[3000];
DatagramPacket proxyReceieve = new DatagramPacket(buffer, buffer.length);
/*proxyConnectSocket*/proxyHostSocket.receive(proxyReceieve);
buffer = getByteOffset(proxyReceieve.getData(),0,proxyReceieve.getLength());
if (buffer[0] == udpByte){
GTAVirtualHostUDP.getInstance().processVHostUDP(getByteOffset(buffer,1,buffer.length));
}
else if (equalsString(buffer,"VHOSTTCP,")){
GTAVirtualHostTCP.getInstance().processVHostTCP(getByteOffset(buffer,"VHOSTTCP,".length(),buffer.length));
}
}
}
public static byte[] addProxyCommandToByte(String command, byte[] binaryData){
byte[] b = new byte[command.length() + binaryData.length + 1];
for (int i = 0; i < command.length(); i++){
b[i] = (byte)command.charAt(i);
}
b[command.length()] = ',';
for (int i = command.length(); i < binaryData.length + command.length(); i++){
b[i] = binaryData[i - command.length()];
}
return b;
}
public void sendHostData(String prefix, byte[] data) throws IOException {
byte[] proxyData = new byte[data.length + prefix.length()];
String stringData = prefix;//"VHOSTTCP,";
int strLen = stringData.getBytes().length;
int dataLen = data.length;
for (int i = 0; i < strLen; i++){
proxyData[i] = stringData.getBytes()[i];
}
for (int i = 0; i< dataLen; i++){
proxyData[i + strLen] = data[i];
}
//DatagramSocket s = new DatagramSocket();
//s.connect(clientIPAddress, 8000);
proxyHostSocket.send(new DatagramPacket(proxyData,proxyData.length));
//s.close();
}
public void sendHostData(byte prefix, byte[] data) throws IOException {
byte[] proxyData = new byte[data.length + 1];
int dataLen = data.length;
proxyData[0] = prefix;
for (int i = dataLen; --i>=0;){
proxyData[i + 1] = data[i];
}
//DatagramSocket s = new DatagramSocket();
//s.connect(clientIPAddress, 8000);
try {
Thread.sleep(1);
proxyHostSocket.send(new DatagramPacket(proxyData,proxyData.length));
} catch (InterruptedException ie){
return;
}
//s.close();
}
public void sendTCPHostData(byte[] data) throws IOException {
sendHostData("VHOSTTCP,", data);
}
public void sendUDPHostData(byte[] data) throws IOException {
sendHostData(udpByte, data);
}
public void sendClientData(String prefix, byte[] data) throws IOException {
byte[] proxyData = new byte[prefix.getBytes().length + data.length];
String stringData = prefix;
int strLen = stringData.getBytes().length;
int dataLen = data.length;
for (int i = 0; i < strLen; i++){
proxyData[i] = stringData.getBytes()[i];
}
for (int i = 0; i< dataLen; i++){
proxyData[i + strLen] = data[i];
}
//DatagramSocket s = new DatagramSocket();
//proxyConnectSocket.connect(hostIPAddress, 8000);
proxyHostSocket.send(new DatagramPacket(proxyData,proxyData.length));
}
public void sendClientData(byte prefix, byte[] data) throws IOException {
byte[] proxyData = new byte[data.length + 1];
int dataLen = data.length;
proxyData[0] = prefix;
for (int i = dataLen; --i>= 0;){
proxyData[i + 1] = data[i];
}
//DatagramSocket s = new DatagramSocket();
//s.connect(hostIPAddress, 8000);
try {
Thread.sleep(1);
proxyHostSocket.send(new DatagramPacket(proxyData,proxyData.length));
} catch (InterruptedException ie){
return;
}
}
public void sendTCPClientData(byte[] data) throws IOException {
sendClientData("VPLAYERTCP,", data);
}
public void sendUDPClientData(byte[] data) throws IOException {
sendClientData(udpByte, data);
}
public void sendDXClientData(byte[] data, boolean tcp) throws IOException {
sendClientData(tcp ? "DXPLAYTCP," : "DXPLAY,", data);
}
public static byte[] convertStringToByte(String s){
byte[] b = new byte[s.length()];
for (int i = 0; i < s.length(); i++){
b[i] = (byte)s.charAt(i);
}
return b;
}
public static byte[] getByteOffset(byte[] data, int start, int offset){
byte[] modifiedByte = new byte[offset - start];
for (int i = offset - start; --i >= 0;){
modifiedByte[i] = data[i + start];
}
return modifiedByte;
}
public static boolean equalsString(byte[] data, String compare){
boolean isSuccess = true;
int searchLimit = data.length > compare.length() ? compare.length() : data.length;
for (int i = 0; i < searchLimit;i++){
if (data[i] != (byte)compare.charAt(i)){
isSuccess = false;
break;
}
}
return isSuccess;
}
public static boolean matchesString(byte[] data, String compare){
boolean isSuccess = false;
int searchLimit = data.length > compare.length() ? compare.length() : data.length;
for (int i = 0; i < searchLimit; i++){
if (data[i] == (byte)compare.charAt(0)){
for (int j = i; j < i + compare.length(); j++){
if (data[j] != (byte)compare.charAt(j)){
break;
}
}
return true;
}
}
return isSuccess;
}
public static void displayByte(byte[] data){
int byteLen = data.length;
for (int i = 0; i < data.length; i++){
System.out.print(data[i] == 0 ? "0 " : (char)data[i] + " ");
}
System.out.println();
}
public static byte[] byteCat(byte[] a, byte[] b){
byte[] tempByte = new byte[a.length + b.length];
for (int i = 0; i < a.length; i++){
tempByte[i] = a[i];
}
for (int i = 0; i < b.length; i++){
tempByte[i + a.length] = b[i];
}
return tempByte;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -