|
| 1 | +package routes |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/shellhub-io/shellhub/api/pkg/gateway" |
| 9 | + svc "github.com/shellhub-io/shellhub/api/services" |
| 10 | + "github.com/shellhub-io/shellhub/pkg/api/requests" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + CreateConnectionURL = "/connections" |
| 15 | + ListConnectionsURL = "/connections" |
| 16 | + GetConnectionURL = "/connections/:id" |
| 17 | + ConnectionStatusURL = "/connections/:id/status" |
| 18 | + UpdateConnectionURL = "/connections/:id" |
| 19 | + DeleteConnectionURL = "/connections/:id" |
| 20 | +) |
| 21 | + |
| 22 | +const ParamConnectionID = "id" |
| 23 | + |
| 24 | +func (h *Handler) CreateConnection(c gateway.Context) error { |
| 25 | + var req requests.ConnectionCreate |
| 26 | + if err := c.Bind(&req); err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + |
| 30 | + if err := c.Validate(&req); err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + // For external connections, probe at save time. A 422 lets the UI distinguish |
| 35 | + // a blocked target (not a permitted address) from an unreachable host (the |
| 36 | + // NAT/firewall hint + install-the-agent funnel). Force skips the probe. |
| 37 | + if req.Kind == "external" && !req.Force { |
| 38 | + reachable, err := h.service.ProbeReachability(c.Ctx(), &requests.ConnectionProbe{Host: req.Host, Port: req.Port}) |
| 39 | + switch { |
| 40 | + case errors.Is(err, svc.ErrEgressBlocked): |
| 41 | + return c.JSON(http.StatusUnprocessableEntity, map[string]string{"error": "blocked"}) |
| 42 | + case err != nil: |
| 43 | + return err |
| 44 | + case !reachable: |
| 45 | + return c.JSON(http.StatusUnprocessableEntity, map[string]string{"error": "unreachable"}) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + connection, err := h.service.CreateConnection(c.Ctx(), &req) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + return c.JSON(http.StatusCreated, connection) |
| 55 | +} |
| 56 | + |
| 57 | +func (h *Handler) ListConnections(c gateway.Context) error { |
| 58 | + req := new(requests.ConnectionList) |
| 59 | + if err := c.Bind(req); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + req.Paginator.Normalize() |
| 64 | + req.Sorter.Normalize() |
| 65 | + |
| 66 | + if err := c.Validate(req); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + connections, count, err := h.service.ListConnections(c.Ctx(), req) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + c.Response().Header().Set("X-Total-Count", strconv.Itoa(count)) |
| 76 | + |
| 77 | + return c.JSON(http.StatusOK, connections) |
| 78 | +} |
| 79 | + |
| 80 | +func (h *Handler) GetConnection(c gateway.Context) error { |
| 81 | + var req requests.ConnectionGet |
| 82 | + if err := c.Bind(&req); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + if err := c.Validate(&req); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + connection, err := h.service.GetConnection(c.Ctx(), &req) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + return c.JSON(http.StatusOK, connection) |
| 96 | +} |
| 97 | + |
| 98 | +func (h *Handler) ConnectionStatus(c gateway.Context) error { |
| 99 | + var req requests.ConnectionGet |
| 100 | + if err := c.Bind(&req); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + if err := c.Validate(&req); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + online, err := h.service.ConnectionStatus(c.Ctx(), &req) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + return c.JSON(http.StatusOK, map[string]bool{"online": online}) |
| 114 | +} |
| 115 | + |
| 116 | +func (h *Handler) UpdateConnection(c gateway.Context) error { |
| 117 | + var req requests.ConnectionUpdate |
| 118 | + if err := c.Bind(&req); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + if err := c.Validate(&req); err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + connection, err := h.service.UpdateConnection(c.Ctx(), &req) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + return c.JSON(http.StatusOK, connection) |
| 132 | +} |
| 133 | + |
| 134 | +func (h *Handler) DeleteConnection(c gateway.Context) error { |
| 135 | + var req requests.ConnectionDelete |
| 136 | + if err := c.Bind(&req); err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + if err := c.Validate(&req); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + if err := h.service.DeleteConnection(c.Ctx(), &req); err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + return c.NoContent(http.StatusOK) |
| 149 | +} |
0 commit comments