diff --git a/Makefile b/Makefile index caa4eada..72a53020 100644 --- a/Makefile +++ b/Makefile @@ -324,8 +324,8 @@ rpm-server-arm64: server-linux-arm64 test-binary: CGO_ENABLED=0 go build -ldflags="-X 'github.com/enfein/mieru/pkg/log.LogPrefix=C'" -o mieru cmd/mieru/mieru.go CGO_ENABLED=0 go build -ldflags="-X 'github.com/enfein/mieru/pkg/log.LogPrefix=S'" -o mita cmd/mita/mita.go - CGO_ENABLED=0 go build -ldflags="-X 'github.com/enfein/mieru/pkg/log.LogPrefix=C2'" -o mieru2 cmd/mieru/mieru.go - CGO_ENABLED=0 go build -ldflags="-X 'github.com/enfein/mieru/pkg/log.LogPrefix=S2'" -o mita2 cmd/mita/mita.go + CGO_ENABLED=1 go build -race -ldflags="-X 'github.com/enfein/mieru/pkg/log.LogPrefix=C2'" -o mieru2 cmd/mieru/mieru.go + CGO_ENABLED=1 go build -race -ldflags="-X 'github.com/enfein/mieru/pkg/log.LogPrefix=S2'" -o mita2 cmd/mita/mita.go CGO_ENABLED=0 go build test/cmd/httpserver/httpserver.go CGO_ENABLED=0 go build test/cmd/sockshttpclient/sockshttpclient.go CGO_ENABLED=0 go build test/cmd/socksudpclient/socksudpclient.go diff --git a/pkg/appctl/server.go b/pkg/appctl/server.go index de486def..fa0fc264 100644 --- a/pkg/appctl/server.go +++ b/pkg/appctl/server.go @@ -97,13 +97,13 @@ func (s *serverLifecycleService) Start(ctx context.Context, req *pb.Empty) (*pb. if err != nil { return &pb.Empty{}, fmt.Errorf("LoadServerConfig() failed: %w", err) } + if err = ValidateFullServerConfig(config); err != nil { + return &pb.Empty{}, fmt.Errorf("ValidateFullServerConfig() failed: %w", err) + } loggingLevel := config.GetLoggingLevel().String() if loggingLevel != pb.LoggingLevel_DEFAULT.String() { log.SetLevel(loggingLevel) } - if err = ValidateFullServerConfig(config); err != nil { - return &pb.Empty{}, fmt.Errorf("ValidateFullServerConfig() failed: %w", err) - } if socks5ServerRef.Load() != nil { log.Infof("socks5 server already exist") return &pb.Empty{}, nil @@ -196,6 +196,29 @@ func (s *serverLifecycleService) Stop(ctx context.Context, req *pb.Empty) (*pb.E return &pb.Empty{}, nil } +func (s *serverLifecycleService) Reload(ctx context.Context, req *pb.Empty) (*pb.Empty, error) { + log.Infof("received start request from RPC caller") + config, err := LoadServerConfig() + if err != nil { + return &pb.Empty{}, fmt.Errorf("LoadServerConfig() failed: %w", err) + } + if err = ValidateFullServerConfig(config); err != nil { + return &pb.Empty{}, fmt.Errorf("ValidateFullServerConfig() failed: %w", err) + } + mux := serverMuxRef.Load() + // Adjust portBindings. + // Adjust users. + if mux != nil { + mux.SetServerUsers(UserListToMap(config.GetUsers())) + } + // Adjust loggingLevel. + loggingLevel := config.GetLoggingLevel().String() + if loggingLevel != pb.LoggingLevel_DEFAULT.String() { + log.SetLevel(loggingLevel) + } + return &pb.Empty{}, nil +} + func (s *serverLifecycleService) Exit(ctx context.Context, req *pb.Empty) (*pb.Empty, error) { SetAppStatus(pb.AppStatus_STOPPING) log.Infof("received exit request from RPC caller") diff --git a/pkg/protocolv2/segment.go b/pkg/protocolv2/segment.go index f7b70636..5ea7214c 100644 --- a/pkg/protocolv2/segment.go +++ b/pkg/protocolv2/segment.go @@ -240,7 +240,7 @@ func (t *segmentTree) DeleteMin() (*segment, bool) { t.mu.Lock() defer t.mu.Unlock() - if t.Len() == 0 { + if t.tr.Len() == 0 { return nil, false } seg, ok := t.tr.DeleteMin() @@ -251,7 +251,7 @@ func (t *segmentTree) DeleteMin() (*segment, bool) { panic("segmentTree.DeleteMin() return nil") } t.notFull.Broadcast() - if t.Len() > 0 { + if t.tr.Len() > 0 { t.notifyNotEmpty() } else { t.notifyEmpty() @@ -266,7 +266,7 @@ func (t *segmentTree) DeleteMinIf(si segmentIterator) (*segment, bool) { t.mu.Lock() defer t.mu.Unlock() - if t.Len() == 0 { + if t.tr.Len() == 0 { return nil, false } seg, ok := t.tr.Min() @@ -287,7 +287,7 @@ func (t *segmentTree) DeleteMinIf(si segmentIterator) (*segment, bool) { } t.notFull.Broadcast() } - if t.Len() > 0 { + if t.tr.Len() > 0 { t.notifyNotEmpty() } else { t.notifyEmpty() @@ -337,11 +337,15 @@ func (t *segmentTree) MaxSeq() (uint32, error) { // Len returns the current size of the tree. func (t *segmentTree) Len() int { + t.mu.Lock() + defer t.mu.Unlock() return t.tr.Len() } // Remaining returns the remaining space of the tree before it is full. func (t *segmentTree) Remaining() int { + t.mu.Lock() + defer t.mu.Unlock() return t.cap - t.tr.Len() } diff --git a/pkg/protocolv2/underlay_udp.go b/pkg/protocolv2/underlay_udp.go index 3aaa924b..30b33806 100644 --- a/pkg/protocolv2/underlay_udp.go +++ b/pkg/protocolv2/underlay_udp.go @@ -194,12 +194,16 @@ func (u *UDPUnderlay) RunEventLoop(ctx context.Context) error { // Close idle sessions. u.sessionMap.Range(func(k, v any) bool { session := v.(*Session) + select { + case <-session.done: + log.Debugf("Found closed %v", session) + if err := u.RemoveSession(session); err != nil { + log.Debugf("%v RemoveSession() failed: %v", u, err) + } + default: + } if time.Since(session.lastRXTime) > idleSessionTimeout { log.Debugf("Found idle %v", session) - if err := session.Close(); err != nil && !stderror.IsEOF(err) && !stderror.IsClosed(err) { - log.Debugf("%v Close() failed: %v", session, err) - } - session.wg.Wait() if err := u.RemoveSession(session); err != nil { log.Debugf("%v RemoveSession() failed: %v", u, err) }