@push.rocks/smartproxy 19.6.2 → 19.6.6

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.
package/readme.md CHANGED
@@ -665,6 +665,661 @@ redirect: {
665
665
  }
666
666
  ```
667
667
 
668
+ ## Forwarding Modes Guide
669
+
670
+ This section provides a comprehensive reference for all forwarding modes available in SmartProxy, helping you choose the right configuration for your use case.
671
+
672
+ ### Visual Overview
673
+
674
+ ```mermaid
675
+ graph TD
676
+ A[Incoming Traffic] --> B{Action Type?}
677
+
678
+ B -->|forward| C{TLS Mode?}
679
+ B -->|socket-handler| D[Custom Handler]
680
+
681
+ C -->|terminate| E[Decrypt TLS]
682
+ C -->|passthrough| F[Forward Encrypted]
683
+ C -->|terminate-and-reencrypt| G[Decrypt & Re-encrypt]
684
+ C -->|none/HTTP| H[Forward HTTP]
685
+
686
+ E --> I{Engine?}
687
+ F --> I
688
+ G --> I
689
+ H --> I
690
+
691
+ I -->|node| J[Node.js Processing]
692
+ I -->|nftables| K[Kernel NAT]
693
+
694
+ J --> L[Backend]
695
+ K --> L
696
+ D --> M[Custom Logic]
697
+
698
+ style B fill:#f9f,stroke:#333,stroke-width:2px
699
+ style C fill:#bbf,stroke:#333,stroke-width:2px
700
+ style I fill:#bfb,stroke:#333,stroke-width:2px
701
+ ```
702
+
703
+ ### Overview
704
+
705
+ SmartProxy offers flexible traffic forwarding through combinations of:
706
+ - **Action Types**: How to handle matched traffic
707
+ - **TLS Modes**: How to handle HTTPS/TLS connections
708
+ - **Forwarding Engines**: Where packet processing occurs
709
+
710
+ ### Quick Reference
711
+
712
+ #### Modern Route-Based Configuration
713
+
714
+ | Use Case | Action Type | TLS Mode | Engine | Performance | Security |
715
+ |----------|------------|----------|---------|-------------|----------|
716
+ | HTTP web server | `forward` | N/A | `node` | Good | Basic |
717
+ | HTTPS web server (inspect traffic) | `forward` | `terminate` | `node` | Good | Full inspection |
718
+ | HTTPS passthrough (no inspection) | `forward` | `passthrough` | `node` | Better | End-to-end encryption |
719
+ | HTTPS gateway (re-encrypt to backend) | `forward` | `terminate-and-reencrypt` | `node` | Moderate | Full control |
720
+ | High-performance TCP forwarding | `forward` | `passthrough` | `nftables` | Excellent | Basic |
721
+ | Custom protocol handling | `socket-handler` | N/A | `node` | Varies | Custom |
722
+
723
+ #### Legacy Forwarding Types (Deprecated)
724
+
725
+ | Legacy Type | Modern Equivalent |
726
+ |------------|------------------|
727
+ | `http-only` | `action.type: 'forward'` with port 80 |
728
+ | `https-passthrough` | `action.type: 'forward'` + `tls.mode: 'passthrough'` |
729
+ | `https-terminate-to-http` | `action.type: 'forward'` + `tls.mode: 'terminate'` |
730
+ | `https-terminate-to-https` | `action.type: 'forward'` + `tls.mode: 'terminate-and-reencrypt'` |
731
+
732
+ ### Forwarding Mode Categories
733
+
734
+ #### 1. Action Types
735
+
736
+ ##### Forward Action
737
+ Routes traffic to a backend server. This is the most common action type.
738
+
739
+ ```typescript
740
+ {
741
+ action: {
742
+ type: 'forward',
743
+ target: {
744
+ host: 'backend-server',
745
+ port: 8080
746
+ }
747
+ }
748
+ }
749
+ ```
750
+
751
+ ##### Socket Handler Action
752
+ Provides custom handling for any TCP protocol. Used for specialized protocols or custom logic.
753
+
754
+ ```typescript
755
+ {
756
+ action: {
757
+ type: 'socket-handler',
758
+ socketHandler: async (socket, context) => {
759
+ // Custom protocol implementation
760
+ }
761
+ }
762
+ }
763
+ ```
764
+
765
+ #### 2. TLS Modes (for Forward Action)
766
+
767
+ ##### Passthrough Mode
768
+ - **What**: Forwards encrypted TLS traffic without decryption
769
+ - **When**: Backend handles its own TLS termination
770
+ - **Pros**: Maximum performance, true end-to-end encryption
771
+ - **Cons**: Cannot inspect or modify HTTPS traffic
772
+
773
+ ```mermaid
774
+ graph LR
775
+ Client -->|TLS| SmartProxy
776
+ SmartProxy -->|TLS| Backend
777
+ style SmartProxy fill:#f9f,stroke:#333,stroke-width:2px
778
+ ```
779
+
780
+ ##### Terminate Mode
781
+ - **What**: Decrypts TLS, forwards as plain HTTP
782
+ - **When**: Backend doesn't support HTTPS or you need to inspect traffic
783
+ - **Pros**: Can modify headers, inspect content, add security headers
784
+ - **Cons**: Backend connection is unencrypted
785
+
786
+ ```mermaid
787
+ graph LR
788
+ Client -->|TLS| SmartProxy
789
+ SmartProxy -->|HTTP| Backend
790
+ style SmartProxy fill:#f9f,stroke:#333,stroke-width:2px
791
+ ```
792
+
793
+ ##### Terminate-and-Reencrypt Mode
794
+ - **What**: Decrypts TLS, then creates new TLS connection to backend
795
+ - **When**: Need traffic inspection but backend requires HTTPS
796
+ - **Pros**: Full control while maintaining backend security
797
+ - **Cons**: Higher CPU usage, increased latency
798
+
799
+ ```mermaid
800
+ graph LR
801
+ Client -->|TLS| SmartProxy
802
+ SmartProxy -->|New TLS| Backend
803
+ style SmartProxy fill:#f9f,stroke:#333,stroke-width:2px
804
+ ```
805
+
806
+ #### 3. Forwarding Engines
807
+
808
+ ##### Node.js Engine (Default)
809
+ - **Processing**: Application-level in Node.js event loop
810
+ - **Features**: Full protocol support, header manipulation, WebSockets
811
+ - **Performance**: Good for most use cases
812
+ - **Use when**: You need application-layer features
813
+
814
+ ##### NFTables Engine
815
+ - **Processing**: Kernel-level packet forwarding
816
+ - **Features**: Basic NAT, minimal overhead
817
+ - **Performance**: Excellent, near wire-speed
818
+ - **Use when**: Maximum performance is critical
819
+ - **Requirements**: Linux, root permissions, NFTables installed
820
+
821
+ ### Detailed Mode Explanations
822
+
823
+ #### HTTP Forwarding (Port 80)
824
+
825
+ Simple HTTP forwarding without encryption:
826
+
827
+ ```typescript
828
+ {
829
+ match: { ports: 80, domains: 'example.com' },
830
+ action: {
831
+ type: 'forward',
832
+ target: { host: 'localhost', port: 8080 }
833
+ }
834
+ }
835
+ ```
836
+
837
+ **Data Flow**: Client → SmartProxy (HTTP) → Backend (HTTP)
838
+
839
+ #### HTTPS with TLS Termination
840
+
841
+ Decrypt HTTPS and forward as HTTP:
842
+
843
+ ```typescript
844
+ {
845
+ match: { ports: 443, domains: 'secure.example.com' },
846
+ action: {
847
+ type: 'forward',
848
+ target: { host: 'localhost', port: 8080 },
849
+ tls: {
850
+ mode: 'terminate',
851
+ certificate: 'auto' // Use Let's Encrypt
852
+ }
853
+ }
854
+ }
855
+ ```
856
+
857
+ **Data Flow**: Client → SmartProxy (HTTPS decrypt) → Backend (HTTP)
858
+
859
+ #### HTTPS Passthrough
860
+
861
+ Forward encrypted traffic without decryption:
862
+
863
+ ```typescript
864
+ {
865
+ match: { ports: 443, domains: 'legacy.example.com' },
866
+ action: {
867
+ type: 'forward',
868
+ target: { host: '192.168.1.10', port: 443 },
869
+ tls: {
870
+ mode: 'passthrough'
871
+ }
872
+ }
873
+ }
874
+ ```
875
+
876
+ **Data Flow**: Client → SmartProxy (TLS forwarding) → Backend (Original TLS)
877
+
878
+ #### HTTPS Gateway (Terminate and Re-encrypt)
879
+
880
+ Decrypt, inspect, then re-encrypt to backend:
881
+
882
+ ```typescript
883
+ {
884
+ match: { ports: 443, domains: 'api.example.com' },
885
+ action: {
886
+ type: 'forward',
887
+ target: { host: 'api-backend', port: 443 },
888
+ tls: {
889
+ mode: 'terminate-and-reencrypt',
890
+ certificate: 'auto'
891
+ },
892
+ advanced: {
893
+ headers: {
894
+ 'X-Forwarded-Proto': 'https',
895
+ 'X-Real-IP': '{clientIp}'
896
+ }
897
+ }
898
+ }
899
+ }
900
+ ```
901
+
902
+ **Data Flow**: Client → SmartProxy (HTTPS decrypt) → SmartProxy (New HTTPS) → Backend
903
+
904
+ #### High-Performance NFTables Forwarding
905
+
906
+ Kernel-level forwarding for maximum performance:
907
+
908
+ ```typescript
909
+ {
910
+ match: { ports: 443, domains: 'fast.example.com' },
911
+ action: {
912
+ type: 'forward',
913
+ target: { host: 'backend', port: 443 },
914
+ tls: { mode: 'passthrough' },
915
+ forwardingEngine: 'nftables',
916
+ nftables: {
917
+ preserveSourceIP: true,
918
+ maxRate: '10gbps'
919
+ }
920
+ }
921
+ }
922
+ ```
923
+
924
+ **Data Flow**: Client → Kernel (NFTables NAT) → Backend
925
+
926
+ #### Custom Socket Handler
927
+
928
+ Handle custom protocols or implement specialized logic:
929
+
930
+ ```typescript
931
+ {
932
+ match: { ports: 9000, domains: 'custom.example.com' },
933
+ action: {
934
+ type: 'socket-handler',
935
+ socketHandler: async (socket, context) => {
936
+ console.log(`Connection from ${context.clientIp}`);
937
+
938
+ socket.write('Welcome to custom protocol server\n');
939
+
940
+ socket.on('data', (data) => {
941
+ // Handle custom protocol
942
+ const response = processCustomProtocol(data);
943
+ socket.write(response);
944
+ });
945
+ }
946
+ }
947
+ }
948
+ ```
949
+
950
+ ### Decision Guide
951
+
952
+ #### Choose HTTP Forwarding When:
953
+ - Backend only supports HTTP
954
+ - Internal services not exposed to internet
955
+ - Development/testing environments
956
+
957
+ #### Choose HTTPS Termination When:
958
+ - Need to inspect/modify HTTP traffic
959
+ - Backend doesn't support HTTPS
960
+ - Want to add security headers
961
+ - Need to cache responses
962
+
963
+ #### Choose HTTPS Passthrough When:
964
+ - Backend manages its own certificates
965
+ - Need true end-to-end encryption
966
+ - Compliance requires no MITM
967
+ - WebSocket connections to backend
968
+
969
+ #### Choose HTTPS Terminate-and-Reencrypt When:
970
+ - Need traffic inspection AND backend requires HTTPS
971
+ - API gateway scenarios
972
+ - Adding authentication layers
973
+ - Different certificates for client/backend
974
+
975
+ #### Choose NFTables Engine When:
976
+ - Handling 1Gbps+ traffic
977
+ - Thousands of concurrent connections
978
+ - Minimal latency is critical
979
+ - Don't need application-layer features
980
+
981
+ #### Choose Socket Handler When:
982
+ - Implementing custom protocols
983
+ - Need fine-grained connection control
984
+ - Building protocol adapters
985
+ - Special authentication flows
986
+
987
+ ### Complete Examples
988
+
989
+ #### Example 1: Complete Web Application
990
+
991
+ ```typescript
992
+ const proxy = new SmartProxy({
993
+ routes: [
994
+ // HTTP to HTTPS redirect
995
+ {
996
+ match: { ports: 80, domains: ['example.com', 'www.example.com'] },
997
+ action: {
998
+ type: 'socket-handler',
999
+ socketHandler: SocketHandlers.httpRedirect('https://{domain}{path}')
1000
+ }
1001
+ },
1002
+
1003
+ // Main website with TLS termination
1004
+ {
1005
+ match: { ports: 443, domains: ['example.com', 'www.example.com'] },
1006
+ action: {
1007
+ type: 'forward',
1008
+ target: { host: 'web-backend', port: 3000 },
1009
+ tls: {
1010
+ mode: 'terminate',
1011
+ certificate: 'auto'
1012
+ },
1013
+ websocket: { enabled: true }
1014
+ }
1015
+ },
1016
+
1017
+ // API with re-encryption
1018
+ {
1019
+ match: { ports: 443, domains: 'api.example.com' },
1020
+ action: {
1021
+ type: 'forward',
1022
+ target: { host: 'api-backend', port: 443 },
1023
+ tls: {
1024
+ mode: 'terminate-and-reencrypt',
1025
+ certificate: 'auto'
1026
+ }
1027
+ },
1028
+ security: {
1029
+ ipAllowList: ['10.0.0.0/8'],
1030
+ rateLimit: {
1031
+ enabled: true,
1032
+ maxRequests: 100,
1033
+ window: 60
1034
+ }
1035
+ }
1036
+ }
1037
+ ]
1038
+ });
1039
+ ```
1040
+
1041
+ #### Example 2: Multi-Mode Proxy Setup
1042
+
1043
+ ```typescript
1044
+ const proxy = new SmartProxy({
1045
+ routes: [
1046
+ // Legacy app with passthrough
1047
+ {
1048
+ match: { ports: 443, domains: 'legacy.example.com' },
1049
+ action: {
1050
+ type: 'forward',
1051
+ target: { host: 'legacy-server', port: 443 },
1052
+ tls: { mode: 'passthrough' }
1053
+ }
1054
+ },
1055
+
1056
+ // High-performance streaming with NFTables
1057
+ {
1058
+ match: { ports: 8080, domains: 'stream.example.com' },
1059
+ action: {
1060
+ type: 'forward',
1061
+ target: { host: 'stream-backend', port: 8080 },
1062
+ forwardingEngine: 'nftables',
1063
+ nftables: {
1064
+ protocol: 'tcp',
1065
+ preserveSourceIP: true
1066
+ }
1067
+ }
1068
+ },
1069
+
1070
+ // Custom protocol handler
1071
+ {
1072
+ match: { ports: 9999 },
1073
+ action: {
1074
+ type: 'socket-handler',
1075
+ socketHandler: SocketHandlers.proxy('custom-backend', 9999)
1076
+ }
1077
+ }
1078
+ ]
1079
+ });
1080
+ ```
1081
+
1082
+ ### Performance Considerations
1083
+
1084
+ #### Node.js Engine Performance
1085
+
1086
+ | Metric | Typical Performance |
1087
+ |--------|-------------------|
1088
+ | Throughput | 1-10 Gbps |
1089
+ | Connections | 10,000-50,000 concurrent |
1090
+ | Latency | 1-5ms added |
1091
+ | CPU Usage | Moderate |
1092
+
1093
+ **Best for**: Most web applications, APIs, sites needing inspection
1094
+
1095
+ #### NFTables Engine Performance
1096
+
1097
+ | Metric | Typical Performance |
1098
+ |--------|-------------------|
1099
+ | Throughput | 10-100 Gbps |
1100
+ | Connections | 100,000+ concurrent |
1101
+ | Latency | <0.1ms added |
1102
+ | CPU Usage | Minimal |
1103
+
1104
+ **Best for**: High-traffic services, streaming, gaming, TCP forwarding
1105
+
1106
+ #### Performance Tips
1107
+
1108
+ 1. **Use passthrough mode** when you don't need inspection
1109
+ 2. **Enable NFTables** for high-traffic services
1110
+ 3. **Terminate TLS only when necessary** - it adds CPU overhead
1111
+ 4. **Use connection pooling** for terminate-and-reencrypt mode
1112
+ 5. **Enable HTTP/2** for better multiplexing
1113
+
1114
+ ### Security Implications
1115
+
1116
+ #### TLS Termination Security
1117
+
1118
+ **Pros:**
1119
+ - Inspect traffic for threats
1120
+ - Add security headers
1121
+ - Implement WAF rules
1122
+ - Log requests for audit
1123
+
1124
+ **Cons:**
1125
+ - Proxy has access to decrypted data
1126
+ - Requires secure certificate storage
1127
+ - Potential compliance issues
1128
+
1129
+ **Best Practices:**
1130
+ - Use auto-renewal with Let's Encrypt
1131
+ - Store certificates securely
1132
+ - Implement proper access controls
1133
+ - Use strong TLS configurations
1134
+
1135
+ #### Passthrough Security
1136
+
1137
+ **Pros:**
1138
+ - True end-to-end encryption
1139
+ - No MITM concerns
1140
+ - Backend controls security
1141
+
1142
+ **Cons:**
1143
+ - Cannot inspect traffic
1144
+ - Cannot add security headers
1145
+ - Limited DDoS protection
1146
+
1147
+ #### Socket Handler Security
1148
+
1149
+ **Risks:**
1150
+ - Custom code may have vulnerabilities
1151
+ - Resource exhaustion possible
1152
+ - Authentication bypass risks
1153
+
1154
+ **Mitigations:**
1155
+ ```typescript
1156
+ {
1157
+ action: {
1158
+ type: 'socket-handler',
1159
+ socketHandler: async (socket, context) => {
1160
+ // Always validate and sanitize input
1161
+ socket.on('data', (data) => {
1162
+ if (data.length > MAX_SIZE) {
1163
+ socket.destroy();
1164
+ return;
1165
+ }
1166
+ // Process safely...
1167
+ });
1168
+
1169
+ // Set timeouts
1170
+ socket.setTimeout(30000);
1171
+
1172
+ // Rate limit connections
1173
+ if (connectionsFromIP(context.clientIp) > 10) {
1174
+ socket.destroy();
1175
+ }
1176
+ }
1177
+ }
1178
+ }
1179
+ ```
1180
+
1181
+ ### Migration from Legacy Types
1182
+
1183
+ #### From `http-only`
1184
+
1185
+ **Old:**
1186
+ ```typescript
1187
+ {
1188
+ type: 'http-only',
1189
+ target: { host: 'localhost', port: 8080 }
1190
+ }
1191
+ ```
1192
+
1193
+ **New:**
1194
+ ```typescript
1195
+ {
1196
+ match: { ports: 80, domains: 'example.com' },
1197
+ action: {
1198
+ type: 'forward',
1199
+ target: { host: 'localhost', port: 8080 }
1200
+ }
1201
+ }
1202
+ ```
1203
+
1204
+ #### From `https-passthrough`
1205
+
1206
+ **Old:**
1207
+ ```typescript
1208
+ {
1209
+ type: 'https-passthrough',
1210
+ target: { host: 'backend', port: 443 }
1211
+ }
1212
+ ```
1213
+
1214
+ **New:**
1215
+ ```typescript
1216
+ {
1217
+ match: { ports: 443, domains: 'example.com' },
1218
+ action: {
1219
+ type: 'forward',
1220
+ target: { host: 'backend', port: 443 },
1221
+ tls: { mode: 'passthrough' }
1222
+ }
1223
+ }
1224
+ ```
1225
+
1226
+ #### From `https-terminate-to-http`
1227
+
1228
+ **Old:**
1229
+ ```typescript
1230
+ {
1231
+ type: 'https-terminate-to-http',
1232
+ target: { host: 'localhost', port: 8080 },
1233
+ ssl: { /* certs */ }
1234
+ }
1235
+ ```
1236
+
1237
+ **New:**
1238
+ ```typescript
1239
+ {
1240
+ match: { ports: 443, domains: 'example.com' },
1241
+ action: {
1242
+ type: 'forward',
1243
+ target: { host: 'localhost', port: 8080 },
1244
+ tls: {
1245
+ mode: 'terminate',
1246
+ certificate: 'auto' // or provide cert/key
1247
+ }
1248
+ }
1249
+ }
1250
+ ```
1251
+
1252
+ #### From `https-terminate-to-https`
1253
+
1254
+ **Old:**
1255
+ ```typescript
1256
+ {
1257
+ type: 'https-terminate-to-https',
1258
+ target: { host: 'backend', port: 443 },
1259
+ ssl: { /* certs */ }
1260
+ }
1261
+ ```
1262
+
1263
+ **New:**
1264
+ ```typescript
1265
+ {
1266
+ match: { ports: 443, domains: 'example.com' },
1267
+ action: {
1268
+ type: 'forward',
1269
+ target: { host: 'backend', port: 443 },
1270
+ tls: {
1271
+ mode: 'terminate-and-reencrypt',
1272
+ certificate: 'auto'
1273
+ }
1274
+ }
1275
+ }
1276
+ ```
1277
+
1278
+ ### Helper Functions Quick Reference
1279
+
1280
+ SmartProxy provides helper functions for common configurations:
1281
+
1282
+ ```typescript
1283
+ // HTTP forwarding
1284
+ createHttpRoute('example.com', { host: 'localhost', port: 8080 })
1285
+
1286
+ // HTTPS with termination
1287
+ createHttpsTerminateRoute('secure.com', { host: 'localhost', port: 8080 }, {
1288
+ certificate: 'auto'
1289
+ })
1290
+
1291
+ // HTTPS passthrough
1292
+ createHttpsPassthroughRoute('legacy.com', { host: 'backend', port: 443 })
1293
+
1294
+ // Complete HTTPS setup (includes HTTP redirect)
1295
+ ...createCompleteHttpsServer('example.com', { host: 'localhost', port: 8080 }, {
1296
+ certificate: 'auto'
1297
+ })
1298
+
1299
+ // NFTables high-performance
1300
+ createNfTablesRoute('fast.com', { host: 'backend', port: 8080 }, {
1301
+ ports: 80,
1302
+ preserveSourceIP: true
1303
+ })
1304
+
1305
+ // Custom socket handler
1306
+ createSocketHandlerRoute('custom.com', 9000, async (socket, context) => {
1307
+ // Handler implementation
1308
+ })
1309
+ ```
1310
+
1311
+ ### Summary
1312
+
1313
+ SmartProxy's forwarding modes provide flexibility for any proxy scenario:
1314
+
1315
+ - **Simple HTTP/HTTPS forwarding** for most web applications
1316
+ - **TLS passthrough** for end-to-end encryption
1317
+ - **TLS termination** for traffic inspection and modification
1318
+ - **NFTables** for extreme performance requirements
1319
+ - **Socket handlers** for custom protocols
1320
+
1321
+ Choose based on your security requirements, performance needs, and whether you need to inspect or modify traffic. The modern route-based configuration provides a consistent interface regardless of the forwarding mode you choose.
1322
+
668
1323
  ### Route Metadata and Prioritization
669
1324
 
670
1325
  You can add metadata to routes to help with organization and control matching priority:
@@ -970,6 +1625,34 @@ The `IProxyStats` interface provides the following methods:
970
1625
  - `getConnectionsByRoute()`: Connection count per route
971
1626
  - `getConnectionsByIP()`: Connection count per client IP
972
1627
 
1628
+ Additional extended methods available:
1629
+
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
1633
+
1634
+ ### Extended Metrics Example
1635
+
1636
+ ```typescript
1637
+ const stats = proxy.getStats() as any; // Extended methods are available
1638
+
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`);
1643
+
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
+ });
1649
+
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
+ }
1654
+ ```
1655
+
973
1656
  ### Monitoring Example
