袜子服务器和客户端

问题描述

我有以下袜子服务器

my $socks_server = IO::Socket::Socks->new(
  ProxyAddr   => "localhost",ProxyPort   => 8000,Listen      => 1,) or die "socket error";
 
while(1) {
  my $client = $socks_server->accept();
  print $client;
  unless ($client) {
    print "ERROR:";
    next;
  }
}
 

以及以下的Socks Client

use strict;
use warnings;
use IO::Socket::Socks;
 
my $socks_client = IO::Socket::Socks->new(
  ProxyAddr   => "localhost",ProxyPort   => "8000",) or die $SOCKS_ERROR;
 
print $socks_client "foo\n";
$socks_client->close();

Socks客户端打印“ foo \ n”,如何在接收到Socks服务器后将其打印到控制台?

解决方法

以下代码仅用于演示目的,为简单起见,关闭了身份验证。

该代码基于IO::Socket::Socks的文件加密

server.pl

的代码
use strict;
use warnings;
use feature 'say';

use IO::Socket::Socks ':constants';

my $SOCKS_ERROR = 'Error: SOCKS';
 
my $socks_server = IO::Socket::Socks->new(
  ProxyAddr   => "localhost",ProxyPort   => 8000,Listen      => 1,UserAuth    => \&auth,RequireAuth => 0
) or die $SOCKS_ERROR;
 
while(1) {
  my $client = $socks_server->accept();
   
  unless ($client) {
    print "ERROR: $SOCKS_ERROR\n";
    next;
  }
 
  my $command = $client->command();
  if ($command->[0] == CMD_CONNECT) {
     # Handle the CONNECT
     $client->command_reply(REPLY_SUCCESS,'localhost',8000);
  }
   
  print while <$client>;
   
  $client->close();
}
 
sub auth {
  my ($user,$pass) = @_;
   
  return 1 if $user eq "foo" && $pass eq "bar";
  return 0;
}

client.pl

的代码
use strict;
use warnings;
use feature 'say';

use IO::Socket::Socks;
 
my $socks_client = IO::Socket::Socks->new(
  ProxyAddr   => "localhost",ProxyPort   => "8000",ConnectAddr => "localhost",ConnectPort => "8022",) or die $SOCKS_ERROR;
 
print $socks_client $_ for <DATA>;

$socks_client->close();

__DATA__
-----------------------------------------------
This a test message sent from remote client for
SOCKS demonstration code.

Enjoy your day.

server.pl 端的输出

C:\....\examples\socks_server.pl
-----------------------------------------------
This a test message sent from remote client for
SOCKS demonstration code.

Enjoy your day.

client.pl 端的输出

C:\...\examples\socks_client.pl
C:\...>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...