Jump to content
Fr0sT.Brutal

SetURL & SetProxyURL helper methods for TWSocket

Recommended Posts

Here are helper methods for quick and easy setting of remote URL (host:port) and proxy URL (proto://[user:password@]host[:port] where "proto" = 'socks5' or 'http') and if port is empty, proto-specific default value is assigned

 

use OverbyteIcsUrl ...

const
  PROXY_PROTO_HTTP = 'http';
  PROXY_PROTO_SOCKS5 = 'socks5';
  PROXY_PORT_HTTP = '8080';    // HTTP tunnel default port
  PROXY_PORT_SOCKS5 = '1080';  // Socks5 default port

type
  TfrWSocket = class(TWSocket)
  public
    procedure SetURL(const URL: string);
    procedure SetSocks(const Host: string = ''; const Port: string = PROXY_PORT_SOCKS5;
      const User: string = ''; const Pass: string = '');
    procedure SetHTTPTunnel(const Host: string = ''; const Port: string = PROXY_PORT_HTTP;
      const User: string = ''; const Pass: string = '');
    procedure SetProxyURL(const URL: string);
  end;

// Set combined 'host:port' string. Accepts any URL string but raises exception
// if it contains anything more than host and port (other parts are unused at
// socket level)
procedure TfrWSocket.SetURL(const URL: string);
var Proto, User, Pass, Host, Port, Path: String;
begin
  if URL <> '' then
    ParseURL(URL, Proto, User, Pass, Host, Port, Path);
  // check that no more than host and port assigned
  if (Proto <> '') or (User <> '') or (Pass <> '') or (Path <> '') then
  begin
    RaiseException('Error with URL "' + URL + ' - it must contain only host and port');
    Exit;
  end;
  Self.Addr := Host;
  Self.Port := Port;
end;

// Set socks requisites by single function. If Host is empty, cleanup everything
procedure TfrWSocket.SetSocks(const Host, Port, User, Pass: string);
begin
  // Cleanup everything
  if Host = '' then
  begin
    SocksServer := '';
    SocksPort := '';
    SocksAuthentication := socksNoAuthentication;
    SocksUsercode := '';
    SocksPassword := '';
    Exit;
  end;

  SocksServer := Host;
  SocksPort := Port;

  if (User = '')
    then SocksAuthentication := socksNoAuthentication
    else SocksAuthentication := socksAuthenticateUsercode;
  SocksUsercode := User;
  SocksPassword := Pass;
end;

// Set http proxy requisites by single function. If Host is empty, cleanup everything
procedure TfrWSocket.SetHTTPTunnel(const Host, Port, User, Pass: string);
begin
  // Cleanup everything
  if Host = '' then
  begin
    HttpTunnelServer := '';
    HttpTunnelPort := '';
    HttpTunnelAuthType := htatNone;
    HttpTunnelUsercode := '';
    HttpTunnelPassword := '';
    Exit;
  end;

  HttpTunnelServer := Host;
  HttpTunnelPort := Port;

  if (User = '')
    then HttpTunnelAuthType := htatNone
    else HttpTunnelAuthType := htatBasic;
  HttpTunnelUsercode := User;
  HttpTunnelPassword := Pass;
end;

// Set HTTP or Socks proxy URL: 'proto://[user:password@]host[:port]' where "proto" = 'socks5' or 'http'
// If port is empty, proto-specific default value is assigned
procedure TfrWSocket.SetProxyURL(const URL: string);
var Proto, User, Pass, Host, Port, Path: String;
begin
  // Cleanup everything
  if URL = '' then
  begin
    SetSocks('');
    SetHTTPTunnel('');
    Exit;
  end;

  ParseURL(URL, Proto, User, Pass, Host, Port, Path);
  // check that Proto and Host are assigned
  if (Proto = '') or (Host = '') then
  begin
    RaiseException('Error with proxy URL "' + URL + ' - it must contain proto and host');
    Exit;
  end;
  // check that Path is not assigned
  if (Path <> '') then
  begin
    RaiseException('Error with proxy URL "' + URL + ' - it must not contain path');
    Exit;
  end;

  // switch proto
  if Proto = PROXY_PROTO_HTTP then
    SetHTTPTunnel(Host, Port, User, Pass)
  else
  if Proto = PROXY_PROTO_SOCKS5 then
    SetSocks(Host, Port, User, Pass)
  else
    RaiseException('Error with proxy URL "' + URL + ' - Unknown proxy protocol');
end;

They're implemented as descendant's methods but you can turn them into functions.

Methods require OverbyteIcsUrl unit so I'm unsure if they could be suggested to merge into upstream

Edited by Fr0sT.Brutal
add default ports

Share this post


Link to post

Thanks, will have a look next week.  BTW, your last proxy fix is in SVN now, seems I broke it adding international domain name support.

 

Angus

 

Share this post


Link to post
4 minutes ago, Angus Robertson said:

Thanks, will have a look next week.  BTW, your last proxy fix is in SVN now, seems I broke it adding international domain name support.

 

Angus

 

Nice to know! The lesser personal changes to the main upstream I have to keep, the better 🙂

I was unsure if adding OverbyteIcsUrl to OverbyteIcsWSocket is an option.

Share this post


Link to post
Quote

I was unsure if adding OverbyteIcsUrl to OverbyteIcsWSocket is an option.

Url is not a large unit and probably linked into most applications anyway, except the simplest, so it can happen.  I've also just saved a lot of space by removing a lot of legacy OpenSSL support code.

 

Angus

 

Share this post


Link to post

I've added  SetSocks and SetHTTPTunnel to TCustomSocksWSocket, with a new ProxyURL property.  Not in SVN yet.

 

I can test HTTP Tunnelling through the ICS Proxy Server I have running on my public servers, but can not find any samples that use it.  We do have OverbyteIcsSocksTst, but I don't currently have a SOCKS server running. 

 

Less sure about SetURL() which only sets the wsocket host and port, is that really worthwhile?

 

Angus

 

Share this post


Link to post
12 hours ago, Angus Robertson said:

I've added  SetSocks and SetHTTPTunnel to TCustomSocksWSocket, with a new ProxyURL property.  Not in SVN yet.

Wonderful! 🙂

12 hours ago, Angus Robertson said:

I can test HTTP Tunnelling through the ICS Proxy Server I have running on my public servers, but can not find any samples that use it.  We do have OverbyteIcsSocksTst, but I don't currently have a SOCKS server running.

You can check with powerful opensource 3proxy ( https://github.com/z3APA3A/3proxy ) or SSH tunnel or Tor or any other software you like more. My ICS-powered app is running thru 3proxy very well

12 hours ago, Angus Robertson said:

Less sure about SetURL() which only sets the wsocket host and port, is that really worthwhile?

No problem, it's a convenience method for my own socket class but it surely does too little to be added to upstream.

Share this post


Link to post

The proxy setters changes are now in SVN, with an updated OverbyteIcsSocksTst sample that now also tests HTTP Tunnelling, which we did not seem to do. Will be zipped overnight.

 

I tested the HTTP Tunnelling changes against the ICS proxy server, which currently deliberately only tunnels HTTP, need to fix that, but have not tested socks since 7Proxy failed my 10 minutes to find out how to configure it limit, sure it works fine once you find some non-empty help files.

 

Angus

 

  • Like 1

Share this post


Link to post

Looks nice, thanks!

16 hours ago, Angus Robertson said:

have not tested socks since 7Proxy failed my 10 minutes to find out how to configure it limit, sure it works fine once you find some non-empty help files

You mean 3proxy? It could run with the most minimal config in the world 🙂

socks

Save it as 3proxy.cfg and just run exe and that's all, socks is available at #1080.

I'm not sure what "its limit" do you mean though.

Edited by Fr0sT.Brutal

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×