2016年11月30日 星期三

VPN for internal service

Have to setup VPN for internal service, using AWS as VPN server
----
AWS VPN in 10 mins:
https://www.webdigi.co.uk/blog/2015/how-to-setup-your-own-private-secure-free-vpn-on-the-amazon-aws-cloud-in-10-minutes/
---
but I don't want all traffic go through VPN, I have to setup routes
using Apple script as below:

property version : "2016-12-01"
property theShellScript : "#!/bin/sh
rm /etc/ppp/ip-up
sudo echo '#!/bin/sh' >> /etc/ppp/ip-up
sudo echo '/sbin/route add -net "destination.com" -interface $1' >> /etc/ppp/ip-up
sudo echo '/sbin/route add -net google.com -interface $1' >> /etc/ppp/ip-up
chmod 0755 /etc/ppp/ip-up
cat /etc/ppp/ip-up"
do shell script theShellScript with administrator privileges

What it does is to put a ip-up file in /etc/ppp, when vpn is connect, the ip-up script will be executed, and route for  "destination.com" will be route through VPN

2016年4月13日 星期三

Update UITableViewCell from itself

The code that can update the cell itself.
// update UI on main thread
dispatch_async(dispatch_get_main_queue(), ^{
    //set cell need update
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [self setNeedsLayout];
    [self layoutIfNeeded];
    //trying to get the tableview
    id view = [self superview];
    // different level of UITableView wrapping in different iOS version,
    // for more detail please see:
    //  stackoverflow 
    while (view && [view isKindOfClass:[UITableView class]] == NO) {
        view = [view superview];
    }
    
    if (view) {
        UITableView *tableView = (UITableView *)view;
        NSIndexPath *indexPath = [tableView indexPathForCell:self];
        if (indexPath) {
            [tableView reloadRowsAtIndexPaths:@[indexPath] 
                             withRowAnimation:UITableViewRowAnimationNone];
        }
    }
});

2016年4月11日 星期一

[JavaScript]Get XMLHttpRequest Traffic

 Case: finding network traffic of a website, can't see relevant in the Console->network in Chrome, dig into code, it is XMLHttpRequest and not log in Console->Network

How: In console add hook into XMLHttpRequest

Code:
var rawOpen = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function() {
    if (!this._hooked) {
        this._hooked = true;
        setupHook(this);
    }
    rawOpen.apply(this, arguments);
}

function setupHook(xhr) {
    function getter() {
        console.log('get responseText');

        delete xhr.responseText;
        var ret = xhr.responseText;
        setup();
        console.log('url : %s',xhr.responseURL);
        console.log('ret : %s',ret);
        return ret;
    }

    function setter(str) {
        console.log('set responseText: %s', str);
    }

    function setup() {
        Object.defineProperty(xhr, 'responseText', {
            get: getter,
            set: setter,
            configurable: true
        });
    }
    setup();



Source : http://stackoverflow.com/questions/16959359/intercept-xmlhttprequest-and-modify-responsetext

2016年4月4日 星期一

[iOS] UITableView scroll to top when typing in the textfield will scroll to a wrong location

My case is this:

I have a static table, with hidden cell(height = 0), custom cell that will open keyboard, and a reset button(that will reload table and scroll to top)

What is the problem:

Scroll to top will go to invalid offset.

How to regenerate:

type in the keyboard, reset(which scroll to top and reload the keyboard) and there will be space between my top cell and top of table (just like hidden cell consumed some space)

---

At first I thought it is the hidden cell problem, but it turns out to be problem on [tableView reloadData]

**[tableView reloadData] will interrupt scrolling, so make sure you call [tableView setContentOffset:] or [tableView scrollToRowAtIndexPath:] after all reload is done.

Reference: stackoverflow

2015年11月17日 星期二

Paypal say "Data does not match input character set or default encoding" when WooCommerce Wordpress Checkout with Chinese words

Recently I am playing with Wordpress and WooCommerce shopping Cart plugin for wordpress to setup a simple E-commerce website, while I am using a plugin to add some extra info to my product ( WooCommerce Extra Fields ) with some chinese character, it randomly give me "Data does not match input character set or default encoding" during checkout.

After some digging on the network call, I found that the product name is in the Paypal link, and the chinese word is trimmed and become weird character.

I found the trim function wc_trim_string is the one to be blamed, and it is using php substr function.

The original wc_trim_string function in "woocommerce/include/wc-formatting-functions.php" contains the line:

 $string = substr( $string, 0, ( $chars - strlen( $suffix ) ) ) . $suffix;


i just replaced it with mb_substr and everything works like charm again. End result will look like:

 $string = mb_substr( $string, 0, ( $chars - strlen( $suffix ) ) ) . $suffix;


The reason is that substr function just trim without handling multiple-byte unicode character, and mb_substr is multi-byte safe.

This workaround should works on other multibyte/unicode character.

Note: Version of my WooCommerce is v2.4.8

2013年3月17日 星期日

[Android] AsyncTask blocking

In different Android version, there is different handling on AsyncTask, some are in single thread, processed one by one, some are multi-threaded and processed at the same time.

1.0-1.5(API 1-3) single thread.
1.6-2.3.3 (API4-10) multi thread.
3.0+ (API 11 or up) single thread.

but if you want to have AsyncTask run on seperate thread, you can call
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
instead of
    task.execute(params);

then the task will run on seperate thread, and not blocked by other threads.

2012年11月16日 星期五

[WPF]Background dynamic fill with one direction

I have a very long and thin image, and I want to use it as a background image in WPF application, which the image should repeat horizontally.
My first code insight is from
WPF: How do i create a background that repeats horizontally without scalling?
But, it fails to repeats properly, the reason is that ViewportUnits="Absolute", so it actually stretch my image to very large. Which is kind of hardcode.

Then I saw this reference TileBrush.Viewport Property
So I use this this code to create the background brush instead
  <imagebrush imagesource="/Resources/Image/pattern.png" 
            tilemode="FlipY" viewport="0,0,0.033,1" 
            viewportunits="RelativeToBoundingBox"
            x:key="BackGroundBrush">
Which 0.033:1 is the width:height ratio of my background. With this brush, the image will be stretch to suitable height.

For repeat horizontally, just change tilemode to FlipX and viewport to "0,0,1,x" where 1:x is the ratio.

p.s. this method is a improved version of the stackoverflow solution on the way that it can show the whole image, but the drawback is the image might be stretched for a little. And seems there is no simple way to do this without binding.