Endpoint to download the wireguard-negotiator binary

master
Ambrose Chua 2020-02-15 23:55:47 +08:00
parent 97630bdef3
commit bdcc304492
1 changed files with 28 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
@ -56,6 +57,11 @@ var CmdServer = &cli.Command{
Aliases: []string{"I"},
Usage: "Enable interactive prompt before accepting new peers",
},
&cli.BoolFlag{
Name: "bin",
Aliases: []string{"B"},
Usage: "Serve the current wireguard-negotiator binary file upon GET request to /",
},
},
Action: runServer,
}
@ -116,6 +122,28 @@ func runServer(ctx *cli.Context) error {
// TODO: Rate limiting
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
bin, err := os.Executable()
if err != nil {
w.WriteHeader(500)
return
}
file, err := os.Open(bin)
if err != nil {
w.WriteHeader(500)
return
}
_, err = io.Copy(w, file)
if err != nil {
log.Println("WARNING: Write binary executable to response failed")
return
}
default:
w.WriteHeader(405)
}
})
http.HandleFunc("/request", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":