2010-10-22 133 views
1

我正在將tor集成到我的Delphi應用程序中;整個故事is explained in this link將PHP代碼移植到Delphi代碼

後,我在網上搜索,然後我找到了一個代碼,在PHP

function tor_new_identity($tor_ip='127.0.0.1', $control_port='9051', $auth_code=''){ 
    $fp = fsockopen($tor_ip, $control_port, $errno, $errstr, 30); 
    if (!$fp) return false; //can't connect to the control port 

    fputs($fp, "AUTHENTICATE $auth_code\r\n"); 
    $response = fread($fp, 1024); 
    list($code, $text) = explode(' ', $response, 2); 
    if ($code != '250') return false; //authentication failed 

    //send the request to for new identity 
    fputs($fp, "signal NEWNYM\r\n"); 
    $response = fread($fp, 1024); 
    list($code, $text) = explode(' ', $response, 2); 
    if ($code != '250') return false; //signal failed 

    fclose($fp); 
    return true; 
} 

切換新身份的任何一個可以幫助我端起這德爾福/帕斯卡爾

我不知道任何PHP基礎知識提前

感謝

問候

回答

4

警告:我沒有在IDE中寫過這個,但任何語法錯誤應該很容易修復。圍繞「發送一個命令,讀一行,看看它是否是一個250響應代碼」的邏輯實際上應該被拉到一個單獨的函數中。我還沒有這麼做,所以代碼更接近於原始的PHP。 (我有一個CheckOK,因爲我無法阻止自己。)

function CheckOK(Response: String): Boolean; 
var 
    Code: Integer; 
    SpacePos: Integer; 
    Token: String; 
begin 
    SpacePos := Pos(' ', Response); 
    Token := Copy(Response, 1, SpacePos); 
    Code := StrToIntDef(Token, -1); 
    Result := Code = 250; 
end; 

function TorNewIdentity(TorIP: String = '127.0.0.1'; ControlPort: Integer = 9051; AuthCode: String = ''): Boolean 
var 
    C: TIdTcpClient; 
    Response: String; 
begin 
    Result := true; 

    C := TIdTcpClient.Create(nil); 
    try 
    C.Host := TorIP; 
    C.Port := ControlPort; 
    C.Connect(5000); // milliseconds 

    C.WriteLn('AUTHENTICATE ' + AuthCode); 
    // I assume here that the response will be a single CRLF-terminated line. 
    Response := C.ReadLn; 
    if not CheckOK(Response) then begin 
     // Authentication failed. 
     Result := false; 
     Exit; 
    end; 

    C.WriteLn('signal NEWNYM'); 
    Response := C.ReadLn; 
    if not CheckOK(Response) then begin 
     // Signal failed. 
     Result := false; 
     Exit; 
    end; 
    finally 
    C.Free; 
    end; 
end; 
+0

你的CheckOK有一個明顯的錯誤:它被聲明爲Integer,但是'Result'類型(和它在TorNewIdentity中被調用的類型)是一個布爾表達式。 – 2010-10-22 20:37:18

+0

+1因爲_因爲我無法阻止自己_:D – jachguate 2010-10-22 21:20:57

+0

@梅森爲我寫了一個文本編輯器。謝謝! – 2010-10-23 06:54:59

1

在前面的回答的一個小錯誤。在功能中CheckOK 結果:=代碼<> 250; 應改爲 結果:=(Code = 250);

PS:對不起,我沒有辦法發表評論到原來的答案。

+0

你說得很對。相應修改! – 2011-01-09 14:54:54