Skip to content

Commit

Permalink
chore: 加上随机 ip
Browse files Browse the repository at this point in the history
  • Loading branch information
adams549659584 committed May 3, 2023
1 parent d651767 commit 370f0d0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
Expand All @@ -28,6 +29,7 @@ var (
KEEP_HEADERS = map[string]bool{
"Accept": true,
"Accept-Encoding": true,
"Accept-Language": true,
"Referer": true,
"Connection": true,
"Cookie": true,
Expand All @@ -36,6 +38,8 @@ var (
"Sec-Websocket-Extensions": true,
"Sec-Websocket-Key": true,
"Sec-Websocket-Version": true,
"X-Request-Id": true,
"X-Forwarded-For": true,
}
)

Expand Down Expand Up @@ -85,6 +89,10 @@ func newSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {

req.Header.Set("Referer", fmt.Sprintf("%s/search?q=Bing+AI", BING_URL.String()))

// 随机ip
randIp := fmt.Sprintf("%d.%d.%d.%d", randInt(1, 10), randInt(1, 255), randInt(1, 255), randInt(1, 255))
req.Header.Set("X-Forwarded-For", randIp)

for hKey, _ := range req.Header {
if _, isExist := KEEP_HEADERS[hKey]; !isExist {
req.Header.Del(hKey)
Expand Down Expand Up @@ -124,6 +132,7 @@ func newSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
return nil
}
errorHandler := func(res http.ResponseWriter, req *http.Request, err error) {
log.Println("代理异常 :", err)
res.Write([]byte(err.Error()))
}
// 代理请求 请求回来的内容 报错自动调用
Expand Down Expand Up @@ -222,3 +231,8 @@ func modifyDefaultBody(res *http.Response, originalScheme string, originalHost s

return nil
}

func randInt(min int, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min+1) + min
}

0 comments on commit 370f0d0

Please sign in to comment.