@push.rocks/smartproxy 19.6.6 → 19.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist_ts/core/models/wrapped-socket.d.ts +4 -0
  2. package/dist_ts/core/models/wrapped-socket.js +18 -1
  3. package/dist_ts/core/routing/matchers/path.js +3 -2
  4. package/dist_ts/proxies/smart-proxy/connection-manager.d.ts +4 -7
  5. package/dist_ts/proxies/smart-proxy/connection-manager.js +22 -22
  6. package/dist_ts/proxies/smart-proxy/http-proxy-bridge.d.ts +4 -3
  7. package/dist_ts/proxies/smart-proxy/http-proxy-bridge.js +9 -9
  8. package/dist_ts/proxies/smart-proxy/metrics-collector.d.ts +68 -56
  9. package/dist_ts/proxies/smart-proxy/metrics-collector.js +252 -176
  10. package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +6 -1
  11. package/dist_ts/proxies/smart-proxy/models/metrics-types.d.ts +99 -47
  12. package/dist_ts/proxies/smart-proxy/nftables-manager.d.ts +4 -4
  13. package/dist_ts/proxies/smart-proxy/nftables-manager.js +6 -6
  14. package/dist_ts/proxies/smart-proxy/port-manager.d.ts +4 -7
  15. package/dist_ts/proxies/smart-proxy/port-manager.js +6 -9
  16. package/dist_ts/proxies/smart-proxy/route-connection-handler.d.ts +4 -15
  17. package/dist_ts/proxies/smart-proxy/route-connection-handler.js +133 -130
  18. package/dist_ts/proxies/smart-proxy/security-manager.d.ts +3 -3
  19. package/dist_ts/proxies/smart-proxy/security-manager.js +9 -9
  20. package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +20 -13
  21. package/dist_ts/proxies/smart-proxy/smart-proxy.js +16 -13
  22. package/dist_ts/proxies/smart-proxy/throughput-tracker.d.ts +36 -0
  23. package/dist_ts/proxies/smart-proxy/throughput-tracker.js +117 -0
  24. package/dist_ts/proxies/smart-proxy/timeout-manager.d.ts +5 -4
  25. package/dist_ts/proxies/smart-proxy/timeout-manager.js +20 -16
  26. package/dist_ts/proxies/smart-proxy/tls-manager.d.ts +3 -3
  27. package/dist_ts/proxies/smart-proxy/tls-manager.js +12 -12
  28. package/dist_ts/routing/router/http-router.js +2 -2
  29. package/package.json +1 -1
  30. package/readme.hints.md +0 -0
  31. package/readme.md +239 -73
  32. package/readme.plan.md +364 -0
  33. package/ts/core/models/wrapped-socket.ts +18 -0
  34. package/ts/core/routing/matchers/path.ts +2 -1
  35. package/ts/proxies/smart-proxy/connection-manager.ts +23 -21
  36. package/ts/proxies/smart-proxy/http-proxy-bridge.ts +9 -8
  37. package/ts/proxies/smart-proxy/metrics-collector.ts +305 -188
  38. package/ts/proxies/smart-proxy/models/interfaces.ts +8 -1
  39. package/ts/proxies/smart-proxy/models/metrics-types.ts +99 -41
  40. package/ts/proxies/smart-proxy/nftables-manager.ts +5 -5
  41. package/ts/proxies/smart-proxy/port-manager.ts +6 -14
  42. package/ts/proxies/smart-proxy/route-connection-handler.ts +141 -138
  43. package/ts/proxies/smart-proxy/security-manager.ts +8 -8
  44. package/ts/proxies/smart-proxy/smart-proxy.ts +26 -35
  45. package/ts/proxies/smart-proxy/throughput-tracker.ts +144 -0
  46. package/ts/proxies/smart-proxy/timeout-manager.ts +22 -16
  47. package/ts/proxies/smart-proxy/tls-manager.ts +11 -11
  48. package/ts/routing/router/http-router.ts +1 -1
package/readme.md CHANGED
@@ -1576,150 +1576,316 @@ Available helper functions:
1576
1576
 
1577
1577
  ## Metrics and Monitoring
1578
1578
 
1579
- SmartProxy includes a comprehensive metrics collection system that provides real-time insights into proxy performance, connection statistics, and throughput data.
1579
+ SmartProxy includes a comprehensive metrics collection system that provides real-time insights into proxy performance, connection statistics, and throughput data. The metrics system uses a clean, grouped API design for intuitive access to different metric categories.
1580
1580
 
