Skip to content

Commit 8690075

Browse files
authored
Merge pull request #616 from fatedier/dev
bump version to v0.15.1
2 parents 14733dd + 33d8816 commit 8690075

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+6885
-19
lines changed

Diff for: Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ ci:
4444
go test -v ./tests/...
4545
cd ./tests && ./clean_test.sh && cd -
4646

47+
ciclean:
48+
cd ./tests && ./clean_test.sh && cd -
49+
4750
alltest: gotest ci
4851

4952
clean:

Diff for: client/proxy.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,11 @@ func HandleTcpWorkConnection(localInfo *config.LocalSvrConf, proxyPlugin plugin.
412412
err error
413413
)
414414
remote = workConn
415-
defer remote.Close()
415+
416416
if baseInfo.UseEncryption {
417417
remote, err = frpIo.WithEncryption(remote, encKey)
418418
if err != nil {
419+
workConn.Close()
419420
workConn.Error("create encryption stream error: %v", err)
420421
return
421422
}
@@ -433,6 +434,7 @@ func HandleTcpWorkConnection(localInfo *config.LocalSvrConf, proxyPlugin plugin.
433434
} else {
434435
localConn, err := frpNet.ConnectServer("tcp", fmt.Sprintf("%s:%d", localInfo.LocalIp, localInfo.LocalPort))
435436
if err != nil {
437+
workConn.Close()
436438
workConn.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIp, localInfo.LocalPort, err)
437439
return
438440
}

Diff for: glide.lock

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: glide.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ import:
7373
- ipv4
7474
- package: github.com/rodaine/table
7575
version: v1.0.0
76+
- package: github.com/gorilla/websocket

Diff for: server/ports.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
const (
12-
MinPort = 1025
12+
MinPort = 1
1313
MaxPort = 65535
1414
MaxPortReservedDuration = time.Duration(24) * time.Hour
1515
CleanReservedPortsInterval = time.Hour

Diff for: tests/conf/auto_test_frpc.ini

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[common]
2-
server_addr = 0.0.0.0
2+
server_addr = 127.0.0.1
33
server_port = 10700
44
log_file = ./frpc.log
55
# debug, info, warn, error
@@ -103,6 +103,18 @@ use_compression = true
103103
http_user = test
104104
http_user = test
105105

106+
[subhost01]
107+
type = http
108+
local_ip = 127.0.0.1
109+
local_port = 10704
110+
subdomain = test01
111+
112+
[subhost02]
113+
type = http
114+
local_ip = 127.0.0.1
115+
local_port = 10704
116+
subdomain = test02
117+
106118
[tcp_port_not_allowed]
107119
type = tcp
108120
local_ip = 127.0.0.1
@@ -144,3 +156,8 @@ type = udp
144156
local_ip = 127.0.0.1
145157
local_port = 10702
146158
remote_port = 0
159+
160+
[http_proxy]
161+
type = tcp
162+
plugin = http_proxy
163+
remote_port = 0

Diff for: tests/conf/auto_test_frps.ini

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ log_file = ./frps.log
66
log_level = debug
77
privilege_token = 123456
88
privilege_allow_ports = 10000-20000,20002,30000-40000
9+
subdomain_host = sub.com

Diff for: tests/func_test.go

+68-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package tests
22

33
import (
44
"fmt"
5+
"net/url"
56
"strings"
67
"testing"
78
"time"
89

10+
"github.com/gorilla/websocket"
911
"github.com/stretchr/testify/assert"
1012

1113
"github.com/fatedier/frp/client"
1214
"github.com/fatedier/frp/server"
15+
"github.com/fatedier/frp/utils/net"
1316
)
1417

1518
var (
@@ -50,6 +53,8 @@ var (
5053
ProxyUdpPortNotAllowed string = "udp_port_not_allowed"
5154
ProxyUdpPortNormal string = "udp_port_normal"
5255
ProxyUdpRandomPort string = "udp_random_port"
56+
57+
ProxyHttpProxy string = "http_proxy"
5358
)
5459

5560
func init() {
@@ -120,57 +125,87 @@ func TestStcp(t *testing.T) {
120125
func TestHttp(t *testing.T) {
121126
assert := assert.New(t)
122127
// web01
123-
code, body, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "", nil)
128+
code, body, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "", nil, "")
124129
if assert.NoError(err) {
125130
assert.Equal(200, code)
126131
assert.Equal(TEST_HTTP_NORMAL_STR, body)
127132
}
128133

129134
// web02
130-
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test2.frp.com", nil)
135+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test2.frp.com", nil, "")
131136
if assert.NoError(err) {
132137
assert.Equal(200, code)
133138
assert.Equal(TEST_HTTP_NORMAL_STR, body)
134139
}
135140

136141
// error host header
137-
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil)
142+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil, "")
138143
if assert.NoError(err) {
139144
assert.Equal(404, code)
140145
}
141146

142147
// web03
143-
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test3.frp.com", nil)
148+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
144149
if assert.NoError(err) {
145150
assert.Equal(200, code)
146151
assert.Equal(TEST_HTTP_NORMAL_STR, body)
147152
}
148153

149-
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", TEST_HTTP_FRP_PORT), "test3.frp.com", nil)
154+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
150155
if assert.NoError(err) {
151156
assert.Equal(200, code)
152157
assert.Equal(TEST_HTTP_FOO_STR, body)
153158
}
154159

155160
// web04
156-
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", TEST_HTTP_FRP_PORT), "test3.frp.com", nil)
161+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
157162
if assert.NoError(err) {
158163
assert.Equal(200, code)
159164
assert.Equal(TEST_HTTP_BAR_STR, body)
160165
}
161166

162167
// web05
163-
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", nil)
168+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", nil, "")
164169
if assert.NoError(err) {
165170
assert.Equal(401, code)
166171
}
167172

168173
header := make(map[string]string)
169174
header["Authorization"] = basicAuth("test", "test")
170-
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", header)
175+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", header, "")
171176
if assert.NoError(err) {
172177
assert.Equal(401, code)
173178
}
179+
180+
// subhost01
181+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test01.sub.com", nil, "")
182+
if assert.NoError(err) {
183+
assert.Equal(200, code)
184+
assert.Equal("test01.sub.com", body)
185+
}
186+
187+
// subhost02
188+
code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test02.sub.com", nil, "")
189+
if assert.NoError(err) {
190+
assert.Equal(200, code)
191+
assert.Equal("test02.sub.com", body)
192+
}
193+
}
194+
195+
func TestWebSocket(t *testing.T) {
196+
assert := assert.New(t)
197+
198+
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", "127.0.0.1", TEST_HTTP_FRP_PORT), Path: "/ws"}
199+
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
200+
assert.NoError(err)
201+
defer c.Close()
202+
203+
err = c.WriteMessage(websocket.TextMessage, []byte(TEST_HTTP_NORMAL_STR))
204+
assert.NoError(err)
205+
206+
_, msg, err := c.ReadMessage()
207+
assert.NoError(err)
208+
assert.Equal(TEST_HTTP_NORMAL_STR, string(msg))
174209
}
175210

176211
func TestPrivilegeAllowPorts(t *testing.T) {
@@ -226,3 +261,28 @@ func TestRandomPort(t *testing.T) {
226261
assert.Equal(TEST_UDP_ECHO_STR, res)
227262
}
228263
}
264+
265+
func TestPluginHttpProxy(t *testing.T) {
266+
assert := assert.New(t)
267+
status, err := getProxyStatus(ProxyHttpProxy)
268+
if assert.NoError(err) {
269+
assert.Equal(client.ProxyStatusRunning, status.Status)
270+
271+
// http proxy
272+
addr := status.RemoteAddr
273+
code, body, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT),
274+
"", nil, "http://"+addr)
275+
if assert.NoError(err) {
276+
assert.Equal(200, code)
277+
assert.Equal(TEST_HTTP_NORMAL_STR, body)
278+
}
279+
280+
// connect method
281+
conn, err := net.ConnectTcpServerByHttpProxy("http://"+addr, fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT))
282+
if assert.NoError(err) {
283+
res, err := sendTcpMsgByConn(conn, TEST_TCP_ECHO_STR)
284+
assert.NoError(err)
285+
assert.Equal(TEST_TCP_ECHO_STR, res)
286+
}
287+
}
288+
}

