mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
Cleanup code
This commit is contained in:
parent
bec606ee88
commit
f75aaa0afa
@ -6,6 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
@ -28,39 +29,33 @@ var defaultClientHeader = http.Header{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
ctx context.Context
|
|
||||||
dialer N.Dialer
|
|
||||||
serverAddr M.Socksaddr
|
|
||||||
transport *http2.Transport
|
transport *http2.Transport
|
||||||
options option.V2RayGRPCOptions
|
request *http.Request
|
||||||
url *url.URL
|
|
||||||
host string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayGRPCOptions, tlsConfig tls.Config) adapter.V2RayClientTransport {
|
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayGRPCOptions, tlsConfig tls.Config) adapter.V2RayClientTransport {
|
||||||
var host string
|
var host string
|
||||||
if tlsConfig != nil && tlsConfig.ServerName() != "" {
|
if tlsConfig != nil && tlsConfig.ServerName() != "" {
|
||||||
host = M.ParseSocksaddrHostPort(tlsConfig.ServerName(), serverAddr.Port).String()
|
host = net.JoinHostPort(tlsConfig.ServerName(), strconv.Itoa(int(serverAddr.Port)))
|
||||||
} else {
|
|
||||||
host = serverAddr.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &Client{
|
client := &Client{
|
||||||
ctx: ctx,
|
|
||||||
dialer: dialer,
|
|
||||||
serverAddr: serverAddr,
|
|
||||||
options: options,
|
|
||||||
transport: &http2.Transport{
|
transport: &http2.Transport{
|
||||||
ReadIdleTimeout: time.Duration(options.IdleTimeout),
|
ReadIdleTimeout: time.Duration(options.IdleTimeout),
|
||||||
PingTimeout: time.Duration(options.PingTimeout),
|
PingTimeout: time.Duration(options.PingTimeout),
|
||||||
DisableCompression: true,
|
DisableCompression: true,
|
||||||
},
|
},
|
||||||
url: &url.URL{
|
request: &http.Request{
|
||||||
|
Method: http.MethodPost,
|
||||||
|
URL: &url.URL{
|
||||||
Scheme: "https",
|
Scheme: "https",
|
||||||
Host: serverAddr.String(),
|
Host: serverAddr.String(),
|
||||||
Path: "/" + options.ServiceName + "/Tun",
|
Path: "/" + options.ServiceName + "/Tun",
|
||||||
RawPath: "/" + url.PathEscape(options.ServiceName) + "/Tun",
|
RawPath: "/" + url.PathEscape(options.ServiceName) + "/Tun",
|
||||||
},
|
},
|
||||||
host: host,
|
Host: host,
|
||||||
|
Header: defaultClientHeader,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if tlsConfig == nil {
|
if tlsConfig == nil {
|
||||||
@ -85,14 +80,8 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
|
|||||||
|
|
||||||
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
||||||
pipeInReader, pipeInWriter := io.Pipe()
|
pipeInReader, pipeInWriter := io.Pipe()
|
||||||
request := &http.Request{
|
request := c.request.WithContext(ctx)
|
||||||
Method: http.MethodPost,
|
request.Body = pipeInReader
|
||||||
Body: pipeInReader,
|
|
||||||
URL: c.url,
|
|
||||||
Header: defaultClientHeader,
|
|
||||||
Host: c.host,
|
|
||||||
}
|
|
||||||
request = request.WithContext(ctx)
|
|
||||||
conn := newLateGunConn(pipeInWriter)
|
conn := newLateGunConn(pipeInWriter)
|
||||||
go func() {
|
go func() {
|
||||||
response, err := c.transport.RoundTrip(request)
|
response, err := c.transport.RoundTrip(request)
|
||||||
|
@ -122,7 +122,7 @@ func (c *GunConn) WriteBuffer(buffer *buf.Buffer) error {
|
|||||||
binary.BigEndian.PutUint32(header[1:5], uint32(1+varLen+dataLen))
|
binary.BigEndian.PutUint32(header[1:5], uint32(1+varLen+dataLen))
|
||||||
header[5] = 0x0A
|
header[5] = 0x0A
|
||||||
binary.PutUvarint(header[6:], uint64(dataLen))
|
binary.PutUvarint(header[6:], uint64(dataLen))
|
||||||
err := rw.WriteBytes(c.writer, buffer.Bytes())
|
_, err := c.writer.Write(buffer.Bytes())
|
||||||
if err == nil && c.flusher != nil {
|
if err == nil && c.flusher != nil {
|
||||||
c.flusher.Flush()
|
c.flusher.Flush()
|
||||||
}
|
}
|
||||||
|
@ -130,7 +130,7 @@ func WriteRequest(writer io.Writer, request Request, payload []byte) error {
|
|||||||
var addonsLen int
|
var addonsLen int
|
||||||
if request.Flow != "" {
|
if request.Flow != "" {
|
||||||
addonsLen += 1 // protobuf header
|
addonsLen += 1 // protobuf header
|
||||||
addonsLen += UvarintLen(uint64(len(request.Flow)))
|
addonsLen += rw.UVariantLen(uint64(len(request.Flow)))
|
||||||
addonsLen += len(request.Flow)
|
addonsLen += len(request.Flow)
|
||||||
requestLen += addonsLen
|
requestLen += addonsLen
|
||||||
}
|
}
|
||||||
@ -150,8 +150,8 @@ func WriteRequest(writer io.Writer, request Request, payload []byte) error {
|
|||||||
)
|
)
|
||||||
if addonsLen > 0 {
|
if addonsLen > 0 {
|
||||||
common.Must(buffer.WriteByte(10))
|
common.Must(buffer.WriteByte(10))
|
||||||
binary.PutUvarint(buffer.Extend(UvarintLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
binary.PutUvarint(buffer.Extend(rw.UVariantLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||||
common.Must(common.Error(buffer.Write([]byte(request.Flow))))
|
common.Must(common.Error(buffer.WriteString(request.Flow)))
|
||||||
}
|
}
|
||||||
common.Must(
|
common.Must(
|
||||||
buffer.WriteByte(request.Command),
|
buffer.WriteByte(request.Command),
|
||||||
@ -174,7 +174,7 @@ func EncodeRequest(request Request, buffer *buf.Buffer) {
|
|||||||
var addonsLen int
|
var addonsLen int
|
||||||
if request.Flow != "" {
|
if request.Flow != "" {
|
||||||
addonsLen += 1 // protobuf header
|
addonsLen += 1 // protobuf header
|
||||||
addonsLen += UvarintLen(uint64(len(request.Flow)))
|
addonsLen += rw.UVariantLen(uint64(len(request.Flow)))
|
||||||
addonsLen += len(request.Flow)
|
addonsLen += len(request.Flow)
|
||||||
requestLen += addonsLen
|
requestLen += addonsLen
|
||||||
}
|
}
|
||||||
@ -189,8 +189,8 @@ func EncodeRequest(request Request, buffer *buf.Buffer) {
|
|||||||
)
|
)
|
||||||
if addonsLen > 0 {
|
if addonsLen > 0 {
|
||||||
common.Must(buffer.WriteByte(10))
|
common.Must(buffer.WriteByte(10))
|
||||||
binary.PutUvarint(buffer.Extend(UvarintLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
binary.PutUvarint(buffer.Extend(rw.UVariantLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||||
common.Must(common.Error(buffer.Write([]byte(request.Flow))))
|
common.Must(common.Error(buffer.WriteString(request.Flow)))
|
||||||
}
|
}
|
||||||
common.Must(
|
common.Must(
|
||||||
buffer.WriteByte(request.Command),
|
buffer.WriteByte(request.Command),
|
||||||
@ -210,7 +210,7 @@ func RequestLen(request Request) int {
|
|||||||
var addonsLen int
|
var addonsLen int
|
||||||
if request.Flow != "" {
|
if request.Flow != "" {
|
||||||
addonsLen += 1 // protobuf header
|
addonsLen += 1 // protobuf header
|
||||||
addonsLen += UvarintLen(uint64(len(request.Flow)))
|
addonsLen += rw.UVariantLen(uint64(len(request.Flow)))
|
||||||
addonsLen += len(request.Flow)
|
addonsLen += len(request.Flow)
|
||||||
requestLen += addonsLen
|
requestLen += addonsLen
|
||||||
}
|
}
|
||||||
@ -229,7 +229,7 @@ func WritePacketRequest(writer io.Writer, request Request, payload []byte) error
|
|||||||
var addonsLen int
|
var addonsLen int
|
||||||
/*if request.Flow != "" {
|
/*if request.Flow != "" {
|
||||||
addonsLen += 1 // protobuf header
|
addonsLen += 1 // protobuf header
|
||||||
addonsLen += UvarintLen(uint64(len(request.Flow)))
|
addonsLen += rw.UVariantLen(uint64(len(request.Flow)))
|
||||||
addonsLen += len(request.Flow)
|
addonsLen += len(request.Flow)
|
||||||
requestLen += addonsLen
|
requestLen += addonsLen
|
||||||
}*/
|
}*/
|
||||||
@ -251,8 +251,8 @@ func WritePacketRequest(writer io.Writer, request Request, payload []byte) error
|
|||||||
|
|
||||||
if addonsLen > 0 {
|
if addonsLen > 0 {
|
||||||
common.Must(buffer.WriteByte(10))
|
common.Must(buffer.WriteByte(10))
|
||||||
binary.PutUvarint(buffer.Extend(UvarintLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
binary.PutUvarint(buffer.Extend(rw.UVariantLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||||
common.Must(common.Error(buffer.Write([]byte(request.Flow))))
|
common.Must(common.Error(buffer.WriteString(request.Flow)))
|
||||||
}
|
}
|
||||||
|
|
||||||
common.Must(
|
common.Must(
|
||||||
@ -290,8 +290,3 @@ func ReadResponse(reader io.Reader) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UvarintLen(value uint64) int {
|
|
||||||
var buffer [binary.MaxVarintLen64]byte
|
|
||||||
return binary.PutUvarint(buffer[:], value)
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user