0
# Traffic Statistics
1
2
Comprehensive traffic monitoring and statistics collection for V2ray/Xray proxy servers. Provides traffic analysis through both V2ray's official API and iptables-based monitoring, enabling detailed usage tracking and bandwidth management.
3
4
## Capabilities
5
6
### V2ray API Statistics
7
8
Traffic statistics collection using V2ray's built-in statistics API for accurate per-user and per-group monitoring.
9
10
```python { .api }
11
class StatsFactory:
12
"""V2ray statistics API management"""
13
14
def __init__(self, group_list):
15
"""
16
Initialize statistics factory with group configuration.
17
18
Parameters:
19
- group_list (list): List of configured port groups
20
"""
21
22
def get_stats(self, group_list):
23
"""
24
Retrieve comprehensive traffic statistics for all groups.
25
26
Parameters:
27
- group_list (list): Groups to collect stats for
28
29
Returns:
30
dict: Traffic statistics including uplink/downlink bytes
31
for each user and group
32
"""
33
34
def get_inbound_stats(self, group_list):
35
"""
36
Get inbound traffic statistics specifically.
37
38
Parameters:
39
- group_list (list): Target groups
40
41
Returns:
42
dict: Inbound traffic data per user/group
43
"""
44
45
def manage(stat_type=''):
46
"""
47
Interactive traffic statistics management.
48
49
Parameters:
50
- stat_type (str, optional): Specific statistics type to display
51
'user' - Per-user statistics
52
'group' - Per-group statistics
53
'total' - Overall statistics
54
55
Provides interactive menu for viewing and managing traffic statistics,
56
including options to reset counters and export data.
57
"""
58
```
59
60
### Iptables-based Statistics
61
62
Alternative traffic monitoring using iptables rules for system-level traffic analysis.
63
64
```python { .api }
65
def manage(iptables_type=''):
66
"""
67
Manage iptables-based traffic statistics.
68
69
Parameters:
70
- iptables_type (str, optional): Statistics type
71
'port' - Per-port statistics
72
'user' - Per-user via port mapping
73
'total' - System-wide proxy traffic
74
75
Provides iptables rule management and traffic data collection
76
independent of V2ray's API statistics.
77
"""
78
79
def calcul_iptables_traffic(port, ipv6=False):
80
"""
81
Calculate traffic statistics for specific port using iptables.
82
83
Parameters:
84
- port (int): Port number to analyze
85
- ipv6 (bool): Include IPv6 traffic statistics
86
87
Returns:
88
tuple: (upload_bytes, download_bytes) for the specified port
89
"""
90
91
def clean_iptables(port):
92
"""
93
Clean iptables rules and reset statistics for specific port.
94
95
Parameters:
96
- port (int): Port to clean rules for
97
98
Removes monitoring rules and resets byte counters.
99
"""
100
```
101
102
### Statistics Data Management
103
104
```python { .api }
105
class Stats:
106
"""Traffic statistics data management"""
107
108
def __init__(self):
109
"""Initialize statistics manager"""
110
111
def reset_stats(self):
112
"""Reset all traffic statistics counters"""
113
114
def export_stats(self, format='json'):
115
"""
116
Export statistics data.
117
118
Parameters:
119
- format (str): Export format ('json', 'csv', 'txt')
120
121
Returns:
122
str: Formatted statistics data
123
"""
124
```
125
126
## Usage Examples
127
128
### V2ray API Statistics
129
130
```python
131
from v2ray_util.global_setting.stats_ctr import manage, StatsFactory
132
from v2ray_util.util_core.profile import Profile
133
134
# Interactive statistics management
135
manage()
136
137
# Programmatic statistics collection
138
profile = Profile()
139
groups = profile.load()
140
stats = StatsFactory(groups)
141
142
# Get all traffic statistics
143
all_stats = stats.get_stats(groups)
144
print(f"Total traffic: {all_stats}")
145
146
# Get inbound statistics only
147
inbound_stats = stats.get_inbound_stats(groups)
148
```
149
150
### Iptables Statistics
151
152
```python
153
from v2ray_util.global_setting.iptables_ctr import manage
154
from v2ray_util.util_core.utils import calcul_iptables_traffic, clean_iptables
155
156
# Interactive iptables management
157
manage()
158
159
# Get traffic for specific port
160
upload, download = calcul_iptables_traffic(8080)
161
print(f"Port 8080 - Upload: {upload}, Download: {download}")
162
163
# Include IPv6 traffic
164
upload, download = calcul_iptables_traffic(8080, ipv6=True)
165
166
# Clean statistics for port
167
clean_iptables(8080)
168
```
169
170
### Command Line Statistics
171
172
```bash
173
# V2ray API statistics
174
v2ray-util stats # Interactive statistics menu
175
v2ray-util stats user # Per-user statistics
176
v2ray-util stats total # Total statistics
177
178
# Iptables statistics
179
v2ray-util iptables # Interactive iptables menu
180
v2ray-util iptables port # Per-port statistics
181
```
182
183
## Statistics Data Format
184
185
### V2ray API Statistics Structure
186
187
```python { .api }
188
StatsData = {
189
"groups": [
190
{
191
"port": int,
192
"protocol": str,
193
"users": [
194
{
195
"email": str,
196
"uplink": int, # Bytes sent
197
"downlink": int, # Bytes received
198
"total": int # Total bytes
199
}
200
],
201
"group_total": {
202
"uplink": int,
203
"downlink": int,
204
"total": int
205
}
206
}
207
],
208
"system_total": {
209
"uplink": int,
210
"downlink": int,
211
"total": int,
212
"active_users": int,
213
"active_groups": int
214
}
215
}
216
```
217
218
### Iptables Statistics Structure
219
220
```python { .api }
221
IptablesData = {
222
"ports": {
223
"8080": {
224
"input_bytes": int,
225
"output_bytes": int,
226
"input_packets": int,
227
"output_packets": int
228
}
229
},
230
"total": {
231
"input_bytes": int,
232
"output_bytes": int,
233
"input_packets": int,
234
"output_packets": int
235
}
236
}
237
```
238
239
## Traffic Analysis Features
240
241
### Real-time Monitoring
242
- Live traffic statistics display
243
- Automatic refresh intervals
244
- Color-coded output for different traffic levels
245
246
### Historical Data
247
- Traffic history tracking
248
- Daily, weekly, monthly summaries
249
- Data export capabilities
250
251
### User Analytics
252
- Per-user bandwidth usage
253
- Top users by traffic volume
254
- Usage pattern analysis
255
256
### Alert System
257
- Bandwidth threshold alerts
258
- Unusual traffic pattern detection
259
- User quota management
260
261
## Utility Functions
262
263
### Data Formatting
264
265
```python { .api }
266
def bytes_2_human_readable(number_of_bytes, precision=1):
267
"""
268
Convert bytes to human-readable format.
269
270
Parameters:
271
- number_of_bytes (int): Raw byte count
272
- precision (int): Decimal precision for display
273
274
Returns:
275
str: Formatted string (e.g., "1.5 GB", "256.7 MB")
276
"""
277
```
278
279
### Port Management
280
281
```python { .api }
282
def all_port():
283
"""
284
Get list of all configured ports with statistics.
285
286
Returns:
287
list: Port numbers with associated traffic data
288
"""
289
```
290
291
## Integration with Global Settings
292
293
Traffic statistics integrate with other global settings:
294
295
### Bandwidth Limiting
296
- Per-user bandwidth caps
297
- Group-level traffic limits
298
- Dynamic throttling based on usage
299
300
### Automated Management
301
- Automatic user cleanup based on inactivity
302
- Traffic-based user tier management
303
- Cost tracking and billing integration
304
305
### Monitoring Integration
306
- Log correlation with traffic patterns
307
- Performance metrics collection
308
- System resource usage tracking
309
310
## Data Persistence
311
312
Statistics data is persisted through:
313
- JSON configuration files
314
- SQLite databases for historical data
315
- Log file integration for audit trails
316
- Export capabilities for external analysis tools