main.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The git-remote-persistent-https binary speeds up SSL operations by running
  15. // a daemon job that keeps a connection open to a Git server. This ensures the
  16. // git-remote-persistent-https--proxy is running and delegating execution
  17. // to the git-remote-http binary with the http_proxy set to the daemon job.
  18. // A unix socket is used to authenticate the proxy and discover the
  19. // HTTP address. Note, both the client and proxy are included in the same
  20. // binary.
  21. package main
  22. import (
  23. "flag"
  24. "fmt"
  25. "log"
  26. "os"
  27. "strings"
  28. "time"
  29. )
  30. var (
  31. forceProxy = flag.Bool("proxy", false, "Whether to start the binary in proxy mode")
  32. proxyBin = flag.String("proxy_bin", "git-remote-persistent-https--proxy", "Path to the proxy binary")
  33. printLabel = flag.Bool("print_label", false, "Prints the build label for the binary")
  34. // Variable that should be defined through the -X linker flag.
  35. _BUILD_EMBED_LABEL string
  36. )
  37. const (
  38. defaultMaxIdleDuration = 24 * time.Hour
  39. defaultPollUpdateInterval = 15 * time.Minute
  40. )
  41. func main() {
  42. flag.Parse()
  43. if *printLabel {
  44. // Short circuit execution to print the build label
  45. fmt.Println(buildLabel())
  46. return
  47. }
  48. var err error
  49. if *forceProxy || strings.HasSuffix(os.Args[0], "--proxy") {
  50. log.SetPrefix("git-remote-persistent-https--proxy: ")
  51. proxy := &Proxy{
  52. BuildLabel: buildLabel(),
  53. MaxIdleDuration: defaultMaxIdleDuration,
  54. PollUpdateInterval: defaultPollUpdateInterval,
  55. }
  56. err = proxy.Run()
  57. } else {
  58. log.SetPrefix("git-remote-persistent-https: ")
  59. client := &Client{
  60. ProxyBin: *proxyBin,
  61. Args: flag.Args(),
  62. }
  63. err = client.Run()
  64. }
  65. if err != nil {
  66. log.Fatalln(err)
  67. }
  68. }
  69. func buildLabel() string {
  70. if _BUILD_EMBED_LABEL == "" {
  71. log.Println(`unlabeled build; build with "make" to label`)
  72. }
  73. return _BUILD_EMBED_LABEL
  74. }