Here is my code:
main.go
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.New("index.html").ParseFiles("index.html")
server, _ := ioutil.ReadFile("main.go")
tmpl.Execute(w, string(server))
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
filepath := r.Form.Get("file")
filepath = fmt.Sprintf("static/%v", filepath)
if len(filepath) == 0 {
_, err := w.Write([]byte("file parameter is required"))
if err != nil {
log.Println("Can't write response")
}
return
}
if strings.Contains(filepath, "..") {
filepath = strings.ReplaceAll(filepath, "..", ".")
}
file, _ := ioutil.ReadFile(filepath)
w.Write(file)
}
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/static/", staticHandler)
addr, _ := os.LookupEnv("ADDR")
log.Fatal(http.ListenAndServe(addr, nil))
}
Flag in /flag