Created
September 13, 2017 11:21
-
-
Save lambrospetrou/11e373766f4278889d6380b75cdf9f32 to your computer and use it in GitHub Desktop.
This server in go allows you to make an HTTP request to an endpoint and pass the body content as STDIN input to a command line script and return the output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os/exec" | |
) | |
func main() { | |
http.HandleFunc("/webcmd", func(w http.ResponseWriter, r *http.Request) { | |
b, err := ioutil.ReadAll(r.Body) | |
txt := string(b) | |
log.Println(txt) | |
/* You can use query parameters to get a dynamic script | |
* but here I just use a hardcoded one. | |
* | |
* queryValues := r.URL.Query() | |
* script := queryValues.Get("script") | |
*/ | |
cmd := exec.Command("/path/to/the/script/to/execute") | |
stdin, err := cmd.StdinPipe() | |
defer stdin.Close() | |
if err != nil { | |
log.Fatalln(err) | |
} | |
_, err = stdin.Write([]byte(txt)) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
out, err := cmd.Output() | |
if err != nil { | |
log.Fatalln(err) | |
} | |
log.Println(string(out)) | |
fmt.Fprintf(w, "%v", string(out)) | |
}) | |
// Uncomment to enable a file server too | |
//fs := http.FileServer(http.Dir(".")) | |
//http.Handle("/", fs) | |
log.Fatal(http.ListenAndServe(":11111", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment