Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e49122e957
Fetching contributors…

Cannot retrieve contributors at this time

executable file 611 lines (573 sloc) 22.709 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
#!/bin/bash
# wemux by Matt Furden @zolrath
#
# wemux allows you to start a shared tmux session using the command 'wemux'.
# Clients have the option of:
# Mirroring, which will give them read-only access,
# Pairing, which will allow them to control the hosts cursor.
# Rogue mode, which allows them to work in the same window (shared cursor) or
# in another window (separate cursors) in the hosts tmux session.
#
# To set a user as host add their username to the host_list in the configuration
# file located at /usr/local/etc/wemux.conf
# Other configuations options are also located in /usr/local/etc/wemux.conf
#
# For environments with multiple hosts running their own independent servers
# on the same machine wemux can join different servers with the wemux join
# command, if enabled. This can be enabled in the configuration file.
#
# WEMUX HOST COMMANDS:
# wemux start : Start the wemux server/join an existing wemux server.
# wemux attach: Join an existing wemux server.
# wemux stop : Stop the wemux server.
# wemux users : List the currently attached wemux users.
# wemux kick : Disconnect an SSH user, remove their wemux server.
# wemux config: Open the wemux configuration file in your $EDITOR.
# wemux help : Display the help screen.
#
# WEMUX CLIENT COMMANDS:
# wemux mirror: Attach to Host in read-only mode.
# wemux pair : Attach to Host in pair mode, which allows editing.
# wemux rogue : Attach to Host in rogue mode, which allows editing and switching
# to windows independently from the host.
# wemux logout: Log out of the wemux rogueing session.
# wemux users : List the currently attached wemux users.
# wemux help : Display the help screen.
#
# To enable multi-host commands, set allow_server_change="true" in wemux.conf
# WEMUX SESSION COMMANDS: can be run by either host or client.
# wemux join : Join wemux server with supplied name.
# wemux reset : Join default wemux server: wemux
# wemux list : List all currently active wemux servers.

###############################################################################

# Current wemux version.
version="3.1.0"

# Setup and Configuration Files.
# Default settings, modify them in the /usr/local/etc/wemux.conf file:
host_list=(change_this_in_wemux_conf)
socket_prefix="/tmp/wemux"
options="-u"
allow_pair_mode="true"
allow_rogue_mode="true"
default_client_mode="mirror"
allow_kick_user="true"
allow_server_change="false"
default_server_name="wemux"
allow_server_list="true"
allow_user_list="true"
announce_attach="true"
announce_server_change="true"

# Prevent users from changing their $USER env variable to become host.
username=`whoami`
# Set $EDITOR default to vi if not configured on host machine.
editor=${EDITOR:="vi"}

# Load configuration options from /usr/local/etc/wemux.conf
[ -f /usr/local/etc/wemux.conf ] && . /usr/local/etc/wemux.conf