974
1657
 
975
1658
  ```typescript
@@ -1736,6 +2419,62 @@ createHttpToHttpsRedirect('old.example.com', 443)
1736
2419
  }
1737
2420
  ```
1738
2421
 
2422
+ ## WebSocket Keep-Alive Configuration
2423
+
2424
+ If your WebSocket connections are disconnecting every 30 seconds in SNI passthrough mode, here's how to configure keep-alive settings:
2425
+
2426
+ ### Extended Keep-Alive Treatment (Recommended)
2427
+
2428
+ ```typescript
2429
+ const proxy = new SmartProxy({
2430
+ // Extend timeout for keep-alive connections
2431
+ keepAliveTreatment: 'extended',
2432
+ keepAliveInactivityMultiplier: 10, // 10x the base timeout
2433
+ inactivityTimeout: 14400000, // 4 hours base (40 hours with multiplier)
2434
+
2435
+ routes: [
2436
+ {
2437
+ name: 'websocket-passthrough',
2438
+ match: {
2439
+ ports: 443,
2440
+ domains: ['ws.example.com', 'wss.example.com']
2441
+ },
2442
+ action: {
2443
+ type: 'forward',
2444
+ target: { host: 'backend', port: 443 },
2445
+ tls: { mode: 'passthrough' }
2446
+ }
2447
+ }
2448
+ ]
2449
+ });
2450
+ ```
2451
+
2452
+ ### Immortal Connections (Never Timeout)
2453
+
2454
+ ```typescript
2455
+ const proxy = new SmartProxy({
2456
+ // Never timeout keep-alive connections
2457
+ keepAliveTreatment: 'immortal',
2458
+
2459
+ routes: [
2460
+ // ... same as above
2461
+ ]
2462
+ });
2463
+ ```
2464
+
2465
+ ### Understanding the Issue
2466
+
2467
+ In SNI passthrough mode:
2468
+ 1. **WebSocket Heartbeat**: The HTTP proxy's WebSocket handler sends ping frames every 30 seconds
2469
+ 2. **SNI Passthrough**: In passthrough mode, traffic is encrypted end-to-end
2470
+ 3. **Can't Inject Pings**: The proxy can't inject ping frames into encrypted traffic
2471
+ 4. **Connection Terminated**: After 30 seconds, connection is marked inactive and closed
2472
+
2473
+ The solution involves:
2474
+ - Longer grace periods for encrypted connections (5 minutes vs 30 seconds)
2475
+ - Relying on OS-level TCP keep-alive instead of application-level heartbeat
2476
+ - Different timeout strategies per route type
2477
+
1739
2478
  ## Configuration Options
1740
2479
 
1741
2480
  ### SmartProxy (IRoutedSmartProxyOptions)
@@ -1746,6 +2485,7 @@ createHttpToHttpsRedirect('old.example.com', 443)
1746
2485
  - `httpProxyPort` (number, default 8443) - Port where HttpProxy listens for forwarded connections
1747
2486
  - Connection timeouts: `initialDataTimeout`, `socketTimeout`, `inactivityTimeout`, etc.
1748
2487
  - Socket opts: `noDelay`, `keepAlive`, `enableKeepAliveProbes`
2488
+ - Keep-alive configuration: `keepAliveTreatment` ('standard'|'extended'|'immortal'), `keepAliveInactivityMultiplier`
1749
2489
  - `certProvisionFunction` (callback) - Custom certificate provisioning
1750
2490
 
1751
2491
  #### SmartProxy Dynamic Port Management Methods