Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

free function which kills jprq processes using cmd #168

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"log"
"net"
"os"
"os/exec"
"os/signal"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)

Expand All @@ -21,11 +24,34 @@ func printHelp() {
fmt.Println(" http <port> Start an HTTP tunnel on the specified port")
fmt.Println(" http <port> -s <subdomain> Start an HTTP tunnel with a custom subdomain")
fmt.Println(" http <port> --debug Debug an HTTP tunnel with Jprq Debugger")
fmt.Println(" --free Free up the port when the client is running in the background")
fmt.Println(" --help Show this help message")
fmt.Println(" --version Show the version number")
os.Exit(0)
}

// freeClient function is called when jprq --free is called.
// It kills the jprq processes running on the system.
// It is called when the user wants to free up the port when the client is running in the background
func freeClient() {
var cmd *exec.Cmd
// check the OS and run the appropriate command
if strings.Contains(runtime.GOOS, "windows") {
cmd = exec.Command("taskkill", "/IM", "jprq.exe", "/F")
} else if strings.Contains(runtime.GOOS, "darwin") || strings.Contains(runtime.GOOS, "linux") {
cmd = exec.Command("killall", "jprq")
} else {
log.Fatal("currently unsupported OS")
}

err := cmd.Run()
if err != nil {
log.Println("error killing jprq process")
os.Exit(1)
}

}

func main() {
log.SetFlags(0)
if len(os.Args) < 2 {
Expand All @@ -45,6 +71,8 @@ func main() {
printHelp()
case "version", "--version":
printVersion()
case "free", "--free": // jprq --free if called
freeClient()
default:
log.Fatalf("unknown command: %s, jprq --help", command)
}
Expand Down
5 changes: 2 additions & 3 deletions server/jprq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -92,7 +91,7 @@ func (j *Jprq) servePublicConn(conn net.Conn) error {
t, found := j.httpTunnels[host]
if !found {
writeResponse(conn, 404, "Not Found", "tunnel not found. create one at jprq.io")
return errors.New(fmt.Sprintf("unknown host requested %s", host))
return fmt.Errorf("unknown host requested %s", host)
}
return t.PublicConnectionHandler(conn, buffer)
}
Expand Down Expand Up @@ -128,7 +127,7 @@ func (j *Jprq) serveEventConn(conn net.Conn) error {
}
hostname := fmt.Sprintf("%s.%s", request.Subdomain, j.config.DomainName)
if _, ok := j.httpTunnels[hostname]; ok {
return events.WriteError(conn, "subdomain is busy: %s, try another one", request.Subdomain)
return events.WriteError(conn, "subdomain is busy: %s, try another one \n or free using 'jrpq --free'", request.Subdomain)
}

var t tunnel.Tunnel
Expand Down