package main import ( "fmt" "net/http" "randomLink/lib" ) //type RequestData struct { // ClientIP string `json:"clientIp"` // UserAgent string `json:"userAgent"` // VisitUrl string `json:"visitUrl"` // ClientLanguage string `json:"clientLanguage"` // Referer string `json:"referer"` // Timestamp int64 `json:"timestamp"` // Sign string `json:"sign"` //} //type Response struct { // Code int `json:"code"` // Message string `json:"message"` // Data struct { // Status bool `json:"status"` // Message string `json:"message"` // Jump string `json:"jump"` // ErrorCode string `json:"errorCode"` // Custom struct { // WebId int `json:"webId"` // Name string `json:"name"` // } `json:"custom"` // } `json:"data"` // Success bool `json:"success"` //} //func getRealIP(r *http.Request) string { // // 优先从 X-Forwarded-For 获取 // xff := r.Header.Get("X-Forwarded-For") // if xff != "" { // // X-Forwarded-For 可能包含多个 IP,取第一个 // ips := strings.Split(xff, ",") // return strings.TrimSpace(ips[0]) // } // // // 尝试从 X-Real-IP 获取 // xri := r.Header.Get("X-Real-IP") // if xri != "" { // return xri // } // // // 最后回退到 RemoteAddr // host, _, err := net.SplitHostPort(r.RemoteAddr) // if err != nil { // return r.RemoteAddr // 返回原始 RemoteAddr // } // return host //} // HTTP 跳转处理器 func redirectHandler(w http.ResponseWriter, r *http.Request) { //data := RequestData{ // ClientIP: getRealIP(r), // UserAgent: r.UserAgent(), // VisitUrl: "https://app.autostock.tech" + r.URL.String(), // ClientLanguage: r.Header.Get("Accept-Language"), // Timestamp: time.Now().Unix(), // Referer: r.Referer(), // Sign: "R0QtsCttYCltUlaUYXSzj", //} //// 将数据编码为 JSON //jsonData, err := json.Marshal(data) //if err != nil { // fmt.Println("Error marshaling JSON:", err) // return //} //resp, err := http.Post("https://api-visitor.fangyu.io/check/2168/BFqEzXbIQYao3w3j5B", "application/json", bytes.NewBuffer(jsonData)) //if err != nil { // fmt.Println("Error sending POST request:", err) // return //} //defer resp.Body.Close() //// 输出响应状态码 //fmt.Println("Response Status:", resp.Status) //body, err := ioutil.ReadAll(resp.Body) //fmt.Println("Response Body:", string(body)) //var response Response //if err := json.Unmarshal([]byte(body), &response); err != nil { // fmt.Println("Error unmarshaling JSON:", err) //} //fmt.Printf("Response: %+v\n", response) //if response.Data.Status { // link, err := lib.GetLink() // if err != nil { // http.Error(w, "No available links", http.StatusInternalServerError) // return // } // http.Redirect(w, r, link, http.StatusFound) // //} else { // http.Redirect(w, r, "https://#", http.StatusFound) //} link, err := lib.GetLink() if err != nil { http.Error(w, "No available links", http.StatusInternalServerError) return } http.Redirect(w, r, link, http.StatusFound) } func main() { go lib.Start() // 启动 HTTP 服务器 http.HandleFunc("/", redirectHandler) fmt.Println("Server is running on :8083") if err := http.ListenAndServe(":8083", nil); err != nil { fmt.Println("Server error:", err) } }