You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gitnonymous 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env bash
  2. # See the README for instructions setting up.
  3. # ./make-anonymous-key KEYNAME
  4. # figure out which directory we are stored in
  5. # https://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
  6. _GITNONYMOUS_SRC="${BASH_SOURCE[0]}"
  7. # resolve $_GITNONYMOUS_SRC until the file is no longer a symlink
  8. while [ -h "$_GITNONYMOUS_SRC" ]; do
  9. dir="$( cd -P "$( dirname "$_GITNONYMOUS_SRC" )" && pwd )"
  10. _GITNONYMOUS_SRC="$(readlink "$_GITNONYMOUS_SRC")"
  11. # if $_GITNONYMOUS_SRC was a relative symlink, we need to resolve it
  12. # relative to the path where the symlink file was located
  13. [[ $_GITNONYMOUS_SRC != /* ]] && _GITNONYMOUS_SRC="$DIR/$_GITNONYMOUS_SRC"
  14. done
  15. dir="$( cd -P "$( dirname "$_GITNONYMOUS_SRC" )" && pwd )"
  16. unset _GITNONYMOUS_SRC
  17. gitnonymous_usage () {
  18. if [ "$1" != "" ]
  19. then
  20. gitnonymous_error "Don't call this script directly. 'Source' it instead."
  21. fi
  22. echo "Usage:"
  23. echo ". ${BASH_SOURCE[0]} KEYNAME"
  24. }
  25. # function to throw error messages
  26. gitnonymous_error () {
  27. >&2 echo $@
  28. }
  29. # when we are missing a config file call this
  30. gitnonymous_error_missing () {
  31. gitnonymous_error $@
  32. gitnonymous_error "To create it run: ${dir}/gitnonymous-setup ${keyname}"
  33. }
  34. # Make sure we were called with 'source' instead of directly
  35. if [[ "${BASH_SOURCE[0]}" != "$0" ]]
  36. then
  37. # check for the keyname parameter
  38. if [ "$1" = "" ]
  39. then
  40. gitnonymous_usage
  41. return 1
  42. else
  43. # set the key and the config file
  44. keyname="${1}"
  45. rootdir="${HOME}/.gitnonymous-${keyname}"
  46. configfile="${rootdir}/config"
  47. sshdir="${rootdir}/ssh"
  48. oldenvfile="${rootdir}/previous-environment"
  49. sshagentsocket="${sshdir}/.ssh-agent-$$"
  50. if [ "$_GITNONYMOUS" != "" ]
  51. # toggle everything off again
  52. then
  53. echo "Unsetting gitnonymous environment ${keyname}"
  54. # kill our special ssh agent
  55. ssh-agent -k
  56. # restore the command prompt
  57. export PS1="${_GITNONYMOUS_OLDPS1}"
  58. unset GIT_COMMITTER_EMAIL
  59. unset GIT_COMMITTER_NAME
  60. unset GIT_AUTHOR_EMAIL
  61. unset GIT_AUTHOR_NAME
  62. unset GIT_SSH
  63. unset GIT_PROXY_COMMAND
  64. unset ALL_PROXY
  65. unset TZ
  66. # restore any GIT_ environment variables set before
  67. . "${oldenvfile}"
  68. # unset our environment variables we set
  69. unset _GITNONYMOUS_OLDPS1
  70. unset _GITNONYMOUS
  71. echo "Done unsetting ${keyname}."
  72. else
  73. # add our custom local SSH key (make the key with ./make-anonymous-key)
  74. if [ -f ${sshdir}/id_rsa ]
  75. then
  76. # if a stale socket already exists then throw an error
  77. if [ -f "${sshagentsocket}" ]
  78. then
  79. gitnonymous_error "Stale SSH agent socket '${sshagentsocket}' exists."
  80. gitnonymous_error "You can probably delete it safely and try again."
  81. return 2
  82. fi
  83. # start a new SSH agent just for this session
  84. eval $(ssh-agent -a "${sshagentsocket}")
  85. # add the key
  86. ssh-add ${sshdir}/id_rsa
  87. else
  88. gitnonymous_error_missing "SSH config in ${sshdir} not found!"
  89. return 3
  90. fi
  91. # load the user's anonymous GIT settings and environment
  92. if [ -f "${configfile}" ]
  93. then
  94. # save the old GIT values
  95. export | grep -E "GIT_|ALL_PROXY|TZ" > ${oldenvfile}
  96. # source the config file
  97. . "${configfile}"
  98. else
  99. gitnonymous_error_missing "GIT config in ${configfile} not found!"
  100. return 4
  101. fi
  102. # trap exit of this shell and kill ssh-agent
  103. trap 'ssh-agent -k' EXIT
  104. # tell GIT to use our customised anonymous SSH wrapper
  105. export GIT_SSH="${rootdir}/git-ssh-wrap"
  106. # tell GIT to use the tor proxy for all other traffic
  107. export GIT_PROXY_COMMAND="${dir}/git-proxy-command"
  108. export ALL_PROXY="socks5://127.0.0.1:9050"
  109. # make it obvious we're in a special environment
  110. _GITNONYMOUS_OLDPS1="${PS1}"
  111. export PS1="${keyname}⚔ ${PS1}"
  112. export _GITNONYMOUS="${keyname}"
  113. fi
  114. fi
  115. else
  116. gitnonymous_usage true
  117. exit 2
  118. fi