Skip to content

Commit

Permalink
feat: support https proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Jul 31, 2022
1 parent 8bfd845 commit 06695ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func apiStart(br *broker) {
handleCmdReq(br, c)
})

r.Any("/web/:devid/:addr/*path", func(c *gin.Context) {
r.Any("/web/:devid/:proto/:addr/*path", func(c *gin.Context) {
httpProxyRedirect(br, c)
})

Expand Down
19 changes: 17 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,17 @@ type HttpProxyWriter struct {
hostHeaderRewrite string
br *broker
devid string
https bool
}

func (rw *HttpProxyWriter) Write(p []byte) (n int, err error) {
msg := append([]byte{}, rw.srcAddr...)
msg := []byte{0}

if rw.https {
msg[0] = 1
}

msg = append(msg, rw.srcAddr...)
msg = append(msg, rw.destAddr...)
msg = append(msg, p...)

Expand Down Expand Up @@ -123,6 +130,12 @@ func doHttpProxy(brk *broker, c net.Conn) {
}
sid := cookie.Value

https := false
cookie, _ = req.Cookie("rtty-http-proto")
if cookie != nil && cookie.Value == "https" {
https = true
}

hostHeaderRewrite := "localhost"
cookie, err = req.Cookie("rtty-http-destaddr")
if err == nil {
Expand Down Expand Up @@ -157,7 +170,7 @@ func doHttpProxy(brk *broker, c net.Conn) {
return
}

hpw := &HttpProxyWriter{destAddr, srcAddr, hostHeaderRewrite, brk, devid}
hpw := &HttpProxyWriter{destAddr, srcAddr, hostHeaderRewrite, brk, devid, https}

req.Host = hostHeaderRewrite
hpw.WriteRequest(req)
Expand Down Expand Up @@ -238,6 +251,7 @@ func httpProxyVaildAddr(addr string) (net.IP, uint16, error) {
func httpProxyRedirect(br *broker, c *gin.Context) {
cfg := br.cfg
devid := c.Param("devid")
proto := c.Param("proto")
addr := c.Param("addr")
rawPath := c.Param("path")

Expand Down Expand Up @@ -294,6 +308,7 @@ func httpProxyRedirect(br *broker, c *gin.Context) {

c.SetCookie("rtty-http-sid", sid, 0, "", "", false, true)
c.SetCookie("rtty-http-devid", devid, 0, "", "", false, true)
c.SetCookie("rtty-http-proto", proto, 0, "", "", false, true)
c.SetCookie("rtty-http-destaddr", addr, 0, "", "", false, true)

c.Redirect(http.StatusFound, location)
Expand Down
20 changes: 18 additions & 2 deletions ui/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,23 @@ export default {
}
}
});
return h('div', [input, h('p', '127.0.0.1, 127.0.0.1:8080, 127.0.0.1/test.html?a=1')]);
return h('div', [
input,
h('p', '127.0.0.1, 127.0.0.1:8080, 127.0.0.1/test.html?a=1'),
h('p', 'http://127.0.0.1, https://127.0.0.1')
]);
},
onOk: () => {
let proto = 'http';
if (addr.startsWith('http://'))
addr = addr.substring(7);
if (addr.startsWith('https://')) {
addr = addr.substring(8);
proto = 'https';
}
let [addrs, ...path] = addr.split('/');
path = '/' + path.join('/');
Expand All @@ -387,10 +401,12 @@ export default {
}
} else {
port = 80;
if (proto === 'https')
port = 443;
}
addr = encodeURIComponent(`${ip}:${port}${path}`);
window.open(`/web/${devid}/${addr}`);
window.open(`/web/${devid}/${proto}/${addr}`);
}
});
},
Expand Down

0 comments on commit 06695ba

Please sign in to comment.