如何保证私有 Dabr 的安全

在如今的互联网环境下,像我这样的重度Twitter使用者必须配备以下设备才能存活:1,VPN;2,iPhone;3,Dabr。当然,还有自建API这种高级产品,但因为我能力有限,实在是没有机会去好好研究一把。

前几天,艺术家刘淼老师(@liumiao)在blog上写了一篇文章《不求人也能获得大量twitter第三方客户端地址的方法》,这唤醒了我。因为Dabr这种东西,用惯之后被封杀还是很痛苦的。这篇文章就是我的一些想法,谈谈如何保证私有Dabr的安全。其实就两点:

  1. 增加授权用户名单
  2. 为跳出链接做个重定向
Add Anonymity to private dabr
进一步保护私有Dabr

授权用户名单

安装完Dabr并做完必要的设置之后,请手工修改/config.php文件,增加一个函数。代码如下:

function config_log_request() {
  // This function is called from menu.php if it exists

  // Ignore people that aren't logged in
  if (!user_is_authenticated()) return;
  // Create a *lowercase* list of allowed users
  $allowed_users = array(
    'bfishadow',
  );

  // Check if the current user is in our allowed user list
  if (!in_array(strtolower(user_current_username()), $allowed_users)) {
  // They're not, kick them out!
    user_logout();
    die("Sorry, you're not on the list of allowed users.");
  }
}

把授权用户的ID放在 $allowed_users 数组里即可,一行一个,全小写。上面这段代码表明,只有 @bfishadow 才可以用。

 

链接重定向

这需要两个步骤,第一步需要在你的服务器上做一个跳转用的PHP文件,比如 /go.php ,代码如下:

<?php
  //get value of parameter URL
  $Url = $_GET['url'];
  //meta jump
  echo '<html><head><meta http-equiv="refresh" content="1;url='.$Url.'"></head><body></body></html>';
?>

主要目的就是接收一个地址,然后使用META方式跳转到目的地。为什么要用META跳转,稍后给解释。

第二步,请打开 common/twitter.php 文件,手工修改其中一个函数,搜索: function replacementURLs($matches) 可以快速定位。

原来的代码:

function replacementURLs($matches) {
    $replacement  = $matches[2];
    $url = $matches[3];
    if (!preg_match("#^https{0,1}://#i", $url)) {
      $url = "http://{$url}";
   }
   if (setting_fetch('gwt') == 'on') {
     $encoded = urlencode($url);
     $replacement .= "<a href='http://google.com/gwt/n?u={$encoded}' target='_blank'>{$url}</a>";
   } else {
     $replacement .= theme('external_link', $url);
   }
   return $replacement;
}

修改完毕的代码:

function replacementURLs($matches) {
  $replacement  = $matches[2];
  $url = $matches[3];
  if (!preg_match("#^https{0,1}://#i", $url)) {
    $url = "http://{$url}";
  }
  if (setting_fetch('gwt') == 'on') {
    $encoded = urlencode($url);
    $replacement .= "<a href='http://google.com/gwt/n?u={$encoded}' target='_blank'>{$url}</a>";
  } else {
    //mod hide referer
    //$replacement .= theme('external_link', $url);
    $encoded = urlencode($url);
    $replacement .= "<a href='http://xxx.com/go.php?url={$encoded}' target='_blank'>{$url}</a>";
  }
  return $replacement;
}

这段代码的意思是说,如果没有打开gwt(Google Web Transcoder),那么Dabr会直接给出外部链接。当你点击的时候,你的Private Dabr地址将作为referer传递出去。为了避免此问题,我在这里作了一个Hack,把目的地URL作为参数传递到之前制作的跳转页,然后由跳转页来传递referer参数,从而实现隐藏Private Dabr地址的目的。

到此,任务完成。我们来做一下测试,访问 http://gongm.in/beta/refchk.php 的时候,看它是否还显示你的Private Dabr地址。如果没有,那么目标达成。

ps. 最后说说为什么要用META跳转,而不是302或者是其他。请看一下这篇文章,它的本意是解决referer丢失的问题,但恰好帮上了我的忙,呵呵。我觉得,META跳转不传递referer应该是浏览器的Bug。站在网站主的立场上来看,没人喜欢这么做。但在这个特殊的环境下,能小心点就多小心点吧。

8 Replies to “如何保证私有 Dabr 的安全”

  1. 在我没有自己搭建时,我靠点别人链接来获取第三方客户端的。开始自己搭建之后,就一直使用API+Tweetdeck来上推了。

  2. 赞,给自己的dabr加上了。另外,最好加个判断,现在直接访问http://xxx.com/go.php会进入死循环的…

    类似这样:

    $YOURDOMAIN = “xxx.com”;
    if (! preg_match(“/https?://$YOURDOMAIN//”, $URL)) {
    //meta jump
    echo ”;
    } else {
    echo “don’t fool me.”;
    }

Leave a Reply to Lauyu Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.