Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
support add socks5 to tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
frimin committed Sep 9, 2017
1 parent ce75bc2 commit c0ca765
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## ?
## 0.1.0b3#3 - 0.1.0b4#4

* 添加执行 SSH 远程指令时通过临时文件分发标准输入流的内容到每个主机上。
* 添加 socks5 转发的创建支持

## 0.1.0b2#2 - 0.1.0b3#3

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ SSH 配置支持基于匹配模式的主机名和 **match** 字段,一个主

$ ser tunnel-add <隧道名> <主机名> local 443 443
$ ser tunnel-add <隧道名> <主机名> remote 22 8000
$ ser tunnel-add <隧道名> <主机名> socks5 1080

则会生成如下的隧道启动指令:

Expand Down
47 changes: 27 additions & 20 deletions ser
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SER_LAST_SCRIPT_URL="${SER_LAST_SCRIPT_URL:-https://frimin.com/update/ser/last/s

## General options

VERSION='0.1.0b3#3'
VERSION='0.1.0b4#4'

SER_BASENAME="$(basename "$0")"
SER_PATH="$(cd "$(dirname "$0")" && pwd)/$SER_BASENAME"
Expand All @@ -31,7 +31,7 @@ REGEXP_PATTERN_HOSTNAME='^.*[\.\*!,].*$'

## Functions

error () { echo "$1" >&2; exit 2; }
error () { echo "$1" >&2; exit 255; }
trim () { while read -r line; do echo "$line"; done; }

help () {
Expand Down Expand Up @@ -77,7 +77,8 @@ tokens:
{name} - config name";;
tunnel-add|add)
echo "Usage: $SER_BASENAME tunnel-add <tunnel_name> <host> local [bind_address:]<bind_port> [host:]<host_port>
$SER_BASENAME tunnel-add <tunnel_name> <host> remote [bind_address:]<bind_port> [host>:]<host_port>";;
$SER_BASENAME tunnel-add <tunnel_name> <host> remote [bind_address:]<bind_port> [host>:]<host_port>
$SER_BASENAME tunnel-add <tunnel_name> <host> socks5 [bind_address:]<bind_port>";;
tunnel-remove|rm)
echo "Usage: $SER_BASENAME tunnel-remove <tunnel_name> [forward_index]" ;;
tunnel-start|start)
Expand Down Expand Up @@ -463,7 +464,7 @@ tunnel_connect() {
'-o' "ControlPath=$SER_TUNNELS_PATH/${TUNNEL}.sock"
'-o' 'ExitOnForwardFailure=yes'
'-o' 'ConnectTimeout=30'
'-o' 'ServerAliveInterval=30'
'-o' 'ServerAliveInterval=60'
'-o' 'ServerAliveCountMax=3'
'-o' 'BatchMode=yes'
)
Expand All @@ -481,6 +482,7 @@ tunnel_connect() {
case "${FORWARD[1]}" in
local) ssh_options+=('-L' "${FORWARD[2]}:${FORWARD[3]}") ;;
remote) ssh_options+=('-R' "${FORWARD[2]}:${FORWARD[3]}") ;;
socks5) ssh_options+=('-D' "${FORWARD[2]}") ;;
*) error "invalid forward type: '${FORWARD[*]}'" ;;
esac
done
Expand Down Expand Up @@ -522,8 +524,7 @@ read_forward_address() {
tunnel_add() {
[[ -z "$1" ]] && error "please enter a tunnel name"
[[ -z "$2" ]] && error "please enter a host"
[[ -z "$3" ]] && error "please enter type name (local/remote)"
[[ -z "$4" || -z "$5" ]] && error "please enter local and remote forward address"
[[ -z "$3" ]] && error "please enter type name (local/remote/socks5)"

if [[ ! "$1" =~ $REGEXP_TUNNEL_NAME ]]; then
error "illegal tunnel name: $1"
Expand All @@ -533,28 +534,33 @@ tunnel_add() {

local target_host

while next_host;
do
if next_host; then
target_host="${HOST[0]}"
break
done
else
error "host '$2' not exists"
fi

if [[ -z "$target_host" ]]; then
error "host not found: $2"
fi
local bind_addr;
local read_addr;
local forward_content="$target_host $3 $bind_addr $read_addr"

case "$3" in
local|remote) ;;
local|remote)
[[ -z "$4" || -z "$5" ]] && error "please enter local and remote forward address"
bind_addr="$(read_forward_address "$4")"
read_addr="$(read_forward_address "$5")"
[[ -z "$bind_addr" || -z "$read_addr" ]] && exit 255
forward_content="$target_host $3 $bind_addr $read_addr" ;;
socks5)
[[ -z "$4" ]] && error "please enter bind address"
bind_addr="$(read_forward_address "$4")"
[[ -z "$bind_addr" ]] && exit 255
forward_content="$target_host $3 $bind_addr #" ;;
*) error "invalid type: $3" ;;
esac

local bind_addr; bind_addr="$(read_forward_address "$4")"
local read_addr; read_addr="$(read_forward_address "$5")"

select_tunnel_by_name "$1"

local forward_content="$target_host $3 $bind_addr $read_addr"

[[ ! -d "$SER_TUNNELS_PATH" ]] && mkdir -p "$SER_TUNNELS_PATH"

if next_tunnel; then
Expand Down Expand Up @@ -776,7 +782,8 @@ case $1 in
case "${FORWARD[1]}" in
local) echo " - forward #$FORWARD_INDEX: (local) ${FORWARD[2]} <= (remote) ${FORWARD[3]}" ;;
remote) echo " - forward #$FORWARD_INDEX: (local) ${FORWARD[3]} => (remote) ${FORWARD[2]}" ;;
*) error "invalid line: '$LINE'" ;;
socks5) echo " - forward #$FORWARD_INDEX: (socks5) ${FORWARD[2]}" ;;
*) error "invalid line: '${FORWARD[*]}'" ;;
esac
done
done
Expand Down

0 comments on commit c0ca765

Please sign in to comment.