# Sanitize server name, replace spaces and underscores with dashes.
# Remove all non alpha-numeric characters, convert to lowercase.
sanitize_server_name() {
  local new_server=$@
  local new_server=${new_server// /\-}
  local new_server=${new_server//_/\-}
  local new_server=${new_server//[^a-zA-Z0-9\-]/}
  local new_server=`echo "$new_server" | tr '[A-Z]' '[a-z]'`
  echo "$new_server"
}

# Load the server name of last wemux server. If empty, set to 'wemux'.
# Ensure server name is 'wemux' if allow_server_change is disabled.
# If default_server_name is set in wemux.conf it will be used instead of 'wemux'
load_server_name() {
  if [ "$allow_server_change" == "true" ]; then
    if [ -f ~/.wemux_last_server ]; then
      unclean_server=`cat ~/.wemux_last_server`
      # Sanitize loaded server name.
      server=`sanitize_server_name $unclean_server`
      if ! [[ $unclean_server == $server ]]; then
        echo "$server" > ~/.wemux_last_server
      fi
    else
      # If ~/.wemux_last_server doesn't exist set to default_server_name
      server="$default_server_name"
    fi
  else
    # If allow_server_change is disabled, set to default_server_name
    server="$default_server_name"
  fi
}

# Build $wemux variable to call proper tmux server.
build_wemux_prefix() {
  load_server_name
  # Set client's rogue mode session name.
  client_session="$server-$username"
  # Set socket to include server name.
  socket="${socket_prefix}-$server"
  # Set $wemux to wemux server socket.
  wemux="tmux -S $socket $options"
}

# List all currently running wemux servers.
list_active_servers() {
  if [ "$allow_server_change" == "true" ]; then
    if [ "$allow_server_list" == "true" ]; then
      wemux_server_sockets=$socket_prefix*
      for wemux_server in $wemux_server_sockets; do
        redirect=`tmux -S $wemux_server server-info 2>&1`; server_running=$?
        # Number each active server, give it its own line.
        if [ "$server_running" == 0 ]; then
          num=$[num+1]
          server_name=`echo "$wemux_server" | sed -e "s,$socket_prefix-,,"`
          # Add indicator for current wemux server.
          if [ "$server_name" == "$server" ]; then
            server_name="$server_name <- current server"
          fi
          server_list="$server_list $num. $server_name\n"
        fi
      done
      if [ -z "$server_list" ]; then
        echo "No wemux servers currently active."
      else
        echo "Currently active wemux servers:"
        # Remove last newline
        echo -e "${server_list%??}"
      fi
    # allow_server_list is disabled:
    else
      echo "Server listing has been disabled."
      return 126
    fi
  # allow_server_change is disabled:
  else
    echo "Server related commands have been disabled."
    return 126
  fi
}

# List users currently attached to wemux server. Username[m] for mirror mode.
# Only contains names. Formatted for being included as part of status bar.
status_users() {
  if [ "$allow_user_list" == "true" ]; then
    while IFS= read line; do
      read tty mode <<<$(echo $line)
      # Get user associated with tty
      name=`stat -f%Su $tty 2>/dev/null` || name=`stat -c%U $tty 2>/dev/null`
      # If user is attached in read-only mode, set mode to [m]
      [[ $mode == "(ro)" ]] && mode="[m]" || mode=""
      # If user/mode is already is userlist, do not add them again.
      if ! [[ "$users" =~ "$name$mode," ]]; then
        users="$users$name$mode, "
      fi
    done < <(wemux list-clients | cut -f1,6 -d ' ' | sed s/://)
    # Strip last two characters ', '
    echo "${users%??}"
  else
    echo "User list disabled."
    return 126
  fi
}

# List users currently attached to wemux server with informative string.
# More verbose, intended for use in terminal.
list_users() {
  if [ "$allow_user_list" == "true" ]; then
    redirect=`wemux server-info 2>&1`; server_running=$?
    if [ "$server_running" == 0 ]; then
      # Get list of users with status_users function.
      users="$(wemux status_users)"
      # Number each user, give it its own line.
      for user in $users; do
          num=$[num+1]
          user=`echo "$user" | sed -e "s/,//"`
          user_list="$user_list $num. $user\n"
      done
      if [ -z "$user_list" ]; then
        echo "No wemux users attached to '$server'."
      else
        echo "Users attached to '$server': "
        # Remove last newline.
        echo -e "${user_list%??}"
      fi
    else
      echo "No wemux server running on '$server'. No server, no users!"
    fi
  else
    echo "User listing has been disabled."
  fi
}

# Display the currently attached users verbosely in a tmux message.
display_users() {
    redirect=`$wemux display-message "$(wemux users)" 2>&1`
}

# The ugly group of redirects below solve the issue where tmux/epoll causes tmux
# to hang when stderr is redirected to /dev/null in a backwards compatible way.

# Returns true if host currently has a running wemux session.
session_exists() {
  redirect=`$wemux has-session -t $server 2>&1`; does_exist=$?
  [ "$does_exist" == 0 ] && return 0 || return 1;
}

# Returns true if rogue session with current host already exists.
has_rogue_session() {
  redirect=`$wemux has-session -t $client_session 2>&1`; does_exist=$?
  [ "$does_exist" == 0 ] && return 0 || return 1;
}

# Returns true if server is successfully killed.
kill_server_successful() {
  redirect=`tmux -S $socket kill-server 2>&1`; killed_successfully=$?
  [ "$killed_successfully" == 0 ] && return 0 || return 1;
}

# Announce when user attaches/detaches from server.
# Can be disabled by changing announce_attach to false in /usr/local/etc/wemux.conf
# The first argument specifies the mode the user is attaching in for the message
# All additional arguments get wrapped in the attach/detach messages.
announce_connection() {
  connection_type=$1; shift; attach_commands="$@"
  [ "$announce_attach" == "true" ] && redirect=`$wemux display-message \
    "$username has attached in $connection_type mode." 2>&1`
  $attach_commands
  [ "$announce_attach" == "true" ] && redirect=`$wemux display-message \
    "$username has detached." 2>&1`
  return 0
}

# Announces when a user joins/changes their server.
# Can be disabled by changing announce_server_change to false in /usr/local/etc/wemux.conf
# Change server name for server, or display server name if no argument is given.
change_server() {
  if [ "$allow_server_change" == "true" ]; then
    # Sanitize input.
    new_server=`sanitize_server_name $@`
    old_server=$server

    # Get list of currently running servers
    server_names=(`echo $socket_prefix* | sed -e "s,$socket_prefix-,,g"`)
    # If user joins a number, go to the server with that number in `wemux list`
    [[ "$new_server" =~ ^[0-9]+$ ]] && new_server=${server_names[$new_server-1]}

    if [ -z "$new_server" ]; then
      echo "Current wemux server: $server"
    elif [ "$new_server" == "$old_server" ]; then
      echo "Your wemux server is already set to '$server'"
    else
      # Announce that the user has changed servers to current server.
      [ "$announce_server_change" == "true" ] && redirect=`$wemux display-message \
        "$username has switched to server: $new_server" 2>&1`
      echo "Changed wemux server from '$old_server' to '$new_server'"
      echo $new_server > ~/.wemux_last_server
      # Rebuild wemux prefix for new server name.
      build_wemux_prefix
      # Announce that the user has joined to new server.
      [ "$announce_server_change" == "true" ] && redirect=`$wemux display-message \
        "$username has joined this server" 2>&1`
    fi
  else
    echo "Changing wemux servers has been disabled."
    return 126
  fi
  return 0
}

# Display version of wemux installed on system. Show URL for wemux.
display_version() {
  echo "wemux $version"
  echo "To check for a newer version visit: http://www.github.com/zolrath/wemux"
}

# Host mode, used when user is listed in the host_list array in /usr/local/etc/wemux.conf
host_mode() {
  # Start the server if it doesn't exist, otherwise reattach.
  start_server() {
    if ! session_exists; then
      $wemux new-session -d -s $server
      # Open tmux socket to all users to allow clients to connect.
      chmod 1777 $socket
      echo "wemux server started on '$server'."
    fi
    reattach
  }

  # Reattach to the wemux server.
  reattach() {
    if session_exists; then
      $wemux attach -t $server
    else
      echo "No wemux server to attach to on '$server'."
    fi
  }

  # Kick an SSH user off the server and remove their rogue session if it exists.
  kick_user() {
    if [ "$allow_kick_user" == "true" ]; then
      kicked_user=$1
      # Get sshd process with users name and get its PID.
      user_pid=`ps aux | grep "$kicked_user.*sshd" | grep -v grep | tr -s ' ' | cut -d ' ' -f 2`
      echo "Kicking $kicked_user from the server. Sudo required."
      # Kill the sshd process of the supplied user.
      redirect=`sudo kill -9 $user_pid 2>&1`; kicked_successfully=$?
      # Remove any tmux sessions ending in "-kicked_user"
      `wemux kill-session -t "*-$kicked_user" > /dev/null 2>&1`
      if [ "$kicked_successfully" == 0 ]; then
        echo "$kicked_user kicked successfully!"
      else
        echo "$kicked_user was not kicked. Are you sure they're connected?"
      fi
    else
      echo "Kicking users has been disabled."
    fi
  }

  # Stop the wemux session and remove the socket file.
  stop_server() {
    server_sockets=(`echo $socket_prefix*`)
    # If a specific server is supplied:
    if [ $1 ]; then
      # If user enters a number, stop the server with that number in `wemux list`
      if [[ $@ =~ ^[0-9]+$ ]]; then
        socket=${server_sockets[$1-1]}
        server=`echo $socket | sed -e "s,$socket_prefix-,,g"`
      # Otherwise, stop the server with the supplied name.
      else
        server=`sanitize_server_name $@`
        socket="$socket_prefix-$server"
      fi
    fi
    # If the user doesn't pass an argument, stop current server.
    if kill_server_successful; then
      echo "wemux server on '$server' stopped."
    else
      echo "No wemux server running on '$server'"
    fi
    # If the socket file exists:
    if [ -e "$socket" ]; then
      if ! rm $socket; then
        echo "Could not remove '$socket'. Please check file ownership."
      fi
    fi
  }

  # Display the commands available in host mode.
  display_host_commands() {
    echo "wemux version $version"
    if [ "$allow_server_change" == "true" ]; then
      echo "Current wemux server: $server"
    fi
    echo ""
    echo "Usage: wemux [command]"
    echo "To host a wemux server please use one of the following:"
    echo ""
    echo " [s] start: Start the wemux server/attach to an existing wemux server."
    echo " [a] attach: Attach to an existing wemux server."
    echo " [k] stop: Kill the wemux server '$server', delete its socket."
    echo ""
    if [ "$allow_server_change" == "true" ]; then
      echo " [j] join [name]: Join the specified wemux server."
      echo " [d] reset: Join default wemux server: $default_server_name"
      if [ "$allow_server_list" == "true" ]; then
        echo " [l] list: List all currently active wemux servers."
      fi
    fi
    if [ "$allow_user_list" == "true" ]; then
      echo " [u] users: List all users currently attached to '$server'"
    fi
    if [ "$allow_kick_user" == "true" ]; then
      echo " kick: Disconnect an SSH user, remove their wemux server."
    fi
    echo ""
    echo " [c] config: Open the wemux configuration file in $EDITOR."
    echo " [h] help: Display this screen."
    echo " no args: Start the wemux server/attach to an existing wemux server."
  }

  # Host mode command handling:
  # If no command given, call start server.
  if [ -z "$1" ]; then
    announce_connection "host" start_server
  else
    case "$1" in
      start|s) announce_connection "host" start_server;;
      attach|a) announce_connection "host" reattach;;
      stop|st) shift; stop_server "$@";;
      kill|k) shift; stop_server "$@";;
      help|h) display_host_commands;;
      join|j) shift; change_server "$@";;
      name|n) shift; change_server "$@";;
      reset|d) change_server "$default_server_name";;
      list|l) list_active_servers;;
      users|u) list_users;;
      kick) kick_user $2;;
      status_users) status_users;;
      display_users) display_users;;
      version|v) display_version;;
      conf*|c) $editor /usr/local/etc/wemux.conf;;
      *) if ! $wemux "$@"; then
                        echo "To see a list of wemux commands enter 'wemux help'"
                        exit 127
                      fi;;
    esac
  fi
}

