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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. unalias "gitnonymous-exit"
  72. echo "Done unsetting ${keyname}."
  73. else
  74. # add our custom local SSH key (make the key with ./make-anonymous-key)
  75. if [ -f ${sshdir}/id_rsa -o -f ${sshdir}/identity ]
  76. then
  77. # if a stale socket already exists then throw an error
  78. if [ -f "${sshagentsocket}" ]
  79. then
  80. gitnonymous_error "Stale SSH agent socket '${sshagentsocket}' exists."
  81. gitnonymous_error "You can probably delete it safely and try again."
  82. return 2
  83. fi
  84. # start a new SSH agent just for this session
  85. eval $(ssh-agent -a "${sshagentsocket}")
  86. # add the key
  87. if [ -f ${sshdir}/id_rsa ]
  88. then
  89. ssh-add ${sshdir}/id_rsa
  90. else
  91. ssh-add ${sshdir}/identity
  92. fi
  93. else
  94. gitnonymous_error_missing "SSH config in ${sshdir} not found!"
  95. return 3
  96. fi
  97. # load the user's anonymous GIT settings and environment
  98. if [ -f "${configfile}" ]
  99. then
  100. # save the old GIT values
  101. export | grep -E "GIT_|ALL_PROXY|TZ" > ${oldenvfile}
  102. # source the config file
  103. . "${configfile}"
  104. else
  105. gitnonymous_error_missing "GIT config in ${configfile} not found!"
  106. return 4
  107. fi
  108. # trap exit of this shell and kill ssh-agent
  109. trap 'ssh-agent -k' EXIT
  110. # tell GIT to use our customised anonymous SSH wrapper
  111. export GIT_SSH="${rootdir}/git-ssh-wrap"
  112. # tell GIT to use the tor proxy for all other traffic
  113. export GIT_PROXY_COMMAND="${dir}/git-proxy-command"
  114. export ALL_PROXY="socks5://127.0.0.1:9050"
  115. # make it obvious we're in a special environment
  116. _GITNONYMOUS_OLDPS1="${PS1}"
  117. export PS1="${keyname}⚔ ${PS1}"
  118. export _GITNONYMOUS="${keyname}"
  119. echo "Run 'gitnonymous-exit' to exit this identity."
  120. # alias exit command
  121. alias "gitnonymous-exit"=". ${BASH_SOURCE[0]} ${keyname}"
  122. fi
  123. fi
  124. else
  125. gitnonymous_usage true
  126. exit 2
  127. fi