@@ -4,10 +4,11 @@ import (
4
4
std_bufio "bufio"
5
5
"context"
6
6
"encoding/base64"
7
+ "fmt"
7
8
"net"
8
9
"net/http"
9
- "net/url"
10
10
"os"
11
+ "strings"
11
12
12
13
"github.com/sagernet/sing/common/buf"
13
14
"github.com/sagernet/sing/common/bufio"
@@ -65,42 +66,52 @@ func (c *Client) DialContext(ctx context.Context, network string, destination M.
65
66
if err != nil {
66
67
return nil , err
67
68
}
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" },
82
75
}
76
+
83
77
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
88
79
}
80
+
81
+ if c .path != "" {
82
+ tempHeaders ["Path" ] = []string {c .path }
83
+ }
84
+
89
85
if c .username != "" {
90
86
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 "
92
95
}
93
- err = request .Write (conn )
96
+
97
+ HeaderString += "\r \n "
98
+
99
+ _ , err = fmt .Fprintf (conn , "%s" , HeaderString )
100
+
94
101
if err != nil {
95
102
conn .Close ()
96
103
return nil , err
97
104
}
105
+
98
106
reader := std_bufio .NewReader (conn )
99
- response , err := http .ReadResponse (reader , request )
107
+
108
+ response , err := http .ReadResponse (reader , nil )
109
+
100
110
if err != nil {
101
111
conn .Close ()
102
112
return nil , err
103
113
}
114
+
104
115
if response .StatusCode == http .StatusOK {
105
116
if reader .Buffered () > 0 {
106
117
buffer := buf .NewSize (reader .Buffered ())
0 commit comments