Skip to content

Commit

Permalink
v0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
VHSgunzo committed Apr 6, 2024
1 parent dff2ac0 commit 451fa22
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Accepted options:
Do not allocate a pseudo-terminal for the server side process
-nosep-cpids
Don't create a separate dir for the server socket to store the list of client PIDs.
-pid-file string
The file for storing the server's PID.
-pty
Force allocate a pseudo-terminal for the server side process
-server
Expand All @@ -56,6 +58,7 @@ Environment variables:
SSRV_SOCKET="tcp:1337" Same as -socket argument
SSRV_CPIDS_DIR=/path/dir Same as -cpids-dir argument
SSRV_NOSEP_CPIDS=1 Same as -nosep-cpids argument
SSRV_PID_FILE=/path/ssrv.pid Same as -pid-file argument
SHELL="/bin/bash" Assigns a default shell (on the server side)
--
Expand Down
18 changes: 18 additions & 0 deletions shellsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Environment variables:
SSRV_SOCKET="tcp:1337" Same as -socket argument
SSRV_CPIDS_DIR=/path/dir Same as -cpids-dir argument
SSRV_NOSEP_CPIDS=1 Same as -nosep-cpids argument
SSRV_PID_FILE=/path/ssrv.pid Same as -pid-file argument
SHELL="/bin/bash" Assigns a default shell (on the server side)
--
Expand Down Expand Up @@ -94,6 +95,10 @@ var nosep_cpids = flag.Bool(
"nosep-cpids", false,
"Don't create a separate dir for the server socket to store the list of client PIDs.",
)
var pid_file = flag.String(
"pid-file", "",
"The file for storing the server's PID.",
)

type win_size struct {
ws_row uint16
Expand Down Expand Up @@ -226,6 +231,9 @@ func ssrv_env_vars_parse() {
if is_env_var_eq("SSRV_NOSEP_CPIDS", "1") {
flag.Set("nosep-cpids", "true")
}
if ssrv_pid_file, ok := os.LookupEnv("SSRV_PID_FILE"); ok {
flag.Set("pid-file", ssrv_pid_file)
}
}

func srv_handle(conn net.Conn, self_cpids_dir string) {
Expand Down Expand Up @@ -387,6 +395,13 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
}

func server(proto, socket string) {
ssrv_pid := fmt.Sprintf("%d", os.Getpid())
if *pid_file != "" {
err := os.WriteFile(*pid_file, []byte(ssrv_pid), 0644)
if err != nil {
log.Fatalf("error writing PID file: %v\n", err)
}
}

listener, err := net.Listen(proto, socket)
if err != nil {
Expand Down Expand Up @@ -422,6 +437,9 @@ func server(proto, socket string) {
if proto == "unix" && is_file_exists(socket) {
os.Remove(socket)
}
if is_file_exists(*pid_file) {
os.Remove(*pid_file)
}
os.RemoveAll(self_cpids_dir)
os.Remove(*cpids_dir)
os.Exit(1)
Expand Down
3 changes: 3 additions & 0 deletions tls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Accepted options:
Do not allocate a pseudo-terminal for the server side process
-nosep-cpids
Don't create a separate dir for the server socket to store the list of client PIDs.
-pid-file string
The file for storing the server's PID.
-pty
Force allocate a pseudo-terminal for the server side process
-server
Expand Down Expand Up @@ -68,6 +70,7 @@ Environment variables:
SSRV_TLS_CERT="/path/cert.pem" Same as -tls-cert argument
SSRV_CPIDS_DIR=/path/dir Same as -cpids-dir argument
SSRV_NOSEP_CPIDS=1 Same as -nosep-cpids argument
SSRV_PID_FILE=/path/ssrv.pid Same as -pid-file argument
SHELL="/bin/bash" Assigns a default shell (on the server side)
--
Expand Down
20 changes: 19 additions & 1 deletion tls/shellsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Environment variables:
SSRV_TLS_CERT="/path/cert.pem" Same as -tls-cert argument
SSRV_CPIDS_DIR=/path/dir Same as -cpids-dir argument
SSRV_NOSEP_CPIDS=1 Same as -nosep-cpids argument
SSRV_PID_FILE=/path/ssrv.pid Same as -pid-file argument
SHELL="/bin/bash" Assigns a default shell (on the server side)
--
Expand Down Expand Up @@ -110,6 +111,10 @@ var nosep_cpids = flag.Bool(
"nosep-cpids", false,
"Don't create a separate dir for the server socket to store the list of client PIDs.",
)
var pid_file = flag.String(
"pid-file", "",
"The file for storing the server's PID.",
)

type win_size struct {
ws_row uint16
Expand Down Expand Up @@ -253,6 +258,9 @@ func ssrv_env_vars_parse() {
if is_env_var_eq("SSRV_NOSEP_CPIDS", "1") {
flag.Set("nosep-cpids", "true")
}
if ssrv_pid_file, ok := os.LookupEnv("SSRV_PID_FILE"); ok {
flag.Set("pid-file", ssrv_pid_file)
}
}

func srv_handle(conn net.Conn, self_cpids_dir string) {
Expand Down Expand Up @@ -414,8 +422,15 @@ func srv_handle(conn net.Conn, self_cpids_dir string) {
}

func server(proto, socket string) {
var err error
ssrv_pid := fmt.Sprintf("%d", os.Getpid())
if *pid_file != "" {
err := os.WriteFile(*pid_file, []byte(ssrv_pid), 0644)
if err != nil {
log.Fatalf("error writing PID file: %v\n", err)
}
}

var err error
var listener net.Listener
if is_file_exists(*tls_cert) && is_file_exists(*tls_key) {
cert, err := tls.LoadX509KeyPair(*tls_cert, *tls_key)
Expand Down Expand Up @@ -469,6 +484,9 @@ func server(proto, socket string) {
if proto == "unix" && is_file_exists(socket) {
os.Remove(socket)
}
if is_file_exists(*pid_file) {
os.Remove(*pid_file)
}
os.RemoveAll(self_cpids_dir)
os.Remove(*cpids_dir)
os.Exit(1)
Expand Down

0 comments on commit 451fa22

Please sign in to comment.