[Java] Bot connection API

03/03/2012 12:26 Vinator#1
Hello again,
This is a release of my Java connection API, which can be used to connect your bot via a proxy.

Code:
/**
 * Class for Silkroad packet transaction via proxy
 * 
 * @author Vinator
 *
 */
public class BotConnection {
    public static final String botName = "Your bot's name";
    public static boolean connectionFlag;
    public static String opCode;
    public static int size;
    private Socket botSocket;
    private BufferedWriter send;
    private BufferedReader receive;
    private String packet;
    
    static {
        connectionFlag = false;
        opCode = "";
        size = 0;
    }
    
    public BotConnection(String ip, int port) {
        try {
            botSocket = new Socket(ip, port);
            send = new BufferedWriter(new OutputStreamWriter(botSocket.getOutputStream()));
            receive = new BufferedReader(new InputStreamReader(botSocket.getInputStream(), "ISO-8859-1"));
        } catch (UnknownHostException e) {
            JOptionPane.showMessageDialog(null, "Proxy connection failed!");
            System.exit(0);
            e.printStackTrace();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Proxy connection failed!");
            System.exit(0);
            e.printStackTrace();
        }
    }
    
    /**
     * Sends a notice to the Silkroad-Client
     * 
     * @param data Message to send
     */
    public void sendNotice(String data) {
        StringBuffer strB = new StringBuffer();
        String msg = "AutoPot: " + data;
        
        strB.append(ToHex((byte)7));
        strB.append(getHex(msg.length()));
        strB.append(stringToHex(msg));

        sendPacket("3026", strB.toString(), "0002");
    }
    
    /**
     * Converts an ASCII-String to a Hex-String
     * 
     * @param data
     * @return Hex-String from ASCII-String
     */
    public String stringToHex(String data) {
        StringBuffer strB = new StringBuffer();
        char[] tempArray = data.toCharArray();
        
        for(int i = 0; i < tempArray.length; i++) {
            strB.append(ToHex((byte)tempArray[i]));
        }
        
        return strB.toString();
    }
    
    /**
     * Converts a byte to a Hex-String
     * 
     * @param data
     * @return Hex-String from byte
     */
    public String ToHex(byte data) {
        if(data < 16) {
            return "0" + Integer.toHexString(data).toUpperCase();
        } else {
            return Integer.toHexString(data).toUpperCase();
        }
    }
    
    /**
     * Converts a Word to a Hex-String
     * 
     * @param data
     * @return Hex-String from Word
     */
    public String ToHex(char data) {
        if(data <= 255) {
            if(data < 16) {
                return "0" + Integer.toHexString(data).toUpperCase();
            } else {
                return Integer.toHexString(data).toUpperCase();
            }
        } else if(data < 4096) {
            return "0" + Integer.toHexString(data).toUpperCase();
        } else {
            return Integer.toHexString(data).toUpperCase();
        }    
    }
    
    /**
     * Converts a Word to a Hex-String
     * 
     * @param data
     * @return Hex-String from Word
     */
    public String ToHex(short data) {
        if(data <= 255) {
            if(data < 16) {
                return "0" + Integer.toHexString(data).toUpperCase();
            } else {
                return Integer.toHexString(data).toUpperCase();
            }
        } else if(data < 4096) {
            return "0" + Integer.toHexString(data).toUpperCase();
        } else {
            return Integer.toHexString(data).toUpperCase();
        }    
    }
    
    /**
     * Sends a packet to the Silkroad-Proxy
     * 
     * @param opCode     OPCode of the Packet
     * @param data         Data of the Packet
     * @param security    Security Bytes
     */
    public void sendPacket(String opCode, String data, String security) {
        StringBuffer strB = new StringBuffer();
        
        strB.append(getHex(data.length() / 2));
        strB.append(reverseWord(opCode));
        strB.append(reverseWord(security));
        strB.append(data);
        
        try {
            send.write(getHexArray(strB.toString()));
            send.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Couldn't send.");
            e.printStackTrace();
        }
    }
    
    /**
     * Converts a Word into a reversed Hex-String
     * 
     * @param len
     * @return reversed Hex-String from Word
     */
    public String getHex(int data) {        
        if(data < 255) {
            if(data < 16) {
                return reverseWord("000" + Integer.toHexString(data).toUpperCase());
            } else {
                return reverseWord("00" + Integer.toHexString(data).toUpperCase());
            }
        } else {
            return reverseWord("00" + Integer.toHexString(data).toUpperCase());
        }
    }
    
    
    /**
     * Reverses a Hex-Word String
     * 
     * @param b
     * @return Reversed Hex-String wordwise
     */
    public String reverseWord(String b) {
        StringBuffer strB = new StringBuffer();
        
        strB.append(b.substring(2, 4));
        strB.append(b.substring(0, 2));
        
        return strB.toString();
    }
    
