mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
sync
This commit is contained in:
94
test/proxy/http3_proxy_test.go
Normal file
94
test/proxy/http3_proxy_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gospider007/gtls"
|
||||
"github.com/gospider007/proxy"
|
||||
"github.com/gospider007/requests"
|
||||
"github.com/quic-go/quic-go/http3"
|
||||
)
|
||||
|
||||
var (
|
||||
proxyHost = "127.0.0.1:1080"
|
||||
remoteHost = "127.0.0.1:8080"
|
||||
)
|
||||
|
||||
func client() {
|
||||
for range 5 {
|
||||
resp, err := requests.Post(nil, "https://"+remoteHost, requests.RequestOption{
|
||||
H3: true,
|
||||
Logger: func(l requests.Log) {
|
||||
log.Print(l)
|
||||
},
|
||||
Proxy: "socks5://" + proxyHost,
|
||||
Body: []byte("hello, server!"),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(resp.StatusCode())
|
||||
fmt.Println(resp.Text())
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func server() {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
fmt.Fprint(w, "hello, world!")
|
||||
case http.MethodPost:
|
||||
result, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fmt.Fprint(w, "error:", err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "echo:'%s'", string(result))
|
||||
default:
|
||||
fmt.Fprint(w, "method is not supported")
|
||||
}
|
||||
})
|
||||
tlsCert, err := gtls.CreateProxyCertWithName("localhost")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
server := http3.Server{
|
||||
Addr: "0.0.0.0:8080",
|
||||
Handler: mux,
|
||||
TLSConfig: &tls.Config{
|
||||
Certificates: []tls.Certificate{tlsCert},
|
||||
NextProtos: []string{"http3-echo-example"},
|
||||
},
|
||||
}
|
||||
fmt.Println("Server is listening...")
|
||||
fmt.Println(server.ListenAndServe())
|
||||
}
|
||||
func proxyServer() {
|
||||
c, err := proxy.NewClient(nil, proxy.ClientOption{
|
||||
Addr: ":1080",
|
||||
Debug: true,
|
||||
DisVerify: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Run()
|
||||
}
|
||||
|
||||
func TestHttp3Proxy(t *testing.T) {
|
||||
go server()
|
||||
go proxyServer()
|
||||
time.Sleep(time.Second * 3)
|
||||
client()
|
||||
}
|
||||
Reference in New Issue
Block a user