├── README.md └── proxc /README.md: -------------------------------------------------------------------------------- 1 | # proxc 2 | 3 | 1. per-process DNS configuration on Linux 4 | 2. per-process proxy configuration on Linux 5 | 6 | ## Configure DNS for Linux command execution 7 | 8 | ``` 9 | proxc -d [-d ] -c 10 | ``` 11 | 12 | Execute command with the DNS you specify. 13 | 14 | It uses bubblewrap to create a mount namespace, in which processes see the DNS you want in a fake `/etc/resolv.conf`. 15 | 16 | It bans in the namespace `/var/run/nscd` so processes can't use system's DNS cache 17 | 18 | > Now it requires bubblewrap >= 0.11.0 . If you're using older bubblewrap, please use old proxc 1.0 19 | 20 | ## Configure proxy for Linux command execution 21 | 22 | ``` 23 | proxc -p socks5|socks4|http -c 24 | ``` 25 | 26 | Execute command with `proxychains` applied without having to edit `proxychains.conf` . Useful when you just want to run some app temporarily behind a single proxy. 27 | 28 | It generates and uses a `proxychains.conf` in memory. 29 | 30 | > **Notice:** proxychains doesn't ensure process must go through the proxy. There can be leaks. 31 | 32 | ## Configure both DNS & proxy 33 | 34 | proxc [-d ] [-p socks5|socks4|http ] -c 35 | -------------------------------------------------------------------------------- /proxc: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | usage() { 4 | ############################################################################### 5 | cat << EOF 6 | Usage: $PROGNAME [options] [-d ] [-p ] -c 7 | 8 | -h, --help Show this help 9 | 10 | -c Command to run 11 | 12 | -d Tell processes what DNS to see in /etc/resolv.conf 13 | (using bubblewrap). Can be used more than once. 14 | Usually not necessary when '-p' is used. 15 | 16 | -p 17 | Use proxychains to make processes use proxy. 18 | proxy-type can be socks5,socks4,http 19 | 20 | -q Quiet mode. Don't display any debug infomation 21 | EOF 22 | } 23 | 24 | get_fd() { 25 | local x 26 | local tmpfile 27 | local _fd_var=$1 28 | 29 | for x in $(seq 10 $(ulimit -n)); do 30 | if [[ ! -a "/proc/$BASHPID/fd/$x" ]]; then 31 | tmpfile=$(mktemp /dev/shm/XXX.tmp) 32 | exec {x}<>$tmpfile 33 | printf -v "$_fd_var" %s "$x" 34 | rm $tmpfile 35 | return 36 | fi 37 | done 38 | echo 0 39 | } 40 | 41 | DNS_IP=() 42 | 43 | [[ ! "$1" ]] && usage && exit 0 44 | while [[ -n "$1" ]]; do 45 | case "$1" in 46 | -h|--help) 47 | usage 48 | exit 0 49 | ;; 50 | -c) 51 | shift 52 | for i in "$@" 53 | do 54 | [[ "$i" =~ " " || "$i" =~ '\' ]] && i="\"$i\"" 55 | #echo $i 56 | CMD="$CMD $i" 57 | done 58 | break 59 | ;; 60 | -p) 61 | shift 62 | PROXY_TYPE="$1" 63 | shift 64 | PROXY_IP="$1" 65 | shift 66 | PROXY_PORT="$1" 67 | shift 68 | ;; 69 | -d) 70 | shift 71 | DNS_IP+=("$1") 72 | shift 73 | ;; 74 | -q) 75 | shift 76 | QUIET_MODE=1 77 | ;; 78 | *) 79 | echo "Invalid parameter: $1" 1>&2 80 | exit 1 81 | ;; 82 | esac 83 | done 84 | 85 | if [ ! "$CMD" ] ; then 86 | echo "Command not specified" 87 | exit 1 88 | fi 89 | 90 | ################################################ 91 | 92 | if [[ ! $QUIET_MODE ]]; then 93 | echo "Apply DNS/proxy to command: $CMD" 94 | echo "" 95 | fi 96 | 97 | ################################ 98 | 99 | get_fd initscript_fd 100 | 101 | 102 | 103 | bwrap_args=() 104 | 105 | if [[ ${#DNS_IP[@]} -gt 0 ]]; then 106 | if [[ ! $QUIET_MODE ]]; then 107 | echo -n "Set DNS:" 108 | fi 109 | get_fd dnsconf 110 | for (( i=0;i<${#DNS_IP[@]};i++ )); do 111 | if [[ ! $QUIET_MODE ]]; then 112 | echo -n " ${DNS_IP[$i]}" 113 | fi 114 | 115 | echo "nameserver ${DNS_IP[$i]}" >> "/dev/fd/$dnsconf" 116 | done 117 | echo "" 118 | bwrap_args+=( 119 | --dev-bind / / 120 | 121 | --overlay-src /etc 122 | --tmp-overlay /etc 123 | 124 | --tmpfs $(readlink -m /var/run/nscd/) 125 | 126 | -- 127 | ) 128 | cat << EOF >> /dev/fd/$initscript_fd 129 | rm /etc/resolv.conf 130 | cat /dev/fd/$dnsconf > /etc/resolv.conf 131 | 132 | EOF 133 | BWRAP_WHOLE="bwrap ${bwrap_args[@]}" 134 | 135 | fi 136 | 137 | 138 | if [[ "$PROXY_IP" ]]; then 139 | PROXYCHAINS_BIN="proxychains4" 140 | if [[ $QUIET_MODE -eq 1 ]]; then 141 | shift 142 | PROXYCHAINS_QUIET="-q" 143 | else 144 | echo "Use proxy ${PROXY_TYPE}://${PROXY_IP}:${PROXY_PORT}" 145 | echo "" 146 | fi 147 | 148 | get_fd conffile 149 | cat << EOF >&$conffile 150 | 151 | # proxychains.conf VER 4.x 152 | strict_chain 153 | proxy_dns 154 | #remote_dns_subnet 127 155 | #remote_dns_subnet 10 156 | remote_dns_subnet 224 157 | # Some timeouts in milliseconds 158 | tcp_read_time_out 15000 159 | tcp_connect_time_out 8000 160 | 161 | localnet 127.0.0.0/255.0.0.0 162 | # localnet 10.0.0.0/255.0.0.0 163 | # localnet 172.16.0.0/255.240.0.0 164 | # localnet 192.168.0.0/255.255.0.0 165 | 166 | [ProxyList] 167 | $PROXY_TYPE $PROXY_IP $PROXY_PORT 168 | 169 | EOF 170 | 171 | PROXYCHAINS_WHOLE="\"$PROXYCHAINS_BIN\" $PROXYCHAINS_QUIET -f /dev/fd/$conffile" 172 | fi 173 | 174 | 175 | 176 | 177 | $BWRAP_WHOLE sh -c "sh /dev/fd/$initscript_fd && $PROXYCHAINS_WHOLE $CMD" 178 | 179 | result=$? 180 | 181 | exit $result 182 | --------------------------------------------------------------------------------