mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
sniff: fix http with port
This commit is contained in:
parent
50ad767f0a
commit
baf68026dc
@ -4,6 +4,7 @@ import (
|
|||||||
std_bufio "bufio"
|
std_bufio "bufio"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
C "github.com/sagernet/sing-box/constant"
|
C "github.com/sagernet/sing-box/constant"
|
||||||
@ -15,5 +16,10 @@ func HTTPHost(ctx context.Context, reader io.Reader) (*adapter.InboundContext, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &adapter.InboundContext{Protocol: C.ProtocolHTTP, Domain: request.Host}, nil
|
domain := request.Host
|
||||||
|
host, _, err := net.SplitHostPort(domain)
|
||||||
|
if err == nil {
|
||||||
|
domain = host
|
||||||
|
}
|
||||||
|
return &adapter.InboundContext{Protocol: C.ProtocolHTTP, Domain: domain}, nil
|
||||||
}
|
}
|
||||||
|
27
common/sniff/http_test.go
Normal file
27
common/sniff/http_test.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package sniff_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing-box/common/sniff"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHTTP1(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
pkt := "GET / HTTP/1.1\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n"
|
||||||
|
metadata, err := sniff.HTTPHost(context.Background(), strings.NewReader(pkt))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, metadata.Domain, "www.google.com")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHTTP1WithPort(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
pkt := "GET / HTTP/1.1\r\nHost: www.gov.cn:8080\r\nAccept: */*\r\n\r\n"
|
||||||
|
metadata, err := sniff.HTTPHost(context.Background(), strings.NewReader(pkt))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, metadata.Domain, "www.gov.cn")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user