Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Jan 5, 2025
1 parent 4c5943e commit a5d6cdd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 106 deletions.
55 changes: 12 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
# HTTPS Listener For HTTP Redirect

If client sent an HTTP request to an HTTPS server `port`, returns [302 redirection](https://developer.mozilla.org/docs/Web/HTTP/Status/302), like [nginx](https://nginx.org)'s ["error_page 497"](https://nginx.org/en/docs/http/ngx_http_ssl_module.html#errors).

Related issue: https://github.com/golang/go/issues/49310
If client sent an HTTP request to an HTTPS server **port**, returns [302 redirection](https://developer.mozilla.org/docs/Web/HTTP/Status/302), like [nginx](https://nginx.org)'s ["error_page 497"](https://nginx.org/en/docs/http/ngx_http_ssl_module.html#errors).

> [!IMPORTANT]
> If you need `http.Hijacker` or `http.ResponseController.EnableFullDuplex`, please use https://github.com/bddjr/hahosp
> If you need `http.Hijacker` or `http.ResponseController.EnableFullDuplex`, please use [hahosp](https://github.com/bddjr/hahosp)
---

## Get
## Use

```
go get github.com/bddjr/hlfhr
```

---

## Example

```go
// Use hlfhr.New
srv := hlfhr.New(&http.Server{
Expand All @@ -38,18 +32,14 @@ flowchart TD
IsLooksLikeHTTP("First byte looks like HTTP ?")
Continue(["✅ Continue..."])
CancelHijacking(["✅ Cancel hijacking..."])
ReadRequest("🔍 Read request")
IsFindHostHeader("Find Host header ?")
IsHandlerExist("`
HttpOnHttpsPort
ErrorHandler
exist?`")
400BadRequest{{"❌ 400 Bad Request"}}
exist ?`")
302Redirect{{"🟡 302 Redirect"}}
Expand All @@ -58,22 +48,12 @@ flowchart TD
Close(["❌ Close."])
Read --> IsLooksLikeHTTP
IsLooksLikeHTTP -- "🔐false" --> Continue
IsLooksLikeHTTP -- "📄true" --> ReadRequest --> IsFindHostHeader
IsFindHostHeader -- "⛔false" --> 400BadRequest --> Close
IsFindHostHeader -- "✅true" --> IsHandlerExist
IsLooksLikeHTTP -- "🔐false" --> CancelHijacking
IsLooksLikeHTTP -- "📄true" --> ReadRequest --> IsHandlerExist
IsHandlerExist -- "✖false" --> 302Redirect --> Close
IsHandlerExist -- "✅true" --> Handler --> Close
```

### See

- [curl](curl.md)
- [src_server.go](src_server.go)
- [src_tlslistener.go](src_tlslistener.go)
- [src_conn.go](src_conn.go)
- [src_conn-looks-like-http.go](src_conn-looks-like-http.go)

---

## Option Example
Expand Down Expand Up @@ -277,34 +257,23 @@ hlfhr.BufioSetReader(br, r)
```
git clone https://github.com/bddjr/hlfhr
cd hlfhr
chmod +x run.sh
./run.sh
```

---

## Reference

https://developer.mozilla.org/docs/Web/HTTP/Session
https://developer.mozilla.org/docs/Web/HTTP/Methods
https://developer.mozilla.org/docs/Web/HTTP/Redirections
https://developer.mozilla.org/docs/Web/HTTP/Status/302
https://developer.mozilla.org/docs/Web/HTTP/Status/307
https://developer.mozilla.org/docs/Web/HTTP/Headers/Connection
https://github.com/golang/go/issues/49310
https://github.com/golang/go

https://tls12.xargs.org/#client-hello
https://tls13.xargs.org/#client-hello

https://nginx.org/en/docs/http/ngx_http_ssl_module.html#errors
https://developer.mozilla.org/docs/Web/HTTP

https://github.com/golang/go/issues/49310
https://github.com/golang/go/issues/66501

"net/http"
"net"
"crypto/tls"
"reflect"
"bufio"
"bytes"
https://nginx.org/en/docs/http/ngx_http_ssl_module.html#errors

---

Expand Down
48 changes: 0 additions & 48 deletions curl.md

This file was deleted.

6 changes: 4 additions & 2 deletions src_bufio.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ func NewBufioReaderWithBytes(buf []byte, contentLength int, rd io.Reader) *bufio
buf = nb
}

return (*bufio.Reader)(unsafe.Pointer(&bufioreader{
br := new(bufio.Reader)
*(*bufioreader)(unsafe.Pointer(br)) = bufioreader{
buf: buf,
rd: rd,
w: contentLength,
lastByte: -1,
lastRuneSize: -1,
}))
}
return br
}

func BufioSetReader(br *bufio.Reader, rd io.Reader) {
Expand Down
10 changes: 2 additions & 8 deletions src_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ type conn struct {
srv *Server
}

func (c *conn) setRawConn() {
(*struct {
conn net.Conn
})(unsafe.Pointer(c.tc)).conn = c.Conn
}

func (c *conn) log(v ...any) {
if c.srv.ErrorLog != nil {
c.srv.ErrorLog.Print(v...)
Expand Down Expand Up @@ -60,7 +54,7 @@ func (c *conn) Read(b []byte) (int, error) {
if !ConnFirstByteLooksLikeHttp(b[0]) {
// Not looks like HTTP.
// TLS handshake: 0x16
c.setRawConn()
(*struct{ conn net.Conn })(unsafe.Pointer(c.tc)).conn = c.Conn
c.tc = nil
return n, nil
}
Expand All @@ -78,7 +72,7 @@ func (c *conn) Read(b []byte) (int, error) {
// Response
w := NewResponse(c.Conn, true)
if r.Host == "" {
// missing "Host" header
// Error: missing HTTP/1.1 required "Host" header
w.WriteHeader(400)
w.WriteString("missing required Host header")
} else if c.srv.HttpOnHttpsPortErrorHandler != nil {
Expand Down
2 changes: 0 additions & 2 deletions src_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
)

// Using for interface [http.ResponseWriter], [io.StringWriter], [io.ByteWriter].
//
// "Connection" header always set "close".
type Response struct {
conn net.Conn
status int // Default: 400
Expand Down
6 changes: 3 additions & 3 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func httpResponseHandle(w http.ResponseWriter, r *http.Request) {
}

func testPrint(srv *hlfhr.Server) {
fmt.Print("\n test:\n ")
fmt.Print("\n test:\n curl")
if runtime.GOOS == "windows" {
fmt.Print("cmd /C ")
fmt.Print(".exe")
}
fmt.Print("curl -v -k -L http://localhost", srv.Addr, "/\n\n")
fmt.Print(" -v -k -L http://localhost", srv.Addr, "/\n\n")
}

0 comments on commit a5d6cdd

Please sign in to comment.