Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.49 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #Socks Proxy Server
  3.  
  4. ##############################################################################
  5. #
  6. # This script is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Library General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2 of the License, or (at your option) any later version.
  10. #
  11. # This script is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Library General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Library General Public
  17. # License along with this library; if not, write to the
  18. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. # Boston, MA 02111-1307, USA.
  20. #
  21. # Copyright (C) 2005 Emmanuel Elango.
  22. #
  23. ##############################################################################
  24.  
  25. BEGIN {
  26. require Errno;
  27. import Errno qw(EWOULDBLOCK EINPROGRESS);
  28. }
  29.  
  30. use Socket;
  31. use IO::Socket;
  32. use IO::Select;
  33. use IO::Socket::Socks;
  34. use FileHandle;
  35. use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
  36.  
  37.  
  38. #############################################
  39. #############################################
  40. # User configurable parameters
  41. #
  42. #Set the proxy hostname or ip below.
  43. $hostname = '0.0.0.0';
  44. #
  45. #Set the proxy port below.
  46. $hostport = '1186';
  47. #
  48. #Set the socks user id below. It is recommended to change the defaults below to prevent misuse of your proxy server.
  49. #Please note that the username and password are sent in clear text and can be easily sniffed on a network. For greater
  50. #security you could allow only certain hosts to connect by modifiying the IO::Socket::Socks module. A modified module
  51. #is available in the same directory as this script. Instructions are in the modifed module.
  52. $socksuser = 'foo';
  53. #
  54. #Set the password below.
  55. $socksuserpass = 'bar';
  56. #
  57. ###############################################
  58. ###############################################
  59.  
  60.  
  61.  
  62. #sub timeout();
  63.  
  64.  
  65. #daemonize?
  66.  
  67. # $pid = fork(); #
  68. # if ($pid != 0) #
  69. # { #
  70. # close (); #
  71. # exit(); #
  72. # } #
  73. # else #
  74. # { #
  75.  
  76.  
  77.  
  78. $SIG{PIPE} = 'IGNORE';
  79. #$SIG{ALRM} = \&timeout;
  80.  
  81. $server = IO::Socket::Socks->new( ProxyAddr=> $hostname,
  82. ProxyPort=> $hostport,
  83. Listen=>1,
  84. UserAuth=>\&auth,
  85. RequireAuth=>1,
  86. ) or die "Unable to bind server socket $!\n";
  87. if ($^O =~ m/Win32/)
  88. {
  89. my $temp = 1;
  90. #ioctl($server, 0x8004667E, \$temp);
  91. }
  92. else
  93. {
  94.  
  95. if ($flags = fcntl($server, F_GETFL, 0)<0)
  96. {
  97. die "Unable to get server socket flags\n";
  98. }
  99.  
  100. if (($flags = fcntl($server, F_SETFL, $flags | O_NONBLOCK)) < 0)
  101. {
  102.  
  103. die "Unable to set server socket to at non-block\n";
  104. }
  105.  
  106. }
  107.  
  108. $readable = IO::Select->new();
  109. $writable = IO::Select->new();
  110. $readable ->add($server);
  111. #print "Server ID $server \n";
  112. while (1)
  113. { #print "Entering Select\n";
  114. if (defined (($sockets_read, $sockets_write) = IO::Select->select($readable,$writable,undef)))
  115. {
  116. #print "Select returned\n";
  117. #$pause = <STDIN>;
  118. #print $sockets_read[0]->[0];
  119. foreach $sock (@$sockets_read)
  120. {
  121. #print "Got a socket to work on $sock\n";
  122. if ($sock == $server)
  123. {
  124. #print "Detected Server Socket Read\n";
  125. #alarm 5;
  126. eval {$client = $server->accept();};
  127. if ($@) {print "$@\n";}
  128. if ($@) {next;}
  129. if(defined($client))
  130. {
  131.  
  132. if ($^O =~ m/Win32/)
  133. {
  134. my $temp = 1;
  135. #ioctl($client, 0x8004667E, \$temp);
  136. }
  137. else
  138. {
  139. if ($flags = fcntl($client, F_GETFL, 0)<0)
  140. {
  141. print "Unable to get client socket flags\n";
  142. close($client);
  143. next;
  144. }
  145. if (($flags = fcntl($client, F_SETFL, $flags | O_NONBLOCK))<0)
  146. {
  147.  
  148. print "Unable to set client socket to non-block\n";
  149. close($client);
  150. next;
  151. }
  152. }
  153. #print "$client Accepted \n";
  154. #alarm 0;
  155. $client->autoflush(1);
  156. $command = $client->command();
  157. #printf "Command = %x", $command->[0], "\n";
  158.  
  159. if ($command->[0] == 1)
  160. {
  161. #print "Command: ",$command->[0]," Host: ",$command->[1], " Port: ", $command->[2],"\n";
  162. if (!($remote = IO::Socket::INET->new()))
  163. {
  164. $client->command_reply(1,undef,undef);
  165. #print "Sent reply error at IO::Socket create Error: $@\n";
  166. close($client);
  167. next;
  168. }
  169. $proto = getprotobyname('tcp');
  170. $remote->socket(AF_INET, SOCK_STREAM, $proto);
  171.  
  172. if ($^O =~ m/Win32/)
  173. {
  174. my $temp = 1;
  175. #ioctl($remote, 0x8004667E, \$temp);
  176. }
  177. else
  178. {
  179. if ($flags = fcntl($remote, F_GETFL, 0)<0)
  180. {
  181. $client->command_reply(1,undef,undef);
  182. #print "Sent reply error at get flags\n";
  183. close($client);
  184. next;
  185. }
  186.  
  187. if (($flags = fcntl($remote, F_SETFL, $flags | O_NONBLOCK))<0)
  188. {
  189. $client->command_reply(1,undef,undef);
  190. #print "Sent reply error at non-block\n";
  191. close($client);
  192. next;
  193. }
  194. }
  195. if (!($iaddr = inet_aton($command->[1])))
  196. {
  197. $client->command_reply(1,undef,undef);
  198. #print "Sent reply error at inet_aton\n";
  199. close($client);
  200. next;
  201. }
  202.  
  203. if (!($sockaddr = sockaddr_in($command->[2], $iaddr)))
  204. {
  205. $client->command_reply(1,undef,undef);
  206. #print "Sent reply error at sockaddr_in\n";
  207. close($client);
  208. next;
  209. }
  210.  
  211. if ($remote->connect($sockaddr))
  212. {
  213. $hash{$client}= $remote;
  214. $hash{$remote}= $client;
  215. #$port = $remote->sockport();
  216. #print "Peerhost = ",$remote->peerhost(),"Port = " ,$remote->peerport(),"\n";
  217. $client->command_reply(0,$remote->sockhost(),$remote->sockport());#$remote->sockhost(), $remote->sockport());
  218. #print "Sent reply at CONNECT\n";
  219. $readable->add($client);
  220. $readable->add($remote);
  221. $remote->autoflush(1);
  222. }
  223. elsif ($! == EWOULDBLOCK or $! == EINPROGRESS)
  224. {
  225. $writable->add($remote);
  226. $connectpending{$client} = $remote;
  227. $connectpending{$remote} = $client;
  228. }
  229. else
  230. {
  231. $client->command_reply(1,undef,undef);
  232. #print "Sent reply error at CONNECT\n";
  233. close($client);
  234. next;
  235. }
  236. }
  237. elsif ($command->[0]==2)
  238. {
  239. print "BIND detected\n";
  240. print "Command 1 = ", $command->[0]," Command 2 = ", inet_ntoa(inet_aton($command->[1]))," Command 3 = ", $command->[2],"\n";
  241. if ($command->[2] < 1024) { $port = 0;} else {$port = $command->[2];}
  242. print "$port\n";
  243. $remote = IO::Socket::INET->new(Listen => 1,
  244. LocalAddr => '202.41.106.14',
  245. LocalPort => $port,
  246. Proto => 'tcp',
  247. Blocking =>0,
  248. Reuse => 1);
  249.  
  250. if($remote)
  251. {
  252. $hash{$client}= $remote;
  253. $hash{$remote}= $client;
  254. $client->command_reply(0, $remote->sockhost(), $remote->sockport());
  255. print $remote->sockhost(), $remote->sockport(),"\n";
  256. #print "Sent reply at BIND\n";
  257. $readable->add($client);
  258. $readable->add($remote);
  259. $servers{$remote} = $client;
  260. #$servers{$client} = $remote;
  261. $commands{$client} = $command->[1];
  262. }
  263. else
  264. {
  265. $client->command_reply(4,undef,undef);
  266. print "Sent error at BIND\n";
  267. close($client);
  268. }
  269.  
  270. }
  271. elsif ($command->[0]==3)
  272. {
  273.  
  274. if ($command->[2] < 1024) { $port = 0;} else {$port = $command->[2];}
  275. (exists $udpinuse1{$port}) ? $port = 0:$port;
  276. $udp = IO::Socket::INET->new(
  277. LocalPort => $port,
  278. LocalAddr => '202.41.106.14',
  279. Proto => 'udp',
  280. Blocking =>0,
  281. Reuse => 1);
  282.  
  283.  
  284.  
  285. if (!$udp)
  286. {
  287. $client->command_reply(1,undef,undef);
  288. print "Sent reply error at new UDP server socket $!\n";
  289. $client->command_reply(1,undef,undef);
  290. close($client);
  291. next;
  292. }
  293. if ($^O =~ m/Win32/)
  294. {
  295. my $temp = 1;
  296. #ioctl($udp, 0x8004667E, \$temp);
  297. }
  298. else
  299. {
  300. if ($flags = fcntl($udp, F_GETFL, 0)<0)
  301. {
  302. $client->command_reply(1,undef,undef);
  303. #print "Sent reply error at get flags\n";
  304. close($client);
  305. next;
  306. }
  307.  
  308. if (($flags = fcntl($udp, F_SETFL, $flags | O_NONBLOCK))<0)
  309. {
  310. $client->command_reply(1,undef,undef);
  311. #print "Sent reply error at non-block\n";
  312. close($client);
  313. next;
  314.  
  315. }
  316. }
  317.  
  318.  
  319. $udpassociate{$client} = $udp;
  320. $udpservers{$udp} = $client;
  321. $command->[1] ? $udpadd{$udp} = $command->[1]:$udpadd{$udp} = $client->peerhost();
  322. $udpport{$udp} = $command->[2];
  323. $client->command_reply(0,$udp->sockhost(), $udp->sockport());
  324. $port = $udp->sockport();
  325. $readable->add($udp);
  326. $readable->add($client);
  327. $udpinuse1{$port} = $client;
  328. $udpinuse2{$client} = $port;
  329. print STDOUT "UDP Server setup Host: ", $udp->sockhost()," Port: ", $udp->sockport(),"\n";
  330. print STDOUT "UDP Server client Host: ", $udpadd{$udp}," Port: ", $udpport{$udp},"\n";
  331.  
  332.  
  333.  
  334. }
  335. else
  336. {
  337. $client->command_reply(7,undef,undef); #Command not supported
  338. close($client);
  339. }
  340.  
  341. }
  342. else
  343. {
  344. #alarm 0;
  345. print "Socks Accept failed Error: $! \n";
  346. }
  347.  
  348.  
  349. }
  350.  
  351. elsif (exists ($servers{$sock}))
  352. {
  353. print "Detected incoming to BIND\n";
  354. if (defined ($remoteserver = $sock->accept()))
  355. {
  356.  
  357. if ((inet_aton($commands{$servers{$sock}})) eq (inet_aton($remoteserver->peeraddr())))
  358. {
  359. $servers{$sock}->command_reply(0,$sock->peeraddr(),$sock->peerport());
  360.  
  361. $hash{$remoteserver} = $servers{$sock};
  362. $hash{$servers{$sock}} = $remoteserver;
  363. delete $servers{$servers{$sock}};
  364. delete $commands{$servers{$sock}};
  365. delete $hash{$sock};
  366. delete $servers{$sock};
  367. #print "Sent reply at BIND CONNECT\n";
  368. $readable->remove($sock);
  369. close($sock);
  370. $readable->add($remoteserver);
  371. $readable->add($hash{$remoteserver});
  372. }
  373. else
  374. {
  375. $servers{$sock}->command_reply(1,undef,undef);
  376. #print "Sent error at BIND CONNECT bad IP\n";
  377. $readable->remove($sock);
  378. close($sock);
  379. close($servers{$sock});
  380. delete $servers{$hash{$sock}};
  381. delete $servers{$sock};
  382. delete $commands{$hash{$sock}};
  383. delete $hash{$hash{$sock}};
  384. delete $hash{$sock};
  385.  
  386.  
  387. }
  388. }
  389. else
  390. {
  391. $servers{$sock}->command_reply(1,undef,undef);
  392. #print "Sent error at BIND CONNECT \n";
  393. $readable->remove($sock);
  394. close($sock);
  395. close($servers{$sock});
  396. delete $servers{$hash{$sock}};
  397. delete $servers{$sock};
  398. delete $commands{$servers{$sock}};
  399. delete $hash{$hash{$sock}};
  400. delete $hash{$sock};
  401. }
  402.  
  403. }
  404. elsif (exists ($udpassociate{$sock}))
  405. {
  406.  
  407. print "Control socket read detected\n";
  408. $bytesread = sysread($sock,$databuf, 102400,0);
  409. if((!defined($bytesread )) or ($bytesread == 0))
  410. {
  411. print "Control socket close detected\n";
  412. $readable->remove($sock);
  413. $readable->remove($udpassociate{$sock});
  414. close($sock);
  415. close($udpassociate{$sock});
  416. delete ($udpport{$udpassociate{$sock}});
  417. delete ($udpadd{$udpassociate{$sock}});
  418. delete $udpinuse1{$udpinuse2{$sock}};
  419. delete $udpinuse2{$sock};
  420. delete ($udpservers{$udpassociate{$sock}});
  421. delete ($udpassociate{$sock});
  422.  
  423. }
  424.  
  425. }
  426. elsif (exists ($udpservers{$sock}))
  427. {
  428. $paddr = recv($sock,$databuf, 102400,0);
  429. #print STDOUT "Reached UDP1\n";
  430. if((defined($paddr)))
  431. {
  432. #print STDOUT "Reached UDP2\n";
  433. ($port, $ipaddr) = sockaddr_in($paddr);
  434.  
  435. #$port = $sock->peerport();
  436. #$ipaddr = $sock->peerhost();
  437. #print STDOUT "From: ",inet_ntoa($ipaddr), " $port\n";
  438. if (($udpadd{$sock} eq inet_ntoa($ipaddr)) and ($udpport{$sock} == $port))
  439. {
  440. #print STDOUT "Detected local auth access UDP\n";
  441. if (ord((substr($databuf,2,1))) != 0)
  442. {
  443. next;
  444. }
  445. if(ord((substr($databuf,3,1))) == 1)
  446. {
  447. #print "UDP IP given\n";
  448. $udpremoteip = substr($databuf,4,4);
  449. $udpremoteport = unpack("n",substr($databuf,8,2));
  450. #($udpremoteport == 9) ? $udpremoteport = 11 : $udpremoteport;
  451. $data = substr($databuf,10,length($databuf) - 10);
  452. #print "To: ", inet_ntoa($udpremoteip), " $udpremoteport\n";
  453. }
  454. elsif (ord((substr($databuf,3,1))) == 3)
  455. {
  456. #print "UDP ADD given\n";
  457. $chars = ord((substr($databuf,4,1)));
  458. $udpremoteadd = substr($databuf,5,$chars);
  459. $udpremoteip = gethostbyname ($udpremoteadd);
  460. $udpremoteport = unpack("n",substr($databuf,4+$chars,2));
  461. $data = substr($databuf,6+$chars,(length($databuf) - (6+$chars)));
  462. #print $udpremoteadd, " $udpremoteport\n";
  463. }
  464. else
  465. {
  466. next;
  467. }
  468.  
  469. $portaddr = sockaddr_in($udpremoteport, $udpremoteip);
  470.  
  471. if (send($sock, $data, 0, $portaddr) != length($data))
  472. {
  473. print STDOUT "Remote UDP Send error $!\n";
  474. }
  475. }
  476. else
  477. {
  478. #print STDOUT "Detected remote incoming access UDP\n";
  479. $portaddr = sockaddr_in($udpport{$sock}, inet_aton($udpadd{$sock}));
  480. if (!(send($sock,chr(0).chr(0).chr(0).chr(1). $ipaddr.pack("n",$port).$databuf, 0, $portaddr) ))
  481. {
  482. print STDOUT "Local UDP Send error\n $!";
  483. }
  484. }
  485. }
  486.  
  487.  
  488.  
  489. }
  490.  
  491. else
  492. { #print "Detected read on normal socket $sock\n";
  493. if (length($data{$sock})>32768)
  494. {
  495. #print "Read paused. Buffers full on $sock\n";
  496. $readpaused{$sock} = 1;
  497. $readable->remove($sock);
  498. next;
  499. }
  500. $bytesread = sysread($sock,$databuf, 102400,0);
  501. #print "Bytes read = $bytesread from Socket $sock \n";#Buffer = $databuf MOTE \n";
  502. if(!defined($bytesread ))
  503. {
  504.  
  505. #print "Error occured in READ. Closing socket. $sock \n";
  506. #$sock->flush();
  507. #$hash{$sock}->flush();
  508. if (exists ($hash{$sock}))
  509. {
  510. $readable->remove($sock);
  511. #print "Removed first readable socket\n";
  512. $readable->remove($hash{$sock});
  513. #print "Removed second readable socket\n";
  514. $writable->remove($sock);
  515. #print "Removed first readable socket\n";
  516. close ($sock);
  517. delete($data{$hash{$sock}});
  518. if (!(length($data{$sock})))
  519. {
  520. $writable->remove($hash{$sock});
  521. #print "Removed second readable socket $hash{$sock}. Cleaning up in READ error\n";
  522. #close($sock);
  523. close($hash{$sock});
  524. delete($data{$hash{$sock}});
  525. delete($data{$sock});
  526. #delete($hash{$hash{$sock}});
  527. delete($hash{$sock});
  528. delete($readpaused{$sock});
  529. }
  530. if (exists($servers{$hash{$sock}}))
  531. {
  532. delete $servers{$hash{$sock}};
  533. delete $commands{$sock};
  534. }
  535.  
  536. }
  537.  
  538.  
  539. }
  540. elsif (($bytesread == 0))
  541. {
  542. #print "Socket closed in READ. Closing socket $sock.\n";
  543.  
  544. if (exists ($hash{$sock}))
  545. {
  546. $readable->remove($sock);
  547. $readable->remove($hash{$sock});
  548. $writable->remove($sock);
  549. close($sock);
  550. delete($data{$hash{$sock}});
  551. if (!(length($data{$sock})))
  552. {
  553. #print "Removed second writable socket $hash{$sock}. Cleaning up in READ close\n";
  554. $writable->remove($hash{$sock});
  555. #print "Removed second readable socket\n";
  556. #close($sock);
  557. close($hash{$sock});
  558. delete($data{$hash{$sock}});
  559. delete($data{$sock});
  560. #delete($hash{$hash{$sock}});
  561. delete($hash{$sock});
  562. delete($readpaused{$sock});
  563. }
  564.  
  565. if (exists ($servers{$hash{$sock}}))
  566. {
  567. delete $servers{$hash{$sock}};
  568. delete $commands{$sock};
  569. }
  570.  
  571. }
  572. }
  573. else
  574. {
  575. if (exists ($hash{$sock}))
  576. {
  577. $data{$sock}.=$databuf;
  578.  
  579. $bytessent= syswrite($hash{$sock},$data{$sock},length($data{$sock}),0);
  580.  
  581.  
  582. if(!defined($bytessent))
  583. {
  584. if ( !(($! == EWOULDBLOCK) or ($! == EAGAIN)))
  585. {
  586. #print "Error occured in READ-WRITE. Closing socket $hash{$sock}\n";
  587. if (defined ($hash{$sock}))
  588. {
  589. $readable->remove($sock);
  590. $readable->remove($hash{$sock});
  591. $writable->remove($hash{$sock});
  592. close ($hash{$sock});
  593. delete($data{$sock});
  594. if (!(length($data{$hash{$sock}})))
  595. {
  596. #print "Removed first writable socket $sock. Cleaning up in READ-WRITE close\n";
  597. $writable->remove($sock);
  598. #close($hash{$sock});
  599. close($sock);
  600. delete($data{$hash{$sock}});
  601. #delete($data{$sock});
  602. delete($hash{$hash{$sock}});
  603. delete($hash{$sock});
  604. delete($readpaused{$sock});
  605. }
  606. }
  607. }
  608. else
  609. {
  610. #print "EWOULDBLOCK on ", $hash{$sock},"\n";
  611. $hash{$sock}->flush();
  612. $writable->add($hash{$sock});
  613. }
  614. }
  615.  
  616.  
  617. elsif ($bytessent < length($data{$sock}))
  618. {
  619. #print "Partial Bytes sent = $bytessent to Socket $hash{$sock} in READ\n";
  620. $hash{$sock}->flush();
  621. $writable->add($hash{$sock});
  622. substr($data{$sock}, 0, $bytessent) = '';
  623.  
  624. }
  625.  
  626. else
  627. {
  628. #print "Complete Bytes sent = $bytessent to Socket $hash{$sock} in READ\n";
  629. $hash{$sock}->flush();
  630. $writable->remove($hash{$sock});
  631. substr($data{$sock}, 0, $bytessent) = '';
  632. }
  633.  
  634.  
  635. #print "Buffer Length = ", length($data{$sock});
  636. }
  637. }
  638. }
  639.  
  640. }
  641. foreach $sock (@$sockets_write)
  642. { #print "Entered write section on $sock \n";
  643. if (exists $connectpending{$sock})
  644. {
  645. if ($sock->peername())
  646. {
  647. #print "Delayed connect success in WRITE\n";
  648. $hash{$sock}= $connectpending{$sock};
  649. $hash{$connectpending{$sock}}= $sock;
  650. #$port = $remote->sockport();
  651. #print "Peerhost = ",$sock->peerhost()," Port = " ,$sock->peerport(),"\n";
  652. $hash{$sock}->command_reply(0, $sock->sockhost(),$sock->sockport());#$hash{$sock}->sockhost(), $hash{$sock}->sockport());
  653. #print "Sent CONNECT reply in WRITE\n";
  654. $readable->add($sock);
  655. $readable->add($hash{$sock});
  656. $writable->remove($sock);
  657. $sock->autoflush(1);
  658. delete $connectpending{$connectpending{$sock}};
  659. delete $connectpending{$sock};
  660. }
  661. else
  662. {
  663. #print "Delayed connect failed in WRITE\n";
  664. $connectpending{$sock}->command_reply(1,undef,undef);
  665. #print "Sent CONNECT reply error in WRITE\n";
  666. $writable->remove($sock);
  667. close($sock);
  668. close($connectpending{$sock});
  669. delete $connectpending{$connectpending{$sock}};
  670. delete $connectpending{$sock};
  671. }
  672.  
  673.  
  674. }
  675.  
  676. elsif ((length($data{$hash{$sock}}))>0)
  677. {
  678. #print "Whew I have something to write to $sock;-)\n";
  679. $bytessent= syswrite($sock,$data{$hash{$sock}},length($data{$hash{$sock}}),0);
  680. if(!defined($bytessent))
  681. {
  682. if (!(($! != EWOULDBLOCK) or ($! != EAGAIN)))
  683. {
  684. #print "Error occured on $sock in WRITE\n";
  685. if (defined ($hash{$sock}))
  686. {
  687. $writable->remove($sock) if $writable->exists($sock);
  688. $readable->remove($sock) if $readable->exists($sock);
  689. $readable->remove($hash{$sock}) if $readable->exists($hash{$sock});
  690. close($sock);
  691. delete($data{$hash{$sock}});
  692. if (!(length($data{$hash{$sock}})))
  693. {
  694. $writable->remove($hash{$sock}) if $writable->exists($hash{$sock});
  695. #print "Removing first writable socket $hash{$sock}. Cleaning up in WRITE error.\n";
  696. close($hash{$sock});
  697. #close($sock);
  698. #delete($data{$hash{$sock}});
  699. delete($data{$sock});
  700. delete($hash{$hash{$sock}});
  701. delete($hash{$sock});
  702. delete($readpaused{$sock});
  703. }
  704.  
  705. }
  706. }
  707. else
  708. {
  709. #print "In write section EWOULDBLOCK on ", $sock,"\n";
  710. #$sock->flush();
  711. }
  712.  
  713. }
  714. elsif ($bytessent == length($data{$hash{$sock}}))
  715. {
  716. #print "Complete Bytes sent = $bytessent to $sock in WRITE\n";
  717. $writable->remove($sock);
  718. substr($data{$hash{$sock}}, 0, $bytessent) = '';
  719. }
  720. else
  721. {
  722. #print "Partial Bytes send = $bytessent to $sock in WRITE\n";
  723. substr($data{$hash{$sock}}, 0, $bytessent) = '';
  724. }
  725.  
  726. #print "Sent $bytessent bytes to $sock Buffer length now = ", length($data{$hash{$sock}}),"\n";
  727.  
  728. if ((length($data{$hash{$sock}}) < 32768) and ($readpaused{$hash{$sock}}))
  729. {
  730. #print "Buffers cleared. Reading renabled on $hash{$sock}\n";
  731. $readpaused{$hash{$sock}} = 0;
  732. $readable->add($hash{$sock});
  733. }
  734.  
  735.  
  736. if ((length($data{$hash{$sock}}) == 0) and (!($readable->exists($hash{$sock}))))
  737. {
  738. #print "Removing first writable socket $hash{$sock}. Cleaning up in WRITE data all sent.\n";
  739. $writable->remove($sock) if $readable->exists($sock);
  740. close($hash{$sock});
  741. close($sock);
  742. delete($data{$hash{$sock}});
  743. delete($data{$sock});
  744. delete($hash{$hash{$sock}});
  745. delete($hash{$sock});
  746. delete($readpaused{$sock});
  747. }
  748.  
  749.  
  750.  
  751.  
  752. }
  753. #else
  754. #{
  755. # $writable->remove($sock);
  756. #}
  757. }
  758. }
  759.  
  760.  
  761. }
  762.  
  763. sub auth
  764. {
  765. my $user = shift;
  766. my $pass = shift;
  767.  
  768. #print "user($user) pass($pass)\n";
  769.  
  770. return 1 if (($user eq $socksuser) && ($pass eq $socksuserpass));
  771. #return 1;
  772. }
  773.  
  774.  
  775.  
  776.  
  777. #sub timeout()
  778. #{
  779. # print "Alarm went off\n";
  780. # ($package, $filename, $line) = caller;
  781. # print $package,"\n", $filename,"\n", $line,"\n";
  782. # if ($package eq "IO::Socket::Socks")
  783. # {
  784. # print 'Closing $self', $package::self,"\n";
  785. #
  786. # close ($self);
  787. #exit;
  788. # return undef;
  789. # }
  790. # exit;
  791. #}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement