From 5eae540e5cae9d26f79142a4a488c5ebbf05f123 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sun, 5 Jun 2016 15:02:15 +0200 Subject: [PATCH 1/3] log access in socks and http --- proxy/http/server.go | 1 + proxy/socks/server.go | 13 +++++++++---- proxy/socks/server_udp.go | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/proxy/http/server.go b/proxy/http/server.go index f8763c2e0..81132c8fa 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -123,6 +123,7 @@ func (this *Server) handleConnection(conn *hub.Connection) { log.Warning("HTTP: Malformed proxy host (", host, "): ", err) return } + log.Access(conn.RemoteAddr(), request.URL, log.AccessAccepted, "") if strings.ToUpper(request.Method) == "CONNECT" { this.handleConnect(request, dest, reader, conn) } else { diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 2484b4fdf..6e3ac29c8 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -110,14 +110,15 @@ func (this *Server) handleConnection(connection *hub.Connection) { return } + cliendAddr := connection.RemoteAddr().String() if err != nil && err == protocol.Socks4Downgrade { - this.handleSocks4(reader, writer, auth4) + this.handleSocks4(clientAddr, reader, writer, auth4) } else { - this.handleSocks5(reader, writer, auth) + this.handleSocks5(clientAddr, reader, writer, auth) } } -func (this *Server) handleSocks5(reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks5AuthenticationRequest) error { +func (this *Server) handleSocks5(clientAddr string, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks5AuthenticationRequest) error { expectedAuthMethod := protocol.AuthNotRequired if this.config.AuthType == AuthTypePassword { expectedAuthMethod = protocol.AuthUserPass @@ -161,6 +162,7 @@ func (this *Server) handleSocks5(reader *v2io.BufferedReader, writer *v2io.Buffe } if status != byte(0) { log.Warning("Socks: Invalid user account: ", upRequest.AuthDetail()) + log.Access(clientAddr, "", log.AccessRejected, proxy.ErrorInvalidAuthentication) return proxy.ErrorInvalidAuthentication } } @@ -209,6 +211,7 @@ func (this *Server) handleSocks5(reader *v2io.BufferedReader, writer *v2io.Buffe dest := request.Destination() log.Info("Socks: TCP Connect request to ", dest) + log.Access(clientAddr, dest, log.AccessAccepted, "") this.transport(reader, writer, dest) return nil @@ -246,7 +249,7 @@ func (this *Server) handleUDP(reader io.Reader, writer *v2io.BufferedWriter) err return nil } -func (this *Server) handleSocks4(reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks4AuthenticationRequest) error { +func (this *Server) handleSocks4(clientAddr string, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks4AuthenticationRequest) error { result := protocol.Socks4RequestGranted if auth.Command == protocol.CmdBind { result = protocol.Socks4RequestRejected @@ -257,6 +260,7 @@ func (this *Server) handleSocks4(reader *v2io.BufferedReader, writer *v2io.Buffe if result == protocol.Socks4RequestRejected { log.Warning("Socks: Unsupported socks 4 command ", auth.Command) + log.Access(clientAddr, "", log.AccessRejected, ErrorUnsupportedSocksCommand) return ErrorUnsupportedSocksCommand } @@ -264,6 +268,7 @@ func (this *Server) handleSocks4(reader *v2io.BufferedReader, writer *v2io.Buffe writer.SetCached(false) dest := v2net.TCPDestination(v2net.IPAddress(auth.IP[:]), auth.Port) + log.Access(clientAddr, dest, log.AccessAccepted, "") this.transport(reader, writer, dest) return nil } diff --git a/proxy/socks/server_udp.go b/proxy/socks/server_udp.go index 57b692194..5dffb5081 100644 --- a/proxy/socks/server_udp.go +++ b/proxy/socks/server_udp.go @@ -43,6 +43,7 @@ func (this *Server) handleUDPPayload(payload *alloc.Buffer, source v2net.Destina } log.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes") + log.Access(source, request.Destination, log.AccessAccepted, "") this.udpServer.Dispatch(source, request.Destination(), request.Data, func(destination v2net.Destination, payload *alloc.Buffer) { response := &protocol.Socks5UDPRequest{ Fragment: 0, From aad10a75d373812bbebfd194e03a232d1a86769f Mon Sep 17 00:00:00 2001 From: v2ray Date: Sun, 5 Jun 2016 23:37:28 +0200 Subject: [PATCH 2/3] typo --- proxy/socks/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 6e3ac29c8..7ea444f5b 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -110,7 +110,7 @@ func (this *Server) handleConnection(connection *hub.Connection) { return } - cliendAddr := connection.RemoteAddr().String() + clientAddr := connection.RemoteAddr().String() if err != nil && err == protocol.Socks4Downgrade { this.handleSocks4(clientAddr, reader, writer, auth4) } else { From 045e1dcc5329b68fee3fc0f94b1c50d6ee303b27 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sun, 5 Jun 2016 23:39:38 +0200 Subject: [PATCH 3/3] refine dynamic strategy --- shell/point/inbound_detour_dynamic.go | 33 +++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/shell/point/inbound_detour_dynamic.go b/shell/point/inbound_detour_dynamic.go index e32500c50..f22de50ac 100644 --- a/shell/point/inbound_detour_dynamic.go +++ b/shell/point/inbound_detour_dynamic.go @@ -18,6 +18,7 @@ type InboundDetourHandlerDynamic struct { config *InboundDetourConfig portsInUse map[v2net.Port]bool ichs []proxy.InboundHandler + ich2Recyle []proxy.InboundHandler lastRefresh time.Time } @@ -74,11 +75,25 @@ func (this *InboundDetourHandlerDynamic) Close() { } } +func (this *InboundDetourHandlerDynamic) RecyleHandles() { + if this.ich2Recyle != nil { + for _, ich := range this.ich2Recyle { + if ich == nil { + continue + } + port := ich.Port() + ich.Close() + delete(this.portsInUse, port) + } + this.ich2Recyle = nil + } +} + func (this *InboundDetourHandlerDynamic) refresh() error { this.lastRefresh = time.Now() config := this.config - ich2Recycle := this.ichs + this.ich2Recyle = this.ichs newIchs := make([]proxy.InboundHandler, config.Allocation.Concurrency) for idx, _ := range newIchs { @@ -102,19 +117,6 @@ func (this *InboundDetourHandlerDynamic) refresh() error { this.ichs = newIchs this.Unlock() - go func() { - time.Sleep(time.Minute) - for _, ich := range ich2Recycle { - if ich == nil { - continue - } - port := ich.Port() - ich.Close() - delete(this.portsInUse, port) - } - ich2Recycle = nil - }() - return nil } @@ -127,7 +129,8 @@ func (this *InboundDetourHandlerDynamic) Start() error { go func() { for { - time.Sleep(time.Duration(this.config.Allocation.Refresh) * time.Minute) + time.Sleep(time.Duration(this.config.Allocation.Refresh)*time.Minute - 1) + this.RecyleHandles() err := this.refresh() if err != nil { log.Error("Point: Failed to refresh dynamic allocations: ", err)