| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Program wpr records and replays web traffic. | 5 // Program wpr records and replays web traffic. |
| 6 package main | 6 package main |
| 7 | 7 |
| 8 import ( | 8 import ( |
| 9 "crypto/tls" | 9 "crypto/tls" |
| 10 "errors" | 10 "errors" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 type ReplayCommand struct { | 76 type ReplayCommand struct { |
| 77 common CommonConfig | 77 common CommonConfig |
| 78 cmd cli.Command | 78 cmd cli.Command |
| 79 | 79 |
| 80 // Custom flags for replay. | 80 // Custom flags for replay. |
| 81 rulesFile string | 81 rulesFile string |
| 82 } | 82 } |
| 83 | 83 |
| 84 type RootCACommand struct { | 84 type RootCACommand struct { |
| 85 certConfig CertConfig | 85 certConfig CertConfig |
| 86 installer webpagereplay.Installer |
| 86 cmd cli.Command | 87 cmd cli.Command |
| 87 } | 88 } |
| 88 | 89 |
| 89 func (certCfg *CertConfig) Flags() []cli.Flag { | 90 func (certCfg *CertConfig) Flags() []cli.Flag { |
| 90 return []cli.Flag{ | 91 return []cli.Flag{ |
| 91 cli.StringFlag{ | 92 cli.StringFlag{ |
| 92 Name: "https_cert_file", | 93 Name: "https_cert_file", |
| 93 Value: "wpr_cert.pem", | 94 Value: "wpr_cert.pem", |
| 94 Usage: "File containing a PEM-encoded X509 certifi
cate to use with SSL.", | 95 Usage: "File containing a PEM-encoded X509 certifi
cate to use with SSL.", |
| 95 Destination: &certCfg.certFile, | 96 Destination: &certCfg.certFile, |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 func (r *ReplayCommand) Flags() []cli.Flag { | 185 func (r *ReplayCommand) Flags() []cli.Flag { |
| 185 return append(r.common.Flags(), | 186 return append(r.common.Flags(), |
| 186 cli.StringFlag{ | 187 cli.StringFlag{ |
| 187 Name: "rules_file", | 188 Name: "rules_file", |
| 188 Value: "", | 189 Value: "", |
| 189 Usage: "File containing rules to apply to response
s during replay", | 190 Usage: "File containing rules to apply to response
s during replay", |
| 190 Destination: &r.rulesFile, | 191 Destination: &r.rulesFile, |
| 191 }) | 192 }) |
| 192 } | 193 } |
| 193 | 194 |
| 195 func (r *RootCACommand) Flags() []cli.Flag { |
| 196 return append(r.certConfig.Flags(), |
| 197 cli.StringFlag{ |
| 198 Name: "android_device_id", |
| 199 Value: "", |
| 200 Usage: "Device id of an android device. Only relev
ant for Android", |
| 201 Destination: &r.installer.AndroidDeviceId, |
| 202 }, |
| 203 cli.StringFlag{ |
| 204 Name: "adb_binary_path", |
| 205 Value: "adb", |
| 206 Usage: "Path to adb binary. Only relevant for Andr
oid", |
| 207 Destination: &r.installer.AdbBinaryPath, |
| 208 }) |
| 209 } |
| 210 |
| 194 func getListener(port int) (net.Listener, error) { | 211 func getListener(port int) (net.Listener, error) { |
| 195 addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", port)
) | 212 addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", port)
) |
| 196 if err != nil { | 213 if err != nil { |
| 197 return nil, err | 214 return nil, err |
| 198 } | 215 } |
| 199 return net.ListenTCP("tcp", addr) | 216 return net.ListenTCP("tcp", addr) |
| 200 } | 217 } |
| 201 | 218 |
| 202 // Copied from https://golang.org/src/net/http/server.go. | 219 // Copied from https://golang.org/src/net/http/server.go. |
| 203 // This is to make dead TCP connections to eventually go away. | 220 // This is to make dead TCP connections to eventually go away. |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 httpsHandler := webpagereplay.NewReplayingProxy(archive, "https", r.comm
on.transformers) | 368 httpsHandler := webpagereplay.NewReplayingProxy(archive, "https", r.comm
on.transformers) |
| 352 tlsconfig, err := webpagereplay.ReplayTLSConfig(r.common.root_cert, arch
ive) | 369 tlsconfig, err := webpagereplay.ReplayTLSConfig(r.common.root_cert, arch
ive) |
| 353 if err != nil { | 370 if err != nil { |
| 354 fmt.Fprintf(os.Stderr, "Error creating TLSConfig: %v", err) | 371 fmt.Fprintf(os.Stderr, "Error creating TLSConfig: %v", err) |
| 355 os.Exit(1) | 372 os.Exit(1) |
| 356 } | 373 } |
| 357 startServers(tlsconfig, httpHandler, httpsHandler, &r.common) | 374 startServers(tlsconfig, httpHandler, httpsHandler, &r.common) |
| 358 } | 375 } |
| 359 | 376 |
| 360 func (r *RootCACommand) Install(c *cli.Context) { | 377 func (r *RootCACommand) Install(c *cli.Context) { |
| 361 » log.Printf("Loading cert from %v\n", r.certConfig.certFile) | 378 » if err := r.installer.InstallRoot(r.certConfig.certFile, r.certConfig.ke
yFile); err != nil { |
| 362 » log.Printf("Loading key from %v\n", r.certConfig.keyFile) | 379 » » fmt.Fprintf(os.Stderr, "Install root failed: %v", err) |
| 363 » root_cert, err := tls.LoadX509KeyPair(r.certConfig.certFile, r.certConfi
g.keyFile) | |
| 364 » if err != nil { | |
| 365 » » fmt.Fprintf(os.Stderr, "error opening cert or key files: %v", er
r) | |
| 366 os.Exit(1) | 380 os.Exit(1) |
| 367 } | 381 } |
| 368 err = webpagereplay.InstallRoot(root_cert.Certificate[0]) | |
| 369 if err != nil { | |
| 370 fmt.Fprintf(os.Stderr, "Install root failed: %v", err) | |
| 371 } | |
| 372 } | 382 } |
| 373 | 383 |
| 374 func (r *RootCACommand) Remove(c *cli.Context) { | 384 func (r *RootCACommand) Remove(c *cli.Context) { |
| 375 » webpagereplay.RemoveRoot() | 385 » r.installer.RemoveRoot() |
| 376 } | 386 } |
| 377 | 387 |
| 378 func main() { | 388 func main() { |
| 379 progName := filepath.Base(os.Args[0]) | 389 progName := filepath.Base(os.Args[0]) |
| 380 | 390 |
| 381 var record RecordCommand | 391 var record RecordCommand |
| 382 var replay ReplayCommand | 392 var replay ReplayCommand |
| 383 var installroot RootCACommand | 393 var installroot RootCACommand |
| 384 var removeroot RootCACommand | 394 var removeroot RootCACommand |
| 385 | 395 |
| 386 record.cmd = cli.Command{ | 396 record.cmd = cli.Command{ |
| 387 Name: "record", | 397 Name: "record", |
| 388 Usage: "Record web pages to an archive", | 398 Usage: "Record web pages to an archive", |
| 389 Flags: record.Flags(), | 399 Flags: record.Flags(), |
| 390 Before: record.common.CheckArgs, | 400 Before: record.common.CheckArgs, |
| 391 Action: record.Run, | 401 Action: record.Run, |
| 392 } | 402 } |
| 393 | 403 |
| 394 replay.cmd = cli.Command{ | 404 replay.cmd = cli.Command{ |
| 395 Name: "replay", | 405 Name: "replay", |
| 396 Usage: "Replay a previously-recorded web page archive", | 406 Usage: "Replay a previously-recorded web page archive", |
| 397 Flags: replay.Flags(), | 407 Flags: replay.Flags(), |
| 398 Before: replay.common.CheckArgs, | 408 Before: replay.common.CheckArgs, |
| 399 Action: replay.Run, | 409 Action: replay.Run, |
| 400 } | 410 } |
| 401 | 411 |
| 402 installroot.cmd = cli.Command{ | 412 installroot.cmd = cli.Command{ |
| 403 Name: "installroot", | 413 Name: "installroot", |
| 404 Usage: "Install a test root CA", | 414 Usage: "Install a test root CA", |
| 405 » » Flags: installroot.certConfig.Flags(), | 415 » » Flags: installroot.Flags(), |
| 406 Action: installroot.Install, | 416 Action: installroot.Install, |
| 407 } | 417 } |
| 408 | 418 |
| 409 removeroot.cmd = cli.Command{ | 419 removeroot.cmd = cli.Command{ |
| 410 Name: "removeroot", | 420 Name: "removeroot", |
| 411 Usage: "Remove a test root CA", | 421 Usage: "Remove a test root CA", |
| 422 Flags: removeroot.Flags(), |
| 412 Action: removeroot.Remove, | 423 Action: removeroot.Remove, |
| 413 } | 424 } |
| 414 | 425 |
| 415 // TODO(xunjieli): Remove ConvertorCommand once crbug.com/730036 is done
. | 426 // TODO(xunjieli): Remove ConvertorCommand once crbug.com/730036 is done
. |
| 416 type ConvertorCommand struct { | 427 type ConvertorCommand struct { |
| 417 cfg webpagereplay.ConvertorConfig | 428 cfg webpagereplay.ConvertorConfig |
| 418 cmd cli.Command | 429 cmd cli.Command |
| 419 } | 430 } |
| 420 var convert ConvertorCommand | 431 var convert ConvertorCommand |
| 421 convert.cmd = cli.Command{ | 432 convert.cmd = cli.Command{ |
| 422 Name: "convert", | 433 Name: "convert", |
| 423 Flags: convert.cfg.Flags(), | 434 Flags: convert.cfg.Flags(), |
| 424 Usage: "Convert a legacy format to the new format", | 435 Usage: "Convert a legacy format to the new format", |
| 425 Action: convert.cfg.Convert, | 436 Action: convert.cfg.Convert, |
| 426 } | 437 } |
| 427 | 438 |
| 428 app := cli.NewApp() | 439 app := cli.NewApp() |
| 429 app.Commands = []cli.Command{record.cmd, replay.cmd, installroot.cmd, re
moveroot.cmd, convert.cmd} | 440 app.Commands = []cli.Command{record.cmd, replay.cmd, installroot.cmd, re
moveroot.cmd, convert.cmd} |
| 430 app.Usage = "Web Page Replay" | 441 app.Usage = "Web Page Replay" |
| 431 app.UsageText = fmt.Sprintf(longUsage, progName, progName) | 442 app.UsageText = fmt.Sprintf(longUsage, progName, progName) |
| 432 app.HideVersion = true | 443 app.HideVersion = true |
| 433 app.Version = "" | 444 app.Version = "" |
| 434 app.Writer = os.Stderr | 445 app.Writer = os.Stderr |
| 435 app.RunAndExitOnError() | 446 app.RunAndExitOnError() |
| 436 } | 447 } |
| OLD | NEW |