1581
- ### Getting Metrics
1581
+ ### Enabling Metrics
1582
1582
 
1583
1583
  ```typescript
1584
- const proxy = new SmartProxy({ /* config */ });
1584
+ const proxy = new SmartProxy({
1585
+ // Enable metrics collection
1586
+ metrics: {
1587
+ enabled: true,
1588
+ sampleIntervalMs: 1000, // Sample throughput every second
1589
+ retentionSeconds: 3600 // Keep 1 hour of history
1590
+ },
1591
+ routes: [/* your routes */]
1592
+ });
1593
+
1585
1594
  await proxy.start();
1595
+ ```
1586
1596
 
1587
- // Access metrics through the getStats() method
1588
- const stats = proxy.getStats();
1597
+ ### Getting Metrics
1589
1598
 
1590
- // Get current active connections
1591
- console.log(`Active connections: ${stats.getActiveConnections()}`);
1599
+ ```typescript
1600
+ // Access metrics through the getMetrics() method
1601
+ const metrics = proxy.getMetrics();
1592
1602
 
1593
- // Get total connections since start
1594
- console.log(`Total connections: ${stats.getTotalConnections()}`);
1603
+ // The metrics object provides grouped methods for different categories
1604
+ ```
1595
1605
 
1596
- // Get requests per second (RPS)
1597
- console.log(`Current RPS: ${stats.getRequestsPerSecond()}`);
1606
+ ### Connection Metrics
1598
1607
 
1599
- // Get throughput data
1600
- const throughput = stats.getThroughput();
1601
- console.log(`Bytes received: ${throughput.bytesIn}`);
1602
- console.log(`Bytes sent: ${throughput.bytesOut}`);
1608
+ Monitor active connections, total connections, and connection distribution:
1609
+
1610
+ ```typescript
1611
+ // Get current active connections
1612
+ console.log(`Active connections: ${metrics.connections.active()}`);
1613
+
1614
+ // Get total connections since start
1615
+ console.log(`Total connections: ${metrics.connections.total()}`);
1603
1616
 
1604
1617
  // Get connections by route
1605
- const routeConnections = stats.getConnectionsByRoute();
1618
+ const routeConnections = metrics.connections.byRoute();
1606
1619
  for (const [route, count] of routeConnections) {
1607
1620
  console.log(`Route ${route}: ${count} connections`);
1608
1621
  }
1609
1622
 
1610
1623
  // Get connections by IP address
1611
- const ipConnections = stats.getConnectionsByIP();
1624
+ const ipConnections = metrics.connections.byIP();
1612
1625
  for (const [ip, count] of ipConnections) {
1613
1626
  console.log(`IP ${ip}: ${count} connections`);
1614
1627
  }
1628
+
1629
+ // Get top IPs by connection count
1630
+ const topIPs = metrics.connections.topIPs(10);
1631
+ topIPs.forEach(({ ip, count }) => {
1632
+ console.log(`${ip}: ${count} connections`);
1633
+ });
1615
1634
  ```
1616
1635
 
1617
- ### Available Metrics
1636
+ ### Throughput Metrics
1618
1637
 
1619
- The `IProxyStats` interface provides the following methods:
1638
+ Real-time and historical throughput data with customizable time windows:
1620
1639
 
