-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
166 lines (145 loc) · 3.11 KB
/
client.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
type Client struct {
server string
token string
userAgent string
}
func NewClient(server string, token string) *Client {
if !strings.Contains(server, "://") {
server = "http://" + server
}
return &Client{
server: server,
token: token,
}
}
// If non-empty, when connecting to the server, this User-Agent will be used
// instead of the default `Go-http-client/1.1`.
func (c *Client) SetUserAgent(userAgent string) {
c.userAgent = userAgent
}
func (c *Client) Std(to string) {
std := NewStdReadWriteCloser()
if err := c.proxy(std, to); err != nil {
log.Println(err)
}
}
func (c *Client) Serve(listen string, to string) {
lis, err := net.Listen("tcp", listen)
if err != nil {
log.Fatalln(err)
}
defer lis.Close()
for {
conn, err := lis.Accept()
if err != nil {
log.Println(err)
time.Sleep(time.Second * 5)
continue
}
go func(conn io.ReadWriteCloser) {
if err := c.proxy(conn, to); err != nil {
log.Println(err)
}
}(conn)
}
}
func (c *Client) proxy(local io.ReadWriteCloser, addr string) error {
onceCloseLocal := &OnceCloser{Closer: local}
defer onceCloseLocal.Close()
u, err := url.Parse(c.server)
if err != nil {
return err
}
host := u.Hostname()
port := u.Port()
if port == `` {
switch u.Scheme {
case `http`:
port = "80"
case `https`:
port = `443`
default:
return fmt.Errorf(`unknown scheme: %s`, u.Scheme)
}
}
serverAddr := net.JoinHostPort(host, port)
var remote net.Conn
if u.Scheme == `http` {
remote, err = net.Dial(`tcp`, serverAddr)
if err != nil {
return err
}
} else if u.Scheme == `https` {
remote, err = tls.Dial(`tcp`, serverAddr, nil)
if err != nil {
return err
}
}
if remote == nil {
return fmt.Errorf("no server connection made")
}
onceCloseRemote := &OnceCloser{Closer: remote}
defer onceCloseRemote.Close()
v := u.Query()
v.Set(`addr`, addr)
u.RawQuery = v.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return err
}
req.Header.Add(`Connection`, `upgrade`)
req.Header.Add(`Upgrade`, httpHeaderUpgrade)
req.Header.Add(`Authorization`, fmt.Sprintf(`%s %s`, authHeaderType, c.token))
if c.userAgent != `` {
req.Header.Add(`User-Agent`, c.userAgent)
}
if err := req.Write(remote); err != nil {
return err
}
bior := bufio.NewReader(remote)
resp, err := http.ReadResponse(bior, req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusSwitchingProtocols {
buf := bytes.NewBuffer(nil)
resp.Write(buf)
return fmt.Errorf("statusCode != 101:\n%s", buf.String())
}
wg := &sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
defer onceCloseRemote.Close()
_, _ = io.Copy(remote, local)
}()
go func() {
defer wg.Done()
if n := int64(bior.Buffered()); n > 0 {
if nc, err := io.CopyN(local, bior, n); err != nil || nc != n {
log.Println("io.CopyN:", nc, err)
return
}
}
defer onceCloseLocal.Close()
_, _ = io.Copy(local, remote)
}()
wg.Wait()
return nil
}