smartproxy/readme.plan.md

8.1 KiB

SmartProxy Codebase Cleanup Plan

Overview

This document outlines a comprehensive plan to clean up the SmartProxy codebase by removing deprecated and unused code, consolidating functionality, and reducing complexity. The goal is to make the codebase more maintainable, easier to understand, and better positioned for future enhancements.

Phase 1: Remove Deprecated Code

1.1 Delete Legacy Migration Utilities

The route migration utilities were created to assist in transitioning from the legacy domain-based configuration to the new route-based configuration system. As this migration is now complete, these utilities can be safely removed.

  • Action: Remove /ts/proxies/smart-proxy/utils/route-migration-utils.ts
  • Impact: Low - This file is explicitly marked as temporary and for migration purposes only
  • Dependencies: Update any imports of these utilities (check forwarding-types.ts)

1.2 Clean Up References to Deleted Files

Several files are marked for deletion in the git status but are still referenced in the codebase.

  • Action: Remove references to deleted route-helpers files:
    • Update /ts/proxies/smart-proxy/utils/index.ts to remove export * from './route-helpers.js';
    • Update /ts/forwarding/config/forwarding-types.ts to remove imports and re-exports of route helper functions
  • Impact: Medium - May break code that still relies on these helpers
  • Dependencies: Ensure route-patterns.js provides equivalent functionality (moved helper functions from route-helpers.js to route-patterns.ts)

1.3 Remove Deprecated Forwarding Types and Helpers

Legacy forwarding types and helper functions in forwarding-types.ts are marked as deprecated.

  • Action:
    • Clean up /ts/forwarding/config/forwarding-types.ts
    • Remove deprecated helper functions: httpOnly, tlsTerminateToHttp, tlsTerminateToHttps, httpsPassthrough
    • Remove deprecated interfaces: IDeprecatedForwardConfig
  • Impact: Medium - May break code that still uses these helpers
  • Dependencies: Ensure route patterns provide equivalent functionality

Phase 2: Consolidate and Simplify Code

2.1 Streamline Interface Definitions

There are several redundant interfaces that could be simplified.

  • Action:
    • Remove legacy type checking functions (isLegacyOptions, isRoutedOptions) in /ts/proxies/smart-proxy/models/interfaces.ts
    • Update ISmartProxyOptions interface to remove obsolete properties
    • Remove backward compatibility aliases like IRoutedSmartProxyOptions
  • Impact: Medium - May break code that relies on these interfaces
  • Dependencies: Update any code that references these interfaces

2.2 Consolidate Route Utilities

The route utilities are spread across multiple files with some overlapping functionality.

  • Action:
    • Consolidate route utilities into a single coherent structure
    • Move common functions from route-utils.ts, route-patterns.ts into a single location
    • Ensure consistent naming conventions for route utility functions
  • Impact: Medium - Requires careful refactoring
  • Dependencies: Update all references to these utilities

2.3 Clean Up Legacy Connection Handling

The route-connection-handler.ts file contains legacy code and parameters kept for backward compatibility.

  • Action:
    • Remove unused parameters and legacy comments from setupDirectConnection method
    • Simplify connection handling logic by removing special cases for legacy configurations
  • Impact: Medium - Requires careful testing to ensure no regressions
  • Dependencies: Test with all current route configurations

Phase 3: Code Modernization

3.1 Standardize on 'preserve' Port Handling

Previously implemented changes to use port: 'preserve' instead of preservePort: true should be consistently applied.

  • Action:
    • Ensure all code paths handle the 'preserve' value for port
    • Remove any remaining references to preservePort in code and documentation
  • Impact: Low - Already implemented in most places
  • Dependencies: None

3.2 Normalize IPv6-Mapped IPv4 Addresses

Implement consistent handling of IPv6-mapped IPv4 addresses throughout the codebase.

  • Action:
    • Ensure any IP address comparisons consistently handle IPv6-mapped IPv4 addresses
    • Standardize on a single approach to IP normalization
  • Impact: Low - Already partially implemented
  • Dependencies: None

3.3 Improve Type Safety

Enhance type safety throughout the codebase to catch errors at compile time.

  • Action:
    • Add stronger types where appropriate
    • Remove any any types that could be replaced with more specific types
    • Add explicit return types to functions
  • Impact: Medium - May uncover existing issues
  • Dependencies: None

Phase 4: Documentation and Tests

4.1 Update API Documentation

Ensure documentation is current and accurately reflects the cleaned-up API.

  • Action:
    • Update comments and JSDoc throughout the codebase
    • Ensure porthandling.md and other documentation reflect current implementation
    • Remove references to deprecated functionality
  • Impact: Low
  • Dependencies: None

4.2 Add or Update Tests

Ensure test coverage for the cleaned-up codebase.

  • Action:
    • Update existing tests to remove references to deprecated functionality
    • Add tests for edge cases in IP normalization
    • Add tests for the updated route utility functions
  • Impact: Medium
  • Dependencies: None

Implementation Sequence

The changes were implemented in this order:

  1. Phase 1.1: Remove Legacy Migration Utilities
  2. Phase 1.2: Clean Up References to Deleted Files
  3. Phase 1.3: Remove Deprecated Forwarding Types and Helpers
  4. Phase 2.1: Streamline Interface Definitions
  5. Phase 3.1: Standardize on 'preserve' Port Handling
  6. Phase 3.2: Normalize IPv6-Mapped IPv4 Addresses
  7. ⏸️ Phase 2.2: Consolidate Route Utilities (Postponed - Low priority)
  8. Phase 2.3: Clean Up Legacy Connection Handling
  9. Phase 3.3: Improve Type Safety
  10. Phase 4.1: Update API Documentation
  11. Phase 4.2: Add or Update Tests

Detailed Implementation Steps

1. Remove Legacy Migration Utilities

# Delete the file
git rm ts/proxies/smart-proxy/utils/route-migration-utils.ts

# Remove the export from the index file
# Edit ts/proxies/smart-proxy/utils/index.ts to remove the export line

2. Clean Up References to Deleted Files

# Update forwarding-types.ts to remove imports from route-helpers.js
# Edit ts/forwarding/config/forwarding-types.ts

# Remove or update imports in index.ts
# Edit ts/proxies/smart-proxy/utils/index.ts

3. Remove Deprecated Forwarding Types

# Edit ts/forwarding/config/forwarding-types.ts to remove deprecated helpers and interfaces

4. Streamline Interface Definitions

# Edit ts/proxies/smart-proxy/models/interfaces.ts to remove legacy functions and aliases

5. Normalize IPv6-Mapped IPv4 Addresses

Ensure all IP matching functions consistently handle IPv6-mapped IPv4 addresses:

// In all IP matching functions:
const normalizeIp = (ip: string): string => {
  return ip.startsWith('::ffff:') ? ip.substring(7) : ip;
};

Implementation Results

The cleanup implementation was successful, resulting in:

  • Reduced Codebase Size: Successfully removed multiple deprecated files and functions
  • Improved Maintainability: Cleaner, more focused code without legacy compatibility layers
  • Reduced Complexity: Eliminated special cases for legacy config formats
  • Better Developer Experience: Standardized on consistent patterns for port handling
  • Future-Proofing: Removed deprecated code that would complicate future upgrades
  • Type Safety: Fixed multiple TypeScript errors and improved type checking

All changes successfully compile and the build process passes with no errors. The codebase is now simpler, more maintainable, and better positioned for future enhancements.