-
Notifications
You must be signed in to change notification settings - Fork 10
/
teleport.go
73 lines (57 loc) · 1.66 KB
/
teleport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package http_api
import (
"net"
"net/http"
"github.com/gin-gonic/gin"
"github.com/PeerXu/meepo/pkg/meepo"
)
type TeleportRequest struct {
ID string `json:"id" binding:"required"`
Name string `json:"name,omitempty"`
RemoteNetwork string `json:"remoteNetwork"`
RemoteAddress string `json:"remoteAddress" binding:"required"`
LocalNetwork string `json:"localNetwork,omitempty"`
LocalAddress string `json:"localAddress,omitempty"`
Secret string `json:"secret,omitempty"`
}
type TeleportResponse struct {
LocalNetwork string `json:"localNetwork"`
LocalAddress string `json:"localAddress"`
}
func (s *HttpServer) Teleport(c *gin.Context) {
var err error
var req TeleportRequest
var local net.Addr
if err = c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, ParseError(err))
return
}
remote, err := net.ResolveTCPAddr(req.RemoteNetwork, req.RemoteAddress)
if err != nil {
c.JSON(http.StatusBadRequest, ParseError(err))
return
}
var opts []meepo.TeleportOption
if req.LocalAddress != "" {
if local, err = net.ResolveTCPAddr(req.LocalNetwork, req.LocalAddress); err != nil {
c.JSON(http.StatusBadRequest, ParseError(err))
return
}
opts = append(opts, meepo.WithLocalAddress(local))
}
if req.Name != "" {
opts = append(opts, meepo.WithName(req.Name))
}
if req.Secret != "" {
opts = append(opts, meepo.WithSecret(req.Secret))
}
if local, err = s.meepo.Teleport(req.ID, remote, opts...); err != nil {
c.JSON(http.StatusInternalServerError, ParseError(err))
return
}
res := &TeleportResponse{
LocalNetwork: local.Network(),
LocalAddress: local.String(),
}
c.JSON(http.StatusOK, res)
}