-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ipc): add SSE status event streaming to clients in real-time #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
533f61d
feat(ipc): add SSE status event streaming and replace string status w…
garmr-ulfr d255847
fix tests
garmr-ulfr 5867baf
address pr comments
garmr-ulfr ebff68f
Merge branch 'main' into stream-status-events
garmr-ulfr d6187c1
fix test
garmr-ulfr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package ipc | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "log/slog" | ||
| "net/http" | ||
|
|
||
| "github.com/getlantern/radiance/events" | ||
| ) | ||
|
|
||
| // StatusUpdateEvent is emitted when the VPN status changes. | ||
| type StatusUpdateEvent struct { | ||
| events.Event | ||
| Status VPNStatus `json:"status"` | ||
| Error string `json:"error,omitempty"` | ||
| } | ||
|
|
||
| func (s *Server) statusEventsHandler(w http.ResponseWriter, r *http.Request) { | ||
| flusher, ok := w.(http.Flusher) | ||
| if !ok { | ||
| http.Error(w, "streaming not supported", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "text/event-stream") | ||
| w.Header().Set("Cache-Control", "no-cache") | ||
| w.Header().Set("Connection", "keep-alive") | ||
|
|
||
| ch := make(chan StatusUpdateEvent, 8) | ||
|
|
||
| // Send the current status immediately so the client doesn't have to wait for a change. | ||
| ch <- StatusUpdateEvent{Status: s.service.Status()} | ||
|
|
||
| sub := events.Subscribe(func(evt StatusUpdateEvent) { | ||
| select { | ||
| case ch <- evt: | ||
| default: // drop if client is slow | ||
| } | ||
| }) | ||
| defer sub.Unsubscribe() | ||
|
|
||
| for { | ||
| select { | ||
| case evt := <-ch: | ||
| buf, err := json.Marshal(evt) | ||
| if err != nil { | ||
| slog.Error("failed to marshal event", "error", err) | ||
| continue | ||
| } | ||
| fmt.Fprintf(w, "%s\r\n", buf) | ||
| flusher.Flush() | ||
| case <-r.Context().Done(): | ||
| slog.Debug("client disconnected") | ||
| return | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package ipc | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log/slog" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/getlantern/radiance/events" | ||
| ) | ||
|
|
||
| // StartStatusStream starts streaming status updates from the server and emits received | ||
| // [StatusUpdateEvent] events until the context is cancelled. If waitForConnect is true, it | ||
| // polls in a background goroutine until the server is reachable. When the stream is lost | ||
| // (server restart, network error, clean EOF), a [StatusUpdateEvent] with [Disconnected] status | ||
| // is emitted. The retry loop continues until a connection is established, the context is cancelled, | ||
| // or a non-recoverable error occurs (e.g. connection refused, invalid response). | ||
| func StartStatusStream(ctx context.Context, waitForConnect bool) error { | ||
| if !waitForConnect { | ||
| return startStream(ctx) | ||
| } | ||
| go func() { | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-time.After(1 * time.Second): | ||
| serverListening, err := tryDial(ctx) | ||
| if err != nil { | ||
| events.Emit(StatusUpdateEvent{ | ||
| Status: ErrorStatus, | ||
| Error: fmt.Sprintf("connection error: %v", err), | ||
| }) | ||
| return | ||
| } | ||
| if !serverListening { | ||
| continue // we started trying to connect before the server is ready | ||
| } | ||
| err = startStream(ctx) | ||
| if ctx.Err() != nil { | ||
| return | ||
| } | ||
| evt := StatusUpdateEvent{Status: Disconnected} | ||
| if err != nil { | ||
| slog.Warn("status stream disconnected", "error", err) | ||
| evt.Error = fmt.Sprintf("stream disconnected: %v", err) | ||
| } | ||
| // Stream ended cleanly (EOF) — server likely shut down. | ||
| events.Emit(evt) | ||
| return | ||
garmr-ulfr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| }() | ||
| return nil | ||
| } | ||
|
|
||
| func startStream(ctx context.Context) error { | ||
| req, err := http.NewRequestWithContext(ctx, "GET", apiURL+statusEventsEndpoint, nil) | ||
| if err != nil { | ||
| return fmt.Errorf("creating request: %w", err) | ||
| } | ||
| client := &http.Client{ | ||
| Transport: &http.Transport{ | ||
| DialContext: dialContext, | ||
| Protocols: protocols, | ||
| }, | ||
| } | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return fmt.Errorf("connecting: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return fmt.Errorf("unexpected status %s", resp.Status) | ||
| } | ||
|
|
||
| scanner := bufio.NewScanner(resp.Body) | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if line == "" { | ||
| continue | ||
| } | ||
| var evt StatusUpdateEvent | ||
| if err := json.Unmarshal([]byte(line), &evt); err != nil { | ||
| continue | ||
| } | ||
| events.Emit(evt) | ||
| } | ||
| return scanner.Err() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package ipc | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/sagernet/sing-box/experimental/clashapi" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/getlantern/radiance/events" | ||
| "github.com/getlantern/radiance/servers" | ||
| ) | ||
|
|
||
| func TestStatusEventsHandler(t *testing.T) { | ||
| svc := &mockService{status: Disconnected} | ||
| s := &Server{service: svc} | ||
|
|
||
| rec := httptest.NewRecorder() | ||
| req := httptest.NewRequest("GET", statusEventsEndpoint, nil) | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| defer close(done) | ||
| s.statusEventsHandler(rec, req) | ||
| }() | ||
|
|
||
| waitAssert := func(want StatusUpdateEvent, msg string) { | ||
| require.Eventually(t, func() bool { | ||
| return strings.Contains(rec.Body.String(), "\r\n") | ||
| }, time.Second, 10*time.Millisecond, msg) | ||
| evt := parseEventLine(t, rec.Body) | ||
| rec.Body.Reset() | ||
| assert.Equal(t, want, evt, msg) | ||
| } | ||
| waitAssert(StatusUpdateEvent{Status: Disconnected}, "initial event not received") | ||
|
|
||
| // Emit a status change and wait for it to arrive. | ||
| evt := StatusUpdateEvent{Status: Connected} | ||
| events.Emit(evt) | ||
| waitAssert(evt, "connected event not received") | ||
|
|
||
| // Emit an error status | ||
| evt = StatusUpdateEvent{Status: ErrorStatus, Error: "something went wrong"} | ||
| events.Emit(evt) | ||
| waitAssert(evt, "error event not received") | ||
| } | ||
|
|
||
| func parseEventLine(t *testing.T, body *bytes.Buffer) StatusUpdateEvent { | ||
| line, err := body.ReadBytes('\n') | ||
| require.NoError(t, err) | ||
|
|
||
| var evt StatusUpdateEvent | ||
| line = bytes.TrimSpace(line) | ||
| require.NoError(t, json.Unmarshal(line, &evt)) | ||
| return evt | ||
| } | ||
|
|
||
| type mockService struct { | ||
| status VPNStatus | ||
| } | ||
|
|
||
| func (m *mockService) Ctx() context.Context { return nil } | ||
| func (m *mockService) Status() VPNStatus { return m.status } | ||
| func (m *mockService) Start(context.Context, string) error { return nil } | ||
| func (m *mockService) Restart(context.Context, string) error { return nil } | ||
| func (m *mockService) ClashServer() *clashapi.Server { return nil } | ||
| func (m *mockService) Close() error { return nil } | ||
| func (m *mockService) UpdateOutbounds(options servers.Servers) error { return nil } | ||
| func (m *mockService) AddOutbounds(group string, options servers.Options) error { return nil } | ||
| func (m *mockService) RemoveOutbounds(group string, tags []string) error { return nil } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.