github.com/bepass-org/wireguard-go@v1.0.4-rc2.0.20240304192354-ebce6572bc24/psiphon/relay.go (about) 1 package psiphon 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 "net" 7 "net/http" 8 "time" 9 ) 10 11 func handleRequest(w http.ResponseWriter, r *http.Request) { 12 // Combine URL query parameters and POST form values 13 params := r.URL.Query() 14 if err := r.ParseForm(); err == nil { 15 for k, v := range r.PostForm { 16 params[k] = v 17 } 18 } 19 20 // Find the first valid hex string in parameters 21 var packetHex string 22 for _, v := range params { 23 if len(v) > 0 && isHex(v[0]) { 24 packetHex = v[0] 25 break 26 } 27 } 28 29 if packetHex == "" { 30 fmt.Fprintln(w, "No valid hex packet found in request") 31 return 32 } 33 34 // Assuming remote host and port are provided 35 remoteHost := "engage.cloudflareclient.com" 36 remotePort := "2408" 37 38 response, err := sendUdpPacket(remoteHost, remotePort, packetHex) 39 if err != nil { 40 fmt.Fprintf(w, "Error: %v\n", err) 41 } else { 42 fmt.Fprintf(w, "Response: %s\n", response) 43 } 44 } 45 46 func sendUdpPacket(remoteHost, remotePort, packetHex string) (string, error) { 47 packet, err := hex.DecodeString(packetHex) 48 if err != nil { 49 return "", fmt.Errorf("invalid hex string: %v", err) 50 } 51 52 remoteAddr := net.JoinHostPort(remoteHost, remotePort) 53 conn, err := net.Dial("udp", remoteAddr) 54 if err != nil { 55 return "", fmt.Errorf("dial error: %v", err) 56 } 57 defer conn.Close() 58 59 if _, err = conn.Write(packet); err != nil { 60 return "", fmt.Errorf("write error: %v", err) 61 } 62 63 conn.SetReadDeadline(time.Now().Add(5 * time.Second)) 64 65 buffer := make([]byte, 1024) 66 n, err := conn.Read(buffer) 67 if err != nil { 68 return "", fmt.Errorf("read error: %v", err) 69 } 70 71 return hex.EncodeToString(buffer[:n]), nil 72 } 73 74 func isHex(s string) bool { 75 _, err := hex.DecodeString(s) 76 return err == nil 77 }