You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-mitmproxy/proxy/websocket.go

66 lines
1.2 KiB
Go

4 years ago
package proxy
import (
"crypto/tls"
"net"
"net/http"
"net/http/httputil"
"strings"
)
// 当前仅做了转发 websocket 流量
2 years ago
type webSocket struct{}
4 years ago
2 years ago
var defaultWebSocket webSocket
4 years ago
2 years ago
func (s *webSocket) ws(conn net.Conn, host string) {
log := log.WithField("in", "webSocket.ws").WithField("host", host)
4 years ago
defer conn.Close()
remoteConn, err := net.Dial("tcp", host)
if err != nil {
2 years ago
logErr(log, err)
4 years ago
return
}
defer remoteConn.Close()
transfer(log, conn, remoteConn)
4 years ago
}
2 years ago
func (s *webSocket) wss(res http.ResponseWriter, req *http.Request) {
log := log.WithField("in", "webSocket.wss").WithField("host", req.Host)
4 years ago
upgradeBuf, err := httputil.DumpRequest(req, false)
if err != nil {
log.Errorf("DumpRequest: %v\n", err)
res.WriteHeader(502)
return
}
cconn, _, err := res.(http.Hijacker).Hijack()
if err != nil {
log.Errorf("Hijack: %v\n", err)
res.WriteHeader(502)
return
}
defer cconn.Close()
host := req.Host
if !strings.Contains(host, ":") {
host = host + ":443"
}
conn, err := tls.Dial("tcp", host, nil)
if err != nil {
log.Errorf("tls.Dial: %v\n", err)
return
}
defer conn.Close()
_, err = conn.Write(upgradeBuf)
if err != nil {
log.Errorf("wss upgrade: %v\n", err)
return
}
transfer(log, conn, cconn)
4 years ago
}