-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
194 lines (149 loc) · 3.67 KB
/
net.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gouse
import (
"context"
"fmt"
"io"
"net"
"net/http"
"strconv"
"sync/atomic"
"time"
"net/url"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
)
/* Port */
func ScanPort(protocol, hostname string, start, end int) {
for i := start; i < end; i++ {
port := strconv.FormatInt(int64(i), 10)
conn, err := net.Dial(protocol, hostname+":"+port)
if err == nil {
fmt.Println("Port", i, "open")
conn.Close()
} else {
fmt.Println("Port", i, "closed")
}
}
}
func CheckPort(protocol, hostname string, port int) bool {
address := hostname + ":" + strconv.Itoa(port)
conn, err := net.DialTimeout(protocol, address, 60*time.Second)
if err != nil {
return false
}
defer func() {
if conn != nil {
conn.Close()
}
}()
return true
}
/* Proxy */
func handleRequest(urls []string) gin.HandlerFunc {
var counter uint64
return func(ctx *gin.Context) {
path := ctx.Param("path")
if path == "" {
ctx.IndentedJSON(http.StatusBadRequest, gin.H{
"message": "Path is required",
})
ctx.Done()
return
}
index := atomic.AddUint64(&counter, 1) % uint64(len(urls))
requestedURL := urls[index] + path[1:]
fmt.Println("Requested URL: ", requestedURL)
req, _ := http.NewRequest(ctx.Request.Method, requestedURL, ctx.Request.Body)
req.Header = ctx.Request.Header.Clone()
req.Header.Del("origin")
req.Header.Del("referer")
queryValues := req.URL.Query()
for k, v := range ctx.Request.URL.Query() {
queryValues.Add(k, v[0])
}
req.URL.RawQuery = queryValues.Encode()
response, err1 := http.DefaultClient.Do(req)
for k, v := range response.Header.Clone() {
ctx.Header(k, v[0])
}
ctx.Header("Access-Control-Allow-Origin", "*")
ctx.Header("Access-Control-Allow-Methods", "*")
ctx.Header("Access-Control-Allow-Headers", "*")
responseBytes, err2 := io.ReadAll(response.Body)
if err1 != nil || err2 != nil {
ctx.IndentedJSON(http.StatusInternalServerError, gin.H{
"message": "Failed to request",
})
ctx.Done()
return
}
ctx.Data(response.StatusCode, response.Header.Get("Content-Type"), responseBytes)
}
}
func Proxy(port string, urls []string) {
router := gin.Default()
router.GET("*path", handleRequest(urls))
router.POST("*path", handleRequest(urls))
router.PUT("*path", handleRequest(urls))
router.PATCH("*path", handleRequest(urls))
router.DELETE("*path", handleRequest(urls))
router.OPTIONS("*path", handleRequest(urls))
router.HEAD("*path", handleRequest(urls))
router.Run(":" + port)
}
/* Validator */
func ReadRequest(ctxBind func() error, ctxReq func() context.Context, req interface{}) error {
validate := validator.New()
if err := ctxBind(); err != nil {
return err
}
ctx := ctxReq()
return validate.StructCtx(ctx, req)
}
/* Href link */
func OpenHref(url string) {
Cmd("explorer " + url)
}
func EncodeHref(s string) string {
return url.QueryEscape(s)
}
func DecodeHref(s string) string {
decoded, err := url.QueryUnescape(s)
if err != nil {
return s
}
return decoded
}
func CheckHref(url string) (bool, error) {
_, err := http.Get(url)
if err != nil {
return false, err
}
return true, nil
}
func CheckHrefStatusCode(url string) (int, error) {
resp, err := http.Get(url)
if err != nil {
return 0, err
}
return resp.StatusCode, nil
}
func HrefHeader(url string) (http.Header, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
return resp.Header, nil
}
func HrefConnectTime(url string) (float64, error) {
startTime := time.Now()
resp, err := http.Get(url)
if err != nil {
return 0, err
}
elapsedTime := time.Since(startTime)
if err := resp.Body.Close(); err != nil {
return 0, err
}
return elapsedTime.Seconds(), nil
}