Skip to content

Commit b15e02c

Browse files
committed
Feat: rewritre http client with native string
1 parent e4d9384 commit b15e02c

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

protocol/http/client.go

+33-22
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
std_bufio "bufio"
55
"context"
66
"encoding/base64"
7+
"fmt"
78
"net"
89
"net/http"
9-
"net/url"
1010
"os"
11+
"strings"
1112

1213
"github.com/sagernet/sing/common/buf"
1314
"github.com/sagernet/sing/common/bufio"
@@ -65,42 +66,52 @@ func (c *Client) DialContext(ctx context.Context, network string, destination M.
6566
if err != nil {
6667
return nil, err
6768
}
68-
request := &http.Request{
69-
Method: http.MethodConnect,
70-
URL: &url.URL{
71-
Host: destination.String(),
72-
},
73-
Header: http.Header{
74-
"Proxy-Connection": []string{"Keep-Alive"},
75-
},
76-
}
77-
if c.path != "" {
78-
err = URLSetPath(request.URL, c.path)
79-
if err != nil {
80-
return nil, err
81-
}
69+
URL := destination.String()
70+
HeaderString := "CONNECT " + URL + " HTTP/1.1\r\n"
71+
tempHeaders := map[string][]string{
72+
"Host": {destination.String()},
73+
"User-Agent": {"Go-http-client/1.1"},
74+
"Proxy-Connection": {"Keep-Alive"},
8275
}
76+
8377
for key, valueList := range c.headers {
84-
request.Header.Set(key, valueList[0])
85-
for _, value := range valueList[1:] {
86-
request.Header.Add(key, value)
87-
}
78+
tempHeaders[key] = valueList
8879
}
80+
81+
if c.path != "" {
82+
tempHeaders["Path"] = []string{c.path}
83+
}
84+
8985
if c.username != "" {
9086
auth := c.username + ":" + c.password
91-
request.Header.Add("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
87+
if _, ok := tempHeaders["Proxy-Authorization"]; ok {
88+
tempHeaders["Proxy-Authorization"][len(tempHeaders["Proxy-Authorization"])] = "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
89+
} else {
90+
tempHeaders["Proxy-Authorization"] = []string{"Basic " + base64.StdEncoding.EncodeToString([]byte(auth))}
91+
}
92+
}
93+
for key, valueList := range tempHeaders {
94+
HeaderString += key + ": " + strings.Join(valueList, "; ") + "\r\n"
9295
}
93-
err = request.Write(conn)
96+
97+
HeaderString += "\r\n"
98+
99+
_, err = fmt.Fprintf(conn, "%s", HeaderString)
100+
94101
if err != nil {
95102
conn.Close()
96103
return nil, err
97104
}
105+
98106
reader := std_bufio.NewReader(conn)
99-
response, err := http.ReadResponse(reader, request)
107+
108+
response, err := http.ReadResponse(reader, nil)
109+
100110
if err != nil {
101111
conn.Close()
102112
return nil, err
103113
}
114+
104115
if response.StatusCode == http.StatusOK {
105116
if reader.Buffered() > 0 {
106117
buffer := buf.NewSize(reader.Buffered())

0 commit comments

Comments
 (0)