StartWithContext starts the SOCKS5 proxy server with context support for graceful shutdown
(ctx context.Context, port int, quicConn quic.Connection)
| 846 | |
| 847 | // StartWithContext starts the SOCKS5 proxy server with context support for graceful shutdown |
| 848 | func (p *DefaultProxy) StartWithContext(ctx context.Context, port int, quicConn quic.Connection) error { |
| 849 | socksAddr := fmt.Sprintf(":%d", port) |
| 850 | socksListener, err := net.Listen("tcp", socksAddr) |
| 851 | if err != nil { |
| 852 | return fmt.Errorf("failed to start SOCKS5 server: %w", err) |
| 853 | } |
| 854 | defer socksListener.Close() |
| 855 | |
| 856 | // Set up graceful shutdown |
| 857 | go func() { |
| 858 | <-ctx.Done() |
| 859 | shared.LogNetwork("Shutting down SOCKS5 proxy server") |
| 860 | socksListener.Close() |
| 861 | }() |
| 862 | |
| 863 | shared.LogSuccessf("SOCKS5 proxy server started on %s", socksAddr) |
| 864 | shared.LogInfof("Configure your browser to use SOCKS5 proxy: localhost%s", socksAddr) |
| 865 | |
| 866 | // Accept SOCKS5 connections |
| 867 | for { |
| 868 | conn, err := socksListener.Accept() |
| 869 | if err != nil { |
| 870 | // Check if this is due to context cancellation (expected) |
| 871 | if ctx.Err() != nil { |
| 872 | shared.LogNetwork("SOCKS5 proxy server shutdown completed") |
| 873 | return nil |
| 874 | } |
| 875 | // Check if listener was closed |
| 876 | if ne, ok := err.(net.Error); ok && !ne.Temporary() { |
| 877 | shared.LogNetwork("SOCKS5 listener closed") |
| 878 | break |
| 879 | } |
| 880 | shared.LogErrorf("Failed to accept connection: %v", err) |
| 881 | continue |
| 882 | } |
| 883 | |
| 884 | go p.handleSOCKS5ConnectionWithContext(ctx, conn, quicConn) |
| 885 | } |
| 886 | |
| 887 | return nil |
| 888 | } |
| 889 | |
| 890 | // StartWithConfigAndContext starts the SOCKS5 proxy server with configuration and context support |
| 891 | func (p *DefaultProxy) StartWithConfigAndContext(ctx context.Context, port int, quicConn quic.Connection, bufferSize int) error { |
no test coverage detected