mirror of
https://github.com/Gouryella/drip.git
synced 2026-02-24 05:10:43 +00:00
- Add Bearer Token authentication, supporting tunnel access control via the --auth-bearer parameter - Refactor large modules into smaller, more focused components to improve code maintainability - Update dependency versions, including golang.org/x/crypto, golang.org/x/net, etc. - Add SilenceUsage and SilenceErrors configuration for all CLI commands - Modify connector configuration structure to support the new authentication method - Update recent change log in README with new feature descriptions BREAKING CHANGE: Authentication via Bearer Token is now supported, requiring the new --auth-bearer parameter
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package httputil
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
// HTTPErrorResponse represents a standard HTTP error response.
|
|
type HTTPErrorResponse struct {
|
|
StatusCode int
|
|
StatusText string
|
|
Message string
|
|
}
|
|
|
|
// Common HTTP error responses
|
|
var (
|
|
ServiceUnavailable = &HTTPErrorResponse{
|
|
StatusCode: 503,
|
|
StatusText: "Service Unavailable",
|
|
Message: "Server busy, please retry later",
|
|
}
|
|
|
|
HandlerNotConfigured = &HTTPErrorResponse{
|
|
StatusCode: 503,
|
|
StatusText: "Service Unavailable",
|
|
Message: "HTTP handler not configured for this TCP port",
|
|
}
|
|
)
|
|
|
|
// WriteErrorResponse writes an HTTP error response to the connection.
|
|
func WriteErrorResponse(conn net.Conn, resp *HTTPErrorResponse) error {
|
|
response := fmt.Sprintf(
|
|
"HTTP/1.1 %d %s\r\n"+
|
|
"Content-Type: text/plain\r\n"+
|
|
"Content-Length: %d\r\n"+
|
|
"Connection: close\r\n"+
|
|
"\r\n"+
|
|
"%s\r\n",
|
|
resp.StatusCode,
|
|
resp.StatusText,
|
|
len(resp.Message)+2,
|
|
resp.Message,
|
|
)
|
|
_, err := conn.Write([]byte(response))
|
|
return err
|
|
}
|
|
|
|
// WriteServiceUnavailable writes a 503 Service Unavailable response.
|
|
func WriteServiceUnavailable(conn net.Conn, message string) error {
|
|
if message == "" {
|
|
message = ServiceUnavailable.Message
|
|
}
|
|
return WriteErrorResponse(conn, &HTTPErrorResponse{
|
|
StatusCode: 503,
|
|
StatusText: "Service Unavailable",
|
|
Message: message,
|
|
})
|
|
}
|