|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/labstack/echo/v4" |
| 9 | + |
| 10 | + "github.com/anywherelan/awl/config" |
| 11 | + "github.com/anywherelan/awl/entity" |
| 12 | +) |
| 13 | + |
| 14 | +const defaultInviteMaxUses = 1 |
| 15 | + |
| 16 | +// @Tags Peers |
| 17 | +// @Summary Create an invite link |
| 18 | +// @Accept json |
| 19 | +// @Produce json |
| 20 | +// @Param body body entity.CreateInviteRequest true "Params" |
| 21 | +// @Success 200 {object} entity.InviteResponse |
| 22 | +// @Failure 400 {object} api.Error |
| 23 | +// @Failure 500 {object} api.Error |
| 24 | +// @Router /peers/invites/create [POST] |
| 25 | +func (h *Handler) CreateInvite(c echo.Context) (err error) { |
| 26 | + req := entity.CreateInviteRequest{} |
| 27 | + err = c.Bind(&req) |
| 28 | + if err != nil { |
| 29 | + return c.JSON(http.StatusBadRequest, ErrorMessage(err.Error())) |
| 30 | + } |
| 31 | + if err = c.Validate(req); err != nil { |
| 32 | + return c.JSON(http.StatusBadRequest, ErrorMessage(err.Error())) |
| 33 | + } |
| 34 | + |
| 35 | + if req.MaxUses == 0 { |
| 36 | + req.MaxUses = defaultInviteMaxUses |
| 37 | + } |
| 38 | + req.Alias = strings.TrimSpace(req.Alias) |
| 39 | + // One alias cannot name several peers, so it only makes sense for a |
| 40 | + // single-use link. Uniqueness itself is not checked here: the peer list will |
| 41 | + // have moved on by the time the link is redeemed, and a collision is |
| 42 | + // resolved then (GenUniqPeerAlias), so checking now would only produce |
| 43 | + // false rejections. |
| 44 | + if req.Alias != "" && req.MaxUses != 1 { |
| 45 | + return c.JSON(http.StatusBadRequest, |
| 46 | + ErrorMessage("alias can only be set for a single-use invite")) |
| 47 | + } |
| 48 | + |
| 49 | + var expiresAt time.Time |
| 50 | + if req.ExpiresInSeconds > 0 { |
| 51 | + expiresAt = time.Now().Add(time.Duration(req.ExpiresInSeconds) * time.Second) |
| 52 | + } |
| 53 | + |
| 54 | + invite, err := h.conf.CreateInvite(config.CreateInviteParams{ |
| 55 | + Label: req.Label, |
| 56 | + Alias: req.Alias, |
| 57 | + AllowUsingAsExitNode: req.AllowUsingAsExitNode, |
| 58 | + MaxUses: req.MaxUses, |
| 59 | + ExpiresAt: expiresAt, |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + return c.JSON(http.StatusInternalServerError, ErrorMessage(err.Error())) |
| 63 | + } |
| 64 | + |
| 65 | + return c.JSON(http.StatusOK, h.inviteResponse(invite)) |
| 66 | +} |
| 67 | + |
| 68 | +// @Tags Peers |
| 69 | +// @Summary Get invite links |
| 70 | +// @Accept json |
| 71 | +// @Produce json |
| 72 | +// @Success 200 {array} entity.InviteResponse |
| 73 | +// @Router /peers/invites/list [GET] |
| 74 | +func (h *Handler) GetInvites(c echo.Context) (err error) { |
| 75 | + invites := h.conf.ListInvites() |
| 76 | + |
| 77 | + result := make([]entity.InviteResponse, 0, len(invites)) |
| 78 | + for _, invite := range invites { |
| 79 | + result = append(result, h.inviteResponse(invite)) |
| 80 | + } |
| 81 | + |
| 82 | + return c.JSON(http.StatusOK, result) |
| 83 | +} |
| 84 | + |
| 85 | +// @Tags Peers |
| 86 | +// @Summary Revoke an invite link |
| 87 | +// @Description Stops new connections through the link; peers already added stay. |
| 88 | +// @Accept json |
| 89 | +// @Produce json |
| 90 | +// @Param body body entity.RevokeInviteRequest true "Params" |
| 91 | +// @Success 200 "OK" |
| 92 | +// @Failure 400 {object} api.Error |
| 93 | +// @Failure 404 {object} api.Error |
| 94 | +// @Router /peers/invites/revoke [POST] |
| 95 | +func (h *Handler) RevokeInvite(c echo.Context) (err error) { |
| 96 | + req := entity.RevokeInviteRequest{} |
| 97 | + err = c.Bind(&req) |
| 98 | + if err != nil { |
| 99 | + return c.JSON(http.StatusBadRequest, ErrorMessage(err.Error())) |
| 100 | + } |
| 101 | + if err = c.Validate(req); err != nil { |
| 102 | + return c.JSON(http.StatusBadRequest, ErrorMessage(err.Error())) |
| 103 | + } |
| 104 | + |
| 105 | + if !h.conf.RevokeInvite(req.ID) { |
| 106 | + return c.JSON(http.StatusNotFound, ErrorMessage("invite not found")) |
| 107 | + } |
| 108 | + |
| 109 | + return c.NoContent(http.StatusOK) |
| 110 | +} |
| 111 | + |
| 112 | +// inviteResponse renders an invite for the API, building the link from our |
| 113 | +// current peer ID and node name. |
| 114 | +func (h *Handler) inviteResponse(invite config.Invite) entity.InviteResponse { |
| 115 | + h.conf.RLock() |
| 116 | + peerID := h.conf.P2pNode.PeerID |
| 117 | + nodeName := h.conf.P2pNode.Name |
| 118 | + h.conf.RUnlock() |
| 119 | + |
| 120 | + return entity.InviteResponse{ |
| 121 | + ID: invite.ID, |
| 122 | + Label: invite.Label, |
| 123 | + Link: entity.BuildInviteLink(peerID, invite.Token, nodeName), |
| 124 | + Alias: invite.Alias, |
| 125 | + AllowUsingAsExitNode: invite.WeAllowUsingAsExitNode, |
| 126 | + MaxUses: invite.MaxUses, |
| 127 | + UsedCount: invite.UsedCount, |
| 128 | + ExpiresAt: invite.ExpiresAt, |
| 129 | + CreatedAt: invite.CreatedAt, |
| 130 | + Revoked: invite.Revoked, |
| 131 | + Status: inviteStatus(invite), |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func inviteStatus(invite config.Invite) string { |
| 136 | + switch { |
| 137 | + case invite.Revoked: |
| 138 | + return entity.InviteStatusRevoked |
| 139 | + case invite.IsExpired(time.Now()): |
| 140 | + return entity.InviteStatusExpired |
| 141 | + case invite.IsUsedUp(): |
| 142 | + return entity.InviteStatusUsedUp |
| 143 | + } |
| 144 | + return entity.InviteStatusActive |
| 145 | +} |
0 commit comments