Skip to content

Commit

Permalink
feat: add query feature endpoint for 'serve' support
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiebens committed Dec 29, 2023
1 parent 038c0af commit 75b58d0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions internal/handlers/query_feature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package handlers

import (
"fmt"
"github.com/jsiebens/ionscale/internal/bind"
"github.com/jsiebens/ionscale/internal/dns"
"github.com/jsiebens/ionscale/internal/domain"
"github.com/labstack/echo/v4"
"net/http"
"tailscale.com/tailcfg"
)

func NewQueryFeatureHandlers(createBinder bind.Factory, dnsProvider dns.Provider, repository domain.Repository) *QueryFeatureHandlers {
return &QueryFeatureHandlers{
createBinder: createBinder,
repository: repository,
}
}

type QueryFeatureHandlers struct {
createBinder bind.Factory
dnsProvider dns.Provider
repository domain.Repository
}

func (h *QueryFeatureHandlers) QueryFeature(c echo.Context) error {
ctx := c.Request().Context()

binder, err := h.createBinder(c)
if err != nil {
return logError(err)
}

req := new(tailcfg.QueryFeatureRequest)
if err := binder.BindRequest(c, req); err != nil {
return logError(err)
}

machineKey := binder.Peer().String()
nodeKey := req.NodeKey.String()

resp := tailcfg.QueryFeatureResponse{}

switch req.Feature {
case "serve":
machine, err := h.repository.GetMachineByKeys(ctx, machineKey, nodeKey)
if err != nil {
return err
}

if machine == nil {
return echo.NewHTTPError(http.StatusBadRequest)
}

if h.dnsProvider == nil || machine.Tailnet.DNSConfig.HttpsCertsEnabled {
resp.Text = fmt.Sprintf(serverMessage, machine.Tailnet.Name)
}
case "funnel":
resp.Text = fmt.Sprintf("Sorry, ionscale has no support for feature '%s'\n", req.Feature)
default:
resp.Text = fmt.Sprintf("Unknown feature request '%s'\n", req.Feature)
}

return binder.WriteResponse(c, http.StatusOK, resp)
}

const serverMessage = `Enabling HTTPS is required to use Serve:
ionscale tailnets set-dns --tailnet %s --https-certs=true --magic-dns
`
2 changes: 2 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func Start(c *config.Config) error {
dnsHandlers := handlers.NewDNSHandlers(binder, dnsProvider)
idTokenHandlers := handlers.NewIDTokenHandlers(binder, c, repository)
sshActionHandlers := handlers.NewSSHActionHandlers(binder, c, repository)
queryFeatureHandlers := handlers.NewQueryFeatureHandlers(binder, dnsProvider, repository)

e := echo.New()
e.Use(EchoMetrics(p), EchoLogger(httpLogger), EchoErrorHandler(), EchoRecover())
Expand All @@ -124,6 +125,7 @@ func Start(c *config.Config) error {
e.GET("/machine/ssh/action/:src_machine_id/to/:dst_machine_id", sshActionHandlers.StartAuth)
e.GET("/machine/ssh/action/:src_machine_id/to/:dst_machine_id/:check_period", sshActionHandlers.StartAuth)
e.GET("/machine/ssh/action/check/:key", sshActionHandlers.CheckAuth)
e.POST("/machine/feature/query", queryFeatureHandlers.QueryFeature)

return e
}
Expand Down

0 comments on commit 75b58d0

Please sign in to comment.