Diff for: tests/http_server.go

+41-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,55 @@ package tests
22

33
import (
44
"fmt"
5+
"log"
56
"net/http"
7+
"regexp"
68
"strings"
9+
10+
"github.com/gorilla/websocket"
711
)
812

13+
var upgrader = websocket.Upgrader{}
14+
915
func StartHttpServer() {
10-
http.HandleFunc("/", request)
16+
http.HandleFunc("/", handleHttp)
17+
http.HandleFunc("/ws", handleWebSocket)
1118
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", TEST_HTTP_PORT), nil)
1219
}
1320

14-
func request(w http.ResponseWriter, r *http.Request) {
21+
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
22+
c, err := upgrader.Upgrade(w, r, nil)
23+
if err != nil {
24+
log.Print("upgrade:", err)
25+
return
26+
}
27+
defer c.Close()
28+
for {
29+
mt, message, err := c.ReadMessage()
30+
if err != nil {
31+
break
32+
}
33+
err = c.WriteMessage(mt, message)
34+
if err != nil {
35+
log.Println("write:", err)
36+
break
37+
}
38+
}
39+
}
40+
41+
func handleHttp(w http.ResponseWriter, r *http.Request) {
42+
match, err := regexp.Match(`.*\.sub\.com`, []byte(r.Host))
43+
if err != nil {
44+
w.WriteHeader(500)
45+
return
46+
}
47+
48+
if match {
49+
w.WriteHeader(200)
50+
w.Write([]byte(r.Host))
51+
return
52+
}
53+
1554
if strings.Contains(r.Host, "127.0.0.1") || strings.Contains(r.Host, "test2.frp.com") ||
1655
strings.Contains(r.Host, "test5.frp.com") {
1756
w.WriteHeader(200)

Diff for: tests/util.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"net"
1010
"net/http"
11+
"net/url"
1112
"strings"
1213
"time"
1314

@@ -81,7 +82,10 @@ func sendTcpMsg(addr string, msg string) (res string, err error) {
8182
return
8283
}
8384
defer c.Close()
85+
return sendTcpMsgByConn(c, msg)
86+
}
8487

88+
func sendTcpMsgByConn(c net.Conn, msg string) (res string, err error) {
8589
timer := time.Now().Add(5 * time.Second)
8690
c.SetDeadline(timer)
8791
c.Write([]byte(msg))
@@ -122,8 +126,8 @@ func sendUdpMsg(addr string, msg string) (res string, err error) {
122126
return string(buf[:n]), nil
123127
}
124128

125-
func sendHttpMsg(method, url string, host string, header map[string]string) (code int, body string, err error) {
126-
req, errRet := http.NewRequest(method, url, nil)
129+
func sendHttpMsg(method, urlStr string, host string, header map[string]string, proxy string) (code int, body string, err error) {
130+
req, errRet := http.NewRequest(method, urlStr, nil)
127131
if errRet != nil {
128132
err = errRet
129133
return
@@ -135,7 +139,29 @@ func sendHttpMsg(method, url string, host string, header map[string]string) (cod
135139
for k, v := range header {
136140
req.Header.Set(k, v)
137141
}
138-
resp, errRet := http.DefaultClient.Do(req)
142+
143+
tr := &http.Transport{
144+
DialContext: (&net.Dialer{
145+
Timeout: 30 * time.Second,
146+
KeepAlive: 30 * time.Second,
147+
DualStack: true,
148+
}).DialContext,
149+
MaxIdleConns: 100,
150+
IdleConnTimeout: 90 * time.Second,
151+
TLSHandshakeTimeout: 10 * time.Second,
152+
ExpectContinueTimeout: 1 * time.Second,
153+
}
154+
155+
if len(proxy) != 0 {
156+
tr.Proxy = func(req *http.Request) (*url.URL, error) {
157+
return url.Parse(proxy)
158+
}
159+
}
160+
client := http.Client{
161+
Transport: tr,
162+
}
163+
164+
resp, errRet := client.Do(req)
139165
if errRet != nil {
140166
err = errRet
141167
return

Diff for: utils/version/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"strings"
2020
)
2121

22-
var version string = "0.15.0"
22+
var version string = "0.15.1"
2323

2424
func Full() string {
2525
return version

Diff for: utils/vhost/newhttp.go

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ func NewHttpReverseProxy() *HttpReverseProxy {
7979
return rp.CreateConnection(host, url)
8080
},
8181
},
82+
WebSocketDialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
83+
url := ctx.Value("url").(string)
84+
host := getHostFromAddr(ctx.Value("host").(string))
85+
return rp.CreateConnection(host, url)
86+
},
8287
BufferPool: newWrapPool(),
8388
ErrorLog: log.New(newWrapLogger(), "", 0),
8489
}

0 commit comments

Comments
 (0)