forked from icho53/TelegramV2rayCollector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.php
196 lines (156 loc) · 5.88 KB
/
collector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
// all output to json
header("Content-type: application/json;");
// stat for cron Log file
$startProcess = date('Y-m-d H:i:s');
// include main config file & modules
include 'config.php';
include 'functions.php';
include CHANNELS_DIR . 'channels.php';
// set timezone
date_default_timezone_set(TIME_ZONE);
// check channels directory
if ( !file_exists( CHANNELS_DIR ) )
mkdir( CHANNELS_DIR ) or die('Error making directory (Line: ' . __LINE__ . ')');
// define all allowed protocol types
$all_types = ['vmess', 'vless', 'trojan'];
// ******************** <Extraction> ********************
// extract configs
$i = 0;
foreach ( $channels as $channel => $types )
{
for ( $type_count = 0; $type_count < count($types); $type_count++ )
{
if ( in_array( $types[$type_count], $all_types ) )
{
$i++;
$mix[$types[$type_count]][] = @get_config($channel, $types[$type_count], $i);
}
}
}
// ******************** </Extraction> ********************
// get protocol details array
$vmess_data = $mix['vmess'];
$vless_data = $mix['vless'];
$trojan_data = $mix['trojan'];
// vmess
foreach ( $vmess_data as $object )
{
if ( is_array( $object ) )
{
foreach ( $object as $details )
{
$vmess_array[] = $details['config'];
}
}
}
// vless
foreach ( $vless_data as $object )
{
if ( is_array( $object ) )
{
foreach ( $object as $details )
{
$vless_array[] = $details['config'];
}
}
}
// trojan
foreach ( $trojan_data as $object )
{
if ( is_array( $object ) )
{
foreach ( $object as $details )
{
$trojan_array[] = $details['config'];
}
}
}
// mix all!
$mix_array = array_merge( $vmess_array, $vless_array, $trojan_array );
// prepare config data plaintext
$vmess = implode("\n", $vmess_array);
$vless = implode("\n", $vless_array);
$trojan = implode("\n", $trojan_array);
// remove duplicates
$fixed_vmess = remove_duplicate_vmess( $vmess );
$fixed_vless = remove_duplicate_xray( str_replace("&", "&", $vless), "vless" );
$fixed_reality = get_reality( $fixed_vless );
$fixed_trojan = remove_duplicate_xray( str_replace("&", "&", $trojan), "trojan");
$fixed_mix = "$fixed_vmess\n$fixed_vless\n$fixed_trojan";
$fixed_tunnel = get_tunnel( $fixed_mix );
// ******************** Create Subscription ********************
// check subscription directory
if ( !file_exists( SUB_DIR ) )
mkdir( SUB_DIR ) or die('Error making directory! (' . __LINE__ . ')');
// normal version
file_put_contents( SUB_DIR . "vmess", $fixed_vmess);
file_put_contents( SUB_DIR . "vless", $fixed_vless);
file_put_contents( SUB_DIR . "reality", $fixed_reality);
file_put_contents( SUB_DIR . "trojan", $fixed_trojan);
file_put_contents( SUB_DIR . "tunnel", $fixed_tunnel);
file_put_contents( SUB_DIR . "mix", $fixed_mix);
// base64 version
file_put_contents( SUB_DIR . "vmess_base64", base64_encode($fixed_vmess));
file_put_contents( SUB_DIR . "vless_base64", base64_encode($fixed_vless));
file_put_contents( SUB_DIR . "reality_base64", base64_encode($fixed_reality));
file_put_contents( SUB_DIR . "trojan_base64", base64_encode($fixed_trojan));
file_put_contents( SUB_DIR . "tunnel_base64", base64_encode($fixed_tunnel));
file_put_contents( SUB_DIR . "mix_base64", base64_encode($fixed_mix));
// ******************** Update Channels Data ********************
$channel_array = [];
$channel_assets_url = "https://raw.githubusercontent.com/HadiDastangoo/TelegramV2RayExtractor/main/" . CHANNELS_ASSETS_DIR;
foreach ( $channels as $channel => $data_array )
{
// get telegram channel contents
$html = file_get_contents("https://t.me/s/" . $channel);
// prepare channel properties' pattern
$title_pattern = '#<meta property="twitter:title" content="(.*?)">#';
$image_pattern = '#<meta property="twitter:image" content="(.*?)">#';
// get channel property
preg_match($image_pattern, $html , $image_match);
preg_match($title_pattern, $html , $title_match);
// set channel property into array
$channel_array[$channel]['types'] = $data_array;
$channel_array[$channel]['title'] = $title_match[1];
// put logo into channels' assets directory (if channel profile image is set)
if ( !empty($image_match[1]) )
{
$channel_logo = @file_get_contents($image_match[1]);
if ( $channel_logo )
file_put_contents( CHANNELS_ASSETS_DIR . $channel . ".jpg", $channel_logo );
$channel_array[$channel]['logo'] = $channel_logo ? ( $channel_assets_url . $channel . ".jpg" ) : null;
}
else
{
$channel_array[$channel]['logo'] = null;
}
}
// update channels data into json
file_put_contents( CHANNELS_DIR . "channels.json", json_encode($channel_array , JSON_PRETTY_PRINT));
// ******************** Report ********************
echo "\n" . str_repeat('=', 50) . "\n";
echo "VMess:\t" . count($vmess_array);
echo "\n" . str_repeat('-', 50) . "\n";
echo "VLESS:\t" . count($vless_array) . "\t(Reality: " . count( explode("\n", $fixed_reality) ) . ")";
echo "\n" . str_repeat('-', 50) . "\n";
echo "Trojan:\t" . count($trojan_array);
echo "\n" . str_repeat('-=', 25) . "\n";
echo "Tunnel:\t" . count( explode("\n", $fixed_tunnel) );
echo "\n" . str_repeat('=', 50) . "\n";
echo "Sum:\t" . count($mix_array);
echo "\n" . str_repeat('-', 50) . "\n";
echo "Total:\t" . count(explode("\n", $fixed_mix)) . "\t(" . ( count($mix_array) - count(explode("\n", $fixed_mix)) ) . " duplicates merged)";
echo "\n" . str_repeat('=', 50) . "\n";
echo "\n";
// ******************** Log ********************
if ( CRON_LOG_ENABLED )
{
$sapi_name = php_sapi_name();
$statFile = fopen( CRON_LOG_FILE, "a+" ) or die('Error openning log file (line:' . __LINE__ . ')');
$endProcess = date('Y-m-d H:i:s');
$log = "$startProcess\t$endProcess\t" . count($mix_array) . "\t" . count(explode("\n", $fixed_mix)) . "\t$sapi_name\n";
fwrite( $statFile, $log );
fclose( $statFile );
}
?>