2010-03-30

Tcl 配合 Tor 取得不同 ip

可以用來做什麼就自己想像吧 XD

安裝 Vidilia,到 settings -> advanced 把 authentication 改成 none (也可以單獨跑 Tor,不過在 windows 上跑 vidilia 方便就直接用了)。Tor 跑起來後要用 Tcl 控制可以透過 control port (預設 9051),詳細內容在 Tor control protocol specification,應用可參考 Tor Control Commands 這篇文章。

以下是透過 Tor 取得不同 ip,若拿到的 ip 已經用過則再建立新 circuit。
proc get_ip {oip} {
  variable ipList
  variable sck
  set tok [http::geturl "http://whatismyip.org/" -timeout 10000 -headers [list Pragma no-cache]]
  set nip [http::data $tok]
  http::cleanup $tok
  while { $oip == $nip || $nip == "" || [string length $nip] > 15 || [lsearch $ipList $nip] != -1 } {
    set tok [http::geturl "http://whatismyip.org/" -timeout 10000 -headers [list Pragma no-cache]]
    set nip [http::data $tok]
    http::cleanup $tok            
    if { [lsearch $ipList $nip] != -1 } {
      set nip $oip
      puts $sck "signal newnym"
      after 5000
     }
  }
  lappend ipList $nip
  return $nip
}

http::config -proxyhost 127.0.0.1 -proxyport 8118
#connect to tor control port
set sck [socket 127.0.0.1 9051]
fconfigure $sck -buffering none -blocking 0 -encoding binary -translation crlf -eofchar {}
puts $sck {authenticate ""}
要更有效率可以先找出全部可用的 exit node 後再建立 two hop circuit,只是這樣得多很多很多行程式,這裡簡單的用 whatismyip.org 查看目前的 ip,若已用過再 signal newnym 要求 tor 建立一個新的 circuit。

Tcl 隨機產生身份證字號

平常都是用 javascript 或 php 檢查身分證號碼,想不到有一天會需要用到身分證號產生器 XD 規則網路上很多,用 tcl 寫也沒幾行。 
proc random_uid {sex} {
  set ret ""
  set wgt 0
  set cityList {A B C D E F G H J K L M N P Q R S T U V X Y W Z I O}
  set city [lpick $cityList]
  append ret $city
  set cityNum [expr [lsearch $cityList $city] + 10]
  set rndNum "$sex[expr {int(rand()*9000000+1000000)}]"
  append ret $rndNum
  set wgt [expr [expr [string index $cityNum 0] * 1] + [expr [string index $cityNum 1] * 9] ]
  set j 8
  for {set i 0} {$i<8} {incr i} {
    set wgt [expr $wgt + [expr [string index $rndNum $i ] * $j]]
    incr j -1
  }
  set chk [expr 10 - ($wgt % 10)]
  if {$chk == 10} {set chk 0}
  append ret $chk
  return $ret
}