Search in sources :

Example 1 with Endpoint

use of org.csploit.android.net.Endpoint in project android by cSploit.

the class PacketForger method onClick.

@Override
public void onClick(View v) {
    if (!mRunning) {
        if (v.getId() == R.id.sendButton) {
            mResponse.setText("");
            mRunning = true;
            mThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    int protocol = mProtocol.getSelectedItemPosition(), port = -1;
                    String data = mData.getText() + "", error = null;
                    boolean waitResponse = mWaitResponse.isChecked();
                    try {
                        port = Integer.parseInt(mPort.getText() + "".trim());
                        if (port <= 0 || port > 65535)
                            port = -1;
                    } catch (Exception e) {
                        port = -1;
                    }
                    if (port == -1)
                        error = getString(R.string.invalid_port);
                    else if (data.isEmpty())
                        error = getString(R.string.request_empty);
                    else {
                        try {
                            if (protocol == TCP_PROTOCOL) {
                                mSocket = new Socket(System.getCurrentTarget().getCommandLineRepresentation(), port);
                                OutputStream writer = mSocket.getOutputStream();
                                writer.write(data.getBytes());
                                writer.flush();
                                if (waitResponse) {
                                    BufferedReader reader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
                                    String line;
                                    final StringBuilder responseBuilder = new StringBuilder();
                                    while ((line = reader.readLine()) != null) {
                                        responseBuilder.append(line).append("\n");
                                    }
                                    final String text = responseBuilder.toString();
                                    PacketForger.this.runOnUiThread(new Runnable() {

                                        public void run() {
                                            mResponse.setText(text);
                                        }
                                    });
                                    reader.close();
                                }
                                writer.close();
                                mSocket.close();
                            } else if (protocol == UDP_PROTOCOL) {
                                mUdpSocket = new DatagramSocket();
                                DatagramPacket packet = null;
                                if (mBinaryData != null)
                                    packet = new DatagramPacket(mBinaryData, mBinaryData.length, System.getCurrentTarget().getAddress(), port);
                                else
                                    packet = new DatagramPacket(data.getBytes(), data.length(), System.getCurrentTarget().getAddress(), port);
                                mUdpSocket.send(packet);
                                if (waitResponse) {
                                    byte[] buffer = new byte[1024];
                                    DatagramPacket response = new DatagramPacket(buffer, buffer.length);
                                    mUdpSocket.receive(response);
                                    final String text = new String(buffer);
                                    PacketForger.this.runOnUiThread(new Runnable() {

                                        public void run() {
                                            mResponse.setText(text);
                                        }
                                    });
                                }
                                mUdpSocket.close();
                            }
                        } catch (Exception e) {
                            error = e.getMessage();
                        }
                    }
                    mBinaryData = null;
                    final String errorMessage = error;
                    PacketForger.this.runOnUiThread(new Runnable() {

                        public void run() {
                            Toast.makeText(PacketForger.this, getString(R.string.request_sent), Toast.LENGTH_SHORT).show();
                            setStoppedState(errorMessage);
                        }
                    });
                }
            });
            mThread.start();
        } else {
            Endpoint endpoint = System.getCurrentTarget().getEndpoint();
            byte[] mac = endpoint.getHardware();
            int i;
            if (mac != null) {
                mResponse.setText("");
                mProtocol.setSelection(UDP_PROTOCOL);
                mPort.setText("9");
                mBinaryData = new byte[6 + 16 * mac.length];
                for (i = 0; i < 6; i++) {
                    mBinaryData[i] = (byte) 0xFF;
                }
                for (i = 6; i < mBinaryData.length; i += mac.length) {
                    java.lang.System.arraycopy(mac, 0, mBinaryData, i, mac.length);
                }
                final StringBuilder hexBuilder = new StringBuilder();
                for (i = 0; i < mBinaryData.length; i++) hexBuilder.append("\\x").append(Integer.toHexString(0xFF & mBinaryData[i]).toUpperCase());
                mData.setText(hexBuilder.toString());
                Toast.makeText(this, getString(R.string.customize_wol_port), Toast.LENGTH_SHORT).show();
            } else
                Toast.makeText(this, getString(R.string.couldnt_send_wol_packet), Toast.LENGTH_SHORT).show();
        }
    } else {
        setStoppedState(null);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Endpoint(org.csploit.android.net.Endpoint) Endpoint(org.csploit.android.net.Endpoint) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 Socket (java.net.Socket)1 Endpoint (org.csploit.android.net.Endpoint)1