Skip to content

feat(blackhole): add configurable HTTP status code - #6711

Closed
raaad1on wants to merge 1 commit into
XTLS:mainfrom
raaad1on:feature/blackhole-custom-status-code
Closed

raaad1on wants to merge 1 commit into
XTLS:mainfrom
raaad1on:feature/blackhole-custom-status-code

Conversation

@raaad1on

@raaad1on raaad1on commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds support for configuring the HTTP status code returned by the blackhole outbound protocol when using HTTP response type.

Problem

Currently, the blackhole protocol with HTTP response always returns 403 Forbidden. This limitation makes it unsuitable for several use cases:

  • Captive portal/connectivity checks: Mobile devices and browsers use specific URLs to detect internet connectivity (e.g., connectivitycheck.gstatic.com, captive.apple.com). These checks expect 204 No Content responses to confirm connectivity.
  • Custom HTTP responses: Some scenarios require different status codes like 200 OK or 404 Not Found.

Solution

Add a statusCode field to the HTTP response configuration that allows specifying any valid HTTP status code. The default behavior (403 Forbidden) is preserved for backward compatibility when no status code is specified.

Configuration Example

{
  "outbounds": [{
    "tag": "block-204",
    "protocol": "blackhole",
    "settings": {
      "response": {
        "type": "http",
        "statusCode": 204
      }
    }
  }]
}

Use Case: Local Connectivity Check

{
  "routing": {
    "rules": [{
      "domain": [
        "domain:gstatic.com",
        "domain:msftconnecttest.com",
        "domain:apple.com"
      ],
      "outboundTag": "connectivity-check"
    }]
  },
  "outbounds": [{
    "tag": "connectivity-check",
    "protocol": "blackhole",
    "settings": {
      "response": {
        "type": "http",
        "statusCode": 204
      }
    }
  }]
}

Changes

  • proxy/blackhole/config.proto: Add status_code field to HTTPResponse message
  • proxy/blackhole/config.pb.go: Regenerated protobuf code
  • proxy/blackhole/config.go: Update WriteTo() method to use configured status code with proper HTTP status text lookup via http.StatusText()
  • infra/conf/blackhole.go: Add statusCode JSON field support for configuration parsing
  • Tests: Add comprehensive tests for various status codes (200, 204, 403, 404, 500)

Backward Compatibility

This change is fully backward compatible:

  • When statusCode is not specified (or set to 0), the default 403 Forbidden response is used
  • Existing configurations continue to work without modification
  • The response format remains the same, only the status code and status text are configurable

Testing

All existing tests pass, and new tests have been added to verify:

  • Default behavior (403) when no status code is specified
  • Correct responses for common status codes (200, 204, 404, 500)
  • Proper JSON configuration parsing

Add support for configuring the HTTP status code returned by the
blackhole outbound protocol when using HTTP response type.

This change allows users to specify a custom status code instead of
the hardcoded 403 Forbidden. Common use cases include:
- 204 No Content for captive portal/connectivity check responses
- 200 OK for custom HTTP responses
- 404 Not Found for stealth configurations

The default behavior (403 Forbidden) is preserved for backward
compatibility when no status code is specified.

Configuration example:
{
  "outbounds": [{
    "tag": "block-204",
    "protocol": "blackhole",
    "settings": {
      "response": {
        "type": "http",
        "statusCode": 204
      }
    }
  }]
}

Changes:
- Add status_code field to HTTPResponse message in config.proto
- Update WriteTo() method to use configured status code with
  proper HTTP status text lookup
- Add statusCode JSON field support in infra/conf/blackhole.go
- Add comprehensive tests for various status codes

Co-authored-by: Cursor <[email protected]>
@Fangliding

Copy link
Copy Markdown
Member

我可能需要稍微整理一下这个出站

@Fangliding Fangliding closed this Sep 2, 2026
@raaad1on

raaad1on commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for reviewing! If you need any clarification on the implementation details or any help with this feature, please let me know — happy to assist.

@Fangliding

Copy link
Copy Markdown
Member

我把它做成了这样 f49d8b8 不能配置code了但是可以自定义回复

@raaad1on

raaad1on commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for implementing this! The customResponseData approach is even more flexible — great solution. Looking forward to the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants