proxy: support arbitraty traceroute arguments

This commit is contained in:
Lan Tian
2021-02-27 16:42:42 +08:00
parent f6ddc5761b
commit f49f8bac5e
4 changed files with 24 additions and 7 deletions

View File

@@ -2,13 +2,14 @@ package main
import (
"fmt"
"html"
"net/http"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/google/shlex"
)
func tracerouteTryExecute(cmd []string, args [][]string) ([]byte, string) {
@@ -31,10 +32,17 @@ func tracerouteTryExecute(cmd []string, args [][]string) ([]byte, string) {
func tracerouteHandler(httpW http.ResponseWriter, httpR *http.Request) {
query := string(httpR.URL.Query().Get("q"))
query = strings.TrimSpace(query)
query = html.EscapeString(query)
if query == "" {
invalidHandler(httpW, httpR)
} else {
args, err := shlex.Split(query)
if err != nil {
httpW.WriteHeader(http.StatusInternalServerError)
httpW.Write([]byte(fmt.Sprintf("failed to parse args: %s\n", err.Error())))
return
}
var result []byte
var errString string
skippedCounter := 0
@@ -45,7 +53,8 @@ func tracerouteHandler(httpW http.ResponseWriter, httpR *http.Request) {
"traceroute",
},
[][]string{
{"-q1", "-w1", query},
append([]string{"-q1", "-w1"}, args...),
args,
},
)
} else if runtime.GOOS == "linux" {
@@ -55,8 +64,9 @@ func tracerouteHandler(httpW http.ResponseWriter, httpR *http.Request) {
"traceroute",
},
[][]string{
{"-q1", "-N32", "-w1", query},
{"-q1", "-w1", query},
append([]string{"-q1", "-N32", "-w1"}, args...),
append([]string{"-q1", "-w1"}, args...),
args,
},
)
} else {