-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
173 lines (157 loc) · 3.38 KB
/
api.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package server
import (
"context"
"fmt"
"net/http"
"time"
"github.com/gabstv/freeport"
"github.com/gabstv/sandpiper/api"
"github.com/gabstv/sandpiper/internal/pkg/route"
"github.com/gin-gonic/gin"
)
func runAPIV1(ctx context.Context, sv Server, listen, key string, debug bool) {
if !debug {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
r.GET("/health-check", func(c *gin.Context) {
c.String(http.StatusOK, "OK")
})
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "sandpiper api")
})
g := r.Group("/v1")
// authorization middleware
g.Use(func(c *gin.Context) {
if c.GetHeader("X-API-KEY") != key {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"success": false,
"error": "invalid X-API-KEY",
})
return
}
c.Next()
})
//PING
g.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"pong": time.Now(),
})
})
g.GET("/routes", func(c *gin.Context) {
vv := sv.Routes()
c.JSON(http.StatusOK, vv)
})
g.GET("/fallback-domain", func(c *gin.Context) {
cfg := sv.GetConfig()
c.JSON(http.StatusOK, gin.H{
"value": cfg.FallbackDomain,
})
})
g.POST("/fallback-domain", func(c *gin.Context) {
jd := struct {
Value string `json:"value"`
}{}
if err := c.BindJSON(&jd); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": "json parse error: " + err.Error(),
})
return
}
routes := sv.Routes()
if _, ok := routes[jd.Value]; !ok {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": "domain does not exist",
})
return
}
cfg := sv.GetConfig()
cfg.FallbackDomain = jd.Value
sv.SetConfig(cfg)
c.JSON(http.StatusOK, gin.H{
"success": true,
})
})
// UPSERT a new route!
g.PUT("/route", func(c *gin.Context) {
jd := &api.NewRoute{}
if err := c.BindJSON(jd); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": err.Error(),
})
return
}
rrr := route.Route{
Domain: jd.Domain,
Autocert: jd.Autocert,
Server: route.RouteServer{
OutAddress: jd.OutPath,
},
}
newport := 0
if jd.OutType == "http" {
sv := rrr.Server
sv.OutConnType = route.HTTP
rrr.Server = sv
} else if jd.OutType == "https_skip_verify" {
sv := rrr.Server
sv.OutConnType = route.HTTPS_SKIP_VERIFY
rrr.Server = sv
} else if jd.OutType == "https" {
sv := rrr.Server
sv.OutConnType = route.HTTPS_VERIFY
rrr.Server = sv
} else if jd.OutType == "auto" {
ntcp, err := freeport.TCP()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": "tcp port err: " + err.Error(),
})
return
}
sv2 := rrr.Server
if sv2.OutAddress == "" {
sv2.OutAddress = fmt.Sprintf("localhost:%d", ntcp)
} else {
sv2.OutAddress = fmt.Sprintf("%v:%d", sv2.OutAddress, ntcp)
}
rrr.Server = sv2
newport = ntcp
}
if err := sv.Add(rrr); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": err.Error(),
})
return
}
if newport > 0 {
c.JSON(http.StatusOK, gin.H{
"success": true,
"port": newport,
})
} else {
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}
})
srv := &http.Server{
Addr: listen,
Handler: r,
}
go srv.ListenAndServe()
for ctx.Err() == nil {
// do nothing
select {
case <-ctx.Done():
break
case <-time.After(time.Second * 5):
}
}
srv.Shutdown(ctx)
}