-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
262 lines (221 loc) · 6.43 KB
/
Copy pathmain.go
File metadata and controls
262 lines (221 loc) · 6.43 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"encoding/base64"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
// return only IP, for no-ip, ddns services alike
r.GET("/ip", func(c *gin.Context) {
c.String(http.StatusOK, "%s", c.ClientIP())
})
// return headers
r.GET("/headers", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, c.Request.Header)
})
// return cookies
r.GET("/cookies", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, c.Request.Cookies())
})
// return User-Agent (register both /ua and /user-agent)
uaHandler := func(c *gin.Context) {
userAgent := c.GetHeader("X-Real-User-Agent")
if userAgent == "" {
userAgent = c.Request.UserAgent()
}
c.String(http.StatusOK, "%s", userAgent)
}
r.GET("/ua", uaHandler)
r.GET("/user-agent", uaHandler)
// return status code
r.GET("/status/:code", func(c *gin.Context) {
code := c.Param("code")
// Convert string to integer
var statusCode int
_, err := fmt.Sscanf(code, "%d", &statusCode)
if err != nil {
c.String(http.StatusBadRequest, "Invalid status code")
return
}
c.Status(statusCode)
})
// redirect n times
r.GET("/redirect/:n", func(c *gin.Context) {
n := c.Param("n")
// Convert string to integer
var redirectCount int
_, err := fmt.Sscanf(n, "%d", &redirectCount)
if err != nil {
c.String(http.StatusBadRequest, "Invalid redirect count")
return
}
// Limit the number of redirects to prevent infinite loops
if redirectCount < 0 || redirectCount > 10 {
c.String(http.StatusBadRequest, "Redirect count must be between 0 and 10")
return
}
// If redirect count is 0, return 200 OK
if redirectCount == 0 {
c.String(http.StatusOK, "OK")
return
}
// Perform the redirect
// Redirect to the same endpoint with decremented count
redirectURL := fmt.Sprintf("/redirect/%d", redirectCount-1)
c.Redirect(http.StatusFound, redirectURL)
})
// basic authentication
r.GET("/auth/basic/:username/:password", func(c *gin.Context) {
username := c.Param("username")
password := c.Param("password")
auth := c.GetHeader("Authorization")
if auth == "" {
c.Header("WWW-Authenticate", "Basic realm=\"Restricted Area\"")
c.Status(http.StatusUnauthorized)
return
}
// Check if the Authorization header starts with "Basic "
if !strings.HasPrefix(auth, "Basic ") {
c.Header("WWW-Authenticate", "Basic realm=\"Restricted Area\"")
c.Status(http.StatusUnauthorized)
return
}
// Extract the base64 encoded part
encoded := strings.TrimPrefix(auth, "Basic ")
if encoded == "" {
c.Header("WWW-Authenticate", "Basic realm=\"Restricted Area\"")
c.Status(http.StatusUnauthorized)
return
}
// Decode the base64 encoded credentials
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
c.Header("WWW-Authenticate", "Basic realm=\"Restricted Area\"")
c.Status(http.StatusUnauthorized)
return
}
// Split the decoded string into username and password
creds := strings.Split(string(decoded), ":")
if len(creds) != 2 {
c.Header("WWW-Authenticate", "Basic realm=\"Restricted Area\"")
c.Status(http.StatusUnauthorized)
return
}
// Check if the credentials match
if creds[0] == username && creds[1] == password {
c.String(http.StatusOK, "Access granted")
} else {
c.Header("WWW-Authenticate", "Basic realm=\"Restricted Area\"")
c.Status(http.StatusUnauthorized)
}
})
// Delay endpoint
r.GET("/delay/:n", func(c *gin.Context) {
delayStr := c.Param("n")
delay, err := strconv.Atoi(delayStr)
if err != nil || delay < 0 || delay > 10 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid or out of range delay value. Must be between 0 and 10 seconds."})
return
}
time.Sleep(time.Duration(delay) * time.Second)
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Response delayed by %d seconds", delay),
})
})
// return all request details for all methods (match both /request and /request/...)
r.Any("/request", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, getAllRequestInfo(c))
})
r.Any("/request/*any", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, getAllRequestInfo(c))
})
r.NoRoute(func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, getClientInfo(c))
})
r.Run()
}
func getClientInfo(c *gin.Context) map[string]string {
client := make(map[string]string)
// Client IP
client["ip"] = c.ClientIP()
// CloudFlare
if c.GetHeader("cf-ipcountry") != "" {
client["country"] = countrycode[c.GetHeader("cf-ipcountry")]
}
// CloudFront
if c.GetHeader("Cloudfront-Viewer-Country-Name") != "" {
client["country"] = c.GetHeader("Cloudfront-Viewer-Country-Name")
}
if c.GetHeader("Cloudfront-Viewer-City") != "" {
client["city"] = c.GetHeader("Cloudfront-Viewer-City")
}
return client
}
func getAllRequestInfo(c *gin.Context) map[string]interface{} {
result := make(map[string]interface{})
// Client IP
result["origin_ip"] = c.ClientIP()
// Query strings
query := make(map[string]string)
for key, values := range c.Request.URL.Query() {
if len(values) > 0 {
query[key] = strings.Join(values, ", ")
}
}
result["query"] = query
// URL parameters
params := make(map[string]string)
for _, param := range c.Params {
// skip the wildcard "any" param — we'll expose it as "path" only
if param.Key == "any" {
continue
}
params[param.Key] = param.Value
}
// include wildcard path (e.g. "/abc/def" -> "abc/def")
if p := c.Param("any"); p != "" {
params["path"] = strings.TrimPrefix(p, "/")
}
result["params"] = params
// Headers
headers := make(map[string]string)
for key, values := range c.Request.Header {
if len(values) > 0 {
headers[key] = strings.Join(values, ", ")
}
}
result["headers"] = headers
// Method
result["method"] = c.Request.Method
// Request URI
result["uri"] = c.Request.RequestURI
// Host
result["host"] = c.Request.Host
// User-Agent
userAgent := c.GetHeader("X-Real-User-Agent")
if userAgent == "" {
userAgent = c.Request.UserAgent()
}
result["user_agent"] = userAgent
// Payload (body)
body := ""
if c.Request.Body != nil {
// For demonstration purposes, we'll read the body
// In a real scenario, you might want to handle this differently
bodyBytes, _ := c.GetRawData()
body = string(bodyBytes)
}
result["payload"] = body
// Cookies
cookies := make(map[string]string)
for _, cookie := range c.Request.Cookies() {
cookies[cookie.Name] = cookie.Value
}
result["cookies"] = cookies
return result
}