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