Initial commit
This commit is contained in:
33
internal/domains/deconnector/connect.go
Normal file
33
internal/domains/deconnector/connect.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package deconnector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (d *Deconnector) handleDeconnect(clientConn net.Conn, connectReq *http.Request, upstreamURL *url.URL) {
|
||||
// Tell client the tunnel is open
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 200 Connection established\r\n\r\n")
|
||||
|
||||
// Read the real HTTP request the client sends through the tunnel
|
||||
innerReq, err := http.ReadRequest(bufio.NewReader(clientConn))
|
||||
if err != nil {
|
||||
log.Printf("failed to read inner request after CONNECT:80: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
innerReq.URL.Scheme = "http"
|
||||
innerReq.URL.Host = connectReq.Host
|
||||
innerReq.RequestURI = ""
|
||||
|
||||
d.app.Logger().
|
||||
WithField("method", innerReq.Method).
|
||||
WithField("url", innerReq.URL).
|
||||
Info("Handling de-CONNECT request")
|
||||
|
||||
d.forwardHTTP(clientConn, innerReq, upstreamURL)
|
||||
}
|
||||
43
internal/domains/deconnector/deconnector.go
Normal file
43
internal/domains/deconnector/deconnector.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package deconnector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"source.hodakov.me/hdkv/deconnect/internal/application"
|
||||
"source.hodakov.me/hdkv/deconnect/internal/domains"
|
||||
)
|
||||
|
||||
var (
|
||||
_ domains.Deconnector = new(Deconnector)
|
||||
_ domains.Domain = new(Deconnector)
|
||||
)
|
||||
|
||||
type Deconnector struct {
|
||||
app *application.App
|
||||
|
||||
dialer domains.Dialer
|
||||
}
|
||||
|
||||
func New(app *application.App) *Deconnector {
|
||||
return &Deconnector{
|
||||
app: app,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Deconnector) ConnectDependencies() error {
|
||||
dialer, ok := d.app.RetrieveDomain(domains.DomainNameDialer).(domains.Dialer)
|
||||
if !ok {
|
||||
return fmt.Errorf(
|
||||
"%w: %w (%s)", ErrDeconnector, ErrConnectDependencies,
|
||||
"dialer domain interface conversion failed",
|
||||
)
|
||||
}
|
||||
|
||||
d.dialer = dialer
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Deconnector) Start() error {
|
||||
return nil
|
||||
}
|
||||
8
internal/domains/deconnector/errors.go
Normal file
8
internal/domains/deconnector/errors.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package deconnector
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrDeconnector = errors.New("deconnector")
|
||||
ErrConnectDependencies = errors.New("failed to connect dependencies")
|
||||
)
|
||||
36
internal/domains/deconnector/forward.go
Normal file
36
internal/domains/deconnector/forward.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package deconnector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (d *Deconnector) forwardHTTP(clientConn net.Conn, req *http.Request, upstreamURL *url.URL) {
|
||||
upstreamConn, err := d.dialer.Dial()
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("upstream dial failed")
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 502 Bad Gateway\r\n\r\n")
|
||||
|
||||
return
|
||||
}
|
||||
defer upstreamConn.Close()
|
||||
|
||||
if err := req.WriteProxy(upstreamConn); err != nil {
|
||||
d.app.Logger().WithError(err).Error("failed to write request")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := http.ReadResponse(bufio.NewReader(upstreamConn), req)
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("failed to read response")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
resp.Write(clientConn)
|
||||
}
|
||||
46
internal/domains/deconnector/handle.go
Normal file
46
internal/domains/deconnector/handle.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package deconnector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (d *Deconnector) Handle(clientConn net.Conn) {
|
||||
upstreamURL, err := d.dialer.UpstreamURL()
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("failed to get upstream URL")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defer clientConn.Close()
|
||||
|
||||
req, err := http.ReadRequest(bufio.NewReader(clientConn))
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("failed to read request")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if req.Method == http.MethodConnect {
|
||||
_, port, _ := net.SplitHostPort(req.Host)
|
||||
if port == "443" {
|
||||
d.handleTunnel(clientConn, req.Host, upstreamURL)
|
||||
} else {
|
||||
d.handleDeconnect(clientConn, req, upstreamURL)
|
||||
}
|
||||
} else {
|
||||
req.RequestURI = ""
|
||||
if req.URL.Host == "" {
|
||||
req.URL.Host = req.Host
|
||||
}
|
||||
req.URL.Scheme = "http"
|
||||
d.app.Logger().
|
||||
WithField("method", req.Method).
|
||||
WithField("url", req.URL).
|
||||
Info("Forwarding HTTP request")
|
||||
|
||||
d.forwardHTTP(clientConn, req, upstreamURL)
|
||||
}
|
||||
}
|
||||
43
internal/domains/deconnector/tunnel.go
Normal file
43
internal/domains/deconnector/tunnel.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package deconnector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (d *Deconnector) handleTunnel(clientConn net.Conn, host string, upstreamURL *url.URL) {
|
||||
d.app.Logger().WithField("host", host).Info("Handling CONNECT tunnel")
|
||||
|
||||
upstreamConn, err := d.dialer.Dial()
|
||||
if err != nil {
|
||||
d.app.Logger().WithError(err).Error("upstream dial failed")
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 502 Bad Gateway\r\n\r\n")
|
||||
|
||||
return
|
||||
}
|
||||
defer upstreamConn.Close()
|
||||
|
||||
connectLine := fmt.Sprintf(
|
||||
"CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", host, host,
|
||||
)
|
||||
fmt.Fprint(upstreamConn, connectLine)
|
||||
|
||||
resp, err := http.ReadResponse(bufio.NewReader(upstreamConn), nil)
|
||||
if err != nil || resp.StatusCode > 499 {
|
||||
d.app.Logger().WithError(err).Error("upstream CONNECT failed")
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 502 Bad Gateway\r\n\r\n")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(clientConn, "HTTP/1.1 200 Connection established\r\n\r\n")
|
||||
|
||||
done := make(chan struct{}, 2)
|
||||
go func() { io.Copy(upstreamConn, clientConn); done <- struct{}{} }()
|
||||
go func() { io.Copy(clientConn, upstreamConn); done <- struct{}{} }()
|
||||
<-done
|
||||
}
|
||||
Reference in New Issue
Block a user