forked from fortuna/ss-example
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(caddy): add a WebSockets handler
- Loading branch information
Showing
5 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package caddy | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/Jigsaw-Code/outline-sdk/transport" | ||
"github.com/caddyserver/caddy/v2" | ||
"github.com/caddyserver/caddy/v2/modules/caddyhttp" | ||
"golang.org/x/net/websocket" | ||
) | ||
|
||
const wsModuleName = "http.handlers.websocket" | ||
|
||
func init() { | ||
caddy.RegisterModule(ModuleRegistration{ | ||
ID: wsModuleName, | ||
New: func() caddy.Module { return new(WebSocketHandler) }, | ||
}) | ||
} | ||
|
||
// WebSocketHandler implements a middleware Caddy handler that proxies | ||
// WebSockets connections. | ||
type WebSocketHandler struct { | ||
// The address of the backend to connect to. | ||
Backend string `json:"backend,omitempty"` | ||
|
||
logger *slog.Logger | ||
} | ||
|
||
var ( | ||
_ caddy.Provisioner = (*WebSocketHandler)(nil) | ||
_ caddy.Validator = (*WebSocketHandler)(nil) | ||
_ caddyhttp.MiddlewareHandler = (*WebSocketHandler)(nil) | ||
) | ||
|
||
func (*WebSocketHandler) CaddyModule() caddy.ModuleInfo { | ||
return caddy.ModuleInfo{ID: wsModuleName} | ||
} | ||
|
||
// Provision implements caddy.Provisioner. | ||
func (h *WebSocketHandler) Provision(ctx caddy.Context) error { | ||
h.logger = ctx.Slogger() | ||
return nil | ||
} | ||
|
||
// Validate implements caddy.Validator. | ||
func (h *WebSocketHandler) Validate() error { | ||
if h.Backend == "" { | ||
return errors.New("must specify `backend`") | ||
} | ||
return nil | ||
} | ||
|
||
// ServeHTTP implements caddyhttp.MiddlewareHandler. | ||
func (h WebSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { | ||
h.logger.Info("handling websocket connection", slog.String("path", r.URL.Path)) | ||
|
||
backendAddr, err := caddy.ParseNetworkAddress(h.Backend) | ||
if err != nil { | ||
return fmt.Errorf("unable to parse `backend` network address: %v", err) | ||
} | ||
|
||
var handler func(wsConn *websocket.Conn) | ||
switch backendAddr.Network { | ||
case "tcp": | ||
streamDialer := &transport.TCPDialer{} | ||
endpoint := transport.StreamDialerEndpoint{Dialer: streamDialer, Address: backendAddr.JoinHostPort(0)} | ||
handler = func(wsConn *websocket.Conn) { | ||
targetConn, err := endpoint.ConnectStream(r.Context()) | ||
if err != nil { | ||
h.logger.Error("failed to connect to backend", slog.Any("err", err)) | ||
w.WriteHeader(http.StatusBadGateway) | ||
return | ||
} | ||
defer targetConn.Close() | ||
|
||
go func() { | ||
io.Copy(targetConn, wsConn) | ||
targetConn.CloseWrite() | ||
}() | ||
|
||
io.Copy(wsConn, targetConn) | ||
wsConn.Close() | ||
} | ||
case "udp": | ||
packetDialer := &transport.UDPDialer{} | ||
endpoint := transport.PacketDialerEndpoint{Dialer: packetDialer, Address: backendAddr.JoinHostPort(0)} | ||
handler = func(wsConn *websocket.Conn) { | ||
targetConn, err := endpoint.ConnectPacket(r.Context()) | ||
if err != nil { | ||
h.logger.Error("failed to connect to backend", slog.Any("err", err)) | ||
w.WriteHeader(http.StatusBadGateway) | ||
return | ||
} | ||
// Expire connection after 5 minutes of idle time, as per | ||
// https://datatracker.ietf.org/doc/html/rfc4787#section-4.3 | ||
targetConn = &natConn{targetConn, 5 * time.Minute} | ||
|
||
go func() { | ||
io.Copy(targetConn, wsConn) | ||
targetConn.Close() | ||
}() | ||
|
||
io.Copy(wsConn, targetConn) | ||
wsConn.Close() | ||
} | ||
default: | ||
return fmt.Errorf("unsupported `backend` network: %v", backendAddr.Network) | ||
} | ||
|
||
websocket.Server{Handler: handler}.ServeHTTP(w, r) | ||
return nil | ||
} | ||
|
||
type natConn struct { | ||
net.Conn | ||
mappingTimeout time.Duration | ||
} | ||
|
||
// Consider ReadFrom/WriteTo | ||
func (c *natConn) Write(b []byte) (int, error) { | ||
c.Conn.SetDeadline(time.Now().Add(c.mappingTimeout)) | ||
return c.Conn.Write(b) | ||
} |