其实之前没接触过GO,只是之前用PHP写的监控在并发和协程上都不太理解,不太能够满足我监控的条件,所以马上开始学习Go,然后搞了个这么个玩意,毕竟穿透CF是必要条件。
代码简单易懂,基本是先穿透CF,并获取cookie,然后使用此cookie直接访问,提高访问效率。
有不懂的可以在下面评论提问
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
func main() {
apiURL := "http://127.0.0.1:8191/v1"
cfURL := "https://www.dmit.io/cart.php?a=add&pid=183"
timeout := 1000 * time.Second
headers := make(http.Header)
headers.Set("Content-Type", "application/json")
// Prepare the request data
data := map[string]interface{}{
"cmd": "request.get",
"url": cfURL,
"maxTimeout": 60000,
}
// Create a HTTP client with timeout
client := &http.Client{
Timeout: timeout,
}
// Encode the request data
requestBody, err := json.Marshal(data)
if err != nil {
fmt.Println("Error encoding request data:", err)
return
}
// Send a POST request to FlareSolverr
response, err := client.Post(apiURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
fmt.Println("Error sending POST request:", err)
return
}
defer response.Body.Close()
// Decode the JSON response
var responseJSON map[string]interface{}
err = json.NewDecoder(response.Body).Decode(&responseJSON)
if err != nil {
fmt.Println("Error decoding JSON response:", err)
return
}
// Extract the cookies from the FlareSolverr response
cookies := make(map[string]string)
cookiesData := responseJSON["solution"].(map[string]interface{})["cookies"].([]interface{})
for _, cookieData := range cookiesData {
cookie := cookieData.(map[string]interface{})
cookies[cookie["name"].(string)] = cookie["value"].(string)
}
// Extract the user agent from the FlareSolverr response
userAgent := responseJSON["solution"].(map[string]interface{})["userAgent"].(string)
// Send a GET request to the target URL with the cookies and user agent
request, err := http.NewRequest("GET", cfURL, nil)
if err != nil {
fmt.Println("Error creating GET request:", err)
return
}
request.Header.Set("User-Agent", userAgent)
for name, value := range cookies {
request.AddCookie(&http.Cookie{Name: name, Value: value})
}
response, err = client.Do(request)
if err != nil {
fmt.Println("Error sending GET request:", err)
return
}
defer response.Body.Close()
// Process the response as needed
// ...
}