Skip to content

Commit

Permalink
v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
VHSgunzo committed Dec 16, 2024
1 parent 837cd4b commit 611bdef
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Accepted options:
Change the current working directory of the process/command.
-env string
Comma separated list of environment variables to pass to the server side process. (default "TERM")
-env-pids string
Comma separated list of PIDs for get environment variables for pass to the server side process.
-no-pty
Do not allocate a pseudo-terminal for the server side process
-nosep-cpids
Expand All @@ -64,6 +66,7 @@ Accepted options:
Environment variables:
SSRV_PTY=1 Same as -pty argument
SSRV_NO_PTY=1 Same as -no-pty argument
SSRV_ENV_PIDS="123,456" Same as -env-pids argument
SSRV_ENV="MY_VAR,MY_VAR1" Same as -env argument
SSRV_UENV="MY_VAR,MY_VAR1" Same as -uenv argument
SSRV_SOCK="tcp:1337" Same as -sock argument
Expand Down
65 changes: 56 additions & 9 deletions ssrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"golang.org/x/term"
)

var VERSION string = "v0.2.9"
var VERSION string = "v0.3.1"

const ENV_VARS = "TERM"
const BINARY_NAME = "ssrv"
Expand All @@ -51,6 +51,7 @@ const USAGE_FOOTER = `
Environment variables:
SSRV_PTY=1 Same as -pty argument
SSRV_NO_PTY=1 Same as -no-pty argument
SSRV_ENV_PIDS="123,456" Same as -env-pids argument
SSRV_ENV="MY_VAR,MY_VAR1" Same as -env argument
SSRV_UENV="MY_VAR,MY_VAR1" Same as -uenv argument
SSRV_SOCK="tcp:1337" Same as -sock argument
Expand Down Expand Up @@ -81,6 +82,10 @@ var socket_addr = flag.String(
"sock", UNIX_SOCKET,
"Socket address listen/connect (unix,tcp,tcp4,tcp6)",
)
var env_pids = flag.String(
"env-pids", "",
"Comma separated list of PIDs for get environment variables for pass to the server side process.",
)
var env_vars = flag.String(
"env", ENV_VARS,
"Comma separated list of environment variables for pass to the server side process.",
Expand Down Expand Up @@ -250,6 +255,10 @@ func ssrv_env_vars_parse() {
!*nosep_cpids {
flag.Set("nosep-cpids", "true")
}
if ssrv_env_pids, ok := os.LookupEnv("SSRV_ENV_PIDS"); ok &&
*env_pids == "" {
flag.Set("env-pids", ssrv_env_pids)
}
if ssrv_env, ok := os.LookupEnv("SSRV_ENV"); ok &&
*env_vars == ENV_VARS {
flag.Set("env", ssrv_env)
Expand Down Expand Up @@ -308,6 +317,40 @@ func is_pid_exist(pid int) bool {
return err == nil
}

func setenv_environ_pids(pids string) {
if len(pids) != 0 {
for _, pid := range strings.Split(pids, ",") {
environ, err := read_environ(pid)
if err != nil {
log.Fatalln(err)
}
for key, value := range environ {
os.Setenv(key, value)
}
}
}
}

func read_environ(pid string) (map[string]string, error) {
environ_path := fmt.Sprintf("/proc/%s/environ", pid)
data, err := os.ReadFile(environ_path)
if err != nil {
return nil, fmt.Errorf("failed to read environ file: %w", err)
}
environ := make(map[string]string)
pairs := strings.Split(string(data), "\000")
for _, pair := range pairs {
if pair == "" {
continue
}
parts := strings.SplitN(pair, "=", 2)
if len(parts) == 2 {
environ[parts[0]] = parts[1]
}
}
return environ, nil
}

func srv_handle(conn net.Conn, self_cpids_dir string) {
var wg sync.WaitGroup
disconnect := func(session *yamux.Session, remote string) {
Expand Down Expand Up @@ -338,7 +381,7 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
return
}
envs_str = envs_str[:len(envs_str)-1]
envs := strings.Split(envs_str, "%&&%")
envs := strings.Split(envs_str, "\000")

is_alloc_pty := true
var stdin_channel net.Conn
Expand Down Expand Up @@ -401,7 +444,7 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
if len(cmd_str) == 0 {
cmd_str = get_shell()
}
cmd := strings.Split(cmd_str, "%&&%")
cmd := strings.Split(cmd_str, "\000")
exec_cmd := exec.Command(cmd[0], cmd[1:]...)

exec_cmd_envs := os.Environ()
Expand Down Expand Up @@ -649,6 +692,8 @@ func server(proto, socket string) {
os.Exit(1)
}()

setenv_environ_pids(*env_pids)

if *env_vars == "all" {
for _, uenv := range strings.Split(*uenv_vars, ",") {
os.Unsetenv(uenv)
Expand Down Expand Up @@ -802,10 +847,12 @@ func client(proto, socket string, exec_args []string) int {
is_alloc_pty = false
}

setenv_environ_pids(*env_pids)

var envs string
if *env_vars == "all" {
for _, env := range os.Environ() {
envs += env + "%&&%"
envs += env + "\000"
}
} else if strings.HasPrefix(*env_vars, "all-:") {
unset_env_vars := strings.Split(strings.Replace(*env_vars, "all-:", "", 1), ",")
Expand All @@ -820,7 +867,7 @@ func client(proto, socket string, exec_args []string) int {
}
}
if is_add_env {
envs += env + "%&&%"
envs += env + "\000"
}
}
} else {
Expand All @@ -829,15 +876,15 @@ func client(proto, socket string, exec_args []string) int {
}
for _, env := range strings.Split(*env_vars, ",") {
if value, ok := os.LookupEnv(env); ok {
envs += env + "=" + value + "%&&%"
envs += env + "=" + value + "\000"
}
}
}
if len(*uenv_vars) != 0 {
envs += fmt.Sprintf("_SSRV_UENV=%s", *uenv_vars) + "%&&%"
envs += fmt.Sprintf("_SSRV_UENV=%s", *uenv_vars) + "\000"
}
if len(*cwd) != 0 {
envs += fmt.Sprintf("_SSRV_CWD=%s", *cwd) + "%&&%"
envs += fmt.Sprintf("_SSRV_CWD=%s", *cwd) + "\000"
}
if !is_alloc_pty {
envs += "_NO_PTY_"
Expand Down Expand Up @@ -874,7 +921,7 @@ func client(proto, socket string, exec_args []string) int {
if err != nil {
log.Fatalf("command channel open error: %v", err)
}
command := strings.Join(exec_args, "%&&%") + "\r"
command := strings.Join(exec_args, "\000") + "\r"
_, err = command_channel.Write([]byte(command))
if err != nil {
log.Fatalf("failed to send command: %v", err)
Expand Down
3 changes: 3 additions & 0 deletions tls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Accepted options:
Change the current working directory of the process/command.
-env string
Comma separated list of environment variables to pass to the server side process. (default "TERM")
-env-pids string
Comma separated list of PIDs for get environment variables for pass to the server side process.
-no-pty
Do not allocate a pseudo-terminal for the server side process
-nosep-cpids
Expand All @@ -74,6 +76,7 @@ Accepted options:
Environment variables:
SSRV_PTY=1 Same as -pty argument
SSRV_NO_PTY=1 Same as -no-pty argument
SSRV_ENV_PIDS="123,456" Same as -env-pids argument
SSRV_ENV="MY_VAR,MY_VAR1" Same as -env argument
SSRV_UENV="MY_VAR,MY_VAR1" Same as -uenv argument
SSRV_SOCK="tcp:1337" Same as -sock argument
Expand Down
2 changes: 1 addition & 1 deletion tls/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ require (
)

require (
golang.org/x/crypto v0.30.0
golang.org/x/crypto v0.31.0
golang.org/x/sys v0.28.0
)
2 changes: 2 additions & 0 deletions tls/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
65 changes: 56 additions & 9 deletions tls/ssrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"golang.org/x/term"
)

var VERSION string = "v0.2.9"
var VERSION string = "v0.3.1"

const ENV_VARS = "TERM"
const TLS_KEY = "key.pem"
Expand All @@ -56,6 +56,7 @@ const USAGE_FOOTER = `
Environment variables:
SSRV_PTY=1 Same as -pty argument
SSRV_NO_PTY=1 Same as -no-pty argument
SSRV_ENV_PIDS="123,456" Same as -env-pids argument
SSRV_ENV="MY_VAR,MY_VAR1" Same as -env argument
SSRV_UENV="MY_VAR,MY_VAR1" Same as -uenv argument
SSRV_SOCK="tcp:1337" Same as -sock argument
Expand Down Expand Up @@ -88,6 +89,10 @@ var socket_addr = flag.String(
"sock", UNIX_SOCKET,
"Socket address listen/connect (unix,tcp,tcp4,tcp6)",
)
var env_pids = flag.String(
"env-pids", "",
"Comma separated list of PIDs for get environment variables for pass to the server side process.",
)
var env_vars = flag.String(
"env", ENV_VARS,
"Comma separated list of environment variables for pass to the server side process.",
Expand Down Expand Up @@ -265,6 +270,10 @@ func ssrv_env_vars_parse() {
!*nosep_cpids {
flag.Set("nosep-cpids", "true")
}
if ssrv_env_pids, ok := os.LookupEnv("SSRV_ENV_PIDS"); ok &&
*env_pids == "" {
flag.Set("env-pids", ssrv_env_pids)
}
if ssrv_env, ok := os.LookupEnv("SSRV_ENV"); ok &&
*env_vars == ENV_VARS {
flag.Set("env", ssrv_env)
Expand Down Expand Up @@ -331,6 +340,40 @@ func is_pid_exist(pid int) bool {
return err == nil
}

func setenv_environ_pids(pids string) {
if len(pids) != 0 {
for _, pid := range strings.Split(pids, ",") {
environ, err := read_environ(pid)
if err != nil {
log.Fatalln(err)
}
for key, value := range environ {
os.Setenv(key, value)
}
}
}
}

func read_environ(pid string) (map[string]string, error) {
environ_path := fmt.Sprintf("/proc/%s/environ", pid)
data, err := os.ReadFile(environ_path)
if err != nil {
return nil, fmt.Errorf("failed to read environ file: %w", err)
}
environ := make(map[string]string)
pairs := strings.Split(string(data), "\000")
for _, pair := range pairs {
if pair == "" {
continue
}
parts := strings.SplitN(pair, "=", 2)
if len(parts) == 2 {
environ[parts[0]] = parts[1]
}
}
return environ, nil
}

func get_cert_sha256(cert string) ([]byte, error) {
cert_bytes, err := os.ReadFile(cert)
if err != nil {
Expand Down Expand Up @@ -414,7 +457,7 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
return
}
envs_str = envs_str[:len(envs_str)-1]
envs := strings.Split(envs_str, "%&&%")
envs := strings.Split(envs_str, "\000")

is_alloc_pty := true
var stdin_channel net.Conn
Expand Down Expand Up @@ -477,7 +520,7 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
if len(cmd_str) == 0 {
cmd_str = get_shell()
}
cmd := strings.Split(cmd_str, "%&&%")
cmd := strings.Split(cmd_str, "\000")
exec_cmd := exec.Command(cmd[0], cmd[1:]...)

exec_cmd_envs := os.Environ()
Expand Down Expand Up @@ -746,6 +789,8 @@ func server(proto, socket string) {
os.Exit(1)
}()

setenv_environ_pids(*env_pids)

if *env_vars == "all" {
for _, uenv := range strings.Split(*uenv_vars, ",") {
os.Unsetenv(uenv)
Expand Down Expand Up @@ -928,10 +973,12 @@ func client(proto, socket string, exec_args []string) int {
is_alloc_pty = false
}

setenv_environ_pids(*env_pids)

var envs string
if *env_vars == "all" {
for _, env := range os.Environ() {
envs += env + "%&&%"
envs += env + "\000"
}
} else if strings.HasPrefix(*env_vars, "all-:") {
unset_env_vars := strings.Split(strings.Replace(*env_vars, "all-:", "", 1), ",")
Expand All @@ -946,7 +993,7 @@ func client(proto, socket string, exec_args []string) int {
}
}
if is_add_env {
envs += env + "%&&%"
envs += env + "\000"
}
}
} else {
Expand All @@ -955,15 +1002,15 @@ func client(proto, socket string, exec_args []string) int {
}
for _, env := range strings.Split(*env_vars, ",") {
if value, ok := os.LookupEnv(env); ok {
envs += env + "=" + value + "%&&%"
envs += env + "=" + value + "\000"
}
}
}
if len(*uenv_vars) != 0 {
envs += fmt.Sprintf("_SSRV_UENV=%s", *uenv_vars) + "%&&%"
envs += fmt.Sprintf("_SSRV_UENV=%s", *uenv_vars) + "\000"
}
if len(*cwd) != 0 {
envs += fmt.Sprintf("_SSRV_CWD=%s", *cwd) + "%&&%"
envs += fmt.Sprintf("_SSRV_CWD=%s", *cwd) + "\000"
}
if !is_alloc_pty {
envs += "_NO_PTY_"
Expand Down Expand Up @@ -1000,7 +1047,7 @@ func client(proto, socket string, exec_args []string) int {
if err != nil {
log.Fatalf("command channel open error: %v", err)
}
command := strings.Join(exec_args, "%&&%") + "\r"
command := strings.Join(exec_args, "\000") + "\r"
_, err = command_channel.Write([]byte(command))
if err != nil {
log.Fatalf("failed to send command: %v", err)
Expand Down

0 comments on commit 611bdef

Please sign in to comment.