-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathclient.go
More file actions
141 lines (123 loc) · 3.23 KB
/
Copy pathclient.go
File metadata and controls
141 lines (123 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package http
import (
std_bufio "bufio"
"context"
"encoding/base64"
"fmt"
"net"
"net/http"
"os"
"strings"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ N.Dialer = (*Client)(nil)
type Client struct {
dialer N.Dialer
serverAddr M.Socksaddr
username string
password string
path string
headers http.Header
}
type Options struct {
Dialer N.Dialer
Server M.Socksaddr
Username string
Password string
Path string
Headers http.Header
}
func NewClient(options Options) *Client {
client := &Client{
dialer: options.Dialer,
serverAddr: options.Server,
username: options.Username,
password: options.Password,
path: options.Path,
headers: options.Headers,
}
if options.Dialer == nil {
client.dialer = N.SystemDialer
}
return client
}
func (c *Client) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
network = N.NetworkName(network)
switch network {
case N.NetworkTCP:
case N.NetworkUDP:
return nil, os.ErrInvalid
default:
return nil, E.Extend(N.ErrUnknownNetwork, network)
}
var conn net.Conn
conn, err := c.dialer.DialContext(ctx, N.NetworkTCP, c.serverAddr)
if err != nil {
return nil, err
}
URL := destination.String()
HeaderString := "CONNECT " + URL + " HTTP/1.1\r\n"
tempHeaders := map[string][]string{
"Host": {destination.String()},
"User-Agent": {"Go-http-client/1.1"},
"Proxy-Connection": {"Keep-Alive"},
}
for key, valueList := range c.headers {
tempHeaders[key] = valueList
}
if c.path != "" {
tempHeaders["Path"] = []string{c.path}
}
if c.username != "" {
auth := c.username + ":" + c.password
if _, ok := tempHeaders["Proxy-Authorization"]; ok {
tempHeaders["Proxy-Authorization"][len(tempHeaders["Proxy-Authorization"])] = "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
} else {
tempHeaders["Proxy-Authorization"] = []string{"Basic " + base64.StdEncoding.EncodeToString([]byte(auth))}
}
}
for key, valueList := range tempHeaders {
HeaderString += key + ": " + strings.Join(valueList, "; ") + "\r\n"
}
HeaderString += "\r\n"
_, err = fmt.Fprintf(conn, "%s", HeaderString)
if err != nil {
conn.Close()
return nil, err
}
reader := std_bufio.NewReader(conn)
response, err := http.ReadResponse(reader, nil)
if err != nil {
conn.Close()
return nil, err
}
if response.StatusCode == http.StatusOK {
if reader.Buffered() > 0 {
buffer := buf.NewSize(reader.Buffered())
_, err = buffer.ReadFullFrom(reader, buffer.FreeLen())
if err != nil {
conn.Close()
return nil, err
}
conn = bufio.NewCachedConn(conn, buffer)
}
return conn, nil
} else {
conn.Close()
switch response.StatusCode {
case http.StatusProxyAuthRequired:
return nil, E.New("authentication required")
case http.StatusMethodNotAllowed:
return nil, E.New("method not allowed")
default:
return nil, E.New("unexpected status: ", response.Status)
}
}
}
func (c *Client) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
return nil, os.ErrInvalid
}