2024-06-18 05:36:36 +00:00
|
|
|
package splithttp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-23 17:05:37 +00:00
|
|
|
gotls "crypto/tls"
|
2024-06-18 05:36:36 +00:00
|
|
|
"fmt"
|
2024-06-23 17:05:37 +00:00
|
|
|
gonet "net"
|
|
|
|
"net/http"
|
2024-06-18 05:36:36 +00:00
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol/tls/cert"
|
|
|
|
"github.com/xtls/xray-core/testing/servers/tcp"
|
2024-07-19 17:53:47 +00:00
|
|
|
"github.com/xtls/xray-core/testing/servers/udp"
|
2024-06-18 05:36:36 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
|
|
. "github.com/xtls/xray-core/transport/internet/splithttp"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/tls"
|
2024-06-23 17:05:37 +00:00
|
|
|
"golang.org/x/net/http2"
|
2024-06-18 05:36:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_listenSHAndDial(t *testing.T) {
|
|
|
|
listenPort := tcp.PickPort()
|
|
|
|
listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "splithttp",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "/sh",
|
|
|
|
},
|
|
|
|
}, func(conn stat.Connection) {
|
|
|
|
go func(c stat.Connection) {
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
var b [1024]byte
|
2024-07-02 02:28:50 +00:00
|
|
|
c.SetReadDeadline(time.Now().Add(2 * time.Second))
|
2024-06-18 05:36:36 +00:00
|
|
|
_, err := c.Read(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.Must2(c.Write([]byte("Response")))
|
|
|
|
}(conn)
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
ctx := context.Background()
|
|
|
|
streamSettings := &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "splithttp",
|
|
|
|
ProtocolSettings: &Config{Path: "sh"},
|
|
|
|
}
|
|
|
|
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
|
|
|
|
|
|
|
|
common.Must(err)
|
|
|
|
_, err = conn.Write([]byte("Test connection 1"))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
fmt.Println("test2")
|
|
|
|
n, _ := conn.Read(b[:])
|
|
|
|
fmt.Println("string is", n)
|
|
|
|
if string(b[:n]) != "Response" {
|
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
|
|
|
|
|
|
|
common.Must(conn.Close())
|
|
|
|
conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
|
2024-07-17 11:41:17 +00:00
|
|
|
|
2024-06-18 05:36:36 +00:00
|
|
|
common.Must(err)
|
|
|
|
_, err = conn.Write([]byte("Test connection 2"))
|
|
|
|
common.Must(err)
|
|
|
|
n, _ = conn.Read(b[:])
|
|
|
|
common.Must(err)
|
|
|
|
if string(b[:n]) != "Response" {
|
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
|
|
|
common.Must(conn.Close())
|
|
|
|
|
|
|
|
common.Must(listen.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialWithRemoteAddr(t *testing.T) {
|
|
|
|
listenPort := tcp.PickPort()
|
|
|
|
listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "splithttp",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "sh",
|
|
|
|
},
|
|
|
|
}, func(conn stat.Connection) {
|
|
|
|
go func(c stat.Connection) {
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
_, err := c.Read(b[:])
|
|
|
|
// common.Must(err)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-17 10:40:25 +00:00
|
|
|
_, err = c.Write([]byte(c.RemoteAddr().String()))
|
2024-06-18 05:36:36 +00:00
|
|
|
common.Must(err)
|
|
|
|
}(conn)
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "splithttp",
|
|
|
|
ProtocolSettings: &Config{Path: "sh", Header: map[string]string{"X-Forwarded-For": "1.1.1.1"}},
|
|
|
|
})
|
|
|
|
|
|
|
|
common.Must(err)
|
|
|
|
_, err = conn.Write([]byte("Test connection 1"))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
n, _ := conn.Read(b[:])
|
2024-07-17 10:40:25 +00:00
|
|
|
if string(b[:n]) != "1.1.1.1:0" {
|
2024-06-18 05:36:36 +00:00
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
|
|
|
|
|
|
|
common.Must(listen.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_listenSHAndDial_TLS(t *testing.T) {
|
|
|
|
if runtime.GOARCH == "arm64" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
listenPort := tcp.PickPort()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
streamSettings := &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "splithttp",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "shs",
|
|
|
|
},
|
|
|
|
SecurityType: "tls",
|
|
|
|
SecuritySettings: &tls.Config{
|
|
|
|
AllowInsecure: true,
|
|
|
|
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
|
|
|
|
go func() {
|
|
|
|
_ = conn.Close()
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
defer listen.Close()
|
|
|
|
|
|
|
|
conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
|
|
|
|
common.Must(err)
|
|
|
|
_ = conn.Close()
|
|
|
|
|
|
|
|
end := time.Now()
|
|
|
|
if !end.Before(start.Add(time.Second * 5)) {
|
|
|
|
t.Error("end: ", end, " start: ", start)
|
|
|
|
}
|
|
|
|
}
|
2024-06-23 17:05:37 +00:00
|
|
|
|
|
|
|
func Test_listenSHAndDial_H2C(t *testing.T) {
|
|
|
|
if runtime.GOARCH == "arm64" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
listenPort := tcp.PickPort()
|
|
|
|
|
|
|
|
streamSettings := &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "splithttp",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "shs",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
|
|
|
|
go func() {
|
|
|
|
_ = conn.Close()
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
defer listen.Close()
|
|
|
|
|
|
|
|
client := http.Client{
|
|
|
|
Transport: &http2.Transport{
|
|
|
|
// So http2.Transport doesn't complain the URL scheme isn't 'https'
|
|
|
|
AllowHTTP: true,
|
|
|
|
// even with AllowHTTP, http2.Transport will attempt to establish
|
|
|
|
// the connection using DialTLSContext. Disable TLS with custom
|
|
|
|
// dial context.
|
|
|
|
DialTLSContext: func(ctx context.Context, network, addr string, cfg *gotls.Config) (gonet.Conn, error) {
|
|
|
|
var d gonet.Dialer
|
|
|
|
return d.DialContext(ctx, network, addr)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Get("http://" + net.LocalHostIP.String() + ":" + listenPort.String())
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
if resp.StatusCode != 404 {
|
|
|
|
t.Error("Expected 404 but got:", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ProtoMajor != 2 {
|
|
|
|
t.Error("Expected h2 but got:", resp.ProtoMajor)
|
|
|
|
}
|
|
|
|
}
|
2024-07-19 17:53:47 +00:00
|
|
|
|
|
|
|
func Test_listenSHAndDial_QUIC(t *testing.T) {
|
|
|
|
if runtime.GOARCH == "arm64" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
listenPort := udp.PickPort()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
streamSettings := &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "splithttp",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "shs",
|
|
|
|
},
|
|
|
|
SecurityType: "tls",
|
|
|
|
SecuritySettings: &tls.Config{
|
|
|
|
AllowInsecure: true,
|
|
|
|
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
|
|
|
|
NextProtocol: []string{"h3"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
|
|
|
|
go func() {
|
|
|
|
_ = conn.Close()
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
defer listen.Close()
|
|
|
|
|
|
|
|
conn, err := Dial(context.Background(), net.UDPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
|
|
|
|
common.Must(err)
|
|
|
|
_ = conn.Close()
|
|
|
|
|
|
|
|
end := time.Now()
|
|
|
|
if !end.Before(start.Add(time.Second * 5)) {
|
|
|
|
t.Error("end: ", end, " start: ", start)
|
|
|
|
}
|
|
|
|
}
|