M
M
Michael Compass2020-04-28 16:43:44
PHP
Michael Compass, 2020-04-28 16:43:44

How to determine the proxy country, remove Chinese proxies and completely overwrite the file with the final result?

There is a php script for checking SOCKS5 proxy.
The script looks at the file: gotovo_vse_sellery_bezdybley.txt and checks if this file is empty or not. If the file is empty, then the script stops immediately.

If the file is not empty, then the script checks the proxy from the file for validity in the following way:
The script sends the string \x05\x01\x00 to each socks it managed to connect to, if the first 2 bytes of the response are \x05\x00\ then socks5 is considered valid

during the check - the script saves valid SOCKS5 to the file: socks5_valid.txt

<?php
 $socks5_list = explode ("\n", str_replace ("\r", "", file_get_contents (dirname(__FILE__)."/gotovo_vse_sellery_bezdybley.txt")));
if (empty($socks5_list))
{
    exit(1);
}
//скрипт получения двухсимвольного кода страны
/* 
$country = geoip_country_code_by_name('www.example.com');
if ($country) {
    echo 'Хост расположен в ' . $country; 
*/ 
 function _check_socks5 ($socks = array(), $filename)
 {
  foreach ($socks as $s)
  {
   list ($ip, $port) = explode (":", $s);
   if ($socket = @fsockopen ($ip, $port, $errno, $errstr, 1))
   {
    $threads [$s] = $socket;
   }
  }
  foreach ($threads as $s => $h)
  {
   fwrite ($h, "\x05\x01\x00");
  }
  foreach ($threads as $s => $h)
  {
   $r = fread ($h, 2);
   if ((ord ($r [0]) == 5) && (ord ($r [1]) == 0))
   {
    file_put_contents ($filename, $s."\r\n", FILE_APPEND);
   }
  }
 }

 _check_socks5 ($socks5_list, dirname(__FILE__)."/socks5_valid.txt");
?>


2 questions:
- Since the script runs every 2 minutes, it litters the file with old results. How to make it not add new results after the old results, but completely overwrite the file: socks5_valid.txt with new results?

- How to correctly write the code for determining the country of these proxies in order to remove Chinese proxies from valid results? China ISO code: CN

I found such a script for getting a two-character country code, but I don’t know how to add it with the main check code. Forgive the noob for not knowing php

Script to get a two-character country code:
$country = geoip_country_code_by_name('www.example.com');
if ($country) {
    echo 'Хост расположен в ' . $country;

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AUser0, 2020-04-28
@mik777em

Does the irreconcilable fight against proxies continue? :P

<?php
 $socks5_list = explode ("\n", str_replace ("\r", "", file_get_contents (dirname(__FILE__)."/gotovo_vse_sellery_bezdybley.txt")));
if (empty($socks5_list))
{
    exit(1);
}
 function _check_socks5 ($socks = array(), $filename)
 {
  foreach ($socks as $s)
  {
   list ($ip, $port) = explode (":", $s);
   if (geoip_country_code_by_name($ip) == "CN") continue;
   if ($socket = @fsockopen ($ip, $port, $errno, $errstr, 1))
   {
    $threads[$s] = $socket;
   }
  }
  foreach ($threads as $s => $h)
  {
   fwrite ($h, "\x05\x01\x00");
  }
  foreach ($threads as $s => $h)
  {
   $r = fread ($h, 2);
   if ((ord ($r [0]) == 5) && (ord ($r [1]) == 0))
   {
    file_put_contents ($filename, $s."\r\n");
   }
  }
 }

 _check_socks5 ($socks5_list, dirname(__FILE__)."/socks5_valid.txt");
?>

A
Andrey, 2020-04-28
@AndryG

How can I make it not add new results after the old results, but completely overwrite the file: socks5_valid.txt with new results?

file_put_contents ($filename, $s."\r\n", FILE_APPEND);
Remove the FILE_APPEND option

Z
zlo1, 2020-04-28
@zlo1

....Script for getting a two-character country code:..

first you need to get the IP at the output of the proxy
and then fasten the MaxMind2/.... .mmdb base

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question