From 415ef42b5a2fd09626d1253eceb1f85dd4fe3ae2 Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Thu, 9 May 2024 09:42:14 +0800 Subject: [PATCH 1/6] add getOnline feature --- extras/trafficlogger/http.go | 47 ++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/extras/trafficlogger/http.go b/extras/trafficlogger/http.go index 428dd94cbd..077d103a4a 100644 --- a/extras/trafficlogger/http.go +++ b/extras/trafficlogger/http.go @@ -5,6 +5,7 @@ import ( "net/http" "strconv" "sync" + "time" "github.com/apernet/hysteria/core/server" ) @@ -22,17 +23,19 @@ type TrafficStatsServer interface { func NewTrafficStatsServer(secret string) TrafficStatsServer { return &trafficStatsServerImpl{ - StatsMap: make(map[string]*trafficStatsEntry), - KickMap: make(map[string]struct{}), - Secret: secret, + StatsMap: make(map[string]*trafficStatsEntry), + KickMap: make(map[string]struct{}), + OnlineMap: make(map[string]int64), + Secret: secret, } } type trafficStatsServerImpl struct { - Mutex sync.RWMutex - StatsMap map[string]*trafficStatsEntry - KickMap map[string]struct{} - Secret string + Mutex sync.RWMutex + StatsMap map[string]*trafficStatsEntry + OnlineMap map[string]int64 + KickMap map[string]struct{} + Secret string } type trafficStatsEntry struct { @@ -58,6 +61,8 @@ func (s *trafficStatsServerImpl) Log(id string, tx, rx uint64) (ok bool) { entry.Tx += tx entry.Rx += rx + s.OnlineMap[id] = time.Now().Unix() + return true } @@ -78,6 +83,10 @@ func (s *trafficStatsServerImpl) ServeHTTP(w http.ResponseWriter, r *http.Reques s.kick(w, r) return } + if r.Method == http.MethodPost && r.URL.Path == "/online" { + s.getOnline(w, r) + return + } http.NotFound(w, r) } @@ -103,6 +112,30 @@ func (s *trafficStatsServerImpl) getTraffic(w http.ResponseWriter, r *http.Reque _, _ = w.Write(jb) } +func (s *trafficStatsServerImpl) getOnline(w http.ResponseWriter, r *http.Request) { + var jb []byte + var err error + + timeNow := time.Now().Unix() + + for id, lastSeen := range s.OnlineMap { + if timeNow-lastSeen > 180 { + delete(s.OnlineMap, id) + } + } + + s.Mutex.RLock() + jb, err = json.Marshal(s.OnlineMap) + s.Mutex.RUnlock() + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json; charset=utf-8") + _, _ = w.Write(jb) +} + func (s *trafficStatsServerImpl) kick(w http.ResponseWriter, r *http.Request) { var ids []string err := json.NewDecoder(r.Body).Decode(&ids) From 2366882bd6e5872e33b8ad7d6dc075704fde7056 Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Thu, 9 May 2024 10:10:20 +0800 Subject: [PATCH 2/6] add getOnline feature --- extras/trafficlogger/http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/trafficlogger/http.go b/extras/trafficlogger/http.go index 077d103a4a..a5a8fc64a4 100644 --- a/extras/trafficlogger/http.go +++ b/extras/trafficlogger/http.go @@ -83,7 +83,7 @@ func (s *trafficStatsServerImpl) ServeHTTP(w http.ResponseWriter, r *http.Reques s.kick(w, r) return } - if r.Method == http.MethodPost && r.URL.Path == "/online" { + if r.Method == http.MethodGet && r.URL.Path == "/online" { s.getOnline(w, r) return } From 88eef7617fd7f9dc701e57c544005e2fc9ca8997 Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Thu, 9 May 2024 10:56:19 +0800 Subject: [PATCH 3/6] refactor getOnline feature --- core/server/config.go | 2 + core/server/server.go | 2 + extras/trafficlogger/http.go | 74 +++++++++++++++++++++++++++++------- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/core/server/config.go b/core/server/config.go index f647f0d960..fe64201b09 100644 --- a/core/server/config.go +++ b/core/server/config.go @@ -196,4 +196,6 @@ type EventLogger interface { // The implementation of this interface must be thread-safe. type TrafficLogger interface { Log(id string, tx, rx uint64) (ok bool) + LogOnline(id string, addr net.Addr) + LogOffline(id string, addr net.Addr) } diff --git a/core/server/server.go b/core/server/server.go index 78ab53166a..99351b09d0 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -83,6 +83,7 @@ func (s *serverImpl) handleClient(conn quic.Connection) { err := h3s.ServeQUICConn(conn) // If the client is authenticated, we need to log the disconnect event if handler.authenticated && s.config.EventLogger != nil { + s.config.TrafficLogger.LogOffline(handler.authID, conn.RemoteAddr()) s.config.EventLogger.Disconnect(conn.RemoteAddr(), handler.authID, err) } _ = conn.CloseWithError(closeErrCodeOK, "") @@ -154,6 +155,7 @@ func (h *h3sHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(protocol.StatusAuthOK) // Call event logger if h.config.EventLogger != nil { + h.config.TrafficLogger.LogOnline(id, h.conn.RemoteAddr()) h.config.EventLogger.Connect(h.conn.RemoteAddr(), id, actualTx) } // Initialize UDP session manager (if UDP is enabled) diff --git a/extras/trafficlogger/http.go b/extras/trafficlogger/http.go index a5a8fc64a4..5f46e0e2f9 100644 --- a/extras/trafficlogger/http.go +++ b/extras/trafficlogger/http.go @@ -2,12 +2,11 @@ package trafficlogger import ( "encoding/json" + "github.com/apernet/hysteria/core/server" + "net" "net/http" "strconv" "sync" - "time" - - "github.com/apernet/hysteria/core/server" ) const ( @@ -25,7 +24,7 @@ func NewTrafficStatsServer(secret string) TrafficStatsServer { return &trafficStatsServerImpl{ StatsMap: make(map[string]*trafficStatsEntry), KickMap: make(map[string]struct{}), - OnlineMap: make(map[string]int64), + OnlineMap: make(map[string]map[string]bool), Secret: secret, } } @@ -33,7 +32,7 @@ func NewTrafficStatsServer(secret string) TrafficStatsServer { type trafficStatsServerImpl struct { Mutex sync.RWMutex StatsMap map[string]*trafficStatsEntry - OnlineMap map[string]int64 + OnlineMap map[string]map[string]bool KickMap map[string]struct{} Secret string } @@ -61,11 +60,45 @@ func (s *trafficStatsServerImpl) Log(id string, tx, rx uint64) (ok bool) { entry.Tx += tx entry.Rx += rx - s.OnlineMap[id] = time.Now().Unix() - return true } +// LogOnline adds the user to the online map. +func (s *trafficStatsServerImpl) LogOnline(id string, addr net.Addr) { + s.Mutex.Lock() + defer s.Mutex.Unlock() + if _, ok := s.OnlineMap[id]; !ok { + s.OnlineMap[id] = make(map[string]bool) + } + userIp, _, err := net.SplitHostPort(addr.String()) + if err != nil { + return + } + s.OnlineMap[id][userIp] = true +} + +// LogOffline removes the user from the online map. +func (s *trafficStatsServerImpl) LogOffline(id string, addr net.Addr) { + s.Mutex.Lock() + defer s.Mutex.Unlock() + userIp, _, err := net.SplitHostPort(addr.String()) + if err != nil { + return + } + if onlineUsers, ok := s.OnlineMap[id]; ok { + if !onlineUsers[userIp] { + //if the user's ip is not in the online map, delete the whole entry + delete(s.OnlineMap, id) + return + } + delete(onlineUsers, userIp) + if len(onlineUsers) == 0 { + delete(s.OnlineMap, id) + } + } + +} + func (s *trafficStatsServerImpl) ServeHTTP(w http.ResponseWriter, r *http.Request) { if s.Secret != "" && r.Header.Get("Authorization") != s.Secret { http.Error(w, "unauthorized", http.StatusUnauthorized) @@ -116,17 +149,30 @@ func (s *trafficStatsServerImpl) getOnline(w http.ResponseWriter, r *http.Reques var jb []byte var err error - timeNow := time.Now().Unix() + bClear, _ := strconv.ParseBool(r.URL.Query().Get("clear")) + OnlineSet := make(map[string][]string) - for id, lastSeen := range s.OnlineMap { - if timeNow-lastSeen > 180 { - delete(s.OnlineMap, id) + if bClear { + s.Mutex.Lock() + for id, addrs := range s.OnlineMap { + for addr := range addrs { + OnlineSet[id] = append(OnlineSet[id], addr) + } + } + s.OnlineMap = make(map[string]map[string]bool) + s.Mutex.Unlock() + + } else { + s.Mutex.RLock() + for id, addrs := range s.OnlineMap { + for addr := range addrs { + OnlineSet[id] = append(OnlineSet[id], addr) + } } + s.Mutex.RUnlock() } - s.Mutex.RLock() - jb, err = json.Marshal(s.OnlineMap) - s.Mutex.RUnlock() + jb, err = json.Marshal(OnlineSet) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) From ba9b3cdebbb3514d4e6d94ab16065b932807e6ed Mon Sep 17 00:00:00 2001 From: Haruue Date: Sat, 11 May 2024 11:05:32 +0800 Subject: [PATCH 4/6] refactor(online): track count instead of raddr --- core/server/config.go | 3 +- core/server/server.go | 4 +- extras/trafficlogger/http.go | 73 +++++++----------------------------- 3 files changed, 17 insertions(+), 63 deletions(-) diff --git a/core/server/config.go b/core/server/config.go index fe64201b09..c630abc198 100644 --- a/core/server/config.go +++ b/core/server/config.go @@ -196,6 +196,5 @@ type EventLogger interface { // The implementation of this interface must be thread-safe. type TrafficLogger interface { Log(id string, tx, rx uint64) (ok bool) - LogOnline(id string, addr net.Addr) - LogOffline(id string, addr net.Addr) + LogOnlineStateChanged(id string, online bool) } diff --git a/core/server/server.go b/core/server/server.go index 99351b09d0..5daf36e16e 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -83,7 +83,7 @@ func (s *serverImpl) handleClient(conn quic.Connection) { err := h3s.ServeQUICConn(conn) // If the client is authenticated, we need to log the disconnect event if handler.authenticated && s.config.EventLogger != nil { - s.config.TrafficLogger.LogOffline(handler.authID, conn.RemoteAddr()) + s.config.TrafficLogger.LogOnlineStateChanged(handler.authID, false) s.config.EventLogger.Disconnect(conn.RemoteAddr(), handler.authID, err) } _ = conn.CloseWithError(closeErrCodeOK, "") @@ -155,7 +155,7 @@ func (h *h3sHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(protocol.StatusAuthOK) // Call event logger if h.config.EventLogger != nil { - h.config.TrafficLogger.LogOnline(id, h.conn.RemoteAddr()) + h.config.TrafficLogger.LogOnlineStateChanged(id, true) h.config.EventLogger.Connect(h.conn.RemoteAddr(), id, actualTx) } // Initialize UDP session manager (if UDP is enabled) diff --git a/extras/trafficlogger/http.go b/extras/trafficlogger/http.go index 5f46e0e2f9..827d64dec2 100644 --- a/extras/trafficlogger/http.go +++ b/extras/trafficlogger/http.go @@ -2,11 +2,11 @@ package trafficlogger import ( "encoding/json" - "github.com/apernet/hysteria/core/server" - "net" "net/http" "strconv" "sync" + + "github.com/apernet/hysteria/core/server" ) const ( @@ -24,7 +24,7 @@ func NewTrafficStatsServer(secret string) TrafficStatsServer { return &trafficStatsServerImpl{ StatsMap: make(map[string]*trafficStatsEntry), KickMap: make(map[string]struct{}), - OnlineMap: make(map[string]map[string]bool), + OnlineMap: make(map[string]int), Secret: secret, } } @@ -32,7 +32,7 @@ func NewTrafficStatsServer(secret string) TrafficStatsServer { type trafficStatsServerImpl struct { Mutex sync.RWMutex StatsMap map[string]*trafficStatsEntry - OnlineMap map[string]map[string]bool + OnlineMap map[string]int KickMap map[string]struct{} Secret string } @@ -63,40 +63,19 @@ func (s *trafficStatsServerImpl) Log(id string, tx, rx uint64) (ok bool) { return true } -// LogOnline adds the user to the online map. -func (s *trafficStatsServerImpl) LogOnline(id string, addr net.Addr) { +// LogOnlineStateChanged updates the online state to the online map. +func (s *trafficStatsServerImpl) LogOnlineStateChanged(id string, online bool) { s.Mutex.Lock() defer s.Mutex.Unlock() - if _, ok := s.OnlineMap[id]; !ok { - s.OnlineMap[id] = make(map[string]bool) - } - userIp, _, err := net.SplitHostPort(addr.String()) - if err != nil { - return - } - s.OnlineMap[id][userIp] = true -} -// LogOffline removes the user from the online map. -func (s *trafficStatsServerImpl) LogOffline(id string, addr net.Addr) { - s.Mutex.Lock() - defer s.Mutex.Unlock() - userIp, _, err := net.SplitHostPort(addr.String()) - if err != nil { - return - } - if onlineUsers, ok := s.OnlineMap[id]; ok { - if !onlineUsers[userIp] { - //if the user's ip is not in the online map, delete the whole entry - delete(s.OnlineMap, id) - return - } - delete(onlineUsers, userIp) - if len(onlineUsers) == 0 { + if online { + s.OnlineMap[id]++ + } else { + s.OnlineMap[id]-- + if s.OnlineMap[id] <= 0 { delete(s.OnlineMap, id) } } - } func (s *trafficStatsServerImpl) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -146,34 +125,10 @@ func (s *trafficStatsServerImpl) getTraffic(w http.ResponseWriter, r *http.Reque } func (s *trafficStatsServerImpl) getOnline(w http.ResponseWriter, r *http.Request) { - var jb []byte - var err error - - bClear, _ := strconv.ParseBool(r.URL.Query().Get("clear")) - OnlineSet := make(map[string][]string) - - if bClear { - s.Mutex.Lock() - for id, addrs := range s.OnlineMap { - for addr := range addrs { - OnlineSet[id] = append(OnlineSet[id], addr) - } - } - s.OnlineMap = make(map[string]map[string]bool) - s.Mutex.Unlock() - - } else { - s.Mutex.RLock() - for id, addrs := range s.OnlineMap { - for addr := range addrs { - OnlineSet[id] = append(OnlineSet[id], addr) - } - } - s.Mutex.RUnlock() - } - - jb, err = json.Marshal(OnlineSet) + s.Mutex.RLock() + defer s.Mutex.RUnlock() + jb, err := json.Marshal(s.OnlineMap) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return From 6a34a9e7a073bdf4b7692687f6c991768ba1a85f Mon Sep 17 00:00:00 2001 From: Haruue Date: Sat, 11 May 2024 15:07:25 +0800 Subject: [PATCH 5/6] test: fix unit tests mock files regenerated with mockery v2.43.0 --- .../mocks/mock_Authenticator.go | 6 ++- .../integration_tests/mocks/mock_Conn.go | 34 +++++++++++++++- .../mocks/mock_EventLogger.go | 2 +- .../integration_tests/mocks/mock_Outbound.go | 10 ++++- .../mocks/mock_TrafficLogger.go | 40 ++++++++++++++++++- .../integration_tests/mocks/mock_UDPConn.go | 14 ++++++- core/server/server.go | 18 ++++++--- 7 files changed, 112 insertions(+), 12 deletions(-) diff --git a/core/internal/integration_tests/mocks/mock_Authenticator.go b/core/internal/integration_tests/mocks/mock_Authenticator.go index 20329f71c0..b42c7374c7 100644 --- a/core/internal/integration_tests/mocks/mock_Authenticator.go +++ b/core/internal/integration_tests/mocks/mock_Authenticator.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package mocks @@ -25,6 +25,10 @@ func (_m *MockAuthenticator) EXPECT() *MockAuthenticator_Expecter { func (_m *MockAuthenticator) Authenticate(addr net.Addr, auth string, tx uint64) (bool, string) { ret := _m.Called(addr, auth, tx) + if len(ret) == 0 { + panic("no return value specified for Authenticate") + } + var r0 bool var r1 string if rf, ok := ret.Get(0).(func(net.Addr, string, uint64) (bool, string)); ok { diff --git a/core/internal/integration_tests/mocks/mock_Conn.go b/core/internal/integration_tests/mocks/mock_Conn.go index 6840332b6a..13e363e999 100644 --- a/core/internal/integration_tests/mocks/mock_Conn.go +++ b/core/internal/integration_tests/mocks/mock_Conn.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package mocks @@ -27,6 +27,10 @@ func (_m *MockConn) EXPECT() *MockConn_Expecter { func (_m *MockConn) Close() error { ret := _m.Called() + if len(ret) == 0 { + panic("no return value specified for Close") + } + var r0 error if rf, ok := ret.Get(0).(func() error); ok { r0 = rf() @@ -68,6 +72,10 @@ func (_c *MockConn_Close_Call) RunAndReturn(run func() error) *MockConn_Close_Ca func (_m *MockConn) LocalAddr() net.Addr { ret := _m.Called() + if len(ret) == 0 { + panic("no return value specified for LocalAddr") + } + var r0 net.Addr if rf, ok := ret.Get(0).(func() net.Addr); ok { r0 = rf() @@ -111,6 +119,10 @@ func (_c *MockConn_LocalAddr_Call) RunAndReturn(run func() net.Addr) *MockConn_L func (_m *MockConn) Read(b []byte) (int, error) { ret := _m.Called(b) + if len(ret) == 0 { + panic("no return value specified for Read") + } + var r0 int var r1 error if rf, ok := ret.Get(0).(func([]byte) (int, error)); ok { @@ -163,6 +175,10 @@ func (_c *MockConn_Read_Call) RunAndReturn(run func([]byte) (int, error)) *MockC func (_m *MockConn) RemoteAddr() net.Addr { ret := _m.Called() + if len(ret) == 0 { + panic("no return value specified for RemoteAddr") + } + var r0 net.Addr if rf, ok := ret.Get(0).(func() net.Addr); ok { r0 = rf() @@ -206,6 +222,10 @@ func (_c *MockConn_RemoteAddr_Call) RunAndReturn(run func() net.Addr) *MockConn_ func (_m *MockConn) SetDeadline(t time.Time) error { ret := _m.Called(t) + if len(ret) == 0 { + panic("no return value specified for SetDeadline") + } + var r0 error if rf, ok := ret.Get(0).(func(time.Time) error); ok { r0 = rf(t) @@ -248,6 +268,10 @@ func (_c *MockConn_SetDeadline_Call) RunAndReturn(run func(time.Time) error) *Mo func (_m *MockConn) SetReadDeadline(t time.Time) error { ret := _m.Called(t) + if len(ret) == 0 { + panic("no return value specified for SetReadDeadline") + } + var r0 error if rf, ok := ret.Get(0).(func(time.Time) error); ok { r0 = rf(t) @@ -290,6 +314,10 @@ func (_c *MockConn_SetReadDeadline_Call) RunAndReturn(run func(time.Time) error) func (_m *MockConn) SetWriteDeadline(t time.Time) error { ret := _m.Called(t) + if len(ret) == 0 { + panic("no return value specified for SetWriteDeadline") + } + var r0 error if rf, ok := ret.Get(0).(func(time.Time) error); ok { r0 = rf(t) @@ -332,6 +360,10 @@ func (_c *MockConn_SetWriteDeadline_Call) RunAndReturn(run func(time.Time) error func (_m *MockConn) Write(b []byte) (int, error) { ret := _m.Called(b) + if len(ret) == 0 { + panic("no return value specified for Write") + } + var r0 int var r1 error if rf, ok := ret.Get(0).(func([]byte) (int, error)); ok { diff --git a/core/internal/integration_tests/mocks/mock_EventLogger.go b/core/internal/integration_tests/mocks/mock_EventLogger.go index cf63773a95..c9f69207f8 100644 --- a/core/internal/integration_tests/mocks/mock_EventLogger.go +++ b/core/internal/integration_tests/mocks/mock_EventLogger.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package mocks diff --git a/core/internal/integration_tests/mocks/mock_Outbound.go b/core/internal/integration_tests/mocks/mock_Outbound.go index 32a747b63e..1a2d0c6325 100644 --- a/core/internal/integration_tests/mocks/mock_Outbound.go +++ b/core/internal/integration_tests/mocks/mock_Outbound.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package mocks @@ -27,6 +27,10 @@ func (_m *MockOutbound) EXPECT() *MockOutbound_Expecter { func (_m *MockOutbound) TCP(reqAddr string) (net.Conn, error) { ret := _m.Called(reqAddr) + if len(ret) == 0 { + panic("no return value specified for TCP") + } + var r0 net.Conn var r1 error if rf, ok := ret.Get(0).(func(string) (net.Conn, error)); ok { @@ -81,6 +85,10 @@ func (_c *MockOutbound_TCP_Call) RunAndReturn(run func(string) (net.Conn, error) func (_m *MockOutbound) UDP(reqAddr string) (server.UDPConn, error) { ret := _m.Called(reqAddr) + if len(ret) == 0 { + panic("no return value specified for UDP") + } + var r0 server.UDPConn var r1 error if rf, ok := ret.Get(0).(func(string) (server.UDPConn, error)); ok { diff --git a/core/internal/integration_tests/mocks/mock_TrafficLogger.go b/core/internal/integration_tests/mocks/mock_TrafficLogger.go index 0188ab3356..5da6aafa2a 100644 --- a/core/internal/integration_tests/mocks/mock_TrafficLogger.go +++ b/core/internal/integration_tests/mocks/mock_TrafficLogger.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package mocks @@ -21,6 +21,10 @@ func (_m *MockTrafficLogger) EXPECT() *MockTrafficLogger_Expecter { func (_m *MockTrafficLogger) Log(id string, tx uint64, rx uint64) bool { ret := _m.Called(id, tx, rx) + if len(ret) == 0 { + panic("no return value specified for Log") + } + var r0 bool if rf, ok := ret.Get(0).(func(string, uint64, uint64) bool); ok { r0 = rf(id, tx, rx) @@ -61,6 +65,40 @@ func (_c *MockTrafficLogger_Log_Call) RunAndReturn(run func(string, uint64, uint return _c } +// LogOnlineStateChanged provides a mock function with given fields: id, online +func (_m *MockTrafficLogger) LogOnlineStateChanged(id string, online bool) { + _m.Called(id, online) +} + +// MockTrafficLogger_LogOnlineStateChanged_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LogOnlineStateChanged' +type MockTrafficLogger_LogOnlineStateChanged_Call struct { + *mock.Call +} + +// LogOnlineStateChanged is a helper method to define mock.On call +// - id string +// - online bool +func (_e *MockTrafficLogger_Expecter) LogOnlineStateChanged(id interface{}, online interface{}) *MockTrafficLogger_LogOnlineStateChanged_Call { + return &MockTrafficLogger_LogOnlineStateChanged_Call{Call: _e.mock.On("LogOnlineStateChanged", id, online)} +} + +func (_c *MockTrafficLogger_LogOnlineStateChanged_Call) Run(run func(id string, online bool)) *MockTrafficLogger_LogOnlineStateChanged_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string), args[1].(bool)) + }) + return _c +} + +func (_c *MockTrafficLogger_LogOnlineStateChanged_Call) Return() *MockTrafficLogger_LogOnlineStateChanged_Call { + _c.Call.Return() + return _c +} + +func (_c *MockTrafficLogger_LogOnlineStateChanged_Call) RunAndReturn(run func(string, bool)) *MockTrafficLogger_LogOnlineStateChanged_Call { + _c.Call.Return(run) + return _c +} + // NewMockTrafficLogger creates a new instance of MockTrafficLogger. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewMockTrafficLogger(t interface { diff --git a/core/internal/integration_tests/mocks/mock_UDPConn.go b/core/internal/integration_tests/mocks/mock_UDPConn.go index 808c18b5dd..b88387086d 100644 --- a/core/internal/integration_tests/mocks/mock_UDPConn.go +++ b/core/internal/integration_tests/mocks/mock_UDPConn.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package mocks @@ -21,6 +21,10 @@ func (_m *MockUDPConn) EXPECT() *MockUDPConn_Expecter { func (_m *MockUDPConn) Close() error { ret := _m.Called() + if len(ret) == 0 { + panic("no return value specified for Close") + } + var r0 error if rf, ok := ret.Get(0).(func() error); ok { r0 = rf() @@ -62,6 +66,10 @@ func (_c *MockUDPConn_Close_Call) RunAndReturn(run func() error) *MockUDPConn_Cl func (_m *MockUDPConn) ReadFrom(b []byte) (int, string, error) { ret := _m.Called(b) + if len(ret) == 0 { + panic("no return value specified for ReadFrom") + } + var r0 int var r1 string var r2 error @@ -121,6 +129,10 @@ func (_c *MockUDPConn_ReadFrom_Call) RunAndReturn(run func([]byte) (int, string, func (_m *MockUDPConn) WriteTo(b []byte, addr string) (int, error) { ret := _m.Called(b, addr) + if len(ret) == 0 { + panic("no return value specified for WriteTo") + } + var r0 int var r1 error if rf, ok := ret.Get(0).(func([]byte, string) (int, error)); ok { diff --git a/core/server/server.go b/core/server/server.go index 5daf36e16e..74640d4f65 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -82,9 +82,13 @@ func (s *serverImpl) handleClient(conn quic.Connection) { } err := h3s.ServeQUICConn(conn) // If the client is authenticated, we need to log the disconnect event - if handler.authenticated && s.config.EventLogger != nil { - s.config.TrafficLogger.LogOnlineStateChanged(handler.authID, false) - s.config.EventLogger.Disconnect(conn.RemoteAddr(), handler.authID, err) + if handler.authenticated { + if tl := s.config.TrafficLogger; tl != nil { + tl.LogOnlineStateChanged(handler.authID, false) + } + if el := s.config.EventLogger; el != nil { + el.Disconnect(conn.RemoteAddr(), handler.authID, err) + } } _ = conn.CloseWithError(closeErrCodeOK, "") } @@ -154,9 +158,11 @@ func (h *h3sHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { }) w.WriteHeader(protocol.StatusAuthOK) // Call event logger - if h.config.EventLogger != nil { - h.config.TrafficLogger.LogOnlineStateChanged(id, true) - h.config.EventLogger.Connect(h.conn.RemoteAddr(), id, actualTx) + if tl := h.config.TrafficLogger; tl != nil { + tl.LogOnlineStateChanged(id, true) + } + if el := h.config.EventLogger; el != nil { + el.Connect(h.conn.RemoteAddr(), id, actualTx) } // Initialize UDP session manager (if UDP is enabled) // We use sync.Once to make sure that only one goroutine is started, From 9d4b3e608af2a3e539a018b222986c22a7fa3c83 Mon Sep 17 00:00:00 2001 From: Toby Date: Sat, 11 May 2024 13:55:55 -0700 Subject: [PATCH 6/6] chore: small changes to TrafficLogger function names & update all mocks to mockery v2.43.0 --- .../proxymux/internal/mocks/mock_Conn.go | 2 +- .../proxymux/internal/mocks/mock_Listener.go | 2 +- core/client/mock_udpIO.go | 10 ++- .../mocks/mock_TrafficLogger.go | 84 +++++++++---------- .../integration_tests/trafficlogger_test.go | 16 ++-- core/server/config.go | 4 +- core/server/copy.go | 4 +- core/server/mock_UDPConn.go | 14 +++- core/server/mock_udpEventLogger.go | 2 +- core/server/mock_udpIO.go | 14 +++- core/server/server.go | 8 +- extras/outbounds/mock_PluggableOutbound.go | 10 ++- extras/outbounds/mock_UDPConn.go | 14 +++- extras/trafficlogger/http.go | 4 +- 14 files changed, 122 insertions(+), 66 deletions(-) diff --git a/app/internal/proxymux/internal/mocks/mock_Conn.go b/app/internal/proxymux/internal/mocks/mock_Conn.go index 0bcbe657d7..13e363e999 100644 --- a/app/internal/proxymux/internal/mocks/mock_Conn.go +++ b/app/internal/proxymux/internal/mocks/mock_Conn.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.42.2. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package mocks diff --git a/app/internal/proxymux/internal/mocks/mock_Listener.go b/app/internal/proxymux/internal/mocks/mock_Listener.go index e4ca2f46cd..842b88f2d8 100644 --- a/app/internal/proxymux/internal/mocks/mock_Listener.go +++ b/app/internal/proxymux/internal/mocks/mock_Listener.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.42.2. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package mocks diff --git a/core/client/mock_udpIO.go b/core/client/mock_udpIO.go index 6e0b1bab5b..a85e006d46 100644 --- a/core/client/mock_udpIO.go +++ b/core/client/mock_udpIO.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package client @@ -24,6 +24,10 @@ func (_m *mockUDPIO) EXPECT() *mockUDPIO_Expecter { func (_m *mockUDPIO) ReceiveMessage() (*protocol.UDPMessage, error) { ret := _m.Called() + if len(ret) == 0 { + panic("no return value specified for ReceiveMessage") + } + var r0 *protocol.UDPMessage var r1 error if rf, ok := ret.Get(0).(func() (*protocol.UDPMessage, error)); ok { @@ -77,6 +81,10 @@ func (_c *mockUDPIO_ReceiveMessage_Call) RunAndReturn(run func() (*protocol.UDPM func (_m *mockUDPIO) SendMessage(_a0 []byte, _a1 *protocol.UDPMessage) error { ret := _m.Called(_a0, _a1) + if len(ret) == 0 { + panic("no return value specified for SendMessage") + } + var r0 error if rf, ok := ret.Get(0).(func([]byte, *protocol.UDPMessage) error); ok { r0 = rf(_a0, _a1) diff --git a/core/internal/integration_tests/mocks/mock_TrafficLogger.go b/core/internal/integration_tests/mocks/mock_TrafficLogger.go index 5da6aafa2a..9de44b976e 100644 --- a/core/internal/integration_tests/mocks/mock_TrafficLogger.go +++ b/core/internal/integration_tests/mocks/mock_TrafficLogger.go @@ -17,84 +17,84 @@ func (_m *MockTrafficLogger) EXPECT() *MockTrafficLogger_Expecter { return &MockTrafficLogger_Expecter{mock: &_m.Mock} } -// Log provides a mock function with given fields: id, tx, rx -func (_m *MockTrafficLogger) Log(id string, tx uint64, rx uint64) bool { - ret := _m.Called(id, tx, rx) - - if len(ret) == 0 { - panic("no return value specified for Log") - } - - var r0 bool - if rf, ok := ret.Get(0).(func(string, uint64, uint64) bool); ok { - r0 = rf(id, tx, rx) - } else { - r0 = ret.Get(0).(bool) - } - - return r0 +// LogOnlineState provides a mock function with given fields: id, online +func (_m *MockTrafficLogger) LogOnlineState(id string, online bool) { + _m.Called(id, online) } -// MockTrafficLogger_Log_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Log' -type MockTrafficLogger_Log_Call struct { +// MockTrafficLogger_LogOnlineState_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LogOnlineState' +type MockTrafficLogger_LogOnlineState_Call struct { *mock.Call } -// Log is a helper method to define mock.On call +// LogOnlineState is a helper method to define mock.On call // - id string -// - tx uint64 -// - rx uint64 -func (_e *MockTrafficLogger_Expecter) Log(id interface{}, tx interface{}, rx interface{}) *MockTrafficLogger_Log_Call { - return &MockTrafficLogger_Log_Call{Call: _e.mock.On("Log", id, tx, rx)} +// - online bool +func (_e *MockTrafficLogger_Expecter) LogOnlineState(id interface{}, online interface{}) *MockTrafficLogger_LogOnlineState_Call { + return &MockTrafficLogger_LogOnlineState_Call{Call: _e.mock.On("LogOnlineState", id, online)} } -func (_c *MockTrafficLogger_Log_Call) Run(run func(id string, tx uint64, rx uint64)) *MockTrafficLogger_Log_Call { +func (_c *MockTrafficLogger_LogOnlineState_Call) Run(run func(id string, online bool)) *MockTrafficLogger_LogOnlineState_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].(uint64), args[2].(uint64)) + run(args[0].(string), args[1].(bool)) }) return _c } -func (_c *MockTrafficLogger_Log_Call) Return(ok bool) *MockTrafficLogger_Log_Call { - _c.Call.Return(ok) +func (_c *MockTrafficLogger_LogOnlineState_Call) Return() *MockTrafficLogger_LogOnlineState_Call { + _c.Call.Return() return _c } -func (_c *MockTrafficLogger_Log_Call) RunAndReturn(run func(string, uint64, uint64) bool) *MockTrafficLogger_Log_Call { +func (_c *MockTrafficLogger_LogOnlineState_Call) RunAndReturn(run func(string, bool)) *MockTrafficLogger_LogOnlineState_Call { _c.Call.Return(run) return _c } -// LogOnlineStateChanged provides a mock function with given fields: id, online -func (_m *MockTrafficLogger) LogOnlineStateChanged(id string, online bool) { - _m.Called(id, online) +// LogTraffic provides a mock function with given fields: id, tx, rx +func (_m *MockTrafficLogger) LogTraffic(id string, tx uint64, rx uint64) bool { + ret := _m.Called(id, tx, rx) + + if len(ret) == 0 { + panic("no return value specified for LogTraffic") + } + + var r0 bool + if rf, ok := ret.Get(0).(func(string, uint64, uint64) bool); ok { + r0 = rf(id, tx, rx) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 } -// MockTrafficLogger_LogOnlineStateChanged_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LogOnlineStateChanged' -type MockTrafficLogger_LogOnlineStateChanged_Call struct { +// MockTrafficLogger_LogTraffic_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LogTraffic' +type MockTrafficLogger_LogTraffic_Call struct { *mock.Call } -// LogOnlineStateChanged is a helper method to define mock.On call +// LogTraffic is a helper method to define mock.On call // - id string -// - online bool -func (_e *MockTrafficLogger_Expecter) LogOnlineStateChanged(id interface{}, online interface{}) *MockTrafficLogger_LogOnlineStateChanged_Call { - return &MockTrafficLogger_LogOnlineStateChanged_Call{Call: _e.mock.On("LogOnlineStateChanged", id, online)} +// - tx uint64 +// - rx uint64 +func (_e *MockTrafficLogger_Expecter) LogTraffic(id interface{}, tx interface{}, rx interface{}) *MockTrafficLogger_LogTraffic_Call { + return &MockTrafficLogger_LogTraffic_Call{Call: _e.mock.On("LogTraffic", id, tx, rx)} } -func (_c *MockTrafficLogger_LogOnlineStateChanged_Call) Run(run func(id string, online bool)) *MockTrafficLogger_LogOnlineStateChanged_Call { +func (_c *MockTrafficLogger_LogTraffic_Call) Run(run func(id string, tx uint64, rx uint64)) *MockTrafficLogger_LogTraffic_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].(bool)) + run(args[0].(string), args[1].(uint64), args[2].(uint64)) }) return _c } -func (_c *MockTrafficLogger_LogOnlineStateChanged_Call) Return() *MockTrafficLogger_LogOnlineStateChanged_Call { - _c.Call.Return() +func (_c *MockTrafficLogger_LogTraffic_Call) Return(ok bool) *MockTrafficLogger_LogTraffic_Call { + _c.Call.Return(ok) return _c } -func (_c *MockTrafficLogger_LogOnlineStateChanged_Call) RunAndReturn(run func(string, bool)) *MockTrafficLogger_LogOnlineStateChanged_Call { +func (_c *MockTrafficLogger_LogTraffic_Call) RunAndReturn(run func(string, uint64, uint64) bool) *MockTrafficLogger_LogTraffic_Call { _c.Call.Return(run) return _c } diff --git a/core/internal/integration_tests/trafficlogger_test.go b/core/internal/integration_tests/trafficlogger_test.go index abbea8cdd3..690a348d72 100644 --- a/core/internal/integration_tests/trafficlogger_test.go +++ b/core/internal/integration_tests/trafficlogger_test.go @@ -36,6 +36,7 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) { go s.Serve() // Create client + trafficLogger.EXPECT().LogOnlineState("nobody", true).Return().Once() c, _, err := client.NewClient(&client.Config{ ServerAddr: udpAddr, TLSConfig: client.TLSConfig{InsecureSkipVerify: true}, @@ -66,7 +67,7 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) { assert.NoError(t, err) // Client reads from server - trafficLogger.EXPECT().Log("nobody", uint64(0), uint64(11)).Return(true).Once() + trafficLogger.EXPECT().LogTraffic("nobody", uint64(0), uint64(11)).Return(true).Once() sobConnCh <- []byte("knock knock") buf := make([]byte, 100) n, err := conn.Read(buf) @@ -75,7 +76,7 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) { assert.Equal(t, "knock knock", string(buf[:n])) // Client writes to server - trafficLogger.EXPECT().Log("nobody", uint64(12), uint64(0)).Return(true).Once() + trafficLogger.EXPECT().LogTraffic("nobody", uint64(12), uint64(0)).Return(true).Once() sobConn.EXPECT().Write([]byte("who is there")).Return(12, nil).Once() n, err = conn.Write([]byte("who is there")) assert.NoError(t, err) @@ -83,7 +84,8 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) { time.Sleep(1 * time.Second) // Need some time for the server to receive the data // Client reads from server again but blocked - trafficLogger.EXPECT().Log("nobody", uint64(0), uint64(4)).Return(false).Once() + trafficLogger.EXPECT().LogTraffic("nobody", uint64(0), uint64(4)).Return(false).Once() + trafficLogger.EXPECT().LogOnlineState("nobody", false).Return().Once() sobConnCh <- []byte("nope") n, err = conn.Read(buf) assert.Zero(t, n) @@ -116,6 +118,7 @@ func TestClientServerTrafficLoggerUDP(t *testing.T) { go s.Serve() // Create client + trafficLogger.EXPECT().LogOnlineState("nobody", true).Return().Once() c, _, err := client.NewClient(&client.Config{ ServerAddr: udpAddr, TLSConfig: client.TLSConfig{InsecureSkipVerify: true}, @@ -146,14 +149,14 @@ func TestClientServerTrafficLoggerUDP(t *testing.T) { assert.NoError(t, err) // Client writes to server - trafficLogger.EXPECT().Log("nobody", uint64(9), uint64(0)).Return(true).Once() + trafficLogger.EXPECT().LogTraffic("nobody", uint64(9), uint64(0)).Return(true).Once() sobConn.EXPECT().WriteTo([]byte("small sad"), addr).Return(9, nil).Once() err = conn.Send([]byte("small sad"), addr) assert.NoError(t, err) time.Sleep(1 * time.Second) // Need some time for the server to receive the data // Client reads from server - trafficLogger.EXPECT().Log("nobody", uint64(0), uint64(7)).Return(true).Once() + trafficLogger.EXPECT().LogTraffic("nobody", uint64(0), uint64(7)).Return(true).Once() sobConnCh <- []byte("big mad") bs, rAddr, err := conn.Receive() assert.NoError(t, err) @@ -161,7 +164,8 @@ func TestClientServerTrafficLoggerUDP(t *testing.T) { assert.Equal(t, "big mad", string(bs)) // Client reads from server again but blocked - trafficLogger.EXPECT().Log("nobody", uint64(0), uint64(4)).Return(false).Once() + trafficLogger.EXPECT().LogTraffic("nobody", uint64(0), uint64(4)).Return(false).Once() + trafficLogger.EXPECT().LogOnlineState("nobody", false).Return().Once() sobConnCh <- []byte("nope") bs, rAddr, err = conn.Receive() assert.Equal(t, err, io.EOF) diff --git a/core/server/config.go b/core/server/config.go index c630abc198..8d5f5dbc05 100644 --- a/core/server/config.go +++ b/core/server/config.go @@ -195,6 +195,6 @@ type EventLogger interface { // bandwidth limits or post-connection authentication, for example. // The implementation of this interface must be thread-safe. type TrafficLogger interface { - Log(id string, tx, rx uint64) (ok bool) - LogOnlineStateChanged(id string, online bool) + LogTraffic(id string, tx, rx uint64) (ok bool) + LogOnlineState(id string, online bool) } diff --git a/core/server/copy.go b/core/server/copy.go index 25831e5d34..d55dcefe3f 100644 --- a/core/server/copy.go +++ b/core/server/copy.go @@ -35,12 +35,12 @@ func copyTwoWayWithLogger(id string, serverRw, remoteRw io.ReadWriter, l Traffic errChan := make(chan error, 2) go func() { errChan <- copyBufferLog(serverRw, remoteRw, func(n uint64) bool { - return l.Log(id, 0, n) + return l.LogTraffic(id, 0, n) }) }() go func() { errChan <- copyBufferLog(remoteRw, serverRw, func(n uint64) bool { - return l.Log(id, n, 0) + return l.LogTraffic(id, n, 0) }) }() // Block until one of the two goroutines returns diff --git a/core/server/mock_UDPConn.go b/core/server/mock_UDPConn.go index 34a34ee900..7299975353 100644 --- a/core/server/mock_UDPConn.go +++ b/core/server/mock_UDPConn.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package server @@ -21,6 +21,10 @@ func (_m *mockUDPConn) EXPECT() *mockUDPConn_Expecter { func (_m *mockUDPConn) Close() error { ret := _m.Called() + if len(ret) == 0 { + panic("no return value specified for Close") + } + var r0 error if rf, ok := ret.Get(0).(func() error); ok { r0 = rf() @@ -62,6 +66,10 @@ func (_c *mockUDPConn_Close_Call) RunAndReturn(run func() error) *mockUDPConn_Cl func (_m *mockUDPConn) ReadFrom(b []byte) (int, string, error) { ret := _m.Called(b) + if len(ret) == 0 { + panic("no return value specified for ReadFrom") + } + var r0 int var r1 string var r2 error @@ -121,6 +129,10 @@ func (_c *mockUDPConn_ReadFrom_Call) RunAndReturn(run func([]byte) (int, string, func (_m *mockUDPConn) WriteTo(b []byte, addr string) (int, error) { ret := _m.Called(b, addr) + if len(ret) == 0 { + panic("no return value specified for WriteTo") + } + var r0 int var r1 error if rf, ok := ret.Get(0).(func([]byte, string) (int, error)); ok { diff --git a/core/server/mock_udpEventLogger.go b/core/server/mock_udpEventLogger.go index 08f3ff8d86..5a54b0ba6a 100644 --- a/core/server/mock_udpEventLogger.go +++ b/core/server/mock_udpEventLogger.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package server diff --git a/core/server/mock_udpIO.go b/core/server/mock_udpIO.go index ddd44b3abe..6df862b5f6 100644 --- a/core/server/mock_udpIO.go +++ b/core/server/mock_udpIO.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package server @@ -24,6 +24,10 @@ func (_m *mockUDPIO) EXPECT() *mockUDPIO_Expecter { func (_m *mockUDPIO) ReceiveMessage() (*protocol.UDPMessage, error) { ret := _m.Called() + if len(ret) == 0 { + panic("no return value specified for ReceiveMessage") + } + var r0 *protocol.UDPMessage var r1 error if rf, ok := ret.Get(0).(func() (*protocol.UDPMessage, error)); ok { @@ -77,6 +81,10 @@ func (_c *mockUDPIO_ReceiveMessage_Call) RunAndReturn(run func() (*protocol.UDPM func (_m *mockUDPIO) SendMessage(_a0 []byte, _a1 *protocol.UDPMessage) error { ret := _m.Called(_a0, _a1) + if len(ret) == 0 { + panic("no return value specified for SendMessage") + } + var r0 error if rf, ok := ret.Get(0).(func([]byte, *protocol.UDPMessage) error); ok { r0 = rf(_a0, _a1) @@ -120,6 +128,10 @@ func (_c *mockUDPIO_SendMessage_Call) RunAndReturn(run func([]byte, *protocol.UD func (_m *mockUDPIO) UDP(reqAddr string) (UDPConn, error) { ret := _m.Called(reqAddr) + if len(ret) == 0 { + panic("no return value specified for UDP") + } + var r0 UDPConn var r1 error if rf, ok := ret.Get(0).(func(string) (UDPConn, error)); ok { diff --git a/core/server/server.go b/core/server/server.go index 74640d4f65..d38097dc70 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -84,7 +84,7 @@ func (s *serverImpl) handleClient(conn quic.Connection) { // If the client is authenticated, we need to log the disconnect event if handler.authenticated { if tl := s.config.TrafficLogger; tl != nil { - tl.LogOnlineStateChanged(handler.authID, false) + tl.LogOnlineState(handler.authID, false) } if el := s.config.EventLogger; el != nil { el.Disconnect(conn.RemoteAddr(), handler.authID, err) @@ -159,7 +159,7 @@ func (h *h3sHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(protocol.StatusAuthOK) // Call event logger if tl := h.config.TrafficLogger; tl != nil { - tl.LogOnlineStateChanged(id, true) + tl.LogOnlineState(id, true) } if el := h.config.EventLogger; el != nil { el.Connect(h.conn.RemoteAddr(), id, actualTx) @@ -276,7 +276,7 @@ func (io *udpIOImpl) ReceiveMessage() (*protocol.UDPMessage, error) { continue } if io.TrafficLogger != nil { - ok := io.TrafficLogger.Log(io.AuthID, uint64(len(udpMsg.Data)), 0) + ok := io.TrafficLogger.LogTraffic(io.AuthID, uint64(len(udpMsg.Data)), 0) if !ok { // TrafficLogger requested to disconnect the client _ = io.Conn.CloseWithError(closeErrCodeTrafficLimitReached, "") @@ -289,7 +289,7 @@ func (io *udpIOImpl) ReceiveMessage() (*protocol.UDPMessage, error) { func (io *udpIOImpl) SendMessage(buf []byte, msg *protocol.UDPMessage) error { if io.TrafficLogger != nil { - ok := io.TrafficLogger.Log(io.AuthID, 0, uint64(len(msg.Data))) + ok := io.TrafficLogger.LogTraffic(io.AuthID, 0, uint64(len(msg.Data))) if !ok { // TrafficLogger requested to disconnect the client _ = io.Conn.CloseWithError(closeErrCodeTrafficLimitReached, "") diff --git a/extras/outbounds/mock_PluggableOutbound.go b/extras/outbounds/mock_PluggableOutbound.go index c8620f2530..79b38e4295 100644 --- a/extras/outbounds/mock_PluggableOutbound.go +++ b/extras/outbounds/mock_PluggableOutbound.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package outbounds @@ -25,6 +25,10 @@ func (_m *mockPluggableOutbound) EXPECT() *mockPluggableOutbound_Expecter { func (_m *mockPluggableOutbound) TCP(reqAddr *AddrEx) (net.Conn, error) { ret := _m.Called(reqAddr) + if len(ret) == 0 { + panic("no return value specified for TCP") + } + var r0 net.Conn var r1 error if rf, ok := ret.Get(0).(func(*AddrEx) (net.Conn, error)); ok { @@ -79,6 +83,10 @@ func (_c *mockPluggableOutbound_TCP_Call) RunAndReturn(run func(*AddrEx) (net.Co func (_m *mockPluggableOutbound) UDP(reqAddr *AddrEx) (UDPConn, error) { ret := _m.Called(reqAddr) + if len(ret) == 0 { + panic("no return value specified for UDP") + } + var r0 UDPConn var r1 error if rf, ok := ret.Get(0).(func(*AddrEx) (UDPConn, error)); ok { diff --git a/extras/outbounds/mock_UDPConn.go b/extras/outbounds/mock_UDPConn.go index e71e1da065..d450322a4f 100644 --- a/extras/outbounds/mock_UDPConn.go +++ b/extras/outbounds/mock_UDPConn.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. +// Code generated by mockery v2.43.0. DO NOT EDIT. package outbounds @@ -21,6 +21,10 @@ func (_m *mockUDPConn) EXPECT() *mockUDPConn_Expecter { func (_m *mockUDPConn) Close() error { ret := _m.Called() + if len(ret) == 0 { + panic("no return value specified for Close") + } + var r0 error if rf, ok := ret.Get(0).(func() error); ok { r0 = rf() @@ -62,6 +66,10 @@ func (_c *mockUDPConn_Close_Call) RunAndReturn(run func() error) *mockUDPConn_Cl func (_m *mockUDPConn) ReadFrom(b []byte) (int, *AddrEx, error) { ret := _m.Called(b) + if len(ret) == 0 { + panic("no return value specified for ReadFrom") + } + var r0 int var r1 *AddrEx var r2 error @@ -123,6 +131,10 @@ func (_c *mockUDPConn_ReadFrom_Call) RunAndReturn(run func([]byte) (int, *AddrEx func (_m *mockUDPConn) WriteTo(b []byte, addr *AddrEx) (int, error) { ret := _m.Called(b, addr) + if len(ret) == 0 { + panic("no return value specified for WriteTo") + } + var r0 int var r1 error if rf, ok := ret.Get(0).(func([]byte, *AddrEx) (int, error)); ok { diff --git a/extras/trafficlogger/http.go b/extras/trafficlogger/http.go index 827d64dec2..d0ec0a4cee 100644 --- a/extras/trafficlogger/http.go +++ b/extras/trafficlogger/http.go @@ -42,7 +42,7 @@ type trafficStatsEntry struct { Rx uint64 `json:"rx"` } -func (s *trafficStatsServerImpl) Log(id string, tx, rx uint64) (ok bool) { +func (s *trafficStatsServerImpl) LogTraffic(id string, tx, rx uint64) (ok bool) { s.Mutex.Lock() defer s.Mutex.Unlock() @@ -64,7 +64,7 @@ func (s *trafficStatsServerImpl) Log(id string, tx, rx uint64) (ok bool) { } // LogOnlineStateChanged updates the online state to the online map. -func (s *trafficStatsServerImpl) LogOnlineStateChanged(id string, online bool) { +func (s *trafficStatsServerImpl) LogOnlineState(id string, online bool) { s.Mutex.Lock() defer s.Mutex.Unlock()