2010-10-22 151 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