mirror of
https://github.com/netfun2000/rttys_zhaojh329.git
synced 2026-02-27 09:53:24 +08:00
Replaced string-based keys with byte arrays for HTTP proxy in sync.Map
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
@@ -447,10 +447,13 @@ func handleFileMsg(dev *Device, data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleHttpMsg(dev *Device, data []byte) error {
|
func handleHttpMsg(dev *Device, data []byte) error {
|
||||||
addr := data[:18]
|
var saddr [18]byte
|
||||||
|
|
||||||
|
copy(saddr[:], data[:18])
|
||||||
|
|
||||||
data = data[18:]
|
data = data[18:]
|
||||||
|
|
||||||
if c, ok := dev.https.Load(string(addr)); ok {
|
if c, ok := dev.https.Load(saddr); ok {
|
||||||
c := c.(net.Conn)
|
c := c.(net.Conn)
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
c.Close()
|
c.Close()
|
||||||
|
|||||||
@@ -142,7 +142,15 @@ func doHttpProxy(srv *RttyServer, c net.Conn) {
|
|||||||
hostHeaderRewrite := ses.destaddr
|
hostHeaderRewrite := ses.destaddr
|
||||||
|
|
||||||
destAddr := genDestAddr(hostHeaderRewrite)
|
destAddr := genDestAddr(hostHeaderRewrite)
|
||||||
srcAddr := tcpAddr2Bytes(c.RemoteAddr().(*net.TCPAddr))
|
|
||||||
|
hpw := &HttpProxyWriter{
|
||||||
|
destAddr: destAddr,
|
||||||
|
hostHeaderRewrite: hostHeaderRewrite,
|
||||||
|
dev: dev,
|
||||||
|
https: ses.https,
|
||||||
|
}
|
||||||
|
|
||||||
|
tcpAddr2Bytes(c.RemoteAddr().(*net.TCPAddr), hpw.srcAddr[:])
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ses.ctx)
|
ctx, cancel := context.WithCancel(ses.ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -151,14 +159,12 @@ func doHttpProxy(srv *RttyServer, c net.Conn) {
|
|||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
c.Close()
|
c.Close()
|
||||||
log.Debug().Msgf("http proxy conn closed: %s", ses)
|
log.Debug().Msgf("http proxy conn closed: %s", ses)
|
||||||
dev.https.Delete(string(srcAddr))
|
dev.https.Delete(hpw.srcAddr)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
log.Debug().Msgf("new http proxy conn: %s", ses)
|
log.Debug().Msgf("new http proxy conn: %s", ses)
|
||||||
|
|
||||||
dev.https.Store(string(srcAddr), c)
|
dev.https.Store(hpw.srcAddr, c)
|
||||||
|
|
||||||
hpw := &HttpProxyWriter{destAddr, srcAddr, hostHeaderRewrite, dev, ses.https}
|
|
||||||
|
|
||||||
req.Host = hostHeaderRewrite
|
req.Host = hostHeaderRewrite
|
||||||
hpw.WriteRequest(req)
|
hpw.WriteRequest(req)
|
||||||
@@ -171,7 +177,7 @@ func doHttpProxy(srv *RttyServer, c net.Conn) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sendHttpReq(dev, ses.https, srcAddr, destAddr, b[:n])
|
sendHttpReq(dev, ses.https, hpw.srcAddr[:], destAddr, b[:n])
|
||||||
ses.Expire()
|
ses.Expire()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -326,14 +332,9 @@ func genDestAddr(addr string) []byte {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func tcpAddr2Bytes(addr *net.TCPAddr) []byte {
|
func tcpAddr2Bytes(addr *net.TCPAddr, b []byte) {
|
||||||
b := make([]byte, 18)
|
|
||||||
|
|
||||||
binary.BigEndian.PutUint16(b[:2], uint16(addr.Port))
|
binary.BigEndian.PutUint16(b[:2], uint16(addr.Port))
|
||||||
|
|
||||||
copy(b[2:], addr.IP)
|
copy(b[2:], addr.IP)
|
||||||
|
|
||||||
return b
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpProxyVaildAddr(addr string) (net.IP, uint16, error) {
|
func httpProxyVaildAddr(addr string) (net.IP, uint16, error) {
|
||||||
@@ -360,14 +361,14 @@ func httpProxyVaildAddr(addr string) (net.IP, uint16, error) {
|
|||||||
|
|
||||||
type HttpProxyWriter struct {
|
type HttpProxyWriter struct {
|
||||||
destAddr []byte
|
destAddr []byte
|
||||||
srcAddr []byte
|
srcAddr [18]byte
|
||||||
hostHeaderRewrite string
|
hostHeaderRewrite string
|
||||||
dev *Device
|
dev *Device
|
||||||
https bool
|
https bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw *HttpProxyWriter) Write(p []byte) (n int, err error) {
|
func (rw *HttpProxyWriter) Write(p []byte) (n int, err error) {
|
||||||
sendHttpReq(rw.dev, rw.https, rw.srcAddr, rw.destAddr, p)
|
sendHttpReq(rw.dev, rw.https, rw.srcAddr[:], rw.destAddr, p)
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user