| 195 | } |
| 196 | |
| 197 | func (p *WrapperProxy) Start() error { |
| 198 | proxy := goproxy.NewProxyHttpServer() |
| 199 | proxy.Tr = p.transport |
| 200 | // zerolog based logger also works but it will print empty lines between logs |
| 201 | proxy.Logger = log.New(&pkg_utils.ToZeroLogDebug{Logger: p.DebugLogger}, "", 0) |
| 202 | |
| 203 | for _, i := range p.interceptors { |
| 204 | proxy.OnRequest(i.GetCondition()).DoFunc(i.GetHandler()) |
| 205 | } |
| 206 | |
| 207 | proxy.OnRequest().HandleConnect(p) |
| 208 | proxy.OnResponse().DoFunc(p.handleResponse) |
| 209 | proxy.Verbose = true |
| 210 | proxyServer := &http.Server{ |
| 211 | Handler: proxy, |
| 212 | } |
| 213 | |
| 214 | p.httpServer = proxyServer |
| 215 | |
| 216 | p.DebugLogger.Print("starting proxy") |
| 217 | address := "127.0.0.1:0" |
| 218 | l, err := net.Listen("tcp", address) |
| 219 | if err != nil { |
| 220 | return err |
| 221 | } |
| 222 | |
| 223 | p.port = l.Addr().(*net.TCPAddr).Port |
| 224 | p.DebugLogger.Print("Wrapper proxy is listening on port: ", p.port) |
| 225 | |
| 226 | go func() { |
| 227 | _ = p.httpServer.Serve(l) // this blocks until the server stops and gives you an error which can be ignored |
| 228 | }() |
| 229 | |
| 230 | return nil |
| 231 | } |
| 232 | |
| 233 | func (p *WrapperProxy) Stop() { |
| 234 | err := p.httpServer.Shutdown(context.Background()) |