Browse Source

hope fix a bug

pull/15/head
Jason 5 years ago
parent
commit
2ab3475783
  1. 4
      common/stats/session/session.go
  2. 9
      proxy/socks/tcp.go
  3. 12
      proxy/socks/udp.go

4
common/stats/session/session.go

@ -128,9 +128,9 @@ func (s *simpleSessionStater) RemoveSession(key interface{}) {
s.completedSessions = s.completedSessions[1:]
}
s.mux.Unlock()
s.sessions.Delete(key)
} else {
log.Warnf("cannot found session key: [%v] ", key)
}
// delete
s.sessions.Delete(key)
}

9
proxy/socks/tcp.go

@ -1,7 +1,6 @@
package socks
import (
"fmt"
"io"
"net"
"strconv"
@ -20,8 +19,6 @@ import (
type tcpHandler struct {
sync.Mutex
sessKey string
proxyHost string
proxyPort uint16
@ -112,7 +109,7 @@ func (h *tcpHandler) relay(localConn, remoteConn net.Conn, sess *stats.Session)
<-upCh // Wait for uplink done.
if h.sessionStater != nil {
h.sessionStater.RemoveSession(h.sessKey)
h.sessionStater.RemoveSession(localConn)
}
}
@ -157,8 +154,7 @@ func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error {
DownloadBytes: 0,
SessionStart: time.Now(),
}
h.sessKey = fmt.Sprintf("%s:%s", localConn.LocalAddr().Network(), localConn.LocalAddr().String())
h.sessionStater.AddSession(h.sessKey, sess)
h.sessionStater.AddSession(localConn, sess)
}
// set keepalive
@ -169,7 +165,6 @@ func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error {
go h.relay(localConn, remoteConn, sess)
log.Access(process, "proxy", target.Network(), localConn.LocalAddr().String(), targetAddr)
return nil
}

12
proxy/socks/udp.go

@ -20,11 +20,10 @@ type udpHandler struct {
sync.Mutex
closed bool
sessKey string
timeout time.Duration
proxyHost string
proxyPort uint16
timeout time.Duration
udpConns map[core.UDPConn]net.PacketConn
tcpConns map[core.UDPConn]net.Conn
@ -88,7 +87,7 @@ func (h *udpHandler) fetchUDPInput(conn core.UDPConn, input net.PacketConn) {
}
n, err = conn.WriteFrom(buf[int(3+len(addr)):n], resolvedAddr)
if n > 0 && h.sessionStater != nil {
if sess := h.sessionStater.GetSession(h.sessKey); sess != nil {
if sess := h.sessionStater.GetSession(conn); sess != nil {
sess.AddDownloadBytes(int64(n))
}
}
@ -196,8 +195,7 @@ func (h *udpHandler) connectInternal(conn core.UDPConn, targetAddr string) error
DownloadBytes: 0,
SessionStart: time.Now(),
}
h.sessKey = fmt.Sprintf("%s:%s", conn.LocalAddr().Network(), conn.LocalAddr().String())
h.sessionStater.AddSession(h.sessKey, sess)
h.sessionStater.AddSession(conn, sess)
}
log.Access(process, "proxy", "udp", conn.LocalAddr().String(), targetAddr)
}
@ -224,7 +222,7 @@ func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr
buf = append(buf, data[:]...)
n, err := remoteUDPConn.WriteTo(buf, remoteAddr)
if n > 0 && h.sessionStater != nil {
if sess := h.sessionStater.GetSession(h.sessKey); sess != nil {
if sess := h.sessionStater.GetSession(conn); sess != nil {
sess.AddUploadBytes(int64(n))
}
}
@ -260,7 +258,7 @@ func (h *udpHandler) Close(conn core.UDPConn) {
delete(h.remoteAddrs, conn)
if h.sessionStater != nil {
h.sessionStater.RemoveSession(h.sessKey)
h.sessionStater.RemoveSession(conn)
}
h.closed = true

Loading…
Cancel
Save