1621
- - `getActiveConnections()`: Current number of active connections
1622
- - `getTotalConnections()`: Total connections handled since proxy start
1623
- - `getRequestsPerSecond()`: Current requests per second (1-minute average)
1624
- - `getThroughput()`: Total bytes transferred (in/out)
1625
- - `getConnectionsByRoute()`: Connection count per route
1626
- - `getConnectionsByIP()`: Connection count per client IP
1640
+ ```typescript
1641
+ // Get instant throughput (last 1 second)
1642
+ const instant = metrics.throughput.instant();
1643
+ console.log(`Current: ${instant.in} bytes/sec in, ${instant.out} bytes/sec out`);
1644
+
1645
+ // Get recent throughput (last 10 seconds average)
1646
+ const recent = metrics.throughput.recent();
1647
+ console.log(`Recent: ${recent.in} bytes/sec in, ${recent.out} bytes/sec out`);
1648
+
1649
+ // Get average throughput (last 60 seconds)
1650
+ const average = metrics.throughput.average();
1651
+ console.log(`Average: ${average.in} bytes/sec in, ${average.out} bytes/sec out`);
1652
+
1653
+ // Get custom time window (e.g., last 5 minutes)
1654
+ const custom = metrics.throughput.custom(300);
1655
+ console.log(`5-min avg: ${custom.in} bytes/sec in, ${custom.out} bytes/sec out`);
1656
+
1657
+ // Get throughput history for graphing
1658
+ const history = metrics.throughput.history(300); // Last 5 minutes
1659
+ history.forEach(point => {
1660
+ console.log(`${new Date(point.timestamp)}: ${point.in} in, ${point.out} out`);
1661
+ });
1662
+
1663
+ // Get throughput by route
1664
+ const routeThroughput = metrics.throughput.byRoute(60); // Last 60 seconds
1665
+ routeThroughput.forEach((stats, route) => {
1666
+ console.log(`Route ${route}: ${stats.in} in, ${stats.out} out bytes/sec`);
1667
+ });
1627
1668
 
1628
- Additional extended methods available:
1669
+ // Get throughput by IP
1670
+ const ipThroughput = metrics.throughput.byIP(60);
1671
+ ipThroughput.forEach((stats, ip) => {
1672
+ console.log(`IP ${ip}: ${stats.in} in, ${stats.out} out bytes/sec`);
1673
+ });
1674
+ ```
1629
1675
 
1630
- - `getThroughputRate()`: Bytes per second rate for the last minute
1631
- - `getTopIPs(limit?: number)`: Get top IPs by connection count
1632
- - `isIPBlocked(ip: string, maxConnectionsPerIP: number)`: Check if an IP has reached the connection limit
1676
+ ### Request Metrics
1633
1677
 
1634
- ### Extended Metrics Example
1678
+ Track request rates:
1635
1679
 
1636
1680
  ```typescript
1637
- const stats = proxy.getStats() as any; // Extended methods are available
1681
+ // Get requests per second
1682
+ console.log(`RPS: ${metrics.requests.perSecond()}`);
1638
1683
 
1639
- // Get throughput rate
1640
- const rate = stats.getThroughputRate();
1641
- console.log(`Incoming: ${rate.bytesInPerSec} bytes/sec`);
1642
- console.log(`Outgoing: ${rate.bytesOutPerSec} bytes/sec`);
1684
+ // Get requests per minute
1685
+ console.log(`RPM: ${metrics.requests.perMinute()}`);
1643
1686
 
1644
- // Get top 10 IPs by connection count
1645
- const topIPs = stats.getTopIPs(10);
1646
- topIPs.forEach(({ ip, connections }) => {
1647
- console.log(`${ip}: ${connections} connections`);
1648
- });
1687
+ // Get total requests
1688
+ console.log(`Total requests: ${metrics.requests.total()}`);
1689
+ ```
1649
1690
 
1650
- // Check if an IP should be rate limited
1651
- if (stats.isIPBlocked('192.168.1.100', 100)) {
1652
- console.log('IP has too many connections');
1653
- }
1691
+ ### Cumulative Totals
1692
+
1693
+ Track total bytes transferred and connections:
1694
+
1695
+ ```typescript
1696
+ // Get total bytes
1697
+ console.log(`Total bytes in: ${metrics.totals.bytesIn()}`);
1698
+ console.log(`Total bytes out: ${metrics.totals.bytesOut()}`);
1699
+ console.log(`Total connections: ${metrics.totals.connections()}`);
1654
1700
  ```
1655
1701
 
1656
- ### Monitoring Example
1702
+ ### Performance Percentiles
1703
+
1704
+ Get percentile statistics (when implemented):
1657
1705
 