# Client Mode, used when user is NOT listed in the host_list in /usr/local/etc/wemux.conf
client_mode() {
  # Mirror mode, allows the user to view wemux server in read only mode.
  mirror_mode() {
    if session_exists; then
      $wemux attach -t $server -r
    else
      echo "No wemux server to mirror on '$server'."
    fi
  }

  # Pair mode, allows user to interact with wemux server.
  pair_mode() {
    if [ "$allow_pair_mode" == "true" ]; then
      if session_exists; then
        $wemux attach -t $server
      else
        echo "No wemux server to pair with on '$server'."
      fi
    else
      echo "Pair mode is disabled."
      return 126
    fi
  }

  # Rogue mode, allows user to interact with wemux server and operate
  # independently in other windows.
  # Will connect to existing rogue session or create one if necessary.
  rogue_mode() {
    if [ "$allow_rogue_mode" == "true" ]; then
      if has_rogue_session; then
        $wemux attach -t $client_session
      elif session_exists; then
        $wemux new-session -d -t $server -s $client_session
        $wemux new-window -n $username
        $wemux attach -t $client_session
      else
        echo "No wemux server to go 'rogue' with on '$server'."
      fi
    else
      echo "Rogue mode is disabled."
      return 126
    fi
  }

  # Log user out of rogue mode, removing their session..
  logout_rogue() {
    if [ "$allow_rogue_mode" == "true" ]; then
      if has_rogue_session; then
        [ "$announce_attach" == "true" ] && $wemux display-message \
          "$username has logged out of rogue mode."
        $wemux kill-session -t $client_session
        echo "Logged out of rogue mode on '$server'."
      else
        echo "No wemux server to log out of on '$server'."
      fi
    else
      echo "Rogue mode is disabled."
      return 126
    fi
  }

  smart_reattach() {
    # If default_client_mode has been set to "rogue" in wemux.conf:
    if [ "$default_client_mode" == "rogue" ] && [ "$allow_rogue_mode" == "true" ]; then
      announce_connection "rogue" rogue_mode
    # If default_client_mode has been set to "pair" in wemux.conf:
    elif [ "$default_client_mode" == "pair" ] && [ "$allow_pair_mode" == "true" ]; then
      announce_connection "pair" pair_mode
    elif has_rogue_session && [ "$allow_rogue_mode" == "true" ]; then
      announce_connection "rogue" rogue_mode
    elif [ "$allow_pair" == "true" ]; then
      announce_connection "pair" pair_mode
    elif session_exists; then
      announce_connection "mirror" $wemux attach -t $server -r
    else
      echo "No wemux server to attach to on '$server'"
    fi
  }

  # Display the commands available in client mode.
  display_client_commands() {
    echo "wemux version $version"
    if [ "$allow_server_change" == "true" ]; then
      echo "Current wemux server: $server"
    fi
    echo ""
    echo "Usage: wemux [command]"
    echo "To connect to wemux please use one of the following:"
    echo ""
    echo " [m] mirror: Attach to '$server' in read-only mode."
    if [ "$allow_pair_mode" == "true" ]; then
      echo " [p] pair: Attach to '$server' in pair mode, which allows editing."
    fi
    if [ "$allow_rogue_mode" == "true" ]; then
      echo " [r] rogue: Attach to '$server' in rogue mode, allowing you to pair"
      echo " and also work in other windows independent of the host."
      echo " [o] logout: Log out of the current wemux rogue session."
    fi
    echo ""
    if [ "$allow_server_change" == "true" ]; then
      echo " [j] join [name]: Join the specified wemux server."
      echo " [d] reset: Join default wemux server: $default_server_name"
      if [ "$allow_server_list" == "true" ]; then
        echo " [l] list: List all currently active wemux servers."
      fi
    fi
    if [ "$allow_user_list" == "true" ]; then
      echo " [u] users: List all users currently attached to '$server'"
    fi
    echo ""
    echo " [h] help: Display this screen."
    if [ "$default_client_mode" == "rogue" ] && [ "$allow_rogue_mode" == "true" ]; then
      echo " no args: Attach to '$server' in rogue mode."
    elif [ "$default_client_mode" == "pair" ] && [ "$allow_pair_mode" == "true" ]; then
      echo " no args: Attach to '$server' in pair mode."
    elif [ "$allow_rogue_mode" == "true" ] && [ "$allow_pair_mode" == "true" ]; then
      echo " no args: Attach to rogue session on '$server' if it already exists,"
      echo " otherwise, attach in pair mode."
    elif [ "$allow_rogue_mode" == "true" ] && [ "$allow_pair_mode" == "false" ]; then
      echo " no args: Attach to rogue session on '$server' if it already exists,"
      echo " otherwise, attach in mirror mode."
    elif [ "$allow_pair_mode" == "true" ]; then
      echo " no args: Attach to '$server' in pair mode."
    else
      echo " no args: Attach to '$server' in mirror mode."
    fi
  }

  # Client mode command handling:
  # If no command given, call smart_reattach
  if [ -z "$1" ]; then
    smart_reattach
  else
    case "$1" in
      mirror|m) announce_connection "mirror" mirror_mode;;
      pair|p) announce_connection "pair" pair_mode;;
      rogue|r) announce_connection "rogue" rogue_mode;;
      edit|e) announce_connection "rogue" rogue_mode;;
      logout|o) logout_rogue;;
      stop|s) logout_rogue;;
      help|h) display_client_commands;;
      join|j) shift; change_server "$@";;
      name|n) shift; change_server "$@";;
      reset|d) change_server "$default_server_name";;
      list|l) list_active_servers;;
      users|u) list_users;;
      status_users) status_users;;
      display_users) display_users;;
      version|v) display_version;;
      *) if ! $wemux "$@"; then
                       echo "To see a list of wemux commands enter 'wemux help'"
                       exit 127
                     fi;;
    esac
  fi
}

# Check if current user is listed in the host_list.
user_is_a_host() {
  for user in "${host_list[@]}"; do
    [[ "$user" == "$username" ]] && return 0
  done
  return 1
}

allowed_nested_command() {
  commands=(users u list l display_users status_users list-clients
  kill-sesssion kick version v)
  for command in "${commands[@]}"; do
    [[ "$command" == "$1" ]] && return 0
  done
  return 1
}
# Create $wemux variable.

build_wemux_prefix

# Don't allow wemux to be run directly within a wemux server.
if [[ "$TMUX" != *$socket* ]] || allowed_nested_command "$1" ; then
  # If user is in host list, use host mode. If not, use client mode.
  if user_is_a_host; then
    host_mode "$@"
  else
    client_mode "$@"
  fi
else
  echo "You're already attached to the wemux server on '$server'"
  echo "wemux does not allow nesting wemux servers directly."
fi
Something went wrong with that request. Please try again.