Files
conduit/tunnel/forwarder.go
Evan Reichard 8dfb14f1e7
All checks were successful
continuous-integration/drone/push Build is passing
feat(tunnel): require explicit target schemes
2026-05-15 13:16:49 -04:00

47 lines
1.0 KiB
Go

package tunnel
import (
"context"
"fmt"
"net/url"
"strings"
"reichard.io/conduit/store"
)
type ForwarderType int
const (
ForwarderTCP ForwarderType = iota
ForwarderHTTP
)
type Forwarder interface {
Type() ForwarderType
Initialize(sourceAddress string) (Stream, error)
Start(context.Context) error
}
func NewForwarder(target string, tunnelStore store.TunnelStore) (Forwarder, error) {
if !strings.Contains(target, "://") {
return nil, fmt.Errorf("target must include a scheme: tcp://, http://, or https://")
}
targetURL, err := url.Parse(target)
if err != nil {
return nil, fmt.Errorf("target is invalid: %w", err)
}
if targetURL.Host == "" {
return nil, fmt.Errorf("target must include a host")
}
switch targetURL.Scheme {
case "http", "https":
return newHTTPForwarder(targetURL, tunnelStore)
case "tcp":
return newTCPForwarder(targetURL.Host, tunnelStore), nil
default:
return nil, fmt.Errorf("unsupported target scheme %q: use tcp://, http://, or https://", targetURL.Scheme)
}
}