Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

gitnonymous 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if [ "$_GITNONYMOUS" != "" ]
  50. # toggle everything off again
  51. then
  52. echo "Unsetting gitnonymous environment ${keyname}"
  53. # kill our special ssh agent
  54. ssh-agent -k
  55. # restore the command prompt
  56. export PS1="${_GITNONYMOUS_OLDPS1}"
  57. unset GIT_COMMITTER_EMAIL
  58. unset GIT_COMMITTER_NAME
  59. unset GIT_AUTHOR_EMAIL
  60. unset GIT_AUTHOR_NAME
  61. unset GIT_SSH
  62. unset GIT_PROXY_COMMAND
  63. unset ALL_PROXY
  64. unset TZ
  65. # restore any GIT_ environment variables set before
  66. . "${oldenvfile}"
  67. # unset our environment variables we set
  68. unset _GITNONYMOUS_OLDPS1
  69. unset _GITNONYMOUS
  70. echo "Done unsetting ${keyname}."
  71. else
  72. # add our custom local SSH key (make the key with ./make-anonymous-key)
  73. if [ -f ${sshdir}/id_rsa ]
  74. then
  75. # start a new SSH agent just for this session
  76. eval $(ssh-agent -a "${sshdir}/.ssh-agent")
  77. # add the key
  78. ssh-add ${sshdir}/id_rsa
  79. else
  80. gitnonymous_error_missing "SSH config in ${sshdir} not found!"
  81. return 3
  82. fi
  83. # load the user's anonymous GIT settings and environment
  84. if [ -f "${configfile}" ]
  85. then
  86. # save the old GIT values
  87. export | grep -E "GIT_|ALL_PROXY|TZ" > ${oldenvfile}
  88. # source the config file
  89. . "${configfile}"
  90. else
  91. gitnonymous_error_missing "GIT config in ${configfile} not found!"
  92. return 4
  93. fi
  94. # trap exit of this shell and kill ssh-agent
  95. trap 'ssh-agent -k' EXIT
  96. # tell GIT to use our customised anonymous SSH wrapper
  97. export GIT_SSH="${rootdir}/git-ssh-wrap"
  98. # tell GIT to use the tor proxy for all other traffic
  99. export GIT_PROXY_COMMAND="${dir}/git-proxy-command"
  100. export ALL_PROXY="socks5://127.0.0.1:9050"
  101. # make it obvious we're in a special environment
  102. _GITNONYMOUS_OLDPS1="${PS1}"
  103. export PS1="${keyname}⚔ ${PS1}"
  104. export _GITNONYMOUS="${keyname}"
  105. fi
  106. fi
  107. else
  108. gitnonymous_usage true
  109. exit 2
  110. fi