Skip to content

Commit

Permalink
forward: add client authentication
Browse files Browse the repository at this point in the history
Adds a '--connect.token' option to 'piko forward' to authenticate client
connections.
  • Loading branch information
andydunstall committed Jul 16, 2024
1 parent 71ff2b5 commit b592d63
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/forward/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func runForward(conf *config.Config, logger log.Logger) error {
}
dialer := &client.Dialer{
URL: connectURL,
Token: conf.Connect.Token,
TLSConfig: connectTLSConfig,
}

Expand Down
12 changes: 11 additions & 1 deletion client/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type Dialer struct {
// 'http://localhost:8000'.
URL *url.URL

// Token configures the API key token to authenticate the listener with the
// Piko server.
//
// Defaults to no authentication.
Token string

// TLSConfig specifies the TLS configuration to use with the Piko server.
//
// If nil, the default configuration is used.
Expand All @@ -33,7 +39,11 @@ type Dialer struct {
func (d *Dialer) Dial(ctx context.Context, endpointID string) (net.Conn, error) {
// Dialing is simply opening a WebSocket connection to the target endpoint,
// then wrapping the WebSocket in a net.Conn.
return websocket.Dial(ctx, d.dialURL(endpointID))
return websocket.Dial(
ctx,
d.dialURL(endpointID),
websocket.WithToken(d.Token),
)
}

func (d *Dialer) dialURL(endpointID string) string {
Expand Down
11 changes: 11 additions & 0 deletions forward/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ type ConnectConfig struct {
// URL is the Piko server URL to connect to.
URL string

// Token is a token to authenticate with the Piko server.
Token string

// Timeout is the timeout attempting to connect to the Piko server.
Timeout time.Duration `json:"timeout" yaml:"timeout"`

Expand Down Expand Up @@ -132,6 +135,14 @@ The Piko server URL to connect to. Note this must be configured to use the
Piko server 'proxy' port.`,
)

fs.StringVar(
&c.Token,
"connect.token",
c.Token,
`
Token is a token to authenticate with the Piko server.`,
)

fs.DurationVar(
&c.Timeout,
"connect.timeout",
Expand Down

0 comments on commit b592d63

Please sign in to comment.