    public String parseByte() {
        String tempStr = packet.substring(BotCore.packetIndex, (BotCore.packetIndex + 2));
        BotCore.packetIndex += 2;
        
        return tempStr;
    }
    
    public String parseWord() {
        String low = parseByte();
        String hi = parseByte();
        
        return hi + low;
    }
    
    public String parseDWord() {
        String low = parseWord();
        String hi = parseWord();
        
        return hi + low;
    }
    
    public String parseQWord() {
        String low = parseDWord();
        String hi = parseDWord();
        
        return hi + low;
    }
    
    public String getPacket() {
        return packet;
    }
    
    /**
     * Converts the Hex-String to a char array
     * 
     * @param packet
     * @return char Array from Hex-String
     */
    public char[] getHexArray(String packet) {
        char[] retArray = new char[packet.length() / 2];
        String[] tempArray = packet.split("(?<=\\G..)");
        
        for(int i = 0; i < retArray.length; i++) {
            retArray[i] = (char) Integer.parseInt(tempArray[i], 16);
        }
        
        return retArray;
    }
    
    /**
     * Reads a packet from the Inputstream
     * 
     * @return Packet as String
     */
    public void readPacket() {
        char[] packetLength = new char[2];
        String packetString;
        
        try {
            receive.read(packetLength);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        if(packetLength[0] == -1) {
            packet = "";
        } else {
            BotConnection.size = Integer.parseInt(ToHex(packetLength[1]) + ToHex(packetLength[0]), 16);
            char[] packetArray = new char[BotConnection.size + 4];
            try {
                receive.read(packetArray);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                JOptionPane.showMessageDialog(null, "Couldn't read packet data.");
                e.printStackTrace();
            }
            packetString = getPacketString(packetArray);
            BotConnection.opCode = reverseWord(packetString.substring(0, 4));
            
            if(packetString.length() > 8) {
                packet = packetString.substring(8, packetString.length());
            } else {
                packet = "0";
            }

        }
    }
    
    public byte[] getByteArray(char[] charArray) {
        byte[] tempArray = new byte[charArray.length];
        
        for(int i = 0; i < tempArray.length; i++) {
            tempArray[i] = (byte)charArray[i];
        }
        
        return tempArray;
    }
    
    /**
     * Creates a String out of an Array
     * 
     * @param value
     * @return Packet as a String
     */
    public String getPacketString(char[] value) {
        StringBuffer strB = new StringBuffer(BotConnection.size * 2 + 4);
        
        for(int i = 0; i < value.length; i++) {
                strB.append(ToHex(value[i]));
        }
        
        return strB.toString();
    }
    
    /**
     * Converts a Hex-String to an ASCII-String
     * 
     * @param packet
     * @return Ascii-String
     */
    public String hexToAscii(String packet) {
        StringBuffer strB = new StringBuffer();
        char[] tempCharArray = getHexArray(packet);
        
        for(char value : tempCharArray) {
            strB.append(value);
        }
        
        return strB.toString();
    }
}
A documentation is attached.
01/16/2015 23:59 toylife#2
I cant run it on eclipse.

Code:
BotCore.packetIndex += 2;
It cant BotCore class error!:(
01/17/2015 17:10 paxemuman#3
i dont think it will work with trunkated packets.
02/02/2015 18:50 Chernobyl__#4
I'm amazed how .NET and expecially c# is similar to Java programming language. It really feels like if you know .NET and general c syntax, you would learn Java in no time.
03/21/2015 16:39 Vinator#5
Quote:
Originally Posted by Chernobyl__ View Post
I'm amazed how .NET and expecially c# is similar to Java programming language. It really feels like if you know .NET and general c syntax, you would learn Java in no time.
You can actually just code in Java and have to look up some specific things that are different. But Java is like 90% the same to C#.

Quote:
Originally Posted by paxemuman View Post
i dont think it will work with trunkated packets.
It's really old and kinda bad programming style anyway.

Quote:
Originally Posted by toylife View Post
I cant run it on eclipse.

Code:
BotCore.packetIndex += 2;
It cant BotCore class error!:(
Gonna see if I find my full source of my MHTC autopot I made, and will upload it.

EDIT: Attached a zip, which contains an eclipse project you can use it and it also should run.

It also shows you how to use the BotConnection Class. Good luck and fun with it.