1658
1706
  ```typescript
1659
- // Create a monitoring loop
1707
+ // Connection duration percentiles
1708
+ const durations = metrics.percentiles.connectionDuration();
1709
+ console.log(`Connection durations - P50: ${durations.p50}ms, P95: ${durations.p95}ms, P99: ${durations.p99}ms`);
1710
+
1711
+ // Bytes transferred percentiles
1712
+ const bytes = metrics.percentiles.bytesTransferred();
1713
+ console.log(`Bytes in - P50: ${bytes.in.p50}, P95: ${bytes.in.p95}, P99: ${bytes.in.p99}`);
1714
+ console.log(`Bytes out - P50: ${bytes.out.p50}, P95: ${bytes.out.p95}, P99: ${bytes.out.p99}`);
1715
+ ```
1716
+
1717
+ ### Complete Monitoring Example
1718
+
1719
+ ```typescript
1720
+ // Create a monitoring dashboard
1660
1721
  setInterval(() => {
1661
- const stats = proxy.getStats();
1722
+ const metrics = proxy.getMetrics();
1662
1723
 
1663
1724
  // Log key metrics
1664
1725
  console.log({
1665
1726
  timestamp: new Date().toISOString(),
1666
- activeConnections: stats.getActiveConnections(),
1667
- rps: stats.getRequestsPerSecond(),
1668
- throughput: stats.getThroughput()
1727
+ connections: {
1728
+ active: metrics.connections.active(),
1729
+ total: metrics.connections.total()
1730
+ },
1731
+ throughput: {
1732
+ instant: metrics.throughput.instant(),
1733
+ average: metrics.throughput.average()
1734
+ },
1735
+ requests: {
1736
+ rps: metrics.requests.perSecond(),
1737
+ total: metrics.requests.total()
1738
+ },
1739
+ totals: {
1740
+ bytesIn: metrics.totals.bytesIn(),
1741
+ bytesOut: metrics.totals.bytesOut()
1742
+ }
1669
1743
  });
1670
1744
 
1671
- // Check for high connection counts from specific IPs
1672
- const ipConnections = stats.getConnectionsByIP();
1673
- for (const [ip, count] of ipConnections) {
1745
+ // Alert on high connection counts
1746
+ const topIPs = metrics.connections.topIPs(5);
1747
+ topIPs.forEach(({ ip, count }) => {
1674
1748
  if (count > 100) {
1675
1749
  console.warn(`High connection count from ${ip}: ${count}`);
1676
1750
  }
1751
+ });
1752
+
1753
+ // Alert on high throughput
1754
+ const instant = metrics.throughput.instant();
1755
+ if (instant.in > 100_000_000) { // 100 MB/s
1756
+ console.warn(`High incoming throughput: ${instant.in} bytes/sec`);
1677
1757
  }
1678
1758
  }, 10000); // Every 10 seconds
1679
1759
  ```
1680
1760
 
1681
1761
  ### Exporting Metrics
1682
1762
 
1683
- You can export metrics in various formats for external monitoring systems:
1763
+ Export metrics in various formats for external monitoring systems:
1684
1764
 
1685
1765
  ```typescript
1686
1766
  // Export as JSON
1687
1767
  app.get('/metrics.json', (req, res) => {
1688
- const stats = proxy.getStats();
1768
+ const metrics = proxy.getMetrics();
1689
1769
  res.json({
1690
- activeConnections: stats.getActiveConnections(),
1691
- totalConnections: stats.getTotalConnections(),
1692
- requestsPerSecond: stats.getRequestsPerSecond(),
1693
- throughput: stats.getThroughput(),
1694
- connectionsByRoute: Object.fromEntries(stats.getConnectionsByRoute()),
1695
- connectionsByIP: Object.fromEntries(stats.getConnectionsByIP())
1770
+ connections: {
1771
+ active: metrics.connections.active(),
1772
+ total: metrics.connections.total(),
1773
+ byRoute: Object.fromEntries(metrics.connections.byRoute()),
1774
+ byIP: Object.fromEntries(metrics.connections.byIP())
1775
+ },
1776
+ throughput: {
1777
+ instant: metrics.throughput.instant(),
1778
+ recent: metrics.throughput.recent(),
1779
+ average: metrics.throughput.average()
1780
+ },
1781
+ requests: {
1782
+ perSecond: metrics.requests.perSecond(),
1783
+ perMinute: metrics.requests.perMinute(),
1784
+ total: metrics.requests.total()
1785
+ },
1786
+ totals: {
1787
+ bytesIn: metrics.totals.bytesIn(),
1788
+ bytesOut: metrics.totals.bytesOut(),
1789
+ connections: metrics.totals.connections()
1790
+ }
1696
1791
  });
1697
1792
  });
1698
1793
 
1699
1794
  // Export as Prometheus format
1700
1795
  app.get('/metrics', (req, res) => {
1701
- const stats = proxy.getStats();
1796
+ const metrics = proxy.getMetrics();
1797
+ const instant = metrics.throughput.instant();
1798
+
1702
1799
  res.set('Content-Type', 'text/plain');
1703
1800
  res.send(`
