2023-02-09 19:18:06 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AutoHttpsConn struct {
|
|
|
|
net.Conn
|
|
|
|
|
|
|
|
firstBuf []byte
|
|
|
|
bufStart int
|
|
|
|
|
|
|
|
readRequestOnce sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAutoHttpsConn(conn net.Conn) net.Conn {
|
|
|
|
return &AutoHttpsConn{
|
|
|
|
Conn: conn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AutoHttpsConn) readRequest() bool {
|
|
|
|
c.firstBuf = make([]byte, 2048)
|
|
|
|
n, err := c.Conn.Read(c.firstBuf)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-11-20 01:39:00 +00:00
|
|
|
c.firstBuf = c.firstBuf[:n]
|
2023-02-09 19:18:06 +00:00
|
|
|
reader := bytes.NewReader(c.firstBuf)
|
|
|
|
bufReader := bufio.NewReader(reader)
|
|
|
|
request, err := http.ReadRequest(bufReader)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-11-20 01:39:00 +00:00
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: http.StatusTemporaryRedirect,
|
|
|
|
Header: make(http.Header),
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
2023-11-20 01:39:00 +00:00
|
|
|
resp.Header.Set("Location", fmt.Sprintf("https://%v%v", request.Host, request.RequestURI))
|
2023-02-09 19:18:06 +00:00
|
|
|
resp.Write(c.Conn)
|
|
|
|
c.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AutoHttpsConn) Read(buf []byte) (int, error) {
|
2023-11-20 01:39:00 +00:00
|
|
|
var err error
|
2023-02-09 19:18:06 +00:00
|
|
|
c.readRequestOnce.Do(func() {
|
2023-11-20 01:39:00 +00:00
|
|
|
if !c.readRequest() {
|
|
|
|
err = fmt.Errorf("failed to read HTTP request")
|
|
|
|
}
|
2023-02-09 19:18:06 +00:00
|
|
|
})
|
2023-11-20 01:39:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
|
|
if c.firstBuf != nil {
|
|
|
|
n := copy(buf, c.firstBuf[c.bufStart:])
|
|
|
|
c.bufStart += n
|
2023-11-20 01:39:00 +00:00
|
|
|
if c.bufStart == len(c.firstBuf) {
|
2023-02-09 19:18:06 +00:00
|
|
|
c.firstBuf = nil
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Conn.Read(buf)
|
|
|
|
}
|