Register for your free account! | Forgot your password?

Go Back   elitepvpers > Popular Games > Silkroad Online > SRO Coding Corner
You last visited: Today at 08:57

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Java] Bot connection API

Discussion on [Java] Bot connection API within the SRO Coding Corner forum part of the Silkroad Online category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Mar 2009
Posts: 248
Received Thanks: 118
[Java] Bot connection API

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.
Attached Files
File Type: rar doc.rar (43.4 KB, 73 views)
File Type: rar src.rar (1.8 KB, 58 views)
Vinator is offline  
Thanks
5 Users
Old 01/16/2015, 23:59   #2
 
elite*gold: 0
Join Date: Aug 2012
Posts: 2
Received Thanks: 0
I cant run it on eclipse.

Code:
BotCore.packetIndex += 2;
It cant BotCore class error!
toylife is offline  
Old 01/17/2015, 17:10   #3
 
paxemuman's Avatar
 
elite*gold: 0
Join Date: Apr 2008
Posts: 934
Received Thanks: 746
i dont think it will work with trunkated packets.
paxemuman is offline  
Old 02/02/2015, 18:50   #4
 
elite*gold: 0
Join Date: Feb 2015
Posts: 3
Received Thanks: 5
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.
Chernobyl__ is offline  
Old 03/21/2015, 16:39   #5
 
elite*gold: 0
Join Date: Mar 2009
Posts: 248
Received Thanks: 118
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.
Attached Files
File Type: zip MHTC_AutoPot.zip (87.7 KB, 36 views)
Vinator is offline  
Reply


Similar Threads Similar Threads
[JAVA Error] Could not create the java virtual machine
07/21/2013 - Technical Support - 10 Replies
Schönen Abend! Leider hat es sich aus einem unerfindlichen Grund ergeben, dass sobald ich die Minecraft.exe starten will die Errormeldung kommt. Die Tips auf Minecraft.net habe ich schon ohne Erfolg befolgt. Hoffe ihr könnt mir weiterhelfen... Mein PC:
Internal excepition: java.net.SocketException: Software caused connection abort: recv
03/13/2013 - Minecraft - 12 Replies
hay wie im titel schon steht kommt bei meinem freund immer diese meldung nach 30 sekunden Internal excepition: java.net.SocketException: Software caused connection abort: recv failed haben firewall alles schon pribiert aber alles klappt nicht was muss man da machen ?
Fehler : java.net.socet.exception:connection reset fehler bitte um hilfe
12/21/2010 - Minecraft - 9 Replies
Hab denn java.net.socet.exception:connection reset fehler immer und immer wieder :( könnt ihr mir dabei vill helfen? :) LG DieVerrückteMango =)



All times are GMT +1. The time now is 09:02.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.