iptables: portue berzuzendu beste portu batera

Aupa aspaldikos (iñok leiduten badozten klaro). Ez dakitz ondo badauen “berzuzendu” berbie, baie euskalterm barridxegaz enai aklaraten eta bueno haulan lotzien da, ingelesezkue “redirect” berbie da. Zelan berzuzendu portu bet, beste batera, eta zetako?

Hasteko ta behin ia, gnu/linux-eko sistemako erabiltzaile “normalok” eztaku baimenik 1024 baño berauko porturen baten prosezurik imintzeko, orduen “root” moduen imili biher duz martxan, hori peligrosue al da ezan, haulan dala ta erabiltzaile normal moduen imiñi martxan prosezue eta gero iptables-egaz root moduen geuk gudun portora eruen, zelan?

su
iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8888
iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8888
exit

1go erregeliek ezpadun imintzen ezin izangu gerue “localhost”-en probak egin.

Neure kaso konkretue, node-gaz okinjot aplikaziño bat martxan 8888 puertuen baie oin 80-gaz sartzien dast ;)

http://serverfault.com/questions/211536/iptables-port-redirect-not-working-for-localhost

jsonp: adibide sinple bat

Zer da jsonp? funtziño baten barruen json bat sartutie dominidxuen artien json-egaz biar eitzeko.

Zetako erabiltzen da? Ajax teknologidxek beran limitaziñuek dakoz, euretariko bat da, beste dominio batzukaz ezin zarala komunike, ta haurretarako erabiltzen da jsonp.

Zelan dabil? SCRIPT tag- bat sortu HTML-ien eta beste dominioko URL-ie bertan karga. URL honek ekarko dau ejekuteko dauen funtziñuen izena eta barruen json bat.

Adibide total sinple bat:

Geure HTML:

<!DOCTYPE html>
<html>
<head>
<title>jsonp example</title>
<script type="text/javascript">
window.onload = function()
{
  jsonp('http://bestedominiobat.com/jsonp.php');
};

function jsonp_callback(res)
{
  if(console)
    console.log(res);
}

function jsonp(url)
{
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = url;

  var head = document.getElementsByTagName('head')[0];
  head.appendChild(script);
}
</script>
</head>
<body>
</body>
</html>

http://bestedominiobat.com/jsonp.php

<?php
header("Content-Type: application/json");

$json = 'jsonp_callback(';
$json .= "{'result' : 'jsonp adibidie :)'}";
$json .= ');';

echo $json;
?>

ikusten dun moduen jsonp.php artxibuen: jsonp_callback(php)-an barruen sartun du json-a, orduen javascript-eko izen bardiñeko funtziñue ejekuteko da jsonp_callback(js).

node.js socket.io: socket bakotzeko sesiñue sortu: txat-an bigarren partie

Bueno ba hamen gabiltzez node.js eta socket.io-gaz barriro be, 1go partie ikusteko hamen linke:
http://zital.hackinbadakigu.net/?p=273
functions.js:

module.exports =
{
        in_array: function (needle, haystack, argStrict)
        {
                // Checks if the given value exists in the array
                //
                // version: 1109.2015
                // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
                // +   improved by: vlado houba
                // +   input by: Billy
                // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
                // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
                // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
                // *     returns 2: false
                // *     example 3: in_array(1, ['1', '2', '3']);
                // *     returns 3: true    // *     example 3: in_array(1, ['1', '2', '3'], false);
                // *     returns 3: true
                // *     example 4: in_array(1, ['1', '2', '3'], true);
                // *     returns 4: false
                var key = '',        strict = !! argStrict;

                if (strict)
                {
                        for (key in haystack)
                        {
                                if (haystack[key] === needle)
                                {
                                        return true;
                                }
                        }
                }
                else
                {
                        for (key in haystack)
                        {
                                if (haystack[key] == needle)
                                {
                                        return true;
                                }
                        }
                }
                return false;
        }
};

server.js:

var functions = require('./functions');

var users_available = ['zital', 'hqh'];
var clients = {};

var server = require("socket.io").listen(6969);

server.sockets.on("connection", function(message)
{
        message.on("login", function(user)
        {
                if(functions.in_array(user, users_available))
                {
                        clients[message.id] = message;
                        clients[message.id].emit("isLogged");
                }
        });

        message.on("newMessage", function(data)
        {
                for(i in clients)
                        clients[i].emit("sendMessage", data);
        });

        message.on("logout", function()
        {
                var tmp = {};
                for(i in clients)
                {
                        if(i!=message.id)
                                tmp[i] = clients[i];
                }
                clients = tmp;
        });
});

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Online chat</title>
<style>
body
{
    color: #fff;
    background: #333;
    font-family: "Helvetica", Arial;
    font-size: 14px;
    text-align: center;
}
.container
{
    background: #ccc;
    border-radius: 1em;
    box-shadow: 0px 5px 5px rgba(0,0,0,0.5);
    text-shadow: 5px 5px 5px rgba(0,0,0,0.5);
    margin: 1em auto;
    padding: 1em;
    width: 90%;
}

input
{
    display: block;
    font-size: 12px;
    margin: 1em auto;
    padding: 0.5em;
    width: 95%;
}

span
{
    display: block;
    font-size: 12px;
    margin: 1em auto;
    padding: 0.5em;
    width: 95%;
    text-align: left;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="socket.io.js"></script>
<script type="text/javascript">
<!--
// IP PUBLIKUE IMI??I HAMEN
var websocket = io.connect("http://10.211.252.10:6969");

$(document).on('ready', function()
{
    $('#f_user').css('display', 'none');
    $('#f_message').css('display', 'none');
    $('#login').trigger('focus');

    websocket.on("isLogged", function()
    {        
        var login = $('#login');
        var user = login.val();

        $('#f_user > label').html('welcome: '+user);        

        $('#f_user').css('display', 'block');
        $('#f_message').css('display', 'block');
        $('#f_login').css('display', 'none');

        $('#message').trigger('focus');
    });

    websocket.on("sendMessage", function(data)
    {
        var chat = $('#zchat');
        var span = $('<span>'+data+'</span>');

        if (chat.children().length > 0 )
            chat.find(">:first-child").before(span);
        else
            chat.append(span);
    });

    $('#f_user').on('submit', function(e)
    {
        e.preventDefault();
        websocket.emit("logout", {});
        $('#f_user').css('display', 'none');
        $('#f_message').css('display', 'none');
        $('#f_login').css('display', 'block');
    });    

    $('#f_login').on('submit', function(e)
    {
        e.preventDefault();
        websocket.emit("login", $('#login').val());        
    });       

    $('#f_message').on('submit', function(e)
    {
        e.preventDefault();
        websocket.emit("newMessage", $('#message').val());
        $('#message').val('');
        $('#message').trigger('focus');
    });    
});
//-->
</script>
</head>
<body>
<div>
    <form id="f_user">
        <label></label>
        <input type="submit" value="logout" />
    </form>
    <form id="f_login">
        <label>login: </label>
        <input type="text" name="login" id="login" required />
        <input type="submit" />
    </form>
    <form id="f_message">
        <label>Message: </label>
        <input type="text" name="message" id="message" required />
        <input type="submit" />
    </form>
</div>
<div id="zchat">
</div>
</body>
</html>

socket.io.js:
http://pastebin.com/shJTm0JE

Oin martxan imintzeko:
server.js eta functions.js path bardiñen on bidiez, eta index.html eta socket.io.js bebai :)

node server.js

eta nabegadoretik index.html igiri

Non dau trukue? clients bariablien barruen sartutiegaz konektaten dizen erabiltzailiek, ta gero logout einkeran bertatik kendu.

php: reflection bidez metodo dinamikuek erabili

php-ntzako reflection API-dxe erabilitze, al’tzagu dinamismue emon geure programari, adibidez metodo dinamikueri deitsu, jakin barik ze motatako metodue dan, adibidez:

<?php
$class = 'a';
$method = 'b';
$params = array('d' => 'e');

// klasie existiten badan
if(class_exists($class)) 
{
        // metodue existiten badan
        if(method_exists($class, $method))
        {
                // reflection objetue sortu
                $reflection = new ReflectionMethod($class, $method);
                // metodue estatikue badan
                if($reflection->isStatic())
                {
                        $f = $class."::".$method;
                        $result = call_user_func($f, $params);
                }
                // metodue publikue badan leleu klasien instantzidxe sortu bi'de
                elseif($reflection->isPublic())
                {
                        $instance = new $class();
                        $result = $instance->$method($params);
                }
        }
}
if(isset($result))
{
        echo "<pre>";
        print_r($result);
        echo "</pre>";
}

class a
{
        public static function b($params = NULL)
        {
                return array('b' => $params);
        }
        public function c($params = NULL)
        {
                return array('c' => $params);
        }
}
?>

Erantzune:
<pre>Array                                                 
(                                                        
    [b] => Array                                                                                                                                                                   
        (                                                                                                                                                                          
            [d] => e                                                                                                      
        )                                                        
)                                                        
</pre>

Oin metodue ‘c’ imindxe:

$method = 'c';

Erantzune:

<pre>Array                                                 
(                                                        
    [c] => Array                                                                                                                                                                   
        (                                                                                                                                                                          
            [d] => e                                                                                                      
        )                                                        
)                                                        
</pre>

Gauze gidxau al diez egin, horretarako leidu RELECTION PHP API-dxe :)

ftp: binary vs ascii – checksum vs checksum

Zelan emon lei artxibo bardiñen checksum-ek ezberdin?

# binaidxo
md5sum -b config.xml 
263fe7751b96df9fc94fa4d6af2cfbb7 *config.xml
# texto moduen
md5sum -t config.xml 
263fe7751b96df9fc94fa4d6af2cfbb7 *config.xml
# binaidxo
md5sum -b config.xml.bak 
e96f4aa7b3a207778b904780db8d141f *config.xml.bak
# texto moduen
md5sum -t config.xml.bak 
e96f4aa7b3a207778b904780db8d141f *config.xml.bak

Sistema bakotzak beran “enter“-ak sortuten dauzelako kontuz okin biher da FTP bidez BINARY edo ASCII moduen gauzek botatiegaz:

file config.xml 
config.xml: UTF-8 Unicode text

file config.xml.bak 
config.xml.bak: UTF-8 Unicode text, with CRLF line terminators

Ikusten dan moduen ezta bardiñe FTP bidez ASCII moduen edo BINARY moduen botatie, holakorik ez pasateko bota beti BINARY moduen duderik dakozuieniko :)