-
Notifications
You must be signed in to change notification settings - Fork 1
feat: datacap direct mode (in-process sidecar replacement) #168
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
4 commits
Select commit
Hold shift + click to select a range
5e262be
feat: move datacap sidecar logic in-process for VPS direct mode
myleshorton bd306fe
fix: address PR review comments
myleshorton a5c6be3
merge: resolve conflicts with main (multi-region OCI PR #169)
myleshorton f738f3f
Merge main into feat/datacap-direct-mode
myleshorton 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,123 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials" | ||
|
|
||
| "github.com/getlantern/lantern-box/tracker/datacap" | ||
| "github.com/getlantern/lantern-box/tracker/datacap/dcpb" | ||
| ) | ||
|
|
||
| // grpcDataCapAPI implements datacap.DataCapAPI using generated gRPC types. | ||
| // The proto types in dcpb/ mirror lantern-cloud's DataCapService definition, | ||
| // keeping lantern-cloud out of lantern-box's dependency graph. | ||
| type grpcDataCapAPI struct { | ||
| client dcpb.DataCapServiceClient | ||
| } | ||
|
|
||
| func newGRPCDataCapAPI(conn grpc.ClientConnInterface) *grpcDataCapAPI { | ||
| return &grpcDataCapAPI{ | ||
| client: dcpb.NewDataCapServiceClient(conn), | ||
| } | ||
| } | ||
|
|
||
| func (g *grpcDataCapAPI) ReportUsage(ctx context.Context, batch *datacap.UsageBatch) (*datacap.ReportUsageResult, error) { | ||
| records := make([]*dcpb.DataCapUsage, 0, len(batch.Records)) | ||
| for _, r := range batch.Records { | ||
| records = append(records, &dcpb.DataCapUsage{ | ||
| DeviceId: r.DeviceID, | ||
| BytesUsed: r.BytesUsed, | ||
| CapLimit: r.CapLimit, | ||
| Platform: r.Platform, | ||
| CountryCode: r.CountryCode, | ||
| }) | ||
| } | ||
|
|
||
| resp, err := g.client.ReportUsage(ctx, &dcpb.DataCapUsageBatch{Records: records}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("gRPC ReportUsage: %w", err) | ||
| } | ||
|
|
||
| result := &datacap.ReportUsageResult{ | ||
| Results: make([]datacap.UsageResultEntry, 0, len(resp.GetResults())), | ||
| } | ||
| if resp != nil { | ||
| for _, r := range resp.Results { | ||
| result.Results = append(result.Results, datacap.UsageResultEntry{ | ||
| DeviceID: r.DeviceId, | ||
| Success: r.Success, | ||
| Error: r.Error, | ||
| }) | ||
| } | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func (g *grpcDataCapAPI) SyncDeviceState(ctx context.Context, deviceID string) (*datacap.DeviceState, error) { | ||
| resp, err := g.client.SyncDeviceState(ctx, &dcpb.DataCapSyncRequest{ | ||
| DeviceId: deviceID, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("gRPC SyncDeviceState: %w", err) | ||
| } | ||
|
|
||
| return &datacap.DeviceState{ | ||
| DeviceID: resp.DeviceId, | ||
| BytesUsed: resp.BytesUsed, | ||
| CapLimit: resp.CapLimit, | ||
| ExpiryTime: resp.ExpiryTime, | ||
| CountryCode: resp.CountryCode, | ||
| Platform: resp.Platform, | ||
| }, nil | ||
| } | ||
|
|
||
| // datacapMTLSConfig holds paths to mTLS credential files for the datacap gRPC connection. | ||
| type datacapMTLSConfig struct { | ||
| CACertPath string | ||
| ClientCertPath string | ||
| ClientKeyPath string | ||
| } | ||
|
|
||
| // newDataCapGRPCConn creates a gRPC client connection for the datacap cloud API. | ||
| // If mtls is provided, the connection uses mTLS with the given client certificate. | ||
| // Otherwise, it uses standard TLS (for local dev). | ||
| func newDataCapGRPCConn(addr string, mtls *datacapMTLSConfig) (*grpc.ClientConn, error) { | ||
| var tlsCfg tls.Config | ||
|
|
||
| if mtls != nil { | ||
| if mtls.CACertPath == "" || mtls.ClientCertPath == "" || mtls.ClientKeyPath == "" { | ||
| return nil, fmt.Errorf("datacap mTLS requires all three paths: --datacap-ca-cert, --datacap-client-cert, --datacap-client-key") | ||
| } | ||
|
|
||
| // Load client certificate | ||
| clientCert, err := tls.LoadX509KeyPair(mtls.ClientCertPath, mtls.ClientKeyPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("loading datacap client cert: %w", err) | ||
| } | ||
| tlsCfg.Certificates = []tls.Certificate{clientCert} | ||
|
|
||
| // Load CA cert to verify the server's self-signed certificate. | ||
| caCert, err := os.ReadFile(mtls.CACertPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading datacap CA cert: %w", err) | ||
| } | ||
| caPool := x509.NewCertPool() | ||
| if !caPool.AppendCertsFromPEM(caCert) { | ||
| return nil, fmt.Errorf("failed to parse datacap CA cert") | ||
| } | ||
| tlsCfg.RootCAs = caPool | ||
| } | ||
|
|
||
| creds := credentials.NewTLS(&tlsCfg) | ||
| conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(creds)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("dialing datacap gRPC server %s: %w", addr, err) | ||
| } | ||
| return conn, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package datacap | ||
|
|
||
| import "context" | ||
|
|
||
| // ReportSink is the interface for reporting datacap consumption. | ||
| // Both the HTTP Client (legacy sidecar mode) and DeviceUsageStore (direct mode) implement this. | ||
| type ReportSink interface { | ||
| Report(ctx context.Context, report *DataCapReport) (*DataCapStatus, error) | ||
| } | ||
|
|
||
| // DataCapAPI abstracts the cloud API for datacap operations. | ||
| // The concrete gRPC implementation lives in cmd/ to isolate the proto dependency. | ||
| type DataCapAPI interface { | ||
| ReportUsage(ctx context.Context, batch *UsageBatch) (*ReportUsageResult, error) | ||
| SyncDeviceState(ctx context.Context, deviceID string) (*DeviceState, error) | ||
| } | ||
|
|
||
| // UsageBatch is a batch of usage records to upload to the cloud API. | ||
| type UsageBatch struct { | ||
| Records []UsageRecord | ||
| } | ||
|
|
||
| // UsageRecord is a single device's usage to report. | ||
| type UsageRecord struct { | ||
| DeviceID string | ||
| BytesUsed int64 | ||
| CapLimit int64 | ||
| Platform string | ||
| CountryCode string | ||
| } | ||
|
|
||
| // ReportUsageResult contains per-device results from a batch upload. | ||
| type ReportUsageResult struct { | ||
| Results []UsageResultEntry | ||
| } | ||
|
|
||
| // UsageResultEntry is the result of reporting usage for a single device. | ||
| type UsageResultEntry struct { | ||
| DeviceID string | ||
| Success bool | ||
| Error string | ||
| } | ||
|
|
||
| // DeviceState is the cloud API's view of a device's datacap state. | ||
| type DeviceState struct { | ||
| DeviceID string | ||
| BytesUsed int64 | ||
| CapLimit int64 | ||
| ExpiryTime int64 | ||
| CountryCode string | ||
| Platform string | ||
| } |
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.
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.