1704
- # HELP smartproxy_active_connections Current active connections
1705
- # TYPE smartproxy_active_connections gauge
1706
- smartproxy_active_connections ${stats.getActiveConnections()}
1801
+ # HELP smartproxy_connections_active Current active connections
1802
+ # TYPE smartproxy_connections_active gauge
1803
+ smartproxy_connections_active ${metrics.connections.active()}
1804
+
1805
+ # HELP smartproxy_connections_total Total connections since start
1806
+ # TYPE smartproxy_connections_total counter
1807
+ smartproxy_connections_total ${metrics.connections.total()}
1808
+
1809
+ # HELP smartproxy_throughput_bytes_per_second Current throughput in bytes per second
1810
+ # TYPE smartproxy_throughput_bytes_per_second gauge
1811
+ smartproxy_throughput_bytes_per_second{direction="in"} ${instant.in}
1812
+ smartproxy_throughput_bytes_per_second{direction="out"} ${instant.out}
1707
1813
 
1708
1814
  # HELP smartproxy_requests_per_second Current requests per second
1709
1815
  # TYPE smartproxy_requests_per_second gauge
1710
- smartproxy_requests_per_second ${stats.getRequestsPerSecond()}
1816
+ smartproxy_requests_per_second ${metrics.requests.perSecond()}
1711
1817
 
1712
- # HELP smartproxy_bytes_in Total bytes received
1713
- # TYPE smartproxy_bytes_in counter
1714
- smartproxy_bytes_in ${stats.getThroughput().bytesIn}
1715
-
1716
- # HELP smartproxy_bytes_out Total bytes sent
1717
- # TYPE smartproxy_bytes_out counter
1718
- smartproxy_bytes_out ${stats.getThroughput().bytesOut}
1818
+ # HELP smartproxy_bytes_total Total bytes transferred
1819
+ # TYPE smartproxy_bytes_total counter
1820
+ smartproxy_bytes_total{direction="in"} ${metrics.totals.bytesIn()}
1821
+ smartproxy_bytes_total{direction="out"} ${metrics.totals.bytesOut()}
1719
1822
  `);
1720
1823
  });
1721
1824
  ```
1722
1825
 
1826
+ ### Metrics API Reference
1827
+
1828
+ The metrics API is organized into logical groups:
1829
+
1830
+ ```typescript
1831
+ interface IMetrics {
1832
+ connections: {
1833
+ active(): number;
1834
+ total(): number;
1835
+ byRoute(): Map<string, number>;
1836
+ byIP(): Map<string, number>;
1837
+ topIPs(limit?: number): Array<{ ip: string; count: number }>;
1838
+ };
1839
+
1840
+ throughput: {
1841
+ instant(): IThroughputData; // Last 1 second
1842
+ recent(): IThroughputData; // Last 10 seconds
1843
+ average(): IThroughputData; // Last 60 seconds
1844
+ custom(seconds: number): IThroughputData;
1845
+ history(seconds: number): Array<IThroughputHistoryPoint>;
1846
+ byRoute(windowSeconds?: number): Map<string, IThroughputData>;
1847
+ byIP(windowSeconds?: number): Map<string, IThroughputData>;
1848
+ };
1849
+
1850
+ requests: {
1851
+ perSecond(): number;
1852
+ perMinute(): number;
1853
+ total(): number;
1854
+ };
1855
+
1856
+ totals: {
1857
+ bytesIn(): number;
1858
+ bytesOut(): number;
1859
+ connections(): number;
1860
+ };
1861
+
1862
+ percentiles: {
1863
+ connectionDuration(): { p50: number; p95: number; p99: number };
1864
+ bytesTransferred(): {
1865
+ in: { p50: number; p95: number; p99: number };
1866
+ out: { p50: number; p95: number; p99: number };
1867
+ };
1868
+ };
1869
+ }
1870
+ ```
1871
+
1872
+ Where `IThroughputData` is:
1873
+ ```typescript
1874
+ interface IThroughputData {
1875
+ in: number; // Bytes per second incoming
1876
+ out: number; // Bytes per second outgoing
1877
+ }
1878
+ ```
1879
+
1880
+ And `IThroughputHistoryPoint` is:
1881
+ ```typescript
1882
+ interface IThroughputHistoryPoint {
1883
+ timestamp: number; // Unix timestamp in milliseconds
1884
+ in: number; // Bytes per second at this point
1885
+ out: number; // Bytes per second at this point
1886
+ }
1887
+ ```
1888
+
1723
1889
  ## Other Components
1724
1890
 
1725
1891
  While SmartProxy provides a unified API for most needs, you can also use individual components: