将PHP代码移植到Delphi代码 [英] Porting PHP code to Delphi code

查看:77
本文介绍了将PHP代码移植到Delphi代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将tor集成到我的Delphi应用程序中;整个在此链接中说明



之后,我在互联网上搜索,然后找到了一个代码,用于在PHP中切换新身份

 函数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)返回false; //无法连接到控制端口

fputs($ fp, AUTHENTICATE $ auth_code\r\n);
$ response = fread($ fp,1024);
list($ code,$ text)= explode(’’,$ response,2);
if($ code!= 250)返回false; //验证失败

//将请求发送到新身份
fputs($ fp, signal NEWNYM\r\n);
$ response = fread($ fp,1024);
list($ code,$ text)= explode(’’,$ response,2);
if($ code!= 250)返回false; //信号失败

fclose($ fp);
返回true;
}

有人可以帮助我将其移植到Delphi / Pascal



我不知道任何PHP基础知识



预先感谢



解决方案

警告:我尚未在IDE中编写此代码,但是任何语法错误都应该易于修复。确实应该将发送命令,阅读一行,看看它是否为250响应代码这一逻辑放在一个单独的函数中。我还没有这样做,因此代码与原始PHP更加相似。 (我有一个CheckOK,因为我无法阻止自己。)

  function CheckOK(Response:String):Boolean; 
var
代码:整数;
SpacePos:整数;
令牌:字符串;
开始
SpacePos:= Pos(’’,Response);
令牌:=复制(Response,1,SpacePos);
代码:= StrToIntDef(Token,-1);
结果:=代码= 250;
结尾;

函数TorNewIdentity(TorIP:String =‘127.0.0.1’; ControlPort:Integer = 9051; AuthCode:String =‘’):布尔值
var
C:TIdTcpClient;
响应:字符串;
开始
结果:= true;

C:= TIdTcpClient.Create(nil);
试试
C.Host:= TorIP;
C.Port:= ControlPort;
C.Connect(5000); //毫秒

C.WriteLn(’AUTHENTICATE’+ AuthCode);
//我在这里假设响应将是一条CRLF终止的行。
响应:= C.ReadLn;
如果没有CheckOK(Response),则开始
//身份验证失败。
结果:= false;
出口;
结尾;

C.WriteLn(’Signal NEWNYM’);
响应:= C.ReadLn;
如果不是CheckOK(Response),则开始
//信号失败。
结果:= false;
出口;
结尾;
最终
C.Free;
结尾;
结尾;


I am working on integrating tor to my Delphi application; the whole story is explained in this link

After that I searched the internet then I found a code to switch new identity in 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;
}

Can any one help me porting this to Delphi/Pascal

I don't know any PHP basics

thanks in advance

regards

解决方案

Warning: I haven't written this in an IDE, but any syntax errors should be easy to fix. The logic around "send a command, read a line, see if it's a 250 response code" should really be pulled out into a separate function. I haven't done so so the code resembles the original PHP a bit more closely. (I have a CheckOK because I couldn't stop myself.)

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;

这篇关于将PHP代码移植到Delphi代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