Tuesday, November 29, 2011

Allowing remote connection in MySQL

When using MySQL, sometime you want to allowing remote connection (from different server)

Run mysql command line console. Usually in /usr/bin/mysql or /usr/local/mysql/bin/mysql

Your shell prompt should be like this

mysql>

Run this command

GRANT ALL PRIVILEGES ON *.* TO USERNAME@IP IDENTIFIED BY "PASSWORD";

Then

FLUSH PRIVILEGES;

Please change USERNAME, IP, PASSWORD, base on your need.
For example:

GRANT ALL PRIVILEGES ON *.* TO root@192.168.1.20 IDENTIFIED BY "MySecretPassword";





Wednesday, November 23, 2011

Using TCP Redirection in Linux

This tutorial shows how to use rinetd for TCP Redirection.
In this case there are two server:

  1. 1st Server
    This server has two network interface. First is set using public IP for example 202.77.10.100
    The second interface is connected to 2nd server in private network using IP 192.168.1.100
  2. 2nd Server
    Connected to 1st server in private network using IP 192.168.1.200
    The 2nd server is installed Postgre server as a database server using port 5432
    And make sure the postgre database server is allowing connection from remote for ip 192.168.1.100 (this can be set on pg_hba.conf)
Now you want connect to database from your laptop. Let's say using PgAdmin tool.
Because the 2nd server is not visible in public, not using public IP, your laptop can't just access to 192.168.1.200. (In this case your laptop is in different network)

The laptop can access the database through the 1st Server (public IP)
Let's say my laptop access to 202.77.10.100 on port 5500, which will be set to be redirected to 192.168.1.200 on port 5432. We can configure this using TCP Redirection server.

Make sure you have installed rinetd on your linux machine (1st server)

Most the executable case is placed on /usr/sbin/rinetd
And the configuration is on /etc/rinetd.conf

Open the /etc/rinetd.conf, add following lines
202.77.10.100 5500 192.168.1.200 5432

Which means if there is incoming request 202.77.10.100 5500 on port 5500, please redirect it to 192.168.1.200 in port 5432

Run the rinetd

/usr/sbin/rinetd -c /etc/rinetd.conf

Run

netstat -atn | grep 202.77.10.100:5500

You should see there is listening port there. for 202.77.10.100:5500

Now you can access the database from PgAdmin by using 202.77.10.100 as host, and using port 5500.


Tuesday, November 22, 2011

Generate QRCode using Java

This is a little sample code to generate code using Java

You will need this class first

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import com.google.zxing.LuminanceSource;

public final class BufferedImageLuminanceSource extends LuminanceSource {

      private final BufferedImage image;
      private final int left;
      private final int top;
      private int[] rgbData;

      public BufferedImageLuminanceSource(BufferedImage image) {
        this(image, 0, 0, image.getWidth(), image.getHeight());
      }

      public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width,
          int height) {
        super(width, height);

        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();
        if (left + width > sourceWidth || top + height > sourceHeight) {
          throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
        }

        this.image = image;
        this.left = left;
        this.top = top;
      }

      // These methods use an integer calculation for luminance derived from:
      // <code>Y = 0.299R + 0.587G + 0.114B</code>
     
      public byte[] getRow(int y, byte[] row) {
        if (y < 0 || y >= getHeight()) {
          throw new IllegalArgumentException("Requested row is outside the image: " + y);
        }
        int width = getWidth();
        if (row == null || row.length < width) {
          row = new byte[width];
        }

        if (rgbData == null || rgbData.length < width) {
          rgbData = new int[width];
        }
        image.getRGB(left, top + y, width, 1, rgbData, 0, width);
        for (int x = 0; x < width; x++) {
          int pixel = rgbData[x];
          int luminance = (306 * ((pixel >> 16) & 0xFF) +
              601 * ((pixel >> 8) & 0xFF) +
              117 * (pixel & 0xFF)) >> 10;
          row[x] = (byte) luminance;
        }
        return row;
      }

     
      public byte[] getMatrix() {
        int width = getWidth();
        int height = getHeight();
        int area = width * height;
        byte[] matrix = new byte[area];

        int[] rgb = new int[area];
        image.getRGB(left, top, width, height, rgb, 0, width);
        for (int y = 0; y < height; y++) {
          int offset = y * width;
          for (int x = 0; x < width; x++) {
            int pixel = rgb[offset + x];
            int luminance = (306 * ((pixel >> 16) & 0xFF) +
                601 * ((pixel >> 8) & 0xFF) +
                117 * (pixel & 0xFF)) >> 10;
            matrix[offset + x] = (byte) luminance;
          }
        }
        return matrix;
      }

     
      public boolean isCropSupported() {
        return true;
      }

     
      public LuminanceSource crop(int left, int top, int width, int height) {
        return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
      }

      // Can't run AffineTransforms on images of unknown format.
     
      public boolean isRotateSupported() {
        return image.getType() != BufferedImage.TYPE_CUSTOM;
      }

     
      public LuminanceSource rotateCounterClockwise() {
        if (!isRotateSupported()) {
          throw new IllegalStateException("Rotate not supported");
        }
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();

        // Rotate 90 degrees counterclockwise.
        AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);

        // Note width/height are flipped since we are rotating 90 degrees.
        BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, image.getType());

        // Draw the original image into rotated, via transformation
        Graphics2D g = rotatedImage.createGraphics();
        g.drawImage(image, transform, null);
        g.dispose();

        // Maintain the cropped region, but rotate it too.
        int width = getWidth();
        return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width),
            getHeight(), width);
      }

    }

Now it's the main class

public class UsingZxingToWrite {
    // Change with other file directory
    private static final String FILENAME = "E:\\test.jpg";
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    /**
     * Renders a {@link BitMatrix} as an image, where "false" bits are rendered
     * as white, and "true" bits are rendered as black.
     */
    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }

    // In case the golden images are not monochromatic, convert the RGB values
    // to greyscale.
    private static BitMatrix createMatrixFromImage(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        int[] pixels = new int[width * height];
        image.getRGB(0, 0, width, height, pixels, 0, width);

        BitMatrix matrix = new BitMatrix(width, height);
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int pixel = pixels[y * width + x];
                int luminance = (306 * ((pixel >> 16) & 0xFF) + 601
                        * ((pixel >> 8) & 0xFF) + 117 * (pixel & 0xFF)) >> 10;
                if (luminance <= 0x7F) {
                    matrix.set(x, y);
                }
            }
        }
        return matrix;
    }

    public static void main(String[] args) {

        int w = 300;
        int h = 300;

        // get a byte matrix for the data
        BitMatrix bitMatrix;
        com.google.zxing.Writer writer = new MultiFormatWriter();
        try {
           
            bitMatrix = writer.encode(
                    "AIRWAYID892045",
                    //new String(contents, Charset.forName("ISO-8859-1")),
                    BarcodeFormat.QR_CODE, w, h);
        } catch (com.google.zxing.WriterException e) {
            // exit the method
            return;
        }

        File f = new File(FILENAME);
        BufferedImage image = toBufferedImage(bitMatrix);
        try {
            System.out.println("Generating... " + f.getAbsolutePath());

            ImageIO.write(image, "jpg", f);
            System.out.println("Done");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        MultiFormatReader reader = new MultiFormatReader();

        BufferedImage capturedImage;
        try {
            capturedImage = ImageIO.read(new File(FILENAME));
           
            // creating luminance source
            LuminanceSource source = new BufferedImageLuminanceSource(capturedImage);
            BinaryBitmap bitmap = new BinaryBitmap(new  HybridBinarizer(source));

            Hashtable hints = new Hashtable(1);
            Vector formats = new Vector(1);

            formats.addElement(BarcodeFormat.QR_CODE);
            hints.put(DecodeHintType.POSSIBLE_FORMATS,formats);
           
            Result result;
            try {
                result = reader.decode(bitmap);
                System.out.println(result.getText());
            } catch (NotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

Here's the sample output


Thursday, November 17, 2011

ARCHANGEL GATHERING 2011

Archangel Guild

2011 Gathering


Lokasi: T-REX Gandaria City
Hari/Tanggal: Sabtu, 17 December 2011
Jam: 13.00-16.00

  • Include BUFFET food
  • DOORPRIZE
    • Archangel Stone
    • Voucher ALO
    • Phantom Egg
    • Polar MOM
    • Kostum merah 1 set tanpa main hand
    • Advanced Descent Certificate
    • Wedding Cake 
    • Gravity Set
    • Natural Egg
    • Product Typelv9 High speed gear

Pendaftaran ke: levantz

Yang sudah mendaftar:
1. Levantz
2. omen666
3. constrea
4. warior_a
5. jessicaimoet
6/7 masmodi + pasangan
8. albrastos
9. hensung
10/11. homunculus + pasangan
12. lordlazy
13. mimiir
14. goldchipmunk
15. night-hunt3r
16. [ex]Pr1nc3
17. Kenji

18. Zefa (Skalangelz)
19. Penguin (destiny)

20. Lara (pantheon)
21/22. Byllrexus + pasangan
23. Anderson
24. littlebuddha
25. Fines
26. erry_dwi (destiny)
27. ShineHikari


ARCHANGEL

Archangel Guild

Angel Love Online Indonesia www.angelloveonline.net

Founded on 24 Dec 2007 by hensung in Holylight with pioneer members TTF999, taufat, devilman89, LordCrazy/LordLazy, Pythia, alexian, Quiet999, etc. Then come more member join with omen666, levantz, polokoku, Vaynard, allnes and other members.

In first generation using initial logo as follow
 There are more other logo submitted by ruru


The RedMoon guild merge to ARCHANGEL, with members, dadi, Albrastos, masmodi, Lamose, AngelinaWu, gun4liv, M-Ray, Lienzz, etc.

Merger with most members from CipiTos, Zhou-Yun, SilentShadow, Kutasf, Dheppa.
More on member joins Homunculus, Rgye, [Ex]Pr1nce, Sea, revero, jessicaimoet, warior_a, zelma, GoldChipmunk, mimiir.
More on member joins, my brother leganoor and my cousin Ruoha.
The Leader change to levantz (myself) and renew the logo



"T-Shirt" with this new ARCHANGEL logo is released for guild members.
More on members join, when the 2 server merge. Nguik, pakyu_d_f_j, mariani, Stealth, Myth, LittleBuddha, Elendia, AngelicaWu, eng_kong, luxio, Ruby_Tuesday, Princesonz, Chronosholy, Paulbod

More on new members jhin, odey, BanditX, ShadowCry, Zulfakar, -NH-Sephi, Night-Hunt3r, playhards, Ryuucarr, Shyfa, Ken, -Kenji-, Bellstar, [Anderson], Constrea, Rukkha, Airith, m3imutz, -shinta-, ricky_28, closehead_79, pisbul, Fines, TheBlackCopz, etc. enhance more strength, friendship, and collaboration.

Vision
"Fight with Honour, Pride, Glory"

We believe that we can do our best together, there's no successful for one person, it's a success for one team, one guild, all of members. Even sometime we are lost or get fail, we still fight with Honour, Pride, and Glory, and will make best effort in another chance.

Our programs
  1. Quest on weekend/spare time
  2. Guild event with prize
  3. Exchange equip/weapon with members
  4. Regular gathering
  5. Emblem WAR
  6. BLT (Bantuan Langsung Tunai) - All members receive free reward such as gold or items

Friday, November 4, 2011

Hash string using MD5

This is an example function to create MD5 hash
Usage:

SecurityUtil.md5("somestring")

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Utility class for security hashing
 *
 * @author Ali Irawan
 * @version 1.0
 */
public class SecurityUtil {

    public static String md5(String text) throws NoSuchAlgorithmException,
            UnsupportedEncodingException {
        MessageDigest md;

        md = MessageDigest.getInstance("MD5");
        byte[] md5Hash = new byte[32];

        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5Hash = md.digest();
        return convertToHex(md5Hash);
    }

    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while (two_halfs++ < 1);
        }
        return buf.toString();
    }
}