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/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
@ -28,39 +29,33 @@ var defaultClientHeader = http.Header{
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
ctx context.Context
|
||||
dialer N.Dialer
|
||||
serverAddr M.Socksaddr
|
||||
transport *http2.Transport
|
||||
options option.V2RayGRPCOptions
|
||||
url *url.URL
|
||||
host string
|
||||
transport *http2.Transport
|
||||
request *http.Request
|
||||
}
|
||||
|
||||
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayGRPCOptions, tlsConfig tls.Config) adapter.V2RayClientTransport {
|
||||
var host string
|
||||
if tlsConfig != nil && tlsConfig.ServerName() != "" {
|
||||
host = M.ParseSocksaddrHostPort(tlsConfig.ServerName(), serverAddr.Port).String()
|
||||
} else {
|
||||
host = serverAddr.String()
|
||||
host = net.JoinHostPort(tlsConfig.ServerName(), strconv.Itoa(int(serverAddr.Port)))
|
||||
}
|
||||
|
||||
client := &Client{
|
||||
ctx: ctx,
|
||||
dialer: dialer,
|
||||
serverAddr: serverAddr,
|
||||
options: options,
|
||||
transport: &http2.Transport{
|
||||
ReadIdleTimeout: time.Duration(options.IdleTimeout),
|
||||
PingTimeout: time.Duration(options.PingTimeout),
|
||||
DisableCompression: true,
|
||||
},
|
||||
url: &url.URL{
|
||||
Scheme: "https",
|
||||
Host: serverAddr.String(),
|
||||
Path: "/" + options.ServiceName + "/Tun",
|
||||
RawPath: "/" + url.PathEscape(options.ServiceName) + "/Tun",
|
||||
request: &http.Request{
|
||||
Method: http.MethodPost,
|
||||
URL: &url.URL{
|
||||
Scheme: "https",
|
||||
Host: serverAddr.String(),
|
||||
Path: "/" + options.ServiceName + "/Tun",
|
||||
RawPath: "/" + url.PathEscape(options.ServiceName) + "/Tun",
|
||||
},
|
||||
Host: host,
|
||||
Header: defaultClientHeader,
|
||||
},
|
||||
host: host,
|
||||
}
|
||||
|
||||
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) {
|
||||
pipeInReader, pipeInWriter := io.Pipe()
|
||||
request := &http.Request{
|
||||
Method: http.MethodPost,
|
||||
Body: pipeInReader,
|
||||
URL: c.url,
|
||||
Header: defaultClientHeader,
|
||||
Host: c.host,
|
||||
}
|
||||
request = request.WithContext(ctx)
|
||||
request := c.request.WithContext(ctx)
|
||||
request.Body = pipeInReader
|
||||
conn := newLateGunConn(pipeInWriter)
|
||||
go func() {
|
||||
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))
|
||||
header[5] = 0x0A
|
||||
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 {
|
||||
c.flusher.Flush()
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ func WriteRequest(writer io.Writer, request Request, payload []byte) error {
|
||||
var addonsLen int
|
||||
if request.Flow != "" {
|
||||
addonsLen += 1 // protobuf header
|
||||
addonsLen += UvarintLen(uint64(len(request.Flow)))
|
||||
addonsLen += rw.UVariantLen(uint64(len(request.Flow)))
|
||||
addonsLen += len(request.Flow)
|
||||
requestLen += addonsLen
|
||||
}
|
||||
@ -150,8 +150,8 @@ func WriteRequest(writer io.Writer, request Request, payload []byte) error {
|
||||
)
|
||||
if addonsLen > 0 {
|
||||
common.Must(buffer.WriteByte(10))
|
||||
binary.PutUvarint(buffer.Extend(UvarintLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||
common.Must(common.Error(buffer.Write([]byte(request.Flow))))
|
||||
binary.PutUvarint(buffer.Extend(rw.UVariantLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||
common.Must(common.Error(buffer.WriteString(request.Flow)))
|
||||
}
|
||||
common.Must(
|
||||
buffer.WriteByte(request.Command),
|
||||
@ -174,7 +174,7 @@ func EncodeRequest(request Request, buffer *buf.Buffer) {
|
||||
var addonsLen int
|
||||
if request.Flow != "" {
|
||||
addonsLen += 1 // protobuf header
|
||||
addonsLen += UvarintLen(uint64(len(request.Flow)))
|
||||
addonsLen += rw.UVariantLen(uint64(len(request.Flow)))
|
||||
addonsLen += len(request.Flow)
|
||||
requestLen += addonsLen
|
||||
}
|
||||
@ -189,8 +189,8 @@ func EncodeRequest(request Request, buffer *buf.Buffer) {
|
||||
)
|
||||
if addonsLen > 0 {
|
||||
common.Must(buffer.WriteByte(10))
|
||||
binary.PutUvarint(buffer.Extend(UvarintLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||
common.Must(common.Error(buffer.Write([]byte(request.Flow))))
|
||||
binary.PutUvarint(buffer.Extend(rw.UVariantLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||
common.Must(common.Error(buffer.WriteString(request.Flow)))
|
||||
}
|
||||
common.Must(
|
||||
buffer.WriteByte(request.Command),
|
||||
@ -210,7 +210,7 @@ func RequestLen(request Request) int {
|
||||
var addonsLen int
|
||||
if request.Flow != "" {
|
||||
addonsLen += 1 // protobuf header
|
||||
addonsLen += UvarintLen(uint64(len(request.Flow)))
|
||||
addonsLen += rw.UVariantLen(uint64(len(request.Flow)))
|
||||
addonsLen += len(request.Flow)
|
||||
requestLen += addonsLen
|
||||
}
|
||||
@ -229,7 +229,7 @@ func WritePacketRequest(writer io.Writer, request Request, payload []byte) error
|
||||
var addonsLen int
|
||||
/*if request.Flow != "" {
|
||||
addonsLen += 1 // protobuf header
|
||||
addonsLen += UvarintLen(uint64(len(request.Flow)))
|
||||
addonsLen += rw.UVariantLen(uint64(len(request.Flow)))
|
||||
addonsLen += len(request.Flow)
|
||||
requestLen += addonsLen
|
||||
}*/
|
||||
@ -251,8 +251,8 @@ func WritePacketRequest(writer io.Writer, request Request, payload []byte) error
|
||||
|
||||
if addonsLen > 0 {
|
||||
common.Must(buffer.WriteByte(10))
|
||||
binary.PutUvarint(buffer.Extend(UvarintLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||
common.Must(common.Error(buffer.Write([]byte(request.Flow))))
|
||||
binary.PutUvarint(buffer.Extend(rw.UVariantLen(uint64(len(request.Flow)))), uint64(len(request.Flow)))
|
||||
common.Must(common.Error(buffer.WriteString(request.Flow)))
|
||||
}
|
||||
|
||||
common.Must(
|
||||
@ -290,8 +290,3 @@ func ReadResponse(reader io.Reader) error {
|
||||
}
|
||||
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