mirror of https://github.com/k3s-io/k3s
netexec: Multiple fixes and enhancements to netexec
* Added upload functionality * More logging * Moved to test/images * Image file fixespull/6/head
parent
6439486512
commit
e5b85194aa
|
@ -3,5 +3,9 @@ MAINTAINER Abhishek Shah "abshah@google.com"
|
||||||
|
|
||||||
ADD netexec netexec
|
ADD netexec netexec
|
||||||
ADD netexec.go netexec.go
|
ADD netexec.go netexec.go
|
||||||
|
EXPOSE 8080
|
||||||
|
EXPOSE 8081
|
||||||
|
|
||||||
|
RUN mkdir /uploads
|
||||||
|
|
||||||
ENTRYPOINT ["/netexec"]
|
ENTRYPOINT ["/netexec"]
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
@ -58,6 +59,7 @@ func startHTTPServer(httpPort int) {
|
||||||
http.HandleFunc("/shutdown", shutdownHandler)
|
http.HandleFunc("/shutdown", shutdownHandler)
|
||||||
http.HandleFunc("/hostName", hostNameHandler)
|
http.HandleFunc("/hostName", hostNameHandler)
|
||||||
http.HandleFunc("/shell", shellHandler)
|
http.HandleFunc("/shell", shellHandler)
|
||||||
|
http.HandleFunc("/upload", uploadHandler)
|
||||||
http.HandleFunc("/dial", dialHandler)
|
http.HandleFunc("/dial", dialHandler)
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil))
|
||||||
}
|
}
|
||||||
|
@ -191,6 +193,7 @@ func dialUDP(request string, remoteAddress *net.UDPAddr) (string, error) {
|
||||||
|
|
||||||
func shellHandler(w http.ResponseWriter, r *http.Request) {
|
func shellHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(r.FormValue("shellCommand"))
|
log.Println(r.FormValue("shellCommand"))
|
||||||
|
log.Printf("%s %s %s\n", shellPath, "-c", r.FormValue("shellCommand"))
|
||||||
cmdOut, err := exec.Command(shellPath, "-c", r.FormValue("shellCommand")).CombinedOutput()
|
cmdOut, err := exec.Command(shellPath, "-c", r.FormValue("shellCommand")).CombinedOutput()
|
||||||
output := map[string]string{}
|
output := map[string]string{}
|
||||||
if len(cmdOut) > 0 {
|
if len(cmdOut) > 0 {
|
||||||
|
@ -207,6 +210,43 @@ func shellHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
file, _, err := r.FormFile("file")
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintf(w, "Unable to upload file.")
|
||||||
|
log.Printf("Unable to upload file: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
f, err := ioutil.TempFile("/uploads", "upload")
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintf(w, "Unable to open file for write.")
|
||||||
|
log.Printf("Unable to open file for write: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
if _, err = io.Copy(f, file); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte("Unable to write file."))
|
||||||
|
log.Printf("Unable to write file: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
UploadFile := f.Name()
|
||||||
|
if err := os.Chmod(UploadFile, 0700); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintf(w, "Unable to chmod file.")
|
||||||
|
log.Printf("Unable to chmod file: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Wrote upload to %s", UploadFile)
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
fmt.Fprintf(w, UploadFile)
|
||||||
|
}
|
||||||
|
|
||||||
func hostNameHandler(w http.ResponseWriter, r *http.Request) {
|
func hostNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, getHostName())
|
fmt.Fprintf(w, getHostName())
|
||||||
}
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: netexec
|
||||||
|
labels:
|
||||||
|
app: netexec
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: netexec
|
||||||
|
image: gcr.io/google_containers/netexec:1.1
|
||||||
|
ports:
|
||||||
|
- containerPort: 8080
|
||||||
|
- containerPort: 8081
|
Loading…
Reference in New Issue