Skip to content
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

Add option to disable capturing network activity log #113

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func main() {
log.Fatalf("Error unmarshaling policy file content: %v", err)
}
}
proxyService := proxy.NewTransparentProxyService(p, ca, proxy.PolicyMode(*policyMode), &pl)
proxyService := proxy.NewTransparentProxyService(p, ca, proxy.PolicyMode(*policyMode), &pl, proxy.TransparentProxyServiceOpts{
SkipLogInit: false,
})
proxyService.Proxy.OnRequest().DoFunc(
func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
return proxyService.ApplyNetworkPolicy(req, ctx)
Expand Down
27 changes: 18 additions & 9 deletions pkg/proxy/proxy/transparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,40 @@ func (m PolicyMode) IsValid() bool {

// TransparentProxyService transparently proxies HTTP and HTTPS traffic.
type TransparentProxyService struct {
Proxy *goproxy.ProxyHttpServer
Ca *tls.Certificate
NetworkLog *netlog.NetworkActivityLog
Policy *policy.Policy
Mode PolicyMode
Proxy *goproxy.ProxyHttpServer
Ca *tls.Certificate
Policy *policy.Policy
Mode PolicyMode

mx *sync.Mutex
mx *sync.Mutex
networkLog *netlog.NetworkActivityLog
}

// TransparentProxyServiceOpts defines the optional parameters for creating a TransparentProxyService.
type TransparentProxyServiceOpts struct {
SkipLogInit bool
}

// NewTransparentProxyService creates a new TransparentProxyService.
func NewTransparentProxyService(p *goproxy.ProxyHttpServer, ca *tls.Certificate, mode PolicyMode, pl *policy.Policy) TransparentProxyService {
func NewTransparentProxyService(p *goproxy.ProxyHttpServer, ca *tls.Certificate, mode PolicyMode, pl *policy.Policy, opts TransparentProxyServiceOpts) TransparentProxyService {
m := new(sync.Mutex)
if !mode.IsValid() {
log.Fatalf("Invalid proxy mode specified: %v", mode)
}
if mode != DisabledMode && pl == nil {
log.Fatalf("Invalid policy: %v", pl)
}
networkLog := &netlog.NetworkActivityLog{}
if !opts.SkipLogInit {
networkLog = netlog.CaptureActivityLog(p, m)
}
return TransparentProxyService{
Proxy: p,
Ca: ca,
NetworkLog: netlog.CaptureActivityLog(p, m),
Mode: mode,
Policy: pl,
mx: m,
networkLog: networkLog,
}
}

Expand Down Expand Up @@ -194,7 +203,7 @@ func (t *TransparentProxyService) ServeAdmin(addr string) {
defer t.mx.Unlock()
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(t.NetworkLog); err != nil {
if err := enc.Encode(t.networkLog); err != nil {
log.Printf("Failed to marshal metadata: %v", err)
http.Error(w, "Internal Error", http.StatusInternalServerError)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/proxy/transparent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestPolicyEndpoint(t *testing.T) {
wantResp: http.StatusMethodNotAllowed,
},
}
proxyService := NewTransparentProxyService(NewTransparentProxyServer(false), nil, "enforce", &policy.Policy{})
proxyService := NewTransparentProxyService(NewTransparentProxyServer(false), nil, "enforce", &policy.Policy{}, TransparentProxyServiceOpts{})
policy.RegisterRule("URLMatchRule", func() policy.Rule { return &policy.URLMatchRule{} })
mux := http.NewServeMux()
mux.HandleFunc("/policy", proxyService.policyHandler)